Print this page
2964 need POSIX 2008 locale object support
Reviewed by: Robert Mustacchi <rm@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libc/port/locale/strftime.c
+++ new/usr/src/lib/libc/port/locale/strftime.c
1 1 /*
2 + * Copyright 2013 Garrett D'Amore <garrett@damore.org>
2 3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 4 * Copyright (c) 1989 The Regents of the University of California.
4 5 * All rights reserved.
5 6 *
6 7 * Redistribution and use in source and binary forms are permitted
7 8 * provided that the above copyright notice and this paragraph are
8 9 * duplicated in all such forms and that any documentation,
9 10 * advertising materials, and other materials related to such
10 11 * distribution and use acknowledge that the software was developed
11 12 * by the University of California, Berkeley. The name of the
12 13 * University may not be used to endorse or promote products derived
13 14 * from this software without specific prior written permission.
14 15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
15 16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 18 */
18 19
19 20 #include "lint.h"
20 21 #include "tzfile.h"
21 22 #include <fcntl.h>
22 23 #include <sys/stat.h>
23 24 #include <string.h>
24 25 #include <stdio.h>
26 +#include <locale.h>
25 27 #include "timelocal.h"
28 +#include "localeimpl.h"
26 29
27 30 static char *_add(const char *, char *, const char *);
28 31 static char *_conv(int, const char *, char *, const char *);
29 -static char *_fmt(const char *, const struct tm *, char *, const char * const);
32 +static char *_fmt(locale_t, const char *, const struct tm *, char *,
33 + const char * const);
30 34 static char *_yconv(int, int, int, int, char *, const char *);
31 35
32 36 extern char *tzname[];
33 37
34 38 #define IN_NONE 0
35 39 #define IN_SOME 1
36 40 #define IN_THIS 2
37 41 #define IN_ALL 3
38 42
39 43 #define PAD_DEFAULT 0
40 44 #define PAD_LESS 1
41 45 #define PAD_SPACE 2
42 46 #define PAD_ZERO 3
43 47
44 48 static const char *fmt_padding[][4] = {
45 49 /* DEFAULT, LESS, SPACE, ZERO */
46 50 #define PAD_FMT_MONTHDAY 0
47 51 #define PAD_FMT_HMS 0
48 52 #define PAD_FMT_CENTURY 0
49 53 #define PAD_FMT_SHORTYEAR 0
50 54 #define PAD_FMT_MONTH 0
51 55 #define PAD_FMT_WEEKOFYEAR 0
52 56 #define PAD_FMT_DAYOFMONTH 0
53 57 { "%02d", "%d", "%2d", "%02d" },
54 58 #define PAD_FMT_SDAYOFMONTH 1
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
55 59 #define PAD_FMT_SHMS 1
56 60 { "%2d", "%d", "%2d", "%02d" },
57 61 #define PAD_FMT_DAYOFYEAR 2
58 62 { "%03d", "%d", "%3d", "%03d" },
59 63 #define PAD_FMT_YEAR 3
60 64 { "%04d", "%d", "%4d", "%04d" }
61 65 };
62 66
63 67
64 68 size_t
65 -strftime(char *_RESTRICT_KYWD s, size_t maxsize,
66 - const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t)
69 +strftime_l(char *_RESTRICT_KYWD s, size_t maxsize,
70 + const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t,
71 + locale_t loc)
67 72 {
68 73 char *p;
69 74
70 75 tzset();
71 - p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize);
76 + p = _fmt(loc, ((format == NULL) ? "%c" : format), t, s, s + maxsize);
72 77 if (p == s + maxsize)
73 78 return (0);
74 79 *p = '\0';
75 80 return (p - s);
76 81 }
77 82
83 +size_t
84 +strftime(char *_RESTRICT_KYWD s, size_t maxsize,
85 + const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t)
86 +{
87 + return (strftime_l(s, maxsize, format, t, uselocale(NULL)));
88 +}
89 +
78 90 static char *
79 -_fmt(const char *format, const struct tm *t, char *pt, const char * const ptlim)
91 +_fmt(locale_t loc, const char *format, const struct tm *t, char *pt,
92 + const char * const ptlim)
80 93 {
81 94 int Ealternative, Oalternative, PadIndex;
82 - struct lc_time_T *tptr = __get_current_time_locale();
95 + const struct lc_time *tptr = loc->time;
83 96
84 97 #define PADDING(x) fmt_padding[x][PadIndex]
85 98
86 99 for (; *format; ++format) {
87 100 if (*format == '%') {
88 101 Ealternative = 0;
89 102 Oalternative = 0;
90 103 PadIndex = PAD_DEFAULT;
91 104 label:
92 105 switch (*++format) {
93 106 case '\0':
94 107 --format;
95 108 break;
96 109 case 'A':
97 110 pt = _add((t->tm_wday < 0 ||
98 111 t->tm_wday >= DAYSPERWEEK) ?
99 112 "?" : tptr->weekday[t->tm_wday],
100 113 pt, ptlim);
101 114 continue;
102 115 case 'a':
103 116 pt = _add((t->tm_wday < 0 ||
104 117 t->tm_wday >= DAYSPERWEEK) ?
105 118 "?" : tptr->wday[t->tm_wday],
106 119 pt, ptlim);
107 120 continue;
108 121 case 'B':
109 122 pt = _add((t->tm_mon < 0 ||
110 123 t->tm_mon >= MONSPERYEAR) ?
111 124 "?" : (tptr->month)[t->tm_mon],
112 125 pt, ptlim);
113 126 continue;
114 127 case 'b':
115 128 case 'h':
116 129 pt = _add((t->tm_mon < 0 ||
117 130 t->tm_mon >= MONSPERYEAR) ?
118 131 "?" : tptr->mon[t->tm_mon],
119 132 pt, ptlim);
120 133 continue;
121 134 case 'C':
122 135 /*
↓ open down ↓ |
30 lines elided |
↑ open up ↑ |
123 136 * %C used to do a...
124 137 * _fmt("%a %b %e %X %Y", t);
125 138 * ...whereas now POSIX 1003.2 calls for
126 139 * something completely different.
127 140 * (ado, 1993-05-24)
128 141 */
129 142 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
130 143 pt, ptlim);
131 144 continue;
132 145 case 'c':
133 - pt = _fmt(tptr->c_fmt, t, pt, ptlim);
146 + pt = _fmt(loc, tptr->c_fmt, t, pt, ptlim);
134 147 continue;
135 148 case 'D':
136 - pt = _fmt("%m/%d/%y", t, pt, ptlim);
149 + pt = _fmt(loc, "%m/%d/%y", t, pt, ptlim);
137 150 continue;
138 151 case 'd':
139 152 pt = _conv(t->tm_mday,
140 153 PADDING(PAD_FMT_DAYOFMONTH), pt, ptlim);
141 154 continue;
142 155 case 'E':
143 156 if (Ealternative || Oalternative)
144 157 break;
145 158 Ealternative++;
146 159 goto label;
147 160 case 'O':
148 161 /*
149 162 * C99 locale modifiers.
150 163 * The sequences
151 164 * %Ec %EC %Ex %EX %Ey %EY
152 165 * %Od %oe %OH %OI %Om %OM
153 166 * %OS %Ou %OU %OV %Ow %OW %Oy
154 167 * are supposed to provide alternate
155 168 * representations.
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
156 169 */
157 170 if (Ealternative || Oalternative)
158 171 break;
159 172 Oalternative++;
160 173 goto label;
161 174 case 'e':
162 175 pt = _conv(t->tm_mday,
163 176 PADDING(PAD_FMT_SDAYOFMONTH), pt, ptlim);
164 177 continue;
165 178 case 'F':
166 - pt = _fmt("%Y-%m-%d", t, pt, ptlim);
179 + pt = _fmt(loc, "%Y-%m-%d", t, pt, ptlim);
167 180 continue;
168 181 case 'H':
169 182 pt = _conv(t->tm_hour, PADDING(PAD_FMT_HMS),
170 183 pt, ptlim);
171 184 continue;
172 185 case 'I':
173 186 pt = _conv((t->tm_hour % 12) ?
174 187 (t->tm_hour % 12) : 12,
175 188 PADDING(PAD_FMT_HMS), pt, ptlim);
176 189 continue;
177 190 case 'j':
178 191 pt = _conv(t->tm_yday + 1,
179 192 PADDING(PAD_FMT_DAYOFYEAR), pt, ptlim);
180 193 continue;
181 194 case 'k':
182 195 /*
183 196 * This used to be...
184 197 * _conv(t->tm_hour % 12 ?
185 198 * t->tm_hour % 12 : 12, 2, ' ');
186 199 * ...and has been changed to the below to
187 200 * match SunOS 4.1.1 and Arnold Robbins'
188 201 * strftime version 3.0. That is, "%k" and
189 202 * "%l" have been swapped.
190 203 * (ado, 1993-05-24)
191 204 */
192 205 pt = _conv(t->tm_hour,
193 206 PADDING(PAD_FMT_SHMS), pt, ptlim);
194 207 continue;
195 208 case 'l':
196 209 /*
197 210 * This used to be...
198 211 * _conv(t->tm_hour, 2, ' ');
199 212 * ...and has been changed to the below to
200 213 * match SunOS 4.1.1 and Arnold Robbin's
201 214 * strftime version 3.0. That is, "%k" and
202 215 * "%l" have been swapped.
203 216 * (ado, 1993-05-24)
204 217 */
205 218 pt = _conv((t->tm_hour % 12) ?
206 219 (t->tm_hour % 12) : 12,
207 220 PADDING(PAD_FMT_SHMS), pt, ptlim);
208 221 continue;
209 222 case 'M':
210 223 pt = _conv(t->tm_min, PADDING(PAD_FMT_HMS),
211 224 pt, ptlim);
212 225 continue;
213 226 case 'm':
214 227 pt = _conv(t->tm_mon + 1,
215 228 PADDING(PAD_FMT_MONTH),
↓ open down ↓ |
39 lines elided |
↑ open up ↑ |
216 229 pt, ptlim);
217 230 continue;
218 231 case 'n':
219 232 pt = _add("\n", pt, ptlim);
220 233 continue;
221 234 case 'p':
222 235 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
223 236 tptr->pm : tptr->am, pt, ptlim);
224 237 continue;
225 238 case 'R':
226 - pt = _fmt("%H:%M", t, pt, ptlim);
239 + pt = _fmt(loc, "%H:%M", t, pt, ptlim);
227 240 continue;
228 241 case 'r':
229 - pt = _fmt(tptr->ampm_fmt, t, pt, ptlim);
242 + pt = _fmt(loc, tptr->ampm_fmt, t, pt, ptlim);
230 243 continue;
231 244 case 'S':
232 245 pt = _conv(t->tm_sec, PADDING(PAD_FMT_HMS),
233 246 pt, ptlim);
234 247 continue;
235 248
236 249 case 's':
237 250 {
238 251 struct tm tm;
239 252 char *buf;
240 253
241 254 tm = *t;
242 255 (void) asprintf(&buf, "%ld", mktime(&tm));
243 256 pt = _add(buf, pt, ptlim);
244 257 continue;
245 258 }
246 259
247 260 case 'T':
248 - pt = _fmt("%H:%M:%S", t, pt, ptlim);
261 + pt = _fmt(loc, "%H:%M:%S", t, pt, ptlim);
249 262 continue;
250 263 case 't':
251 264 pt = _add("\t", pt, ptlim);
252 265 continue;
253 266 case 'U':
254 267 pt = _conv((t->tm_yday + DAYSPERWEEK -
255 268 t->tm_wday) / DAYSPERWEEK,
256 269 PADDING(PAD_FMT_WEEKOFYEAR),
257 270 pt, ptlim);
258 271 continue;
259 272 case 'u':
260 273 /*
261 274 * From Arnold Robbins' strftime version 3.0:
262 275 * "ISO 8601: Weekday as a decimal number
263 276 * [1 (Monday) - 7]"
264 277 * (ado, 1993-05-24)
265 278 */
266 279 pt = _conv((t->tm_wday == 0) ?
267 280 DAYSPERWEEK : t->tm_wday,
268 281 "%d", pt, ptlim);
269 282 continue;
270 283 case 'V': /* ISO 8601 week number */
271 284 case 'G': /* ISO 8601 year (four digits) */
272 285 case 'g': /* ISO 8601 year (two digits) */
273 286 /*
274 287 * From Arnold Robbins' strftime version 3.0: "the week number of the
275 288 * year (the first Monday as the first day of week 1) as a decimal number
276 289 * (01-53)."
277 290 * (ado, 1993-05-24)
278 291 *
279 292 * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
280 293 * "Week 01 of a year is per definition the first week which has the
281 294 * Thursday in this year, which is equivalent to the week which contains
282 295 * the fourth day of January. In other words, the first week of a new year
283 296 * is the week which has the majority of its days in the new year. Week 01
284 297 * might also contain days from the previous year and the week before week
285 298 * 01 of a year is the last week (52 or 53) of the previous year even if
286 299 * it contains days from the new year. A week starts with Monday (day 1)
287 300 * and ends with Sunday (day 7). For example, the first week of the year
288 301 * 1997 lasts from 1996-12-30 to 1997-01-05..."
289 302 * (ado, 1996-01-02)
290 303 */
291 304 {
292 305 int year;
293 306 int base;
294 307 int yday;
295 308 int wday;
296 309 int w;
297 310
298 311 year = t->tm_year;
299 312 base = TM_YEAR_BASE;
300 313 yday = t->tm_yday;
301 314 wday = t->tm_wday;
302 315 for (;;) {
303 316 int len;
304 317 int bot;
305 318 int top;
306 319
307 320 len = isleap_sum(year, base) ?
308 321 DAYSPERLYEAR : DAYSPERNYEAR;
309 322 /*
310 323 * What yday (-3 ... 3) does
311 324 * the ISO year begin on?
312 325 */
313 326 bot = ((yday + 11 - wday) %
314 327 DAYSPERWEEK) - 3;
315 328 /*
316 329 * What yday does the NEXT
317 330 * ISO year begin on?
318 331 */
319 332 top = bot - (len % DAYSPERWEEK);
320 333 if (top < -3)
321 334 top += DAYSPERWEEK;
322 335 top += len;
323 336 if (yday >= top) {
324 337 ++base;
325 338 w = 1;
326 339 break;
327 340 }
328 341 if (yday >= bot) {
329 342 w = 1 + ((yday - bot) /
330 343 DAYSPERWEEK);
331 344 break;
332 345 }
333 346 --base;
334 347 yday += isleap_sum(year, base) ?
335 348 DAYSPERLYEAR : DAYSPERNYEAR;
336 349 }
337 350 #ifdef XPG4_1994_04_09
338 351 if ((w == 52 && t->tm_mon == TM_JANUARY) ||
339 352 (w == 1 && t->tm_mon == TM_DECEMBER))
340 353 w = 53;
341 354 #endif /* defined XPG4_1994_04_09 */
342 355 if (*format == 'V')
343 356 pt = _conv(w,
344 357 PADDING(PAD_FMT_WEEKOFYEAR),
345 358 pt, ptlim);
346 359 else if (*format == 'g') {
347 360 pt = _yconv(year, base, 0, 1,
348 361 pt, ptlim);
349 362 } else
↓ open down ↓ |
91 lines elided |
↑ open up ↑ |
350 363 pt = _yconv(year, base, 1, 1,
351 364 pt, ptlim);
352 365 }
353 366 continue;
354 367 case 'v':
355 368 /*
356 369 * From Arnold Robbins' strftime version 3.0:
357 370 * "date as dd-bbb-YYYY"
358 371 * (ado, 1993-05-24)
359 372 */
360 - pt = _fmt("%e-%b-%Y", t, pt, ptlim);
373 + pt = _fmt(loc, "%e-%b-%Y", t, pt, ptlim);
361 374 continue;
362 375 case 'W':
363 376 pt = _conv((t->tm_yday + DAYSPERWEEK -
364 377 (t->tm_wday ?
365 378 (t->tm_wday - 1) :
366 379 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
367 380 PADDING(PAD_FMT_WEEKOFYEAR),
368 381 pt, ptlim);
369 382 continue;
370 383 case 'w':
371 384 pt = _conv(t->tm_wday, "%d", pt, ptlim);
372 385 continue;
373 386 case 'X':
374 - pt = _fmt(tptr->X_fmt, t, pt, ptlim);
387 + pt = _fmt(loc, tptr->X_fmt, t, pt, ptlim);
375 388 continue;
376 389 case 'x':
377 - pt = _fmt(tptr->x_fmt, t, pt, ptlim);
390 + pt = _fmt(loc, tptr->x_fmt, t, pt, ptlim);
378 391 continue;
379 392 case 'y':
380 393 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
381 394 pt, ptlim);
382 395 continue;
383 396 case 'Y':
384 397 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
385 398 pt, ptlim);
386 399 continue;
387 400 case 'Z':
388 401 if (t->tm_isdst >= 0)
389 402 pt = _add(tzname[t->tm_isdst != 0],
390 403 pt, ptlim);
391 404 /*
392 405 * C99 says that %Z must be replaced by the
393 406 * empty string if the time zone is not
394 407 * determinable.
395 408 */
396 409 continue;
397 410 case 'z':
398 411 {
399 412 int diff;
400 413 char const * sign;
401 414
402 415 if (t->tm_isdst < 0)
403 416 continue;
404 417 /*
405 418 * C99 says that the UTC offset must
406 419 * be computed by looking only at
407 420 * tm_isdst. This requirement is
408 421 * incorrect, since it means the code
409 422 * must rely on magic (in this case
410 423 * altzone and timezone), and the
411 424 * magic might not have the correct
412 425 * offset. Doing things correctly is
413 426 * tricky and requires disobeying C99;
414 427 * see GNU C strftime for details.
415 428 * For now, punt and conform to the
416 429 * standard, even though it's incorrect.
417 430 *
418 431 * C99 says that %z must be replaced by the
419 432 * empty string if the time zone is not
420 433 * determinable, so output nothing if the
421 434 * appropriate variables are not available.
422 435 */
423 436 if (t->tm_isdst == 0)
424 437 diff = -timezone;
425 438 else
426 439 diff = -altzone;
427 440 if (diff < 0) {
428 441 sign = "-";
429 442 diff = -diff;
430 443 } else
↓ open down ↓ |
43 lines elided |
↑ open up ↑ |
431 444 sign = "+";
432 445 pt = _add(sign, pt, ptlim);
433 446 diff /= SECSPERMIN;
434 447 diff = (diff / MINSPERHOUR) * 100 +
435 448 (diff % MINSPERHOUR);
436 449 pt = _conv(diff, PADDING(PAD_FMT_YEAR),
437 450 pt, ptlim);
438 451 }
439 452 continue;
440 453 case '+':
441 - pt = _fmt(tptr->date_fmt, t, pt, ptlim);
454 + pt = _fmt(loc, tptr->date_fmt, t, pt, ptlim);
442 455 continue;
443 456 case '-':
444 457 if (PadIndex != PAD_DEFAULT)
445 458 break;
446 459 PadIndex = PAD_LESS;
447 460 goto label;
448 461 case '_':
449 462 if (PadIndex != PAD_DEFAULT)
450 463 break;
451 464 PadIndex = PAD_SPACE;
452 465 goto label;
453 466 case '0':
454 467 if (PadIndex != PAD_DEFAULT)
455 468 break;
456 469 PadIndex = PAD_ZERO;
457 470 goto label;
458 471 case '%':
459 472 /*
460 473 * X311J/88-090 (4.12.3.5): if conversion char is
461 474 * undefined, behavior is undefined. Print out the
462 475 * character itself as printf(3) also does.
463 476 */
464 477 default:
465 478 break;
466 479 }
467 480 }
468 481 if (pt == ptlim)
469 482 break;
470 483 *pt++ = *format;
471 484 }
472 485 return (pt);
473 486 }
474 487
475 488 static char *
476 489 _conv(const int n, const char *format, char *const pt,
477 490 const char *const ptlim)
478 491 {
479 492 char buf[12];
480 493
481 494 (void) sprintf(buf, format, n);
482 495 return (_add(buf, pt, ptlim));
483 496 }
484 497
485 498 static char *
486 499 _add(const char *str, char *pt, const char *const ptlim)
487 500 {
488 501 while (pt < ptlim && (*pt = *str++) != '\0')
489 502 ++pt;
490 503 return (pt);
491 504 }
492 505
493 506 /*
494 507 * POSIX and the C Standard are unclear or inconsistent about
495 508 * what %C and %y do if the year is negative or exceeds 9999.
496 509 * Use the convention that %C concatenated with %y yields the
497 510 * same output as %Y, and that %Y contains at least 4 bytes,
498 511 * with more only if necessary.
499 512 */
500 513
501 514 static char *
502 515 _yconv(const int a, const int b, const int convert_top, const int convert_yy,
503 516 char *pt, const char * const ptlim)
504 517 {
505 518 register int lead;
506 519 register int trail;
507 520
508 521 #define DIVISOR 100
509 522 trail = a % DIVISOR + b % DIVISOR;
510 523 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
511 524 trail %= DIVISOR;
512 525 if (trail < 0 && lead > 0) {
513 526 trail += DIVISOR;
514 527 --lead;
515 528 } else if (lead < 0 && trail > 0) {
516 529 trail -= DIVISOR;
517 530 ++lead;
518 531 }
519 532 if (convert_top) {
520 533 if (lead == 0 && trail < 0)
521 534 pt = _add("-0", pt, ptlim);
522 535 else pt = _conv(lead, "%02d", pt, ptlim);
523 536 }
524 537 if (convert_yy)
525 538 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
526 539 return (pt);
527 540 }
↓ open down ↓ |
76 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX