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