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