1 /* strftime - formatted time and date to a string */
3 * Modified slightly by Chet Ramey for inclusion in Bash
8 * Public-domain implementation of ISO C library routine.
10 * If you can't do prototypes, get GCC.
12 * The C99 standard now specifies just about all of the formats
13 * that were additional in the earlier versions of this file.
15 * For extensions from SunOS, add SUNOS_EXT.
16 * For extensions from HP/UX, add HPUX_EXT.
17 * For VMS dates, add VMS_EXT.
18 * For complete POSIX semantics, add POSIX_SEMANTICS.
20 * The code for %c, %x, and %X follows the C99 specification for
23 * This version ignores LOCALE information.
24 * It also doesn't worry about multi-byte characters.
28 * January, February, March, 1991
29 * Updated March, April 1992
31 * Updated February, 1994
33 * Updated January, 1995
34 * Updated September, 1995
35 * Updated January, 1996
37 * Updated October, 1999
38 * Updated September, 2000
39 * Updated December, 2001
40 * Updated January, 2011
42 * Fixes from ado@elsie.nci.nih.gov,
43 * February 1991, May 1992
44 * Fixes from Tor Lillqvist tml@tik.vtt.fi,
46 * Further fixes from ado@elsie.nci.nih.gov,
48 * %z code from chip@chinacat.unicom.com,
49 * Applied September 1995
50 * %V code fixed (again) and %G, %g added,
52 * %v code fixed, better configuration,
54 * Moved to C99 specification.
56 * Fixes from Tanaka Akira <akr@m17n.org>
65 #if defined(TM_IN_SYS_TIME)
66 #include <sys/types.h>
73 /* defaults: season to taste */
74 #define SUNOS_EXT 1 /* stuff in SunOS strftime routine */
75 #define VMS_EXT 1 /* include %v for VMS date format */
76 #define HPUX_EXT 1 /* non-conflicting stuff in HP-UX date */
77 #define POSIX_SEMANTICS 1 /* call tzset() if TZ changes */
79 #undef strchr /* avoid AIX weirdness */
82 extern char *get_string_value (const char *);
85 extern void tzset(void);
86 static int weeknumber(const struct tm *timeptr, int firstweekday);
87 static int iso8601wknum(const struct tm *timeptr);
91 #define inline __inline__
97 #define range(low, item, hi) max(low, min(item, hi))
99 #if !defined(OS2) && !defined(MSDOS) && defined(HAVE_TZNAME)
100 extern char *tzname[2];
102 #if defined(SOLARIS) || defined(mips) || defined (M_UNIX)
103 extern long int timezone, altzone;
106 extern long int timezone;
108 extern int timezone, altzone;
110 #endif /* !SOLARIS && !mips && !M_UNIX */
113 #undef min /* just in case */
115 /* format for %+ -- currently unused */
116 #ifndef NATIONAL_FORMAT
117 #define NATIONAL_FORMAT "%a %b %e %H:%M:%S %Z %Y"
120 /* min --- return minimum of two numbers */
125 return (a < b ? a : b);
128 #undef max /* also, just in case */
130 /* max --- return maximum of two numbers */
135 return (a > b ? a : b);
138 /* strftime --- produce formatted time */
141 strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
143 char *endp = s + maxsize;
149 static short first = 1;
150 #ifdef POSIX_SEMANTICS
151 static char *savetz = NULL;
152 static int savetzlen = 0;
154 #endif /* POSIX_SEMANTICS */
158 extern char *timezone();
160 struct timezone zone;
161 #endif /* HAVE_TZNAME */
162 #endif /* HAVE_TM_NAME */
163 #endif /* HAVE_TM_ZONE */
165 /* various tables, useful in North America */
166 static const char *days_a[] = {
167 "Sun", "Mon", "Tue", "Wed",
170 static const char *days_l[] = {
171 "Sunday", "Monday", "Tuesday", "Wednesday",
172 "Thursday", "Friday", "Saturday",
174 static const char *months_a[] = {
175 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
176 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
178 static const char *months_l[] = {
179 "January", "February", "March", "April",
180 "May", "June", "July", "August", "September",
181 "October", "November", "December",
183 static const char *ampm[] = { "AM", "PM", };
185 if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
188 /* quick check if we even need to bother */
189 if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
192 #ifndef POSIX_SEMANTICS
197 #else /* POSIX_SEMANTICS */
199 tz = get_string_value ("TZ");
205 int tzlen = strlen(tz);
207 savetz = (char *) malloc(tzlen + 1);
208 if (savetz != NULL) {
209 savetzlen = tzlen + 1;
216 /* if we have a saved TZ, and it is different, recapture and reset */
217 if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
220 savetz = (char *) realloc(savetz, i);
229 #endif /* POSIX_SEMANTICS */
231 for (; *format && s < endp - 1; format++) {
233 if (*format != '%') {
247 case 'a': /* abbreviated weekday name */
248 if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
251 strcpy(tbuf, days_a[timeptr->tm_wday]);
254 case 'A': /* full weekday name */
255 if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
258 strcpy(tbuf, days_l[timeptr->tm_wday]);
261 case 'b': /* abbreviated month name */
263 if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
266 strcpy(tbuf, months_a[timeptr->tm_mon]);
269 case 'B': /* full month name */
270 if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
273 strcpy(tbuf, months_l[timeptr->tm_mon]);
276 case 'c': /* appropriate date and time representation */
280 * strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
282 * Now, per the ISO 1999 C standard, it this:
284 strftime(tbuf, sizeof tbuf, "%A %B %d %T %Y", timeptr);
289 sprintf(tbuf, "%02ld", (timeptr->tm_year + 1900L) / 100);
292 case 'd': /* day of the month, 01 - 31 */
293 i = range(1, timeptr->tm_mday, 31);
294 sprintf(tbuf, "%02d", i);
297 case 'D': /* date as %m/%d/%y */
298 strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
301 case 'e': /* day of month, blank padded */
302 sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
306 /* POSIX (now C99) locale extensions, ignored for now */
309 case 'F': /* ISO 8601 date representation */
310 strftime(tbuf, sizeof tbuf, "%Y-%m-%d", timeptr);
318 * If it's December but the ISO week number is one,
319 * that week is in next year.
320 * If it's January but the ISO week number is 52 or
321 * 53, that week is in last year.
322 * Otherwise, it's this year.
324 w = iso8601wknum(timeptr);
325 if (timeptr->tm_mon == 11 && w == 1)
326 y = 1900L + timeptr->tm_year + 1;
327 else if (timeptr->tm_mon == 0 && w >= 52)
328 y = 1900L + timeptr->tm_year - 1;
330 y = 1900L + timeptr->tm_year;
333 sprintf(tbuf, "%ld", y);
335 sprintf(tbuf, "%02ld", y % 100);
338 case 'h': /* abbreviated month name */
341 case 'H': /* hour, 24-hour clock, 00 - 23 */
342 i = range(0, timeptr->tm_hour, 23);
343 sprintf(tbuf, "%02d", i);
346 case 'I': /* hour, 12-hour clock, 01 - 12 */
347 i = range(0, timeptr->tm_hour, 23);
352 sprintf(tbuf, "%02d", i);
355 case 'j': /* day of the year, 001 - 366 */
356 sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
359 case 'm': /* month, 01 - 12 */
360 i = range(0, timeptr->tm_mon, 11);
361 sprintf(tbuf, "%02d", i + 1);
364 case 'M': /* minute, 00 - 59 */
365 i = range(0, timeptr->tm_min, 59);
366 sprintf(tbuf, "%02d", i);
369 case 'n': /* same as \n */
375 /* POSIX (now C99) locale extensions, ignored for now */
378 case 'p': /* am or pm based on 12-hour clock */
379 i = range(0, timeptr->tm_hour, 23);
381 strcpy(tbuf, ampm[0]);
383 strcpy(tbuf, ampm[1]);
386 case 'r': /* time as %I:%M:%S %p */
387 strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
390 case 'R': /* time as %H:%M */
391 strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
394 #if defined(HAVE_MKTIME)
395 case 's': /* time as seconds since the Epoch */
397 struct tm non_const_timeptr;
399 non_const_timeptr = *timeptr;
400 sprintf(tbuf, "%ld", mktime(& non_const_timeptr));
403 #endif /* defined(HAVE_MKTIME) */
405 case 'S': /* second, 00 - 60 */
406 i = range(0, timeptr->tm_sec, 60);
407 sprintf(tbuf, "%02d", i);
410 case 't': /* same as \t */
415 case 'T': /* time as %H:%M:%S */
417 strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
421 /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
422 sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
426 case 'U': /* week of year, Sunday is first day of week */
427 sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
430 case 'V': /* week of year according ISO 8601 */
431 sprintf(tbuf, "%02d", iso8601wknum(timeptr));
434 case 'w': /* weekday, Sunday == 0, 0 - 6 */
435 i = range(0, timeptr->tm_wday, 6);
436 sprintf(tbuf, "%d", i);
439 case 'W': /* week of year, Monday is first day of week */
440 sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
443 case 'x': /* appropriate date representation */
444 strftime(tbuf, sizeof tbuf, "%A %B %d %Y", timeptr);
447 case 'X': /* appropriate time representation */
451 case 'y': /* year without a century, 00 - 99 */
453 i = timeptr->tm_year % 100;
454 sprintf(tbuf, "%02d", i);
457 case 'Y': /* year with century */
459 sprintf(tbuf, "%ld", 1900L + timeptr->tm_year);
463 * From: Chip Rosenthal <chip@chinacat.unicom.com>
464 * Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST)
466 * Warning: the %z [code] is implemented by inspecting the
467 * timezone name conditional compile settings, and
468 * inferring a method to get timezone offsets. I've tried
469 * this code on a couple of machines, but I don't doubt
470 * there is some system out there that won't like it.
471 * Maybe the easiest thing to do would be to bracket this
472 * with an #ifdef that can turn it off. The %z feature
473 * would be an admittedly obscure one that most folks can
474 * live without, but it would be a great help to those of
475 * us that muck around with various message processors.
477 case 'z': /* time zone offset east of GMT e.g. -0600 */
478 if (timeptr->tm_isdst < 0)
482 * Systems with tm_name probably have tm_tzadj as
483 * secs west of GMT. Convert to mins east of GMT.
485 off = -timeptr->tm_tzadj / 60;
486 #else /* !HAVE_TM_NAME */
489 * Systems with tm_zone probably have tm_gmtoff as
490 * secs east of GMT. Convert to mins east of GMT.
492 off = timeptr->tm_gmtoff / 60;
493 #else /* !HAVE_TM_ZONE */
496 * Systems with tzname[] probably have timezone as
497 * secs west of GMT. Convert to mins east of GMT.
500 off = -timezone / 60;
502 /* ADR: 4 August 2001, fixed this per gazelle@interaccess.com */
503 off = -(daylight ? altzone : timezone) / 60;
505 #else /* !HAVE_TZNAME */
506 gettimeofday(& tv, & zone);
507 off = -zone.tz_minuteswest;
508 #endif /* !HAVE_TZNAME */
509 #endif /* !HAVE_TM_ZONE */
510 #endif /* !HAVE_TM_NAME */
517 sprintf(tbuf+1, "%02ld%02ld", off/60, off%60);
520 case 'Z': /* time zone name or abbrevation */
522 i = (daylight && timeptr->tm_isdst > 0); /* 0 or 1 */
523 strcpy(tbuf, tzname[i]);
526 strcpy(tbuf, timeptr->tm_zone);
529 strcpy(tbuf, timeptr->tm_name);
531 gettimeofday(& tv, & zone);
532 strcpy(tbuf, timezone(zone.tz_minuteswest,
533 timeptr->tm_isdst > 0));
534 #endif /* HAVE_TM_NAME */
535 #endif /* HAVE_TM_ZONE */
536 #endif /* HAVE_TZNAME */
540 case 'k': /* hour, 24-hour clock, blank pad */
541 sprintf(tbuf, "%2d", range(0, timeptr->tm_hour, 23));
544 case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
545 i = range(0, timeptr->tm_hour, 23);
550 sprintf(tbuf, "%2d", i);
555 case 'N': /* Emperor/Era name */
556 /* this is essentially the same as the century */
557 goto century; /* %C */
559 case 'o': /* Emperor/Era year */
561 #endif /* HPUX_EXT */
565 case 'v': /* date as dd-bbb-YYYY */
566 sprintf(tbuf, "%2d-%3.3s-%4ld",
567 range(1, timeptr->tm_mday, 31),
568 months_a[range(0, timeptr->tm_mon, 11)],
569 timeptr->tm_year + 1900L);
570 for (i = 3; i < 6; i++)
571 if (islower(tbuf[i]))
572 tbuf[i] = toupper(tbuf[i]);
584 if (s + i < endp - 1) {
592 if (s < endp && *format == '\0') {
599 /* isleap --- is a year a leap year? */
604 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
608 /* iso8601wknum --- compute week number according to ISO 8601 */
611 iso8601wknum(const struct tm *timeptr)
615 * If the week (Monday to Sunday) containing January 1
616 * has four or more days in the new year, then it is week 1;
617 * otherwise it is the highest numbered week of the previous
618 * year (52 or 53), and the next week is week 1.
620 * ADR: This means if Jan 1 was Monday through Thursday,
621 * it was week 1, otherwise week 52 or 53.
623 * XPG4 erroneously included POSIX.2 rationale text in the
624 * main body of the standard. Thus it requires week 53.
627 int weeknum, jan1day, diff;
629 /* get week number, Monday as first day of the week */
630 weeknum = weeknumber(timeptr, 1);
633 * With thanks and tip of the hatlo to tml@tik.vtt.fi
635 * What day of the week does January 1 fall on?
637 * (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
638 * (timeptr->tm_wday - jan1.tm_wday) MOD 7
642 * timeptr->tm_wday MOD 7 == timeptr->tm_wday
643 * from which it follows that. . .
645 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
650 * If Jan 1 was a Monday through Thursday, it was in
651 * week 1. Otherwise it was last year's highest week, which is
652 * this year's week 0.
654 * What does that mean?
655 * If Jan 1 was Monday, the week number is exactly right, it can
657 * If it was Tuesday through Thursday, the weeknumber is one
658 * less than it should be, so we add one.
659 * Otherwise, Friday, Saturday or Sunday, the week number is
660 * OK, but if it is 0, it needs to be 52 or 53.
665 case 2: /* Tuesday */
666 case 3: /* Wednesday */
667 case 4: /* Thursday */
671 case 6: /* Saturday */
674 #ifdef USE_BROKEN_XPG4
675 /* XPG4 (as of March 1994) says 53 unconditionally */
678 /* get week number of last week of last year */
679 struct tm dec31ly; /* 12/31 last year */
683 dec31ly.tm_mday = 31;
684 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
685 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
686 weeknum = iso8601wknum(& dec31ly);
692 if (timeptr->tm_mon == 11) {
694 * The last week of the year
695 * can be in week 1 of next year.
698 * This can only happen if
706 wday = timeptr->tm_wday;
707 mday = timeptr->tm_mday;
708 if ( (wday == 1 && (mday >= 29 && mday <= 31))
709 || (wday == 2 && (mday == 30 || mday == 31))
710 || (wday == 3 && mday == 31))
717 /* weeknumber --- figure how many weeks into the year */
719 /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
722 weeknumber(const struct tm *timeptr, int firstweekday)
724 int wday = timeptr->tm_wday;
727 if (firstweekday == 1) {
728 if (wday == 0) /* sunday */
733 ret = ((timeptr->tm_yday + 7 - wday) / 7);
740 /* ADR --- I'm loathe to mess with ado's code ... */
742 Date: Wed, 24 Apr 91 20:54:08 MDT
743 From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
744 To: arnold@audiofax.com
747 in a process of fixing of strftime() in libraries on Atari ST I grabbed
748 some pieces of code from your own strftime. When doing that it came
749 to mind that your weeknumber() function compiles a little bit nicer
750 in the following form:
752 * firstweekday is 0 if starting in Sunday, non-zero if in Monday
755 return (timeptr->tm_yday - timeptr->tm_wday +
756 (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
758 How nicer it depends on a compiler, of course, but always a tiny bit.
762 ntomczak@vm.ucs.ualberta.ca
775 * "tst" is a test driver for the function "strftime".
782 * Control Data Systems, Inc.
783 * vogelke@c-17igp.wpafb.af.mil
789 * cc -o tst -DTEST_STRFTIME strftime.c
792 /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
797 #include <sys/time.h>
803 * Array of time formats.
806 static char *array[] =
808 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
809 "(%%B) full month name, var length (January..December) %B",
811 "(%%D) date (%%m/%%d/%%y) %D",
812 "(%%E) Locale extensions (ignored) %E",
813 "(%%F) full month name, var length (January..December) %F",
814 "(%%H) hour (24-hour clock, 00..23) %H",
815 "(%%I) hour (12-hour clock, 01..12) %I",
816 "(%%M) minute (00..59) %M",
817 "(%%N) Emporer/Era Name %N",
818 "(%%O) Locale extensions (ignored) %O",
819 "(%%R) time, 24-hour (%%H:%%M) %R",
820 "(%%S) second (00..60) %S",
821 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
822 "(%%U) week of year, Sunday as first day of week (00..53) %U",
823 "(%%V) week of year according to ISO 8601 %V",
824 "(%%W) week of year, Monday as first day of week (00..53) %W",
825 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
826 "(%%Y) year with century (1970...) %Y",
827 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
828 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
829 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
830 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
831 "(%%d) day of the month (01..31) %d",
832 "(%%e) day of the month, blank-padded ( 1..31) %e",
833 "(%%h) should be same as (%%b) %h",
834 "(%%j) day of the year (001..366) %j",
835 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
836 "(%%l) hour, 12-hour clock, blank pad ( 0..12) %l",
837 "(%%m) month (01..12) %m",
838 "(%%o) Emporer/Era Year %o",
839 "(%%p) locale's AM or PM based on 12-hour clock %p",
840 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
841 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
842 "(%%v) VMS date (dd-bbb-YYYY) %v",
843 "(%%w) day of week (0..6, Sunday == 0) %w",
844 "(%%x) appropriate locale date representation %x",
845 "(%%y) last two digits of year (00..99) %y",
846 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
860 char string[MAXTIME];
869 /* Call the function. */
871 clock = time((long *) 0);
872 tm = localtime(&clock);
874 for (k = 0; next = array[k]; k++) {
875 length = strftime(string, MAXTIME, next, tm);
876 printf("%s\n", string);
881 #endif /* TEST_STRFTIME */