strftime.c revision edbe7fc97bab7ff0684053d1be564330689bf3ad
1#ifndef lint 2#ifndef NOID 3static char elsieid[] = "@(#)strftime.c 8.1"; 4/* 5** Based on the UCB version with the ID appearing below. 6** This is ANSIish only when "multibyte character == plain character". 7*/ 8#endif /* !defined NOID */ 9#endif /* !defined lint */ 10 11#include "private.h" 12 13/* 14** Copyright (c) 1989 The Regents of the University of California. 15** All rights reserved. 16** 17** Redistribution and use in source and binary forms are permitted 18** provided that the above copyright notice and this paragraph are 19** duplicated in all such forms and that any documentation, 20** advertising materials, and other materials related to such 21** distribution and use acknowledge that the software was developed 22** by the University of California, Berkeley. The name of the 23** University may not be used to endorse or promote products derived 24** from this software without specific prior written permission. 25** THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 26** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 27** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 28*/ 29 30#ifndef LIBC_SCCS 31#ifndef lint 32static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89"; 33#endif /* !defined lint */ 34#endif /* !defined LIBC_SCCS */ 35 36#include "tzfile.h" 37#include "fcntl.h" 38#include "locale.h" 39#include <ctype.h> 40#include <time64.h> 41 42/* struct lc_time_T is now defined as strftime_locale 43 * in <time.h> 44 */ 45#if 1 46#define lc_time_T strftime_locale 47#else 48struct lc_time_T { 49 const char * mon[MONSPERYEAR]; 50 const char * month[MONSPERYEAR]; 51 const char * wday[DAYSPERWEEK]; 52 const char * weekday[DAYSPERWEEK]; 53 const char * X_fmt; 54 const char * x_fmt; 55 const char * c_fmt; 56 const char * am; 57 const char * pm; 58 const char * date_fmt; 59}; 60#endif 61 62#define Locale (&C_time_locale) 63 64static const struct lc_time_T C_time_locale = { 65 { 66 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 67 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 68 }, { 69 "January", "February", "March", "April", "May", "June", 70 "July", "August", "September", "October", "November", "December" 71 }, { 72 "Sun", "Mon", "Tue", "Wed", 73 "Thu", "Fri", "Sat" 74 }, { 75 "Sunday", "Monday", "Tuesday", "Wednesday", 76 "Thursday", "Friday", "Saturday" 77 }, 78 79 /* X_fmt */ 80 "%H:%M:%S", 81 82 /* 83 ** x_fmt 84 ** C99 requires this format. 85 ** Using just numbers (as here) makes Quakers happier; 86 ** it's also compatible with SVR4. 87 */ 88 "%m/%d/%y", 89 90 /* 91 ** c_fmt 92 ** C99 requires this format. 93 ** Previously this code used "%D %X", but we now conform to C99. 94 ** Note that 95 ** "%a %b %d %H:%M:%S %Y" 96 ** is used by Solaris 2.3. 97 */ 98 "%a %b %e %T %Y", 99 100 /* am */ 101 "AM", 102 103 /* pm */ 104 "PM", 105 106 /* date_fmt */ 107 "%a %b %e %H:%M:%S %Z %Y" 108}; 109 110static char * _add P((const char *, char *, const char *, int)); 111static char * _conv P((int, const char *, char *, const char *)); 112static char * _fmt P((const char *, const struct tm *, char *, const char *, 113 int *, const struct strftime_locale*)); 114static char * _yconv P((int, int, int, int, char *, const char *, int)); 115static char * getformat P((int, char *, char *, char *, char *)); 116 117extern char * tzname[]; 118 119#ifndef YEAR_2000_NAME 120#define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS" 121#endif /* !defined YEAR_2000_NAME */ 122 123#define IN_NONE 0 124#define IN_SOME 1 125#define IN_THIS 2 126#define IN_ALL 3 127 128#define FORCE_LOWER_CASE 0x100 129 130size_t 131strftime(s, maxsize, format, t) 132char * const s; 133const size_t maxsize; 134const char * const format; 135const struct tm * const t; 136{ 137 return strftime_tz(s, maxsize, format, t, Locale); 138} 139 140size_t 141strftime_tz(s, maxsize, format, t, locale) 142char * const s; 143const size_t maxsize; 144const char * const format; 145const struct tm * const t; 146const struct strftime_locale *locale; 147{ 148 char * p; 149 int warn; 150 151 tzset(); 152 warn = IN_NONE; 153 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, locale); 154#if 0 /* ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */ 155 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) { 156 (void) fprintf(stderr, "\n"); 157 if (format == NULL) 158 (void) fprintf(stderr, "NULL strftime format "); 159 else (void) fprintf(stderr, "strftime format \"%s\" ", 160 format); 161 (void) fprintf(stderr, "yields only two digits of years in "); 162 if (warn == IN_SOME) 163 (void) fprintf(stderr, "some locales"); 164 else if (warn == IN_THIS) 165 (void) fprintf(stderr, "the current locale"); 166 else (void) fprintf(stderr, "all locales"); 167 (void) fprintf(stderr, "\n"); 168 } 169#endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */ 170 if (p == s + maxsize) 171 return 0; 172 *p = '\0'; 173 return p - s; 174} 175 176static char *getformat(int modifier, char *normal, char *underscore, 177 char *dash, char *zero) { 178 switch (modifier) { 179 case '_': 180 return underscore; 181 182 case '-': 183 return dash; 184 185 case '0': 186 return zero; 187 } 188 189 return normal; 190} 191 192static char * 193_fmt(format, t, pt, ptlim, warnp, locale) 194const char * format; 195const struct tm * const t; 196char * pt; 197const char * const ptlim; 198int * warnp; 199const struct strftime_locale* locale; 200{ 201 for ( ; *format; ++format) { 202 if (*format == '%') { 203 int modifier = 0; 204label: 205 switch (*++format) { 206 case '\0': 207 --format; 208 break; 209 case 'A': 210 pt = _add((t->tm_wday < 0 || 211 t->tm_wday >= DAYSPERWEEK) ? 212 "?" : locale->weekday[t->tm_wday], 213 pt, ptlim, modifier); 214 continue; 215 case 'a': 216 pt = _add((t->tm_wday < 0 || 217 t->tm_wday >= DAYSPERWEEK) ? 218 "?" : locale->wday[t->tm_wday], 219 pt, ptlim, modifier); 220 continue; 221 case 'B': 222 pt = _add((t->tm_mon < 0 || 223 t->tm_mon >= MONSPERYEAR) ? 224 "?" : locale->month[t->tm_mon], 225 pt, ptlim, modifier); 226 continue; 227 case 'b': 228 case 'h': 229 pt = _add((t->tm_mon < 0 || 230 t->tm_mon >= MONSPERYEAR) ? 231 "?" : locale->mon[t->tm_mon], 232 pt, ptlim, modifier); 233 continue; 234 case 'C': 235 /* 236 ** %C used to do a... 237 ** _fmt("%a %b %e %X %Y", t); 238 ** ...whereas now POSIX 1003.2 calls for 239 ** something completely different. 240 ** (ado, 1993-05-24) 241 */ 242 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0, 243 pt, ptlim, modifier); 244 continue; 245 case 'c': 246 { 247 int warn2 = IN_SOME; 248 249 pt = _fmt(locale->c_fmt, t, pt, ptlim, warnp, locale); 250 if (warn2 == IN_ALL) 251 warn2 = IN_THIS; 252 if (warn2 > *warnp) 253 *warnp = warn2; 254 } 255 continue; 256 case 'D': 257 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, locale); 258 continue; 259 case 'd': 260 pt = _conv(t->tm_mday, 261 getformat(modifier, "%02d", 262 "%2d", "%d", "%02d"), 263 pt, ptlim); 264 continue; 265 case 'E': 266 case 'O': 267 /* 268 ** C99 locale modifiers. 269 ** The sequences 270 ** %Ec %EC %Ex %EX %Ey %EY 271 ** %Od %oe %OH %OI %Om %OM 272 ** %OS %Ou %OU %OV %Ow %OW %Oy 273 ** are supposed to provide alternate 274 ** representations. 275 */ 276 goto label; 277 case '_': 278 case '-': 279 case '0': 280 case '^': 281 case '#': 282 modifier = *format; 283 goto label; 284 case 'e': 285 pt = _conv(t->tm_mday, 286 getformat(modifier, "%2d", 287 "%2d", "%d", "%02d"), 288 pt, ptlim); 289 continue; 290 case 'F': 291 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, locale); 292 continue; 293 case 'H': 294 pt = _conv(t->tm_hour, 295 getformat(modifier, "%02d", 296 "%2d", "%d", "%02d"), 297 pt, ptlim); 298 continue; 299 case 'I': 300 pt = _conv((t->tm_hour % 12) ? 301 (t->tm_hour % 12) : 12, 302 getformat(modifier, "%02d", 303 "%2d", "%d", "%02d"), 304 pt, ptlim); 305 continue; 306 case 'j': 307 pt = _conv(t->tm_yday + 1, 308 getformat(modifier, "%03d", "%3d", "%d", "%03d"), 309 pt, ptlim); 310 continue; 311 case 'k': 312 /* 313 ** This used to be... 314 ** _conv(t->tm_hour % 12 ? 315 ** t->tm_hour % 12 : 12, 2, ' '); 316 ** ...and has been changed to the below to 317 ** match SunOS 4.1.1 and Arnold Robbins' 318 ** strftime version 3.0. That is, "%k" and 319 ** "%l" have been swapped. 320 ** (ado, 1993-05-24) 321 */ 322 pt = _conv(t->tm_hour, 323 getformat(modifier, "%2d", 324 "%2d", "%d", "%02d"), 325 pt, ptlim); 326 continue; 327#ifdef KITCHEN_SINK 328 case 'K': 329 /* 330 ** After all this time, still unclaimed! 331 */ 332 pt = _add("kitchen sink", pt, ptlim, modifier); 333 continue; 334#endif /* defined KITCHEN_SINK */ 335 case 'l': 336 /* 337 ** This used to be... 338 ** _conv(t->tm_hour, 2, ' '); 339 ** ...and has been changed to the below to 340 ** match SunOS 4.1.1 and Arnold Robbin's 341 ** strftime version 3.0. That is, "%k" and 342 ** "%l" have been swapped. 343 ** (ado, 1993-05-24) 344 */ 345 pt = _conv((t->tm_hour % 12) ? 346 (t->tm_hour % 12) : 12, 347 getformat(modifier, "%2d", 348 "%2d", "%d", "%02d"), 349 pt, ptlim); 350 continue; 351 case 'M': 352 pt = _conv(t->tm_min, 353 getformat(modifier, "%02d", 354 "%2d", "%d", "%02d"), 355 pt, ptlim); 356 continue; 357 case 'm': 358 pt = _conv(t->tm_mon + 1, 359 getformat(modifier, "%02d", 360 "%2d", "%d", "%02d"), 361 pt, ptlim); 362 continue; 363 case 'n': 364 pt = _add("\n", pt, ptlim, modifier); 365 continue; 366 case 'p': 367 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ? 368 locale->pm : 369 locale->am, 370 pt, ptlim, modifier); 371 continue; 372 case 'P': 373 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ? 374 locale->pm : 375 locale->am, 376 pt, ptlim, FORCE_LOWER_CASE); 377 continue; 378 case 'R': 379 pt = _fmt("%H:%M", t, pt, ptlim, warnp, locale); 380 continue; 381 case 'r': 382 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp, locale); 383 continue; 384 case 'S': 385 pt = _conv(t->tm_sec, 386 getformat(modifier, "%02d", 387 "%2d", "%d", "%02d"), 388 pt, ptlim); 389 continue; 390 case 's': 391 { 392 struct tm tm; 393 char buf[INT_STRLEN_MAXIMUM( 394 time64_t) + 1]; 395 time64_t mkt; 396 397 tm = *t; 398 mkt = mktime64(&tm); 399 if (TYPE_SIGNED(time64_t)) 400 (void) sprintf(buf, "%lld", 401 (long long) mkt); 402 else (void) sprintf(buf, "%llu", 403 (unsigned long long) mkt); 404 pt = _add(buf, pt, ptlim, modifier); 405 } 406 continue; 407 case 'T': 408 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, locale); 409 continue; 410 case 't': 411 pt = _add("\t", pt, ptlim, modifier); 412 continue; 413 case 'U': 414 pt = _conv((t->tm_yday + DAYSPERWEEK - 415 t->tm_wday) / DAYSPERWEEK, 416 getformat(modifier, "%02d", 417 "%2d", "%d", "%02d"), 418 pt, ptlim); 419 continue; 420 case 'u': 421 /* 422 ** From Arnold Robbins' strftime version 3.0: 423 ** "ISO 8601: Weekday as a decimal number 424 ** [1 (Monday) - 7]" 425 ** (ado, 1993-05-24) 426 */ 427 pt = _conv((t->tm_wday == 0) ? 428 DAYSPERWEEK : t->tm_wday, "%d", pt, ptlim); 429 continue; 430 case 'V': /* ISO 8601 week number */ 431 case 'G': /* ISO 8601 year (four digits) */ 432 case 'g': /* ISO 8601 year (two digits) */ 433/* 434** From Arnold Robbins' strftime version 3.0: "the week number of the 435** year (the first Monday as the first day of week 1) as a decimal number 436** (01-53)." 437** (ado, 1993-05-24) 438** 439** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn: 440** "Week 01 of a year is per definition the first week which has the 441** Thursday in this year, which is equivalent to the week which contains 442** the fourth day of January. In other words, the first week of a new year 443** is the week which has the majority of its days in the new year. Week 01 444** might also contain days from the previous year and the week before week 445** 01 of a year is the last week (52 or 53) of the previous year even if 446** it contains days from the new year. A week starts with Monday (day 1) 447** and ends with Sunday (day 7). For example, the first week of the year 448** 1997 lasts from 1996-12-30 to 1997-01-05..." 449** (ado, 1996-01-02) 450*/ 451 { 452 int year; 453 int base; 454 int yday; 455 int wday; 456 int w; 457 458 year = t->tm_year; 459 base = TM_YEAR_BASE; 460 yday = t->tm_yday; 461 wday = t->tm_wday; 462 for ( ; ; ) { 463 int len; 464 int bot; 465 int top; 466 467 len = isleap_sum(year, base) ? 468 DAYSPERLYEAR : 469 DAYSPERNYEAR; 470 /* 471 ** What yday (-3 ... 3) does 472 ** the ISO year begin on? 473 */ 474 bot = ((yday + 11 - wday) % 475 DAYSPERWEEK) - 3; 476 /* 477 ** What yday does the NEXT 478 ** ISO year begin on? 479 */ 480 top = bot - 481 (len % DAYSPERWEEK); 482 if (top < -3) 483 top += DAYSPERWEEK; 484 top += len; 485 if (yday >= top) { 486 ++base; 487 w = 1; 488 break; 489 } 490 if (yday >= bot) { 491 w = 1 + ((yday - bot) / 492 DAYSPERWEEK); 493 break; 494 } 495 --base; 496 yday += isleap_sum(year, base) ? 497 DAYSPERLYEAR : 498 DAYSPERNYEAR; 499 } 500#ifdef XPG4_1994_04_09 501 if ((w == 52 && 502 t->tm_mon == TM_JANUARY) || 503 (w == 1 && 504 t->tm_mon == TM_DECEMBER)) 505 w = 53; 506#endif /* defined XPG4_1994_04_09 */ 507 if (*format == 'V') 508 pt = _conv(w, 509 getformat(modifier, 510 "%02d", 511 "%2d", 512 "%d", 513 "%02d"), 514 pt, ptlim); 515 else if (*format == 'g') { 516 *warnp = IN_ALL; 517 pt = _yconv(year, base, 0, 1, 518 pt, ptlim, modifier); 519 } else pt = _yconv(year, base, 1, 1, 520 pt, ptlim, modifier); 521 } 522 continue; 523 case 'v': 524 /* 525 ** From Arnold Robbins' strftime version 3.0: 526 ** "date as dd-bbb-YYYY" 527 ** (ado, 1993-05-24) 528 */ 529 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, locale); 530 continue; 531 case 'W': 532 pt = _conv((t->tm_yday + DAYSPERWEEK - 533 (t->tm_wday ? 534 (t->tm_wday - 1) : 535 (DAYSPERWEEK - 1))) / DAYSPERWEEK, 536 getformat(modifier, "%02d", 537 "%2d", "%d", "%02d"), 538 pt, ptlim); 539 continue; 540 case 'w': 541 pt = _conv(t->tm_wday, "%d", pt, ptlim); 542 continue; 543 case 'X': 544 pt = _fmt(locale->X_fmt, t, pt, ptlim, warnp, locale); 545 continue; 546 case 'x': 547 { 548 int warn2 = IN_SOME; 549 550 pt = _fmt(locale->x_fmt, t, pt, ptlim, &warn2, locale); 551 if (warn2 == IN_ALL) 552 warn2 = IN_THIS; 553 if (warn2 > *warnp) 554 *warnp = warn2; 555 } 556 continue; 557 case 'y': 558 *warnp = IN_ALL; 559 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1, 560 pt, ptlim, modifier); 561 continue; 562 case 'Y': 563 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1, 564 pt, ptlim, modifier); 565 continue; 566 case 'Z': 567#ifdef TM_ZONE 568 if (t->TM_ZONE != NULL) 569 pt = _add(t->TM_ZONE, pt, ptlim, 570 modifier); 571 else 572#endif /* defined TM_ZONE */ 573 if (t->tm_isdst >= 0) 574 pt = _add(tzname[t->tm_isdst != 0], 575 pt, ptlim, modifier); 576 /* 577 ** C99 says that %Z must be replaced by the 578 ** empty string if the time zone is not 579 ** determinable. 580 */ 581 continue; 582 case 'z': 583 { 584 int diff; 585 char const * sign; 586 587 if (t->tm_isdst < 0) 588 continue; 589#ifdef TM_GMTOFF 590 diff = t->TM_GMTOFF; 591#else /* !defined TM_GMTOFF */ 592 /* 593 ** C99 says that the UTC offset must 594 ** be computed by looking only at 595 ** tm_isdst. This requirement is 596 ** incorrect, since it means the code 597 ** must rely on magic (in this case 598 ** altzone and timezone), and the 599 ** magic might not have the correct 600 ** offset. Doing things correctly is 601 ** tricky and requires disobeying C99; 602 ** see GNU C strftime for details. 603 ** For now, punt and conform to the 604 ** standard, even though it's incorrect. 605 ** 606 ** C99 says that %z must be replaced by the 607 ** empty string if the time zone is not 608 ** determinable, so output nothing if the 609 ** appropriate variables are not available. 610 */ 611 if (t->tm_isdst == 0) 612#ifdef USG_COMPAT 613 diff = -timezone; 614#else /* !defined USG_COMPAT */ 615 continue; 616#endif /* !defined USG_COMPAT */ 617 else 618#ifdef ALTZONE 619 diff = -altzone; 620#else /* !defined ALTZONE */ 621 continue; 622#endif /* !defined ALTZONE */ 623#endif /* !defined TM_GMTOFF */ 624 if (diff < 0) { 625 sign = "-"; 626 diff = -diff; 627 } else sign = "+"; 628 pt = _add(sign, pt, ptlim, modifier); 629 diff /= SECSPERMIN; 630 diff = (diff / MINSPERHOUR) * 100 + 631 (diff % MINSPERHOUR); 632 pt = _conv(diff, 633 getformat(modifier, "%04d", 634 "%4d", "%d", "%04d"), 635 pt, ptlim); 636 } 637 continue; 638 case '+': 639 pt = _fmt(locale->date_fmt, t, pt, ptlim, 640 warnp, locale); 641 continue; 642 case '%': 643 /* 644 ** X311J/88-090 (4.12.3.5): if conversion char is 645 ** undefined, behavior is undefined. Print out the 646 ** character itself as printf(3) also does. 647 */ 648 default: 649 break; 650 } 651 } 652 if (pt == ptlim) 653 break; 654 *pt++ = *format; 655 } 656 return pt; 657} 658 659static char * 660_conv(n, format, pt, ptlim) 661const int n; 662const char * const format; 663char * const pt; 664const char * const ptlim; 665{ 666 char buf[INT_STRLEN_MAXIMUM(int) + 1]; 667 668 (void) snprintf(buf, sizeof(buf), format, n); 669 return _add(buf, pt, ptlim, 0); 670} 671 672static char * 673_add(str, pt, ptlim, modifier) 674const char * str; 675char * pt; 676const char * const ptlim; 677int modifier; 678{ 679 int c; 680 681 switch (modifier) { 682 case FORCE_LOWER_CASE: 683 while (pt < ptlim && (*pt = tolower(*str++)) != '\0') { 684 ++pt; 685 } 686 break; 687 688 case '^': 689 while (pt < ptlim && (*pt = toupper(*str++)) != '\0') { 690 ++pt; 691 } 692 break; 693 694 case '#': 695 while (pt < ptlim && (c = *str++) != '\0') { 696 if (isupper(c)) { 697 c = tolower(c); 698 } else if (islower(c)) { 699 c = toupper(c); 700 } 701 *pt = c; 702 ++pt; 703 } 704 705 break; 706 707 default: 708 while (pt < ptlim && (*pt = *str++) != '\0') { 709 ++pt; 710 } 711 } 712 713 return pt; 714} 715 716/* 717** POSIX and the C Standard are unclear or inconsistent about 718** what %C and %y do if the year is negative or exceeds 9999. 719** Use the convention that %C concatenated with %y yields the 720** same output as %Y, and that %Y contains at least 4 bytes, 721** with more only if necessary. 722*/ 723 724static char * 725_yconv(a, b, convert_top, convert_yy, pt, ptlim, modifier) 726const int a; 727const int b; 728const int convert_top; 729const int convert_yy; 730char * pt; 731const char * const ptlim; 732int modifier; 733{ 734 register int lead; 735 register int trail; 736 737#define DIVISOR 100 738 trail = a % DIVISOR + b % DIVISOR; 739 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR; 740 trail %= DIVISOR; 741 if (trail < 0 && lead > 0) { 742 trail += DIVISOR; 743 --lead; 744 } else if (lead < 0 && trail > 0) { 745 trail -= DIVISOR; 746 ++lead; 747 } 748 if (convert_top) { 749 if (lead == 0 && trail < 0) 750 pt = _add("-0", pt, ptlim, modifier); 751 else pt = _conv(lead, getformat(modifier, "%02d", 752 "%2d", "%d", "%02d"), 753 pt, ptlim); 754 } 755 if (convert_yy) 756 pt = _conv(((trail < 0) ? -trail : trail), 757 getformat(modifier, "%02d", "%2d", "%d", "%02d"), 758 pt, ptlim); 759 return pt; 760} 761