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