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