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