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