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