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