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