| 1 | /*
|
|---|
| 2 | * strftime.c
|
|---|
| 3 | *
|
|---|
| 4 | * Public-domain relatively quick-and-dirty implemenation of
|
|---|
| 5 | * ANSI library routine for System V Unix systems.
|
|---|
| 6 | *
|
|---|
| 7 | * It's written in old-style C for maximal portability.
|
|---|
| 8 | * However, since I'm used to prototypes, I've included them too.
|
|---|
| 9 | *
|
|---|
| 10 | * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
|
|---|
| 11 | * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
|
|---|
| 12 | * For complete POSIX semantics, add POSIX_SEMANTICS.
|
|---|
| 13 | *
|
|---|
| 14 | * The code for %c, %x, and %X is my best guess as to what's "appropriate".
|
|---|
| 15 | * This version ignores LOCALE information.
|
|---|
| 16 | * It also doesn't worry about multi-byte characters.
|
|---|
| 17 | * So there.
|
|---|
| 18 | *
|
|---|
| 19 | * Arnold Robbins
|
|---|
| 20 | * January, February, March, 1991
|
|---|
| 21 | * Updated March 1992
|
|---|
| 22 | *
|
|---|
| 23 | * Fixes from ado@elsie.nci.nih.gov
|
|---|
| 24 | * February 1991
|
|---|
| 25 | *
|
|---|
| 26 | * Fixed compiler warnings from GCC
|
|---|
| 27 | * Olly Betts 2005-01-12
|
|---|
| 28 | *
|
|---|
| 29 | * Fix memory leak if realloc() fails
|
|---|
| 30 | * Olly Betts 2010-12-21
|
|---|
| 31 | *
|
|---|
| 32 | * Fix compilation error with GCC
|
|---|
| 33 | * Olly Betts 2010-12-21
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <stdio.h>
|
|---|
| 37 | #include <ctype.h>
|
|---|
| 38 | #include <string.h>
|
|---|
| 39 | #include <time.h>
|
|---|
| 40 | #include <sys/types.h>
|
|---|
| 41 |
|
|---|
| 42 | #ifndef __STDC__
|
|---|
| 43 | #define const /**/
|
|---|
| 44 |
|
|---|
| 45 | extern void *malloc();
|
|---|
| 46 | extern void *realloc();
|
|---|
| 47 | extern void free();
|
|---|
| 48 | extern void tzset();
|
|---|
| 49 | extern char *strchr();
|
|---|
| 50 | extern char *getenv();
|
|---|
| 51 | static int weeknumber();
|
|---|
| 52 | #else
|
|---|
| 53 | #include <stdlib.h>
|
|---|
| 54 | extern void tzset(void);
|
|---|
| 55 | static int weeknumber(const struct tm *timeptr, int firstweekday);
|
|---|
| 56 | #endif
|
|---|
| 57 |
|
|---|
| 58 | #ifdef __GNUC__
|
|---|
| 59 | #define inline __inline__
|
|---|
| 60 | #else
|
|---|
| 61 | #define inline /**/
|
|---|
| 62 | #endif
|
|---|
| 63 |
|
|---|
| 64 | #define range(low, item, hi) max(low, min(item, hi))
|
|---|
| 65 |
|
|---|
| 66 | extern char *tzname[2];
|
|---|
| 67 | extern int daylight;
|
|---|
| 68 |
|
|---|
| 69 | #define SYSV_EXT 1 /* stuff in System V ascftime routine */
|
|---|
| 70 | #define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */
|
|---|
| 71 | #define VMS_EXT 1 /* include %V for VMS date format */
|
|---|
| 72 | #define POSIX_SEMANTICS 1 /* call tzset() if TZ changes */
|
|---|
| 73 |
|
|---|
| 74 | #if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
|
|---|
| 75 | #define SYSV_EXT 1
|
|---|
| 76 | #endif
|
|---|
| 77 |
|
|---|
| 78 | /* min --- return minimum of two numbers */
|
|---|
| 79 |
|
|---|
| 80 | #ifndef __STDC__
|
|---|
| 81 | static inline int
|
|---|
| 82 | min(a, b)
|
|---|
| 83 | int a, b;
|
|---|
| 84 | #else
|
|---|
| 85 | static inline int
|
|---|
| 86 | min(int a, int b)
|
|---|
| 87 | #endif
|
|---|
| 88 | {
|
|---|
| 89 | return (a < b ? a : b);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /* max --- return maximum of two numbers */
|
|---|
| 93 |
|
|---|
| 94 | #ifndef __STDC__
|
|---|
| 95 | static inline int
|
|---|
| 96 | max(a, b)
|
|---|
| 97 | int a, b;
|
|---|
| 98 | #else
|
|---|
| 99 | static inline int
|
|---|
| 100 | max(int a, int b)
|
|---|
| 101 | #endif
|
|---|
| 102 | {
|
|---|
| 103 | return (a > b ? a : b);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /* strftime --- produce formatted time */
|
|---|
| 107 |
|
|---|
| 108 | #ifndef __STDC__
|
|---|
| 109 | size_t
|
|---|
| 110 | strftime(s, maxsize, format, timeptr)
|
|---|
| 111 | char *s;
|
|---|
| 112 | size_t maxsize;
|
|---|
| 113 | const char *format;
|
|---|
| 114 | const struct tm *timeptr;
|
|---|
| 115 | #else
|
|---|
| 116 | size_t
|
|---|
| 117 | strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
|
|---|
| 118 | #endif
|
|---|
| 119 | {
|
|---|
| 120 | char *endp = s + maxsize;
|
|---|
| 121 | char *start = s;
|
|---|
| 122 | char tbuf[100];
|
|---|
| 123 | int i;
|
|---|
| 124 | static short first = 1;
|
|---|
| 125 | static char *savetz = NULL;
|
|---|
| 126 | static int savetzlen = 0;
|
|---|
| 127 | char *tz;
|
|---|
| 128 | int tzlen;
|
|---|
| 129 |
|
|---|
| 130 | /* various tables, useful in North America */
|
|---|
| 131 | static const char *days_a[] = {
|
|---|
| 132 | "Sun", "Mon", "Tue", "Wed",
|
|---|
| 133 | "Thu", "Fri", "Sat",
|
|---|
| 134 | };
|
|---|
| 135 | static const char *days_l[] = {
|
|---|
| 136 | "Sunday", "Monday", "Tuesday", "Wednesday",
|
|---|
| 137 | "Thursday", "Friday", "Saturday",
|
|---|
| 138 | };
|
|---|
| 139 | static const char *months_a[] = {
|
|---|
| 140 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|---|
| 141 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
|---|
| 142 | };
|
|---|
| 143 | static const char *months_l[] = {
|
|---|
| 144 | "January", "February", "March", "April",
|
|---|
| 145 | "May", "June", "July", "August", "September",
|
|---|
| 146 | "October", "November", "December",
|
|---|
| 147 | };
|
|---|
| 148 | static const char *ampm[] = { "AM", "PM", };
|
|---|
| 149 |
|
|---|
| 150 | if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
|
|---|
| 151 | return 0;
|
|---|
| 152 |
|
|---|
| 153 | if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
|
|---|
| 154 | return 0;
|
|---|
| 155 |
|
|---|
| 156 | #ifndef POSIX_SEMANTICS
|
|---|
| 157 | if (first) {
|
|---|
| 158 | tzset();
|
|---|
| 159 | first = 0;
|
|---|
| 160 | }
|
|---|
| 161 | #else /* POSIX_SEMANTICS */
|
|---|
| 162 | tz = getenv("TZ");
|
|---|
| 163 | tzlen = strlen(tz);
|
|---|
| 164 | if (first) {
|
|---|
| 165 | if (tz != NULL) {
|
|---|
| 166 | savetz = (char *) malloc(tzlen + 1);
|
|---|
| 167 | if (savetz != NULL) {
|
|---|
| 168 | savetzlen = tzlen + 1;
|
|---|
| 169 | strcpy(savetz, tz);
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 | tzset();
|
|---|
| 173 | first = 0;
|
|---|
| 174 | }
|
|---|
| 175 | /* if we have a saved TZ, and it is different, recapture and reset */
|
|---|
| 176 | if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
|
|---|
| 177 | i = strlen(tz) + 1;
|
|---|
| 178 | if (i > savetzlen) {
|
|---|
| 179 | char * newsavetz = (char *) realloc(savetz, i);
|
|---|
| 180 | if (newsavetz) {
|
|---|
| 181 | savetzlen = i;
|
|---|
| 182 | strcpy(newsavetz, tz);
|
|---|
| 183 | } else {
|
|---|
| 184 | free(savetz);
|
|---|
| 185 | }
|
|---|
| 186 | savetz = newsavetz;
|
|---|
| 187 | } else
|
|---|
| 188 | strcpy(savetz, tz);
|
|---|
| 189 | tzset();
|
|---|
| 190 | }
|
|---|
| 191 | #endif /* POSIX_SEMANTICS */
|
|---|
| 192 |
|
|---|
| 193 | for (; *format && s < endp - 1; format++) {
|
|---|
| 194 | tbuf[0] = '\0';
|
|---|
| 195 | if (*format != '%') {
|
|---|
| 196 | *s++ = *format;
|
|---|
| 197 | continue;
|
|---|
| 198 | }
|
|---|
| 199 | again:
|
|---|
| 200 | switch (*++format) {
|
|---|
| 201 | case '\0':
|
|---|
| 202 | *s++ = '%';
|
|---|
| 203 | goto out;
|
|---|
| 204 |
|
|---|
| 205 | case '%':
|
|---|
| 206 | *s++ = '%';
|
|---|
| 207 | continue;
|
|---|
| 208 |
|
|---|
| 209 | case 'a': /* abbreviated weekday name */
|
|---|
| 210 | if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
|
|---|
| 211 | strcpy(tbuf, "?");
|
|---|
| 212 | else
|
|---|
| 213 | strcpy(tbuf, days_a[timeptr->tm_wday]);
|
|---|
| 214 | break;
|
|---|
| 215 |
|
|---|
| 216 | case 'A': /* full weekday name */
|
|---|
| 217 | if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
|
|---|
| 218 | strcpy(tbuf, "?");
|
|---|
| 219 | else
|
|---|
| 220 | strcpy(tbuf, days_l[timeptr->tm_wday]);
|
|---|
| 221 | break;
|
|---|
| 222 |
|
|---|
| 223 | #ifdef SYSV_EXT
|
|---|
| 224 | case 'h': /* abbreviated month name */
|
|---|
| 225 | #endif
|
|---|
| 226 | case 'b': /* abbreviated month name */
|
|---|
| 227 | if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
|
|---|
| 228 | strcpy(tbuf, "?");
|
|---|
| 229 | else
|
|---|
| 230 | strcpy(tbuf, months_a[timeptr->tm_mon]);
|
|---|
| 231 | break;
|
|---|
| 232 |
|
|---|
| 233 | case 'B': /* full month name */
|
|---|
| 234 | if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
|
|---|
| 235 | strcpy(tbuf, "?");
|
|---|
| 236 | else
|
|---|
| 237 | strcpy(tbuf, months_l[timeptr->tm_mon]);
|
|---|
| 238 | break;
|
|---|
| 239 |
|
|---|
| 240 | case 'c': /* appropriate date and time representation */
|
|---|
| 241 | sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
|
|---|
| 242 | days_a[range(0, timeptr->tm_wday, 6)],
|
|---|
| 243 | months_a[range(0, timeptr->tm_mon, 11)],
|
|---|
| 244 | range(1, timeptr->tm_mday, 31),
|
|---|
| 245 | range(0, timeptr->tm_hour, 23),
|
|---|
| 246 | range(0, timeptr->tm_min, 59),
|
|---|
| 247 | range(0, timeptr->tm_sec, 61),
|
|---|
| 248 | timeptr->tm_year + 1900);
|
|---|
| 249 | break;
|
|---|
| 250 |
|
|---|
| 251 | case 'd': /* day of the month, 01 - 31 */
|
|---|
| 252 | i = range(1, timeptr->tm_mday, 31);
|
|---|
| 253 | sprintf(tbuf, "%02d", i);
|
|---|
| 254 | break;
|
|---|
| 255 |
|
|---|
| 256 | case 'H': /* hour, 24-hour clock, 00 - 23 */
|
|---|
| 257 | i = range(0, timeptr->tm_hour, 23);
|
|---|
| 258 | sprintf(tbuf, "%02d", i);
|
|---|
| 259 | break;
|
|---|
| 260 |
|
|---|
| 261 | case 'I': /* hour, 12-hour clock, 01 - 12 */
|
|---|
| 262 | i = range(0, timeptr->tm_hour, 23);
|
|---|
| 263 | if (i == 0)
|
|---|
| 264 | i = 12;
|
|---|
| 265 | else if (i > 12)
|
|---|
| 266 | i -= 12;
|
|---|
| 267 | sprintf(tbuf, "%02d", i);
|
|---|
| 268 | break;
|
|---|
| 269 |
|
|---|
| 270 | case 'j': /* day of the year, 001 - 366 */
|
|---|
| 271 | sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
|
|---|
| 272 | break;
|
|---|
| 273 |
|
|---|
| 274 | case 'm': /* month, 01 - 12 */
|
|---|
| 275 | i = range(0, timeptr->tm_mon, 11);
|
|---|
| 276 | sprintf(tbuf, "%02d", i + 1);
|
|---|
| 277 | break;
|
|---|
| 278 |
|
|---|
| 279 | case 'M': /* minute, 00 - 59 */
|
|---|
| 280 | i = range(0, timeptr->tm_min, 59);
|
|---|
| 281 | sprintf(tbuf, "%02d", i);
|
|---|
| 282 | break;
|
|---|
| 283 |
|
|---|
| 284 | case 'p': /* am or pm based on 12-hour clock */
|
|---|
| 285 | i = range(0, timeptr->tm_hour, 23);
|
|---|
| 286 | if (i < 12)
|
|---|
| 287 | strcpy(tbuf, ampm[0]);
|
|---|
| 288 | else
|
|---|
| 289 | strcpy(tbuf, ampm[1]);
|
|---|
| 290 | break;
|
|---|
| 291 |
|
|---|
| 292 | case 'S': /* second, 00 - 61 */
|
|---|
| 293 | i = range(0, timeptr->tm_sec, 61);
|
|---|
| 294 | sprintf(tbuf, "%02d", i);
|
|---|
| 295 | break;
|
|---|
| 296 |
|
|---|
| 297 | case 'U': /* week of year, Sunday is first day of week */
|
|---|
| 298 | sprintf(tbuf, "%d", weeknumber(timeptr, 0));
|
|---|
| 299 | break;
|
|---|
| 300 |
|
|---|
| 301 | case 'w': /* weekday, Sunday == 0, 0 - 6 */
|
|---|
| 302 | i = range(0, timeptr->tm_wday, 6);
|
|---|
| 303 | sprintf(tbuf, "%d", i);
|
|---|
| 304 | break;
|
|---|
| 305 |
|
|---|
| 306 | case 'W': /* week of year, Monday is first day of week */
|
|---|
| 307 | sprintf(tbuf, "%d", weeknumber(timeptr, 1));
|
|---|
| 308 | break;
|
|---|
| 309 |
|
|---|
| 310 | case 'x': /* appropriate date representation */
|
|---|
| 311 | sprintf(tbuf, "%s %s %2d %d",
|
|---|
| 312 | days_a[range(0, timeptr->tm_wday, 6)],
|
|---|
| 313 | months_a[range(0, timeptr->tm_mon, 11)],
|
|---|
| 314 | range(1, timeptr->tm_mday, 31),
|
|---|
| 315 | timeptr->tm_year + 1900);
|
|---|
| 316 | break;
|
|---|
| 317 |
|
|---|
| 318 | case 'X': /* appropriate time representation */
|
|---|
| 319 | sprintf(tbuf, "%02d:%02d:%02d",
|
|---|
| 320 | range(0, timeptr->tm_hour, 23),
|
|---|
| 321 | range(0, timeptr->tm_min, 59),
|
|---|
| 322 | range(0, timeptr->tm_sec, 61));
|
|---|
| 323 | break;
|
|---|
| 324 |
|
|---|
| 325 | case 'y': /* year without a century, 00 - 99 */
|
|---|
| 326 | i = timeptr->tm_year % 100;
|
|---|
| 327 | sprintf(tbuf, "%d", i);
|
|---|
| 328 | break;
|
|---|
| 329 |
|
|---|
| 330 | case 'Y': /* year with century */
|
|---|
| 331 | sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
|
|---|
| 332 | break;
|
|---|
| 333 |
|
|---|
| 334 | case 'Z': /* time zone name or abbrevation */
|
|---|
| 335 | i = 0;
|
|---|
| 336 | if (daylight && timeptr->tm_isdst)
|
|---|
| 337 | i = 1;
|
|---|
| 338 | strcpy(tbuf, tzname[i]);
|
|---|
| 339 | break;
|
|---|
| 340 |
|
|---|
| 341 | #ifdef SYSV_EXT
|
|---|
| 342 | case 'n': /* same as \n */
|
|---|
| 343 | tbuf[0] = '\n';
|
|---|
| 344 | tbuf[1] = '\0';
|
|---|
| 345 | break;
|
|---|
| 346 |
|
|---|
| 347 | case 't': /* same as \t */
|
|---|
| 348 | tbuf[0] = '\t';
|
|---|
| 349 | tbuf[1] = '\0';
|
|---|
| 350 | break;
|
|---|
| 351 |
|
|---|
| 352 | case 'D': /* date as %m/%d/%y */
|
|---|
| 353 | strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
|
|---|
| 354 | break;
|
|---|
| 355 |
|
|---|
| 356 | case 'e': /* day of month, blank padded */
|
|---|
| 357 | sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
|
|---|
| 358 | break;
|
|---|
| 359 |
|
|---|
| 360 | case 'r': /* time as %I:%M:%S %p */
|
|---|
| 361 | strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
|
|---|
| 362 | break;
|
|---|
| 363 |
|
|---|
| 364 | case 'R': /* time as %H:%M */
|
|---|
| 365 | strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
|
|---|
| 366 | break;
|
|---|
| 367 |
|
|---|
| 368 | case 'T': /* time as %H:%M:%S */
|
|---|
| 369 | strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
|
|---|
| 370 | break;
|
|---|
| 371 | #endif
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 | #ifdef VMS_EXT
|
|---|
| 375 | case 'V': /* date as dd-bbb-YYYY */
|
|---|
| 376 | sprintf(tbuf, "%2d-%3.3s-%4d",
|
|---|
| 377 | range(1, timeptr->tm_mday, 31),
|
|---|
| 378 | months_a[range(0, timeptr->tm_mon, 11)],
|
|---|
| 379 | timeptr->tm_year + 1900);
|
|---|
| 380 | for (i = 3; i < 6; i++)
|
|---|
| 381 | if (islower(tbuf[i]))
|
|---|
| 382 | tbuf[i] = toupper(tbuf[i]);
|
|---|
| 383 | break;
|
|---|
| 384 | #endif
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 | #ifdef POSIX2_DATE
|
|---|
| 388 | case 'C':
|
|---|
| 389 | sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
|
|---|
| 390 | break;
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 | case 'E':
|
|---|
| 394 | case 'O':
|
|---|
| 395 | /* POSIX locale extensions, ignored for now */
|
|---|
| 396 | goto again;
|
|---|
| 397 | #endif
|
|---|
| 398 | default:
|
|---|
| 399 | tbuf[0] = '%';
|
|---|
| 400 | tbuf[1] = *format;
|
|---|
| 401 | tbuf[2] = '\0';
|
|---|
| 402 | break;
|
|---|
| 403 | }
|
|---|
| 404 | i = strlen(tbuf);
|
|---|
| 405 | if (i) {
|
|---|
| 406 | if (s + i < endp - 1) {
|
|---|
| 407 | strcpy(s, tbuf);
|
|---|
| 408 | s += i;
|
|---|
| 409 | } else
|
|---|
| 410 | return 0;
|
|---|
| 411 | }
|
|---|
| 412 | }
|
|---|
| 413 | out:
|
|---|
| 414 | if (s < endp && *format == '\0') {
|
|---|
| 415 | *s = '\0';
|
|---|
| 416 | return (s - start);
|
|---|
| 417 | } else
|
|---|
| 418 | return 0;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | /* weeknumber --- figure how many weeks into the year */
|
|---|
| 422 |
|
|---|
| 423 | /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
|
|---|
| 424 | /* Modified byMichal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK> */
|
|---|
| 425 |
|
|---|
| 426 | #ifndef __STDC__
|
|---|
| 427 | static int
|
|---|
| 428 | weeknumber(timeptr, firstweekday)
|
|---|
| 429 | const struct tm *timeptr;
|
|---|
| 430 | int firstweekday;
|
|---|
| 431 | #else
|
|---|
| 432 | static int
|
|---|
| 433 | weeknumber(const struct tm *timeptr, int firstweekday)
|
|---|
| 434 | #endif
|
|---|
| 435 | /*
|
|---|
| 436 | * firstweekday is 0 if starting in Sunday, non-zero if in Monday
|
|---|
| 437 | */
|
|---|
| 438 | {
|
|---|
| 439 | return (timeptr->tm_yday - timeptr->tm_wday +
|
|---|
| 440 | (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
|
|---|
| 441 | }
|
|---|