1 /*
2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 * Copyright (c) 1989 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms are permitted
7 * provided that the above copyright notice and this paragraph are
8 * duplicated in all such forms and that any documentation,
9 * advertising materials, and other materials related to such
10 * distribution and use acknowledge that the software was developed
11 * by the University of California, Berkeley. The name of the
12 * University may not be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 */
18
19 #include "lint.h"
20 #include "tzfile.h"
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "timelocal.h"
26
27 static char *_add(const char *, char *, const char *);
28 static char *_conv(int, const char *, char *, const char *);
29 static char *_fmt(const char *, const struct tm *, char *, const char * const);
30 static char *_yconv(int, int, int, int, char *, const char *);
31
32 extern char *tzname[];
33
34 #define IN_NONE 0
35 #define IN_SOME 1
36 #define IN_THIS 2
37 #define IN_ALL 3
38
39 #define PAD_DEFAULT 0
40 #define PAD_LESS 1
41 #define PAD_SPACE 2
42 #define PAD_ZERO 3
43
44 static const char *fmt_padding[][4] = {
45 /* DEFAULT, LESS, SPACE, ZERO */
46 #define PAD_FMT_MONTHDAY 0
47 #define PAD_FMT_HMS 0
48 #define PAD_FMT_CENTURY 0
49 #define PAD_FMT_SHORTYEAR 0
50 #define PAD_FMT_MONTH 0
51 #define PAD_FMT_WEEKOFYEAR 0
52 #define PAD_FMT_DAYOFMONTH 0
53 { "%02d", "%d", "%2d", "%02d" },
54 #define PAD_FMT_SDAYOFMONTH 1
55 #define PAD_FMT_SHMS 1
56 { "%2d", "%d", "%2d", "%02d" },
57 #define PAD_FMT_DAYOFYEAR 2
58 { "%03d", "%d", "%3d", "%03d" },
59 #define PAD_FMT_YEAR 3
60 { "%04d", "%d", "%4d", "%04d" }
61 };
62
63
64 size_t
65 strftime(char *_RESTRICT_KYWD s, size_t maxsize,
66 const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t)
67 {
68 char *p;
69
70 tzset();
71 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize);
72 if (p == s + maxsize)
73 return (0);
74 *p = '\0';
75 return (p - s);
76 }
77
78 static char *
79 _fmt(const char *format, const struct tm *t, char *pt, const char * const ptlim)
80 {
81 int Ealternative, Oalternative, PadIndex;
82 struct lc_time_T *tptr = __get_current_time_locale();
83
84 #define PADDING(x) fmt_padding[x][PadIndex]
85
86 for (; *format; ++format) {
87 if (*format == '%') {
88 Ealternative = 0;
89 Oalternative = 0;
90 PadIndex = PAD_DEFAULT;
91 label:
92 switch (*++format) {
93 case '\0':
94 --format;
95 break;
96 case 'A':
97 pt = _add((t->tm_wday < 0 ||
98 t->tm_wday >= DAYSPERWEEK) ?
99 "?" : tptr->weekday[t->tm_wday],
100 pt, ptlim);
101 continue;
102 case 'a':
113 continue;
114 case 'b':
115 case 'h':
116 pt = _add((t->tm_mon < 0 ||
117 t->tm_mon >= MONSPERYEAR) ?
118 "?" : tptr->mon[t->tm_mon],
119 pt, ptlim);
120 continue;
121 case 'C':
122 /*
123 * %C used to do a...
124 * _fmt("%a %b %e %X %Y", t);
125 * ...whereas now POSIX 1003.2 calls for
126 * something completely different.
127 * (ado, 1993-05-24)
128 */
129 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
130 pt, ptlim);
131 continue;
132 case 'c':
133 pt = _fmt(tptr->c_fmt, t, pt, ptlim);
134 continue;
135 case 'D':
136 pt = _fmt("%m/%d/%y", t, pt, ptlim);
137 continue;
138 case 'd':
139 pt = _conv(t->tm_mday,
140 PADDING(PAD_FMT_DAYOFMONTH), pt, ptlim);
141 continue;
142 case 'E':
143 if (Ealternative || Oalternative)
144 break;
145 Ealternative++;
146 goto label;
147 case 'O':
148 /*
149 * C99 locale modifiers.
150 * The sequences
151 * %Ec %EC %Ex %EX %Ey %EY
152 * %Od %oe %OH %OI %Om %OM
153 * %OS %Ou %OU %OV %Ow %OW %Oy
154 * are supposed to provide alternate
155 * representations.
156 */
157 if (Ealternative || Oalternative)
158 break;
159 Oalternative++;
160 goto label;
161 case 'e':
162 pt = _conv(t->tm_mday,
163 PADDING(PAD_FMT_SDAYOFMONTH), pt, ptlim);
164 continue;
165 case 'F':
166 pt = _fmt("%Y-%m-%d", t, pt, ptlim);
167 continue;
168 case 'H':
169 pt = _conv(t->tm_hour, PADDING(PAD_FMT_HMS),
170 pt, ptlim);
171 continue;
172 case 'I':
173 pt = _conv((t->tm_hour % 12) ?
174 (t->tm_hour % 12) : 12,
175 PADDING(PAD_FMT_HMS), pt, ptlim);
176 continue;
177 case 'j':
178 pt = _conv(t->tm_yday + 1,
179 PADDING(PAD_FMT_DAYOFYEAR), pt, ptlim);
180 continue;
181 case 'k':
182 /*
183 * This used to be...
184 * _conv(t->tm_hour % 12 ?
185 * t->tm_hour % 12 : 12, 2, ' ');
186 * ...and has been changed to the below to
206 (t->tm_hour % 12) : 12,
207 PADDING(PAD_FMT_SHMS), pt, ptlim);
208 continue;
209 case 'M':
210 pt = _conv(t->tm_min, PADDING(PAD_FMT_HMS),
211 pt, ptlim);
212 continue;
213 case 'm':
214 pt = _conv(t->tm_mon + 1,
215 PADDING(PAD_FMT_MONTH),
216 pt, ptlim);
217 continue;
218 case 'n':
219 pt = _add("\n", pt, ptlim);
220 continue;
221 case 'p':
222 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
223 tptr->pm : tptr->am, pt, ptlim);
224 continue;
225 case 'R':
226 pt = _fmt("%H:%M", t, pt, ptlim);
227 continue;
228 case 'r':
229 pt = _fmt(tptr->ampm_fmt, t, pt, ptlim);
230 continue;
231 case 'S':
232 pt = _conv(t->tm_sec, PADDING(PAD_FMT_HMS),
233 pt, ptlim);
234 continue;
235
236 case 's':
237 {
238 struct tm tm;
239 char *buf;
240
241 tm = *t;
242 (void) asprintf(&buf, "%ld", mktime(&tm));
243 pt = _add(buf, pt, ptlim);
244 continue;
245 }
246
247 case 'T':
248 pt = _fmt("%H:%M:%S", t, pt, ptlim);
249 continue;
250 case 't':
251 pt = _add("\t", pt, ptlim);
252 continue;
253 case 'U':
254 pt = _conv((t->tm_yday + DAYSPERWEEK -
255 t->tm_wday) / DAYSPERWEEK,
256 PADDING(PAD_FMT_WEEKOFYEAR),
257 pt, ptlim);
258 continue;
259 case 'u':
260 /*
261 * From Arnold Robbins' strftime version 3.0:
262 * "ISO 8601: Weekday as a decimal number
263 * [1 (Monday) - 7]"
264 * (ado, 1993-05-24)
265 */
266 pt = _conv((t->tm_wday == 0) ?
267 DAYSPERWEEK : t->tm_wday,
268 "%d", pt, ptlim);
340 w = 53;
341 #endif /* defined XPG4_1994_04_09 */
342 if (*format == 'V')
343 pt = _conv(w,
344 PADDING(PAD_FMT_WEEKOFYEAR),
345 pt, ptlim);
346 else if (*format == 'g') {
347 pt = _yconv(year, base, 0, 1,
348 pt, ptlim);
349 } else
350 pt = _yconv(year, base, 1, 1,
351 pt, ptlim);
352 }
353 continue;
354 case 'v':
355 /*
356 * From Arnold Robbins' strftime version 3.0:
357 * "date as dd-bbb-YYYY"
358 * (ado, 1993-05-24)
359 */
360 pt = _fmt("%e-%b-%Y", t, pt, ptlim);
361 continue;
362 case 'W':
363 pt = _conv((t->tm_yday + DAYSPERWEEK -
364 (t->tm_wday ?
365 (t->tm_wday - 1) :
366 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
367 PADDING(PAD_FMT_WEEKOFYEAR),
368 pt, ptlim);
369 continue;
370 case 'w':
371 pt = _conv(t->tm_wday, "%d", pt, ptlim);
372 continue;
373 case 'X':
374 pt = _fmt(tptr->X_fmt, t, pt, ptlim);
375 continue;
376 case 'x':
377 pt = _fmt(tptr->x_fmt, t, pt, ptlim);
378 continue;
379 case 'y':
380 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
381 pt, ptlim);
382 continue;
383 case 'Y':
384 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
385 pt, ptlim);
386 continue;
387 case 'Z':
388 if (t->tm_isdst >= 0)
389 pt = _add(tzname[t->tm_isdst != 0],
390 pt, ptlim);
391 /*
392 * C99 says that %Z must be replaced by the
393 * empty string if the time zone is not
394 * determinable.
395 */
396 continue;
397 case 'z':
421 * appropriate variables are not available.
422 */
423 if (t->tm_isdst == 0)
424 diff = -timezone;
425 else
426 diff = -altzone;
427 if (diff < 0) {
428 sign = "-";
429 diff = -diff;
430 } else
431 sign = "+";
432 pt = _add(sign, pt, ptlim);
433 diff /= SECSPERMIN;
434 diff = (diff / MINSPERHOUR) * 100 +
435 (diff % MINSPERHOUR);
436 pt = _conv(diff, PADDING(PAD_FMT_YEAR),
437 pt, ptlim);
438 }
439 continue;
440 case '+':
441 pt = _fmt(tptr->date_fmt, t, pt, ptlim);
442 continue;
443 case '-':
444 if (PadIndex != PAD_DEFAULT)
445 break;
446 PadIndex = PAD_LESS;
447 goto label;
448 case '_':
449 if (PadIndex != PAD_DEFAULT)
450 break;
451 PadIndex = PAD_SPACE;
452 goto label;
453 case '0':
454 if (PadIndex != PAD_DEFAULT)
455 break;
456 PadIndex = PAD_ZERO;
457 goto label;
458 case '%':
459 /*
460 * X311J/88-090 (4.12.3.5): if conversion char is
461 * undefined, behavior is undefined. Print out the
|
1 /*
2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 * Copyright (c) 1989 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Copyright (c) 2011 The FreeBSD Foundation
7 * All rights reserved.
8 * Portions of this software were developed by David Chisnall
9 * under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms are permitted
12 * provided that the above copyright notice and this paragraph are
13 * duplicated in all such forms and that any documentation,
14 * advertising materials, and other materials related to such
15 * distribution and use acknowledge that the software was developed
16 * by the University of California, Berkeley. The name of the
17 * University may not be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 */
23
24 #include "lint.h"
25 #include "tzfile.h"
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include "timelocal.h"
31
32 static char *_add(const char *, char *, const char *);
33 static char *_conv(int, const char *, char *, const char *);
34 static char *_fmt(const char *, const struct tm *, char *, const char * const,
35 int *, locale_t);
36 static char *_yconv(int, int, int, int, char *, const char *);
37
38 extern char *tzname[];
39
40 #define IN_NONE 0
41 #define IN_SOME 1
42 #define IN_THIS 2
43 #define IN_ALL 3
44
45 #define PAD_DEFAULT 0
46 #define PAD_LESS 1
47 #define PAD_SPACE 2
48 #define PAD_ZERO 3
49
50 static const char *fmt_padding[][4] = {
51 /* DEFAULT, LESS, SPACE, ZERO */
52 #define PAD_FMT_MONTHDAY 0
53 #define PAD_FMT_HMS 0
54 #define PAD_FMT_CENTURY 0
55 #define PAD_FMT_SHORTYEAR 0
56 #define PAD_FMT_MONTH 0
57 #define PAD_FMT_WEEKOFYEAR 0
58 #define PAD_FMT_DAYOFMONTH 0
59 { "%02d", "%d", "%2d", "%02d" },
60 #define PAD_FMT_SDAYOFMONTH 1
61 #define PAD_FMT_SHMS 1
62 { "%2d", "%d", "%2d", "%02d" },
63 #define PAD_FMT_DAYOFYEAR 2
64 { "%03d", "%d", "%3d", "%03d" },
65 #define PAD_FMT_YEAR 3
66 { "%04d", "%d", "%4d", "%04d" }
67 };
68
69
70 size_t
71 strftime_l(char *_RESTRICT_KYWD s, size_t maxsize,
72 const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t,
73 locale_t loc)
74 {
75 char *p;
76 int warn;
77 FIX_LOCALE(loc);
78
79 tzset();
80 warn = IN_NONE;
81 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize,
82 &warn, loc);
83 /* XXX YEAR_2000 */
84 if (p == s + maxsize)
85 return (0);
86 *p = '\0';
87 return (p - s);
88 }
89
90 size_t
91 strftime(char *_RESTRICT_KYWD s, size_t maxsize,
92 const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t)
93 {
94 return (strftime_l(s, maxsize, format, t, __get_locale()));
95 }
96
97 static char *
98 _fmt(const char *format, const struct tm *t, char *pt, const char * const ptlim,
99 int *warnp, locale_t loc)
100 {
101 int Ealternative, Oalternative, PadIndex;
102 struct lc_time_T *tptr = __get_current_time_locale(loc);
103
104 #define PADDING(x) fmt_padding[x][PadIndex]
105
106 for (; *format; ++format) {
107 if (*format == '%') {
108 Ealternative = 0;
109 Oalternative = 0;
110 PadIndex = PAD_DEFAULT;
111 label:
112 switch (*++format) {
113 case '\0':
114 --format;
115 break;
116 case 'A':
117 pt = _add((t->tm_wday < 0 ||
118 t->tm_wday >= DAYSPERWEEK) ?
119 "?" : tptr->weekday[t->tm_wday],
120 pt, ptlim);
121 continue;
122 case 'a':
133 continue;
134 case 'b':
135 case 'h':
136 pt = _add((t->tm_mon < 0 ||
137 t->tm_mon >= MONSPERYEAR) ?
138 "?" : tptr->mon[t->tm_mon],
139 pt, ptlim);
140 continue;
141 case 'C':
142 /*
143 * %C used to do a...
144 * _fmt("%a %b %e %X %Y", t);
145 * ...whereas now POSIX 1003.2 calls for
146 * something completely different.
147 * (ado, 1993-05-24)
148 */
149 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
150 pt, ptlim);
151 continue;
152 case 'c':
153 {
154 int warn2 = IN_SOME;
155
156 pt = _fmt(tptr->c_fmt, t, pt, ptlim, &warn2,
157 loc);
158 /* XXX */
159 }
160 continue;
161 case 'D':
162 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp,
163 loc);
164 continue;
165 case 'd':
166 pt = _conv(t->tm_mday,
167 PADDING(PAD_FMT_DAYOFMONTH), pt, ptlim);
168 continue;
169 case 'E':
170 if (Ealternative || Oalternative)
171 break;
172 Ealternative++;
173 goto label;
174 case 'O':
175 /*
176 * C99 locale modifiers.
177 * The sequences
178 * %Ec %EC %Ex %EX %Ey %EY
179 * %Od %oe %OH %OI %Om %OM
180 * %OS %Ou %OU %OV %Ow %OW %Oy
181 * are supposed to provide alternate
182 * representations.
183 */
184 if (Ealternative || Oalternative)
185 break;
186 Oalternative++;
187 goto label;
188 case 'e':
189 pt = _conv(t->tm_mday,
190 PADDING(PAD_FMT_SDAYOFMONTH), pt, ptlim);
191 continue;
192 case 'F':
193 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp,
194 loc);
195 continue;
196 case 'H':
197 pt = _conv(t->tm_hour, PADDING(PAD_FMT_HMS),
198 pt, ptlim);
199 continue;
200 case 'I':
201 pt = _conv((t->tm_hour % 12) ?
202 (t->tm_hour % 12) : 12,
203 PADDING(PAD_FMT_HMS), pt, ptlim);
204 continue;
205 case 'j':
206 pt = _conv(t->tm_yday + 1,
207 PADDING(PAD_FMT_DAYOFYEAR), pt, ptlim);
208 continue;
209 case 'k':
210 /*
211 * This used to be...
212 * _conv(t->tm_hour % 12 ?
213 * t->tm_hour % 12 : 12, 2, ' ');
214 * ...and has been changed to the below to
234 (t->tm_hour % 12) : 12,
235 PADDING(PAD_FMT_SHMS), pt, ptlim);
236 continue;
237 case 'M':
238 pt = _conv(t->tm_min, PADDING(PAD_FMT_HMS),
239 pt, ptlim);
240 continue;
241 case 'm':
242 pt = _conv(t->tm_mon + 1,
243 PADDING(PAD_FMT_MONTH),
244 pt, ptlim);
245 continue;
246 case 'n':
247 pt = _add("\n", pt, ptlim);
248 continue;
249 case 'p':
250 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
251 tptr->pm : tptr->am, pt, ptlim);
252 continue;
253 case 'R':
254 pt = _fmt("%H:%M", t, pt, ptlim, warnp, loc);
255 continue;
256 case 'r':
257 pt = _fmt(tptr->ampm_fmt, t, pt, ptlim,
258 warnp, loc);
259 continue;
260 case 'S':
261 pt = _conv(t->tm_sec, PADDING(PAD_FMT_HMS),
262 pt, ptlim);
263 continue;
264
265 case 's':
266 {
267 struct tm tm;
268 char *buf;
269 /* XXX */
270 tm = *t;
271 (void) asprintf(&buf, "%ld", mktime(&tm));
272 pt = _add(buf, pt, ptlim);
273 }
274 continue;
275 case 'T':
276 pt = _fmt("%H:%M:%S", t, pt, ptlim,
277 warnp, loc);
278 continue;
279 case 't':
280 pt = _add("\t", pt, ptlim);
281 continue;
282 case 'U':
283 pt = _conv((t->tm_yday + DAYSPERWEEK -
284 t->tm_wday) / DAYSPERWEEK,
285 PADDING(PAD_FMT_WEEKOFYEAR),
286 pt, ptlim);
287 continue;
288 case 'u':
289 /*
290 * From Arnold Robbins' strftime version 3.0:
291 * "ISO 8601: Weekday as a decimal number
292 * [1 (Monday) - 7]"
293 * (ado, 1993-05-24)
294 */
295 pt = _conv((t->tm_wday == 0) ?
296 DAYSPERWEEK : t->tm_wday,
297 "%d", pt, ptlim);
369 w = 53;
370 #endif /* defined XPG4_1994_04_09 */
371 if (*format == 'V')
372 pt = _conv(w,
373 PADDING(PAD_FMT_WEEKOFYEAR),
374 pt, ptlim);
375 else if (*format == 'g') {
376 pt = _yconv(year, base, 0, 1,
377 pt, ptlim);
378 } else
379 pt = _yconv(year, base, 1, 1,
380 pt, ptlim);
381 }
382 continue;
383 case 'v':
384 /*
385 * From Arnold Robbins' strftime version 3.0:
386 * "date as dd-bbb-YYYY"
387 * (ado, 1993-05-24)
388 */
389 pt = _fmt("%e-%b-%Y", t, pt, ptlim,
390 warnp, loc);
391 continue;
392 case 'W':
393 pt = _conv((t->tm_yday + DAYSPERWEEK -
394 (t->tm_wday ?
395 (t->tm_wday - 1) :
396 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
397 PADDING(PAD_FMT_WEEKOFYEAR),
398 pt, ptlim);
399 continue;
400 case 'w':
401 pt = _conv(t->tm_wday, "%d", pt, ptlim);
402 continue;
403 case 'X':
404 pt = _fmt(tptr->X_fmt, t, pt, ptlim,
405 warnp, loc);
406 continue;
407 case 'x':
408 {
409 int warn2 = IN_SOME;
410
411 pt = _fmt(tptr->x_fmt, t, pt, ptlim,
412 &warn2, loc);
413 }
414 continue;
415 case 'y':
416 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
417 pt, ptlim);
418 continue;
419 case 'Y':
420 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
421 pt, ptlim);
422 continue;
423 case 'Z':
424 if (t->tm_isdst >= 0)
425 pt = _add(tzname[t->tm_isdst != 0],
426 pt, ptlim);
427 /*
428 * C99 says that %Z must be replaced by the
429 * empty string if the time zone is not
430 * determinable.
431 */
432 continue;
433 case 'z':
457 * appropriate variables are not available.
458 */
459 if (t->tm_isdst == 0)
460 diff = -timezone;
461 else
462 diff = -altzone;
463 if (diff < 0) {
464 sign = "-";
465 diff = -diff;
466 } else
467 sign = "+";
468 pt = _add(sign, pt, ptlim);
469 diff /= SECSPERMIN;
470 diff = (diff / MINSPERHOUR) * 100 +
471 (diff % MINSPERHOUR);
472 pt = _conv(diff, PADDING(PAD_FMT_YEAR),
473 pt, ptlim);
474 }
475 continue;
476 case '+':
477 pt = _fmt(tptr->date_fmt, t, pt, ptlim,
478 warnp, loc);
479 continue;
480 case '-':
481 if (PadIndex != PAD_DEFAULT)
482 break;
483 PadIndex = PAD_LESS;
484 goto label;
485 case '_':
486 if (PadIndex != PAD_DEFAULT)
487 break;
488 PadIndex = PAD_SPACE;
489 goto label;
490 case '0':
491 if (PadIndex != PAD_DEFAULT)
492 break;
493 PadIndex = PAD_ZERO;
494 goto label;
495 case '%':
496 /*
497 * X311J/88-090 (4.12.3.5): if conversion char is
498 * undefined, behavior is undefined. Print out the
|