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