]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Time/strftime.c
Add device abstraction code for the UEFI Console and UEFI Shell-based file systems.
[mirror_edk2.git] / StdLib / LibC / Time / strftime.c
CommitLineData
2aa62f2b 1/** @file\r
2 Implementation of the strftime function for <time.h>.\r
3\r
4 Based on the UCB version with the ID appearing below.\r
5 This is ANSIish only when "multibyte character == plain character".\r
6\r
7 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
8 This program and the accompanying materials are licensed and made available under\r
9 the terms and conditions of the BSD License that accompanies this distribution.\r
10 The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php.\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16 Copyright (c) 1989, 1993\r
17 The Regents of the University of California. All rights reserved.\r
18\r
19 Redistribution and use in source and binary forms, with or without\r
20 modification, are permitted provided that the following conditions\r
21 are met:\r
22 1. Redistributions of source code must retain the above copyright\r
23 notice, this list of conditions and the following disclaimer.\r
24 2. Redistributions in binary form must reproduce the above copyright\r
25 notice, this list of conditions and the following disclaimer in the\r
26 documentation and/or other materials provided with the distribution.\r
27 3. All advertising materials mentioning features or use of this software\r
28 must display the following acknowledgement:\r
29 This product includes software developed by the University of\r
30 California, Berkeley and its contributors.\r
31 4. Neither the name of the University nor the names of its contributors\r
32 may be used to endorse or promote products derived from this software\r
33 without specific prior written permission.\r
34\r
35 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
36 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
37 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
38 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
39 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
40 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
41 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
43 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
44 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
45 SUCH DAMAGE.\r
46\r
47 NetBSD: strftime.c,v 1.17.4.1 2007/08/21 20:08:21 liamjfoy Exp\r
48**/\r
49#include <LibConfig.h>\r
50#include <sys/EfiCdefs.h>\r
51\r
52#include "namespace.h"\r
53#include <time.h>\r
54#include "tzfile.h"\r
53e1e5c6 55#include "TimeVals.h"\r
2aa62f2b 56#include "fcntl.h"\r
57#include "locale.h"\r
58\r
59#include "sys/localedef.h"\r
60#include <MainData.h>\r
61\r
62/*\r
63** We don't use these extensions in strftime operation even when\r
64** supported by the local tzcode configuration. A strictly\r
65** conforming C application may leave them in undefined state.\r
66*/\r
67\r
68#ifdef _LIBC\r
69#undef TM_ZONE\r
70#undef TM_GMTOFF\r
71#endif\r
72\r
73#define Locale _CurrentTimeLocale\r
74\r
75static char * EFIAPI _add(const char *, char *, const char * const);\r
76static char * EFIAPI _conv(const int, const char * const, char * const, const char * const);\r
77static char * EFIAPI _fmt(const char *, const struct tm * const, char *, const char * const, int *);\r
78\r
79#define NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU\r
80\r
81#ifndef YEAR_2000_NAME\r
82#define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"\r
83#endif /* !defined YEAR_2000_NAME */\r
84\r
85\r
86#define IN_NONE 0\r
87#define IN_SOME 1\r
88#define IN_THIS 2\r
89#define IN_ALL 3\r
90\r
91size_t\r
92EFIAPI\r
93strftime(\r
94 char * __restrict s,\r
95 size_t maxsize,\r
96 const char * __restrict format,\r
97 const struct tm * __restrict timeptr\r
98 )\r
99{\r
100 char * p;\r
101 int warn;\r
102\r
103 tzset();\r
104 warn = IN_NONE;\r
105 p = _fmt(((format == NULL) ? "%c" : format), timeptr, s, s + maxsize, &warn);\r
106\r
107#ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU\r
108 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {\r
109 (void) fprintf(stderr, "\n");\r
110 if (format == NULL)\r
111 (void) fprintf(stderr, "NULL strftime format ");\r
112 else (void) fprintf(stderr, "strftime format \"%s\" ",\r
113 format);\r
114 (void) fprintf(stderr, "yields only two digits of years in ");\r
115 if (warn == IN_SOME)\r
116 (void) fprintf(stderr, "some locales");\r
117 else if (warn == IN_THIS)\r
118 (void) fprintf(stderr, "the current locale");\r
119 else (void) fprintf(stderr, "all locales");\r
120 (void) fprintf(stderr, "\n");\r
121 }\r
122#endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */\r
123\r
124 if (p == s + maxsize)\r
125 return 0;\r
126 *p = '\0';\r
127 return p - s;\r
128}\r
129\r
130static char *\r
131EFIAPI\r
132_fmt(\r
133 const char * format,\r
134 const struct tm * const t,\r
135 char * pt,\r
136 const char * const ptlim,\r
137 int * warnp\r
138 )\r
139{\r
140 for ( ; *format; ++format) {\r
141 if (*format == '%') {\r
142label:\r
143 switch (*++format) {\r
144 case '\0':\r
145 --format;\r
146 break;\r
147 case 'A':\r
148 pt = _add((t->tm_wday < 0 ||\r
149 t->tm_wday >= DAYSPERWEEK) ?\r
150 "?" : Locale->day[t->tm_wday],\r
151 pt, ptlim);\r
152 continue;\r
153 case 'a':\r
154 pt = _add((t->tm_wday < 0 ||\r
155 t->tm_wday >= DAYSPERWEEK) ?\r
156 "?" : Locale->abday[t->tm_wday],\r
157 pt, ptlim);\r
158 continue;\r
159 case 'B':\r
160 pt = _add((t->tm_mon < 0 ||\r
161 t->tm_mon >= MONSPERYEAR) ?\r
162 "?" : Locale->mon[t->tm_mon],\r
163 pt, ptlim);\r
164 continue;\r
165 case 'b':\r
166 case 'h':\r
167 pt = _add((t->tm_mon < 0 ||\r
168 t->tm_mon >= MONSPERYEAR) ?\r
169 "?" : Locale->abmon[t->tm_mon],\r
170 pt, ptlim);\r
171 continue;\r
172 case 'C':\r
173 /*\r
174 ** %C used to do a...\r
175 ** _fmt("%a %b %e %X %Y", t);\r
176 ** ...whereas now POSIX 1003.2 calls for\r
177 ** something completely different.\r
178 ** (ado, 1993-05-24)\r
179 */\r
180 pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,\r
181 "%02d", pt, ptlim);\r
182 continue;\r
183 case 'c':\r
184 {\r
185 int warn2 = IN_SOME;\r
186\r
187 pt = _fmt(Locale->d_t_fmt, t, pt, ptlim, &warn2);\r
188 if (warn2 == IN_ALL)\r
189 warn2 = IN_THIS;\r
190 if (warn2 > *warnp)\r
191 *warnp = warn2;\r
192 }\r
193 continue;\r
194 case 'D':\r
195 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);\r
196 continue;\r
197 case 'd':\r
198 pt = _conv(t->tm_mday, "%02d", pt, ptlim);\r
199 continue;\r
200 case 'E':\r
201 case 'O':\r
202 /*\r
203 ** C99 locale modifiers.\r
204 ** The sequences\r
205 ** %Ec %EC %Ex %EX %Ey %EY\r
206 ** %Od %oe %OH %OI %Om %OM\r
207 ** %OS %Ou %OU %OV %Ow %OW %Oy\r
208 ** are supposed to provide alternate\r
209 ** representations.\r
210 */\r
211 goto label;\r
212 case 'e':\r
213 pt = _conv(t->tm_mday, "%2d", pt, ptlim);\r
214 continue;\r
215 case 'F':\r
216 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);\r
217 continue;\r
218 case 'H':\r
219 pt = _conv(t->tm_hour, "%02d", pt, ptlim);\r
220 continue;\r
221 case 'I':\r
222 pt = _conv((t->tm_hour % 12) ?\r
223 (t->tm_hour % 12) : 12,\r
224 "%02d", pt, ptlim);\r
225 continue;\r
226 case 'j':\r
227 pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);\r
228 continue;\r
229 case 'k':\r
230 /*\r
231 ** This used to be...\r
232 ** _conv(t->tm_hour % 12 ?\r
233 ** t->tm_hour % 12 : 12, 2, ' ');\r
234 ** ...and has been changed to the below to\r
235 ** match SunOS 4.1.1 and Arnold Robbins'\r
236 ** strftime version 3.0. That is, "%k" and\r
237 ** "%l" have been swapped.\r
238 ** (ado, 1993-05-24)\r
239 */\r
240 pt = _conv(t->tm_hour, "%2d", pt, ptlim);\r
241 continue;\r
242#ifdef KITCHEN_SINK\r
243 case 'K':\r
244 /*\r
245 ** After all this time, still unclaimed!\r
246 */\r
247 pt = _add("kitchen sink", pt, ptlim);\r
248 continue;\r
249#endif /* defined KITCHEN_SINK */\r
250 case 'l':\r
251 /*\r
252 ** This used to be...\r
253 ** _conv(t->tm_hour, 2, ' ');\r
254 ** ...and has been changed to the below to\r
255 ** match SunOS 4.1.1 and Arnold Robbin's\r
256 ** strftime version 3.0. That is, "%k" and\r
257 ** "%l" have been swapped.\r
258 ** (ado, 1993-05-24)\r
259 */\r
260 pt = _conv((t->tm_hour % 12) ?\r
261 (t->tm_hour % 12) : 12,\r
262 "%2d", pt, ptlim);\r
263 continue;\r
264 case 'M':\r
265 pt = _conv(t->tm_min, "%02d", pt, ptlim);\r
266 continue;\r
267 case 'm':\r
268 pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);\r
269 continue;\r
270 case 'n':\r
271 pt = _add("\n", pt, ptlim);\r
272 continue;\r
273 case 'p':\r
274 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?\r
275 Locale->am_pm[1] :\r
276 Locale->am_pm[0],\r
277 pt, ptlim);\r
278 continue;\r
279 case 'R':\r
280 pt = _fmt("%H:%M", t, pt, ptlim, warnp);\r
281 continue;\r
282 case 'r':\r
283 pt = _fmt(Locale->t_fmt_ampm, t, pt, ptlim,\r
284 warnp);\r
285 continue;\r
286 case 'S':\r
287 pt = _conv(t->tm_sec, "%02d", pt, ptlim);\r
288 continue;\r
289 case 's':\r
290 {\r
291 struct tm tm;\r
292 char buf[INT_STRLEN_MAXIMUM(\r
293 time_t) + 1];\r
294 time_t mkt;\r
295\r
296 tm = *t;\r
297 mkt = mktime(&tm);\r
298 /* CONSTCOND */\r
299 if (TYPE_SIGNED(time_t))\r
300 (void) sprintf(buf, "%ld",\r
301 (long) mkt);\r
302 else (void) sprintf(buf, "%lu",\r
303 (unsigned long) mkt);\r
304 pt = _add(buf, pt, ptlim);\r
305 }\r
306 continue;\r
307 case 'T':\r
308 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);\r
309 continue;\r
310 case 't':\r
311 pt = _add("\t", pt, ptlim);\r
312 continue;\r
313 case 'U':\r
314 pt = _conv((t->tm_yday + DAYSPERWEEK -\r
315 t->tm_wday) / DAYSPERWEEK,\r
316 "%02d", pt, ptlim);\r
317 continue;\r
318 case 'u':\r
319 /*\r
320 ** From Arnold Robbins' strftime version 3.0:\r
321 ** "ISO 8601: Weekday as a decimal number\r
322 ** [1 (Monday) - 7]"\r
323 ** (ado, 1993-05-24)\r
324 */\r
325 pt = _conv((t->tm_wday == 0) ?\r
326 DAYSPERWEEK : t->tm_wday,\r
327 "%d", pt, ptlim);\r
328 continue;\r
329 case 'V': /* ISO 8601 week number */\r
330 case 'G': /* ISO 8601 year (four digits) */\r
331 case 'g': /* ISO 8601 year (two digits) */\r
332/*\r
333** From Arnold Robbins' strftime version 3.0: "the week number of the\r
334** year (the first Monday as the first day of week 1) as a decimal number\r
335** (01-53)."\r
336** (ado, 1993-05-24)\r
337**\r
338** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:\r
339** "Week 01 of a year is per definition the first week which has the\r
340** Thursday in this year, which is equivalent to the week which contains\r
341** the fourth day of January. In other words, the first week of a new year\r
342** is the week which has the majority of its days in the new year. Week 01\r
343** might also contain days from the previous year and the week before week\r
344** 01 of a year is the last week (52 or 53) of the previous year even if\r
345** it contains days from the new year. A week starts with Monday (day 1)\r
346** and ends with Sunday (day 7). For example, the first week of the year\r
347** 1997 lasts from 1996-12-30 to 1997-01-05..."\r
348** (ado, 1996-01-02)\r
349*/\r
350 {\r
351 int year;\r
352 int yday;\r
353 int wday;\r
354 int w;\r
355\r
356 year = t->tm_year + TM_YEAR_BASE;\r
357 yday = t->tm_yday;\r
358 wday = t->tm_wday;\r
359 for ( ; ; ) {\r
360 int len;\r
361 int bot;\r
362 int top;\r
363\r
364 len = isleap(year) ?\r
365 DAYSPERLYEAR :\r
366 DAYSPERNYEAR;\r
367 /*\r
368 ** What yday (-3 ... 3) does\r
369 ** the ISO year begin on?\r
370 */\r
371 bot = ((yday + 11 - wday) %\r
372 DAYSPERWEEK) - 3;\r
373 /*\r
374 ** What yday does the NEXT\r
375 ** ISO year begin on?\r
376 */\r
377 top = bot -\r
378 (len % DAYSPERWEEK);\r
379 if (top < -3)\r
380 top += DAYSPERWEEK;\r
381 top += len;\r
382 if (yday >= top) {\r
383 ++year;\r
384 w = 1;\r
385 break;\r
386 }\r
387 if (yday >= bot) {\r
388 w = 1 + ((yday - bot) /\r
389 DAYSPERWEEK);\r
390 break;\r
391 }\r
392 --year;\r
393 yday += isleap(year) ?\r
394 DAYSPERLYEAR :\r
395 DAYSPERNYEAR;\r
396 }\r
397#ifdef XPG4_1994_04_09\r
398 if ((w == 52\r
399 && t->tm_mon == TM_JANUARY)\r
400 || (w == 1\r
401 && t->tm_mon == TM_DECEMBER))\r
402 w = 53;\r
403#endif /* defined XPG4_1994_04_09 */\r
404 if (*format == 'V')\r
405 pt = _conv(w, "%02d",\r
406 pt, ptlim);\r
407 else if (*format == 'g') {\r
408 *warnp = IN_ALL;\r
409 pt = _conv(year % 100, "%02d",\r
410 pt, ptlim);\r
411 } else pt = _conv(year, "%04d",\r
412 pt, ptlim);\r
413 }\r
414 continue;\r
415 case 'v':\r
416 /*\r
417 ** From Arnold Robbins' strftime version 3.0:\r
418 ** "date as dd-bbb-YYYY"\r
419 ** (ado, 1993-05-24)\r
420 */\r
421 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);\r
422 continue;\r
423 case 'W':\r
424 pt = _conv((t->tm_yday + DAYSPERWEEK -\r
425 (t->tm_wday ?\r
426 (t->tm_wday - 1) :\r
427 (DAYSPERWEEK - 1))) / DAYSPERWEEK,\r
428 "%02d", pt, ptlim);\r
429 continue;\r
430 case 'w':\r
431 pt = _conv(t->tm_wday, "%d", pt, ptlim);\r
432 continue;\r
433 case 'X':\r
434 pt = _fmt(Locale->t_fmt, t, pt, ptlim, warnp);\r
435 continue;\r
436 case 'x':\r
437 {\r
438 int warn2 = IN_SOME;\r
439\r
440 pt = _fmt(Locale->d_fmt, t, pt, ptlim, &warn2);\r
441 if (warn2 == IN_ALL)\r
442 warn2 = IN_THIS;\r
443 if (warn2 > *warnp)\r
444 *warnp = warn2;\r
445 }\r
446 continue;\r
447 case 'y':\r
448 *warnp = IN_ALL;\r
449 pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,\r
450 "%02d", pt, ptlim);\r
451 continue;\r
452 case 'Y':\r
453 pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",\r
454 pt, ptlim);\r
455 continue;\r
456 case 'Z':\r
457#ifdef TM_ZONE\r
458 if (t->TM_ZONE != NULL)\r
459 pt = _add(t->TM_ZONE, pt, ptlim);\r
460 else\r
461#endif /* defined TM_ZONE */\r
462 if (t->tm_isdst >= 0)\r
463 pt = _add(tzname[t->tm_isdst != 0],\r
464 pt, ptlim);\r
465 /*\r
466 ** C99 says that %Z must be replaced by the\r
467 ** empty string if the time zone is not\r
468 ** determinable.\r
469 */\r
470 continue;\r
471 case 'z':\r
472 {\r
473 int diff;\r
474 char const * sign;\r
475\r
476 if (t->tm_isdst < 0)\r
477 continue;\r
478#ifdef TM_GMTOFF\r
479 diff = (int)t->TM_GMTOFF;\r
480#else /* !defined TM_GMTOFF */\r
481 /*\r
482 ** C99 says that the UTC offset must\r
483 ** be computed by looking only at\r
484 ** tm_isdst. This requirement is\r
485 ** incorrect, since it means the code\r
486 ** must rely on magic (in this case\r
487 ** altzone and timezone), and the\r
488 ** magic might not have the correct\r
489 ** offset. Doing things correctly is\r
490 ** tricky and requires disobeying C99;\r
491 ** see GNU C strftime for details.\r
492 ** For now, punt and conform to the\r
493 ** standard, even though it's incorrect.\r
494 **\r
495 ** C99 says that %z must be replaced by the\r
496 ** empty string if the time zone is not\r
497 ** determinable, so output nothing if the\r
498 ** appropriate variables are not available.\r
499 */\r
500#ifndef STD_INSPIRED\r
501 if (t->tm_isdst == 0)\r
502#ifdef USG_COMPAT\r
503 diff = -timezone;\r
504#else /* !defined USG_COMPAT */\r
505 continue;\r
506#endif /* !defined USG_COMPAT */\r
507 else\r
508#ifdef ALTZONE\r
509 diff = -altzone;\r
510#else /* !defined ALTZONE */\r
511 continue;\r
512#endif /* !defined ALTZONE */\r
513#else /* defined STD_INSPIRED */\r
514 {\r
515 struct tm tmp;\r
516 time_t lct, gct;\r
517\r
518 /*\r
519 ** Get calendar time from t\r
520 ** being treated as local.\r
521 */\r
522 tmp = *t; /* mktime discards const */\r
523 lct = mktime(&tmp);\r
524\r
525 if (lct == (time_t)-1)\r
526 continue;\r
527\r
528 /*\r
529 ** Get calendar time from t\r
530 ** being treated as GMT.\r
531 **/\r
532 tmp = *t; /* mktime discards const */\r
533 gct = timegm(&tmp);\r
534\r
535 if (gct == (time_t)-1)\r
536 continue;\r
537\r
538 /* LINTED difference will fit int */\r
539 diff = (intmax_t)gct - (intmax_t)lct;\r
540 }\r
541#endif /* defined STD_INSPIRED */\r
542#endif /* !defined TM_GMTOFF */\r
543 if (diff < 0) {\r
544 sign = "-";\r
545 diff = -diff;\r
546 } else sign = "+";\r
547 pt = _add(sign, pt, ptlim);\r
548 diff /= 60;\r
549 pt = _conv((diff/60)*100 + diff%60,\r
550 "%04d", pt, ptlim);\r
551 }\r
552 continue;\r
553#if 0\r
554 case '+':\r
555 pt = _fmt(Locale->date_fmt, t, pt, ptlim,\r
556 warnp);\r
557 continue;\r
558#endif\r
559 case '%':\r
560 /*\r
561 ** X311J/88-090 (4.12.3.5): if conversion char is\r
562 ** undefined, behavior is undefined. Print out the\r
563 ** character itself as printf(3) also does.\r
564 */\r
565 default:\r
566 break;\r
567 }\r
568 }\r
569 if (pt == ptlim)\r
570 break;\r
571 *pt++ = *format;\r
572 }\r
573 return pt;\r
574}\r
575\r
576static char *\r
577EFIAPI\r
578_conv(\r
579 const int n,\r
580 const char * const format,\r
581 char * const pt,\r
582 const char * const ptlim\r
583)\r
584{\r
585 char buf[INT_STRLEN_MAXIMUM(int) + 1];\r
586\r
587 (void) sprintf(buf, format, n);\r
588 return _add(buf, pt, ptlim);\r
589}\r
590\r
591static char *\r
592EFIAPI\r
593_add(\r
594 const char * str,\r
595 char * pt,\r
596 const char * const ptlim\r
597)\r
598{\r
599 while (pt < ptlim && (*pt = *str++) != '\0')\r
600 ++pt;\r
601 return pt;\r
602}\r