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 #ifdef LIBC_RCS 19 static const char rcsid[] = 20 "$FreeBSD$"; 21 #endif 22 23 #ifndef lint 24 #ifndef NOID 25 static const char elsieid[] = "@(#)strftime.c 7.38"; 26 /* 27 ** Based on the UCB version with the ID appearing below. 28 ** This is ANSIish only when "multibyte character == plain character". 29 */ 30 #endif /* !defined NOID */ 31 #endif /* !defined lint */ 32 33 #include "namespace.h" 34 #include "private.h" 35 36 #ifndef LIBC_SCCS 37 #ifndef lint 38 static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89"; 39 #endif /* !defined lint */ 40 #endif /* !defined LIBC_SCCS */ 41 42 #include "tzfile.h" 43 #include <fcntl.h> 44 #include <sys/stat.h> 45 #include "un-namespace.h" 46 #include "timelocal.h" 47 48 static char * _add P((const char *, char *, const char *)); 49 static char * _conv P((int, const char *, char *, const char *)); 50 static char * _fmt P((const char *, const struct tm *, char *, const char *)); 51 52 size_t strftime P((char *, size_t, const char *, const struct tm *)); 53 54 extern char * tzname[]; 55 56 size_t 57 strftime(s, maxsize, format, t) 58 char *const s; 59 const size_t maxsize; 60 const char *const format; 61 const struct tm *const t; 62 { 63 char *p; 64 65 tzset(); 66 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize); 67 if (p == s + maxsize) 68 return 0; 69 *p = '\0'; 70 return p - s; 71 } 72 73 static char * 74 _fmt(format, t, pt, ptlim) 75 const char *format; 76 const struct tm *const t; 77 char *pt; 78 const char *const ptlim; 79 { 80 int Ealternative, Oalternative; 81 struct lc_time_T *tptr = __get_current_time_locale(); 82 83 for ( ; *format; ++format) { 84 if (*format == '%') { 85 Ealternative = 0; 86 Oalternative = 0; 87 label: 88 switch (*++format) { 89 case '\0': 90 --format; 91 break; 92 case 'A': 93 pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ? 94 "?" : tptr->weekday[t->tm_wday], 95 pt, ptlim); 96 continue; 97 case 'a': 98 pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ? 99 "?" : tptr->wday[t->tm_wday], 100 pt, ptlim); 101 continue; 102 case 'B': 103 pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ? 104 "?" : (Oalternative ? tptr->alt_month : 105 tptr->month)[t->tm_mon], 106 pt, ptlim); 107 continue; 108 case 'b': 109 case 'h': 110 pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ? 111 "?" : tptr->mon[t->tm_mon], 112 pt, ptlim); 113 continue; 114 case 'C': 115 /* 116 ** %C used to do a... 117 ** _fmt("%a %b %e %X %Y", t); 118 ** ...whereas now POSIX 1003.2 calls for 119 ** something completely different. 120 ** (ado, 5/24/93) 121 */ 122 pt = _conv((t->tm_year + TM_YEAR_BASE) / 100, 123 "%02d", pt, ptlim); 124 continue; 125 case 'c': 126 pt = _fmt(tptr->c_fmt, t, pt, ptlim); 127 continue; 128 case 'D': 129 pt = _fmt("%m/%d/%y", t, pt, ptlim); 130 continue; 131 case 'd': 132 pt = _conv(t->tm_mday, "%02d", pt, ptlim); 133 continue; 134 case 'E': 135 if (Ealternative || Oalternative) 136 break; 137 Ealternative++; 138 goto label; 139 case 'O': 140 /* 141 ** POSIX locale extensions, a la 142 ** Arnold Robbins' strftime version 3.0. 143 ** The sequences 144 ** %Ec %EC %Ex %EX %Ey %EY 145 ** %Od %oe %OH %OI %Om %OM 146 ** %OS %Ou %OU %OV %Ow %OW %Oy 147 ** are supposed to provide alternate 148 ** representations. 149 ** (ado, 5/24/93) 150 ** 151 ** FreeBSD extensions 152 ** %OB %Ef %EF 153 */ 154 if (Ealternative || Oalternative) 155 break; 156 Oalternative++; 157 goto label; 158 case 'e': 159 pt = _conv(t->tm_mday, "%2d", pt, ptlim); 160 continue; 161 case 'f': 162 if (!Ealternative) 163 break; 164 pt = _fmt(*(tptr->md_order) == 'd' ? 165 "%e %b" : "%b %e", 166 t, pt, ptlim); 167 continue; 168 case 'F': 169 if (!Ealternative) 170 pt = _fmt("%Y-%m-%d", t, pt, ptlim); 171 else 172 pt = _fmt(*(tptr->md_order) == 'd' ? 173 "%e %B" : "%B %e", 174 t, pt, ptlim); 175 continue; 176 case 'H': 177 pt = _conv(t->tm_hour, "%02d", pt, ptlim); 178 continue; 179 case 'I': 180 pt = _conv((t->tm_hour % 12) ? 181 (t->tm_hour % 12) : 12, 182 "%02d", pt, ptlim); 183 continue; 184 case 'j': 185 pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim); 186 continue; 187 case 'k': 188 /* 189 ** This used to be... 190 ** _conv(t->tm_hour % 12 ? 191 ** t->tm_hour % 12 : 12, 2, ' '); 192 ** ...and has been changed to the below to 193 ** match SunOS 4.1.1 and Arnold Robbins' 194 ** strftime version 3.0. That is, "%k" and 195 ** "%l" have been swapped. 196 ** (ado, 5/24/93) 197 */ 198 pt = _conv(t->tm_hour, "%2d", pt, ptlim); 199 continue; 200 #ifdef KITCHEN_SINK 201 case 'K': 202 /* 203 ** After all this time, still unclaimed! 204 */ 205 pt = _add("kitchen sink", pt, ptlim); 206 continue; 207 #endif /* defined KITCHEN_SINK */ 208 case 'l': 209 /* 210 ** This used to be... 211 ** _conv(t->tm_hour, 2, ' '); 212 ** ...and has been changed to the below to 213 ** match SunOS 4.1.1 and Arnold Robbin's 214 ** strftime version 3.0. That is, "%k" and 215 ** "%l" have been swapped. 216 ** (ado, 5/24/93) 217 */ 218 pt = _conv((t->tm_hour % 12) ? 219 (t->tm_hour % 12) : 12, 220 "%2d", pt, ptlim); 221 continue; 222 case 'M': 223 pt = _conv(t->tm_min, "%02d", pt, ptlim); 224 continue; 225 case 'm': 226 pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim); 227 continue; 228 case 'n': 229 pt = _add("\n", pt, ptlim); 230 continue; 231 case 'p': 232 pt = _add((t->tm_hour >= 12) ? 233 tptr->pm : 234 tptr->am, 235 pt, ptlim); 236 continue; 237 case 'R': 238 pt = _fmt("%H:%M", t, pt, ptlim); 239 continue; 240 case 'r': 241 pt = _fmt(tptr->ampm_fmt, t, pt, ptlim); 242 continue; 243 case 'S': 244 pt = _conv(t->tm_sec, "%02d", pt, ptlim); 245 continue; 246 case 's': 247 { 248 struct tm tm; 249 char buf[INT_STRLEN_MAXIMUM( 250 time_t) + 1]; 251 time_t mkt; 252 253 tm = *t; 254 mkt = mktime(&tm); 255 if (TYPE_SIGNED(time_t)) 256 (void) sprintf(buf, "%ld", 257 (long) mkt); 258 else (void) sprintf(buf, "%lu", 259 (unsigned long) mkt); 260 pt = _add(buf, pt, ptlim); 261 } 262 continue; 263 case 'T': 264 pt = _fmt("%H:%M:%S", t, pt, ptlim); 265 continue; 266 case 't': 267 pt = _add("\t", pt, ptlim); 268 continue; 269 case 'U': 270 pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7, 271 "%02d", pt, ptlim); 272 continue; 273 case 'u': 274 /* 275 ** From Arnold Robbins' strftime version 3.0: 276 ** "ISO 8601: Weekday as a decimal number 277 ** [1 (Monday) - 7]" 278 ** (ado, 5/24/93) 279 */ 280 pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday, 281 "%d", pt, ptlim); 282 continue; 283 case 'V': /* ISO 8601 week number */ 284 case 'G': /* ISO 8601 year (four digits) */ 285 case 'g': /* ISO 8601 year (two digits) */ 286 /* 287 ** From Arnold Robbins' strftime version 3.0: "the week number of the 288 ** year (the first Monday as the first day of week 1) as a decimal number 289 ** (01-53)." 290 ** (ado, 1993-05-24) 291 ** 292 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn: 293 ** "Week 01 of a year is per definition the first week which has the 294 ** Thursday in this year, which is equivalent to the week which contains 295 ** the fourth day of January. In other words, the first week of a new year 296 ** is the week which has the majority of its days in the new year. Week 01 297 ** might also contain days from the previous year and the week before week 298 ** 01 of a year is the last week (52 or 53) of the previous year even if 299 ** it contains days from the new year. A week starts with Monday (day 1) 300 ** and ends with Sunday (day 7). For example, the first week of the year 301 ** 1997 lasts from 1996-12-30 to 1997-01-05..." 302 ** (ado, 1996-01-02) 303 */ 304 { 305 int year; 306 int yday; 307 int wday; 308 int w; 309 310 year = t->tm_year + TM_YEAR_BASE; 311 yday = t->tm_yday; 312 wday = t->tm_wday; 313 for ( ; ; ) { 314 int len; 315 int bot; 316 int top; 317 318 len = isleap(year) ? 319 DAYSPERLYEAR : 320 DAYSPERNYEAR; 321 /* 322 ** What yday (-3 ... 3) does 323 ** the ISO year begin on? 324 */ 325 bot = ((yday + 11 - wday) % 326 DAYSPERWEEK) - 3; 327 /* 328 ** What yday does the NEXT 329 ** ISO year begin on? 330 */ 331 top = bot - 332 (len % DAYSPERWEEK); 333 if (top < -3) 334 top += DAYSPERWEEK; 335 top += len; 336 if (yday >= top) { 337 ++year; 338 w = 1; 339 break; 340 } 341 if (yday >= bot) { 342 w = 1 + ((yday - bot) / 343 DAYSPERWEEK); 344 break; 345 } 346 --year; 347 yday += isleap(year) ? 348 DAYSPERLYEAR : 349 DAYSPERNYEAR; 350 } 351 #ifdef XPG4_1994_04_09 352 if ((w == 52 353 && t->tm_mon == TM_JANUARY) 354 || (w == 1 355 && t->tm_mon == TM_DECEMBER)) 356 w = 53; 357 #endif /* defined XPG4_1994_04_09 */ 358 if (*format == 'V') 359 pt = _conv(w, "%02d", 360 pt, ptlim); 361 else if (*format == 'g') { 362 pt = _conv(year % 100, "%02d", 363 pt, ptlim); 364 } else pt = _conv(year, "%04d", 365 pt, ptlim); 366 } 367 continue; 368 case 'v': 369 /* 370 ** From Arnold Robbins' strftime version 3.0: 371 ** "date as dd-bbb-YYYY" 372 ** (ado, 5/24/93) 373 */ 374 pt = _fmt("%e-%b-%Y", t, pt, ptlim); 375 continue; 376 case 'W': 377 pt = _conv((t->tm_yday + 7 - 378 (t->tm_wday ? 379 (t->tm_wday - 1) : 6)) / 7, 380 "%02d", pt, ptlim); 381 continue; 382 case 'w': 383 pt = _conv(t->tm_wday, "%d", pt, ptlim); 384 continue; 385 case 'X': 386 pt = _fmt(tptr->X_fmt, t, pt, ptlim); 387 continue; 388 case 'x': 389 pt = _fmt(tptr->x_fmt, t, pt, ptlim); 390 continue; 391 case 'y': 392 pt = _conv((t->tm_year + TM_YEAR_BASE) % 100, 393 "%02d", pt, ptlim); 394 continue; 395 case 'Y': 396 pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d", 397 pt, ptlim); 398 continue; 399 case 'Z': 400 if (t->tm_zone != NULL) 401 pt = _add(t->tm_zone, pt, ptlim); 402 else 403 if (t->tm_isdst == 0 || t->tm_isdst == 1) { 404 pt = _add(tzname[t->tm_isdst], 405 pt, ptlim); 406 } else pt = _add("?", pt, ptlim); 407 continue; 408 case 'z': 409 { 410 long absoff; 411 if (t->tm_gmtoff >= 0) { 412 absoff = t->tm_gmtoff; 413 pt = _add("+", pt, ptlim); 414 } else { 415 absoff = -t->tm_gmtoff; 416 pt = _add("-", pt, ptlim); 417 } 418 pt = _conv(absoff / 3600, "%02d", 419 pt, ptlim); 420 pt = _conv((absoff % 3600) / 60, "%02d", 421 pt, ptlim); 422 }; 423 continue; 424 case '+': 425 pt = _fmt(tptr->date_fmt, t, pt, ptlim); 426 continue; 427 case '%': 428 /* 429 * X311J/88-090 (4.12.3.5): if conversion char is 430 * undefined, behavior is undefined. Print out the 431 * character itself as printf(3) also does. 432 */ 433 default: 434 break; 435 } 436 } 437 if (pt == ptlim) 438 break; 439 *pt++ = *format; 440 } 441 return pt; 442 } 443 444 static char * 445 _conv(n, format, pt, ptlim) 446 const int n; 447 const char *const format; 448 char *const pt; 449 const char *const ptlim; 450 { 451 char buf[INT_STRLEN_MAXIMUM(int) + 1]; 452 453 (void) sprintf(buf, format, n); 454 return _add(buf, pt, ptlim); 455 } 456 457 static char * 458 _add(str, pt, ptlim) 459 const char *str; 460 char *pt; 461 const char *const ptlim; 462 { 463 while (pt < ptlim && (*pt = *str++) != '\0') 464 ++pt; 465 return pt; 466 } 467