2 * 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.
27 * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
28 * code are included if GAWK is defined.
31 * January, February, March, 1991
32 * Updated March, April 1992
34 * Updated February, 1994
36 * Updated January, 1995
37 * Updated September, 1995
38 * Updated January, 1996
40 * Updated October, 1999
41 * Updated September, 2000
43 * Fixes from ado@elsie.nci.nih.gov,
44 * February 1991, May 1992
45 * Fixes from Tor Lillqvist tml@tik.vtt.fi,
47 * Further fixes from ado@elsie.nci.nih.gov,
49 * %z code from chip@chinacat.unicom.com,
50 * Applied September 1995
51 * %V code fixed (again) and %G, %g added,
53 * %v code fixed, better configuration,
55 * Moved to C99 specification.
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 */
78 #define POSIX_SEMANTICS 1 /* call tzset() if TZ changes */
81 #undef strchr /* avoid AIX weirdness */
83 extern void tzset(void);
84 static int weeknumber(const struct tm *timeptr, int firstweekday);
85 static int iso8601wknum(const struct tm *timeptr);
88 #define inline __inline__
93 #define range(low, item, hi) max(low, min(item, hi))
95 #if !defined(OS2) && !defined(MSDOS) && defined(HAVE_TZNAME)
96 extern char *tzname[2];
98 #if defined(SOLARIS) || defined(mips) || defined (M_UNIX)
99 extern long int timezone, altzone;
101 extern int timezone, altzone;
105 #undef min /* just in case */
107 /* min --- return minimum of two numbers */
112 return (a < b ? a : b);
115 #undef max /* also, just in case */
117 /* max --- return maximum of two numbers */
122 return (a > b ? a : b);
125 /* strftime --- produce formatted time */
128 strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
130 char *endp = s + maxsize;
135 static short first = 1;
136 #ifdef POSIX_SEMANTICS
137 static char *savetz = NULL;
138 static int savetzlen = 0;
140 #endif /* POSIX_SEMANTICS */
144 extern char *timezone();
146 struct timezone zone;
147 #endif /* HAVE_TZNAME */
148 #endif /* HAVE_TM_NAME */
149 #endif /* HAVE_TM_ZONE */
151 /* various tables, useful in North America */
152 static const char *days_a[] = {
153 "Sun", "Mon", "Tue", "Wed",
156 static const char *days_l[] = {
157 "Sunday", "Monday", "Tuesday", "Wednesday",
158 "Thursday", "Friday", "Saturday",
160 static const char *months_a[] = {
161 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
162 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
164 static const char *months_l[] = {
165 "January", "February", "March", "April",
166 "May", "June", "July", "August", "September",
167 "October", "November", "December",
169 static const char *ampm[] = { "AM", "PM", };
171 if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
174 /* quick check if we even need to bother */
175 if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
178 #ifndef POSIX_SEMANTICS
183 #else /* POSIX_SEMANTICS */
185 tz = get_string_value ("TZ");
191 int tzlen = strlen(tz);
193 savetz = (char *) malloc(tzlen + 1);
194 if (savetz != NULL) {
195 savetzlen = tzlen + 1;
202 /* if we have a saved TZ, and it is different, recapture and reset */
203 if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
206 savetz = (char *) realloc(savetz, i);
215 #endif /* POSIX_SEMANTICS */
217 for (; *format && s < endp - 1; format++) {
219 if (*format != '%') {
233 case 'a': /* abbreviated weekday name */
234 if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
237 strcpy(tbuf, days_a[timeptr->tm_wday]);
240 case 'A': /* full weekday name */
241 if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
244 strcpy(tbuf, days_l[timeptr->tm_wday]);
247 case 'b': /* abbreviated month name */
249 if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
252 strcpy(tbuf, months_a[timeptr->tm_mon]);
255 case 'B': /* full month name */
256 if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
259 strcpy(tbuf, months_l[timeptr->tm_mon]);
262 case 'c': /* appropriate date and time representation */
266 * strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
268 * Now, per the ISO 1999 C standard, it this:
270 strftime(tbuf, sizeof tbuf, "%A %B %d %T %Y", timeptr);
275 sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
278 case 'd': /* day of the month, 01 - 31 */
279 i = range(1, timeptr->tm_mday, 31);
280 sprintf(tbuf, "%02d", i);
283 case 'D': /* date as %m/%d/%y */
284 strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
287 case 'e': /* day of month, blank padded */
288 sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
292 /* POSIX (now C99) locale extensions, ignored for now */
295 case 'F': /* ISO 8601 date representation */
296 strftime(tbuf, sizeof tbuf, "%Y-%m-%d", timeptr);
304 * If it's December but the ISO week number is one,
305 * that week is in next year.
306 * If it's January but the ISO week number is 52 or
307 * 53, that week is in last year.
308 * Otherwise, it's this year.
310 w = iso8601wknum(timeptr);
311 if (timeptr->tm_mon == 11 && w == 1)
312 y = 1900 + timeptr->tm_year + 1;
313 else if (timeptr->tm_mon == 0 && w >= 52)
314 y = 1900 + timeptr->tm_year - 1;
316 y = 1900 + timeptr->tm_year;
319 sprintf(tbuf, "%d", y);
321 sprintf(tbuf, "%02d", y % 100);
324 case 'h': /* abbreviated month name */
327 case 'H': /* hour, 24-hour clock, 00 - 23 */
328 i = range(0, timeptr->tm_hour, 23);
329 sprintf(tbuf, "%02d", i);
332 case 'I': /* hour, 12-hour clock, 01 - 12 */
333 i = range(0, timeptr->tm_hour, 23);
338 sprintf(tbuf, "%02d", i);
341 case 'j': /* day of the year, 001 - 366 */
342 sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
345 case 'm': /* month, 01 - 12 */
346 i = range(0, timeptr->tm_mon, 11);
347 sprintf(tbuf, "%02d", i + 1);
350 case 'M': /* minute, 00 - 59 */
351 i = range(0, timeptr->tm_min, 59);
352 sprintf(tbuf, "%02d", i);
355 case 'n': /* same as \n */
361 /* POSIX (now C99) locale extensions, ignored for now */
364 case 'p': /* am or pm based on 12-hour clock */
365 i = range(0, timeptr->tm_hour, 23);
367 strcpy(tbuf, ampm[0]);
369 strcpy(tbuf, ampm[1]);
372 case 'r': /* time as %I:%M:%S %p */
373 strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
376 case 'R': /* time as %H:%M */
377 strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
380 #if defined(HAVE_MKTIME) || defined(GAWK)
381 case 's': /* time as seconds since the Epoch */
383 struct tm non_const_timeptr;
385 non_const_timeptr = *timeptr;
386 sprintf(tbuf, "%ld", mktime(& non_const_timeptr));
389 #endif /* defined(HAVE_MKTIME) || defined(GAWK) */
391 case 'S': /* second, 00 - 60 */
392 i = range(0, timeptr->tm_sec, 60);
393 sprintf(tbuf, "%02d", i);
396 case 't': /* same as \t */
401 case 'T': /* time as %H:%M:%S */
403 strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
407 /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
408 sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
412 case 'U': /* week of year, Sunday is first day of week */
413 sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
416 case 'V': /* week of year according ISO 8601 */
417 sprintf(tbuf, "%02d", iso8601wknum(timeptr));
420 case 'w': /* weekday, Sunday == 0, 0 - 6 */
421 i = range(0, timeptr->tm_wday, 6);
422 sprintf(tbuf, "%d", i);
425 case 'W': /* week of year, Monday is first day of week */
426 sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
429 case 'x': /* appropriate date representation */
430 strftime(tbuf, sizeof tbuf, "%A %B %d %Y", timeptr);
433 case 'X': /* appropriate time representation */
437 case 'y': /* year without a century, 00 - 99 */
439 i = timeptr->tm_year % 100;
440 sprintf(tbuf, "%02d", i);
443 case 'Y': /* year with century */
445 sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
449 * From: Chip Rosenthal <chip@chinacat.unicom.com>
450 * Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST)
452 * Warning: the %z [code] is implemented by inspecting the
453 * timezone name conditional compile settings, and
454 * inferring a method to get timezone offsets. I've tried
455 * this code on a couple of machines, but I don't doubt
456 * there is some system out there that won't like it.
457 * Maybe the easiest thing to do would be to bracket this
458 * with an #ifdef that can turn it off. The %z feature
459 * would be an admittedly obscure one that most folks can
460 * live without, but it would be a great help to those of
461 * us that muck around with various message processors.
463 case 'z': /* time zone offset east of GMT e.g. -0600 */
466 * Systems with tm_name probably have tm_tzadj as
467 * secs west of GMT. Convert to mins east of GMT.
469 off = -timeptr->tm_tzadj / 60;
470 #else /* !HAVE_TM_NAME */
473 * Systems with tm_zone probably have tm_gmtoff as
474 * secs east of GMT. Convert to mins east of GMT.
476 off = timeptr->tm_gmtoff / 60;
477 #else /* !HAVE_TM_ZONE */
480 * Systems with tzname[] probably have timezone as
481 * secs west of GMT. Convert to mins east of GMT.
483 off = -(daylight ? timezone : altzone) / 60;
484 #else /* !HAVE_TZNAME */
485 off = -zone.tz_minuteswest;
486 #endif /* !HAVE_TZNAME */
487 #endif /* !HAVE_TM_ZONE */
488 #endif /* !HAVE_TM_NAME */
495 sprintf(tbuf+1, "%02d%02d", off/60, off%60);
498 case 'Z': /* time zone name or abbrevation */
500 i = (daylight && timeptr->tm_isdst > 0); /* 0 or 1 */
501 strcpy(tbuf, tzname[i]);
504 strcpy(tbuf, timeptr->tm_zone);
507 strcpy(tbuf, timeptr->tm_name);
509 gettimeofday(& tv, & zone);
510 strcpy(tbuf, timezone(zone.tz_minuteswest,
511 timeptr->tm_isdst > 0));
512 #endif /* HAVE_TM_NAME */
513 #endif /* HAVE_TM_ZONE */
514 #endif /* HAVE_TZNAME */
518 case 'k': /* hour, 24-hour clock, blank pad */
519 sprintf(tbuf, "%2d", range(0, timeptr->tm_hour, 23));
522 case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
523 i = range(0, timeptr->tm_hour, 23);
528 sprintf(tbuf, "%2d", i);
533 case 'N': /* Emperor/Era name */
534 /* this is essentially the same as the century */
535 goto century; /* %C */
537 case 'o': /* Emperor/Era year */
539 #endif /* HPUX_EXT */
543 case 'v': /* date as dd-bbb-YYYY */
544 sprintf(tbuf, "%2d-%3.3s-%4d",
545 range(1, timeptr->tm_mday, 31),
546 months_a[range(0, timeptr->tm_mon, 11)],
547 timeptr->tm_year + 1900);
548 for (i = 3; i < 6; i++)
549 if (islower(tbuf[i]))
550 tbuf[i] = toupper(tbuf[i]);
562 if (s + i < endp - 1) {
570 if (s < endp && *format == '\0') {
577 /* isleap --- is a year a leap year? */
582 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
586 /* iso8601wknum --- compute week number according to ISO 8601 */
589 iso8601wknum(const struct tm *timeptr)
593 * If the week (Monday to Sunday) containing January 1
594 * has four or more days in the new year, then it is week 1;
595 * otherwise it is the highest numbered week of the previous
596 * year (52 or 53), and the next week is week 1.
598 * ADR: This means if Jan 1 was Monday through Thursday,
599 * it was week 1, otherwise week 52 or 53.
601 * XPG4 erroneously included POSIX.2 rationale text in the
602 * main body of the standard. Thus it requires week 53.
605 int weeknum, jan1day, diff;
607 /* get week number, Monday as first day of the week */
608 weeknum = weeknumber(timeptr, 1);
611 * With thanks and tip of the hatlo to tml@tik.vtt.fi
613 * What day of the week does January 1 fall on?
615 * (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
616 * (timeptr->tm_wday - jan1.tm_wday) MOD 7
620 * timeptr->tm_wday MOD 7 == timeptr->tm_wday
621 * from which it follows that. . .
623 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
628 * If Jan 1 was a Monday through Thursday, it was in
629 * week 1. Otherwise it was last year's highest week, which is
630 * this year's week 0.
632 * What does that mean?
633 * If Jan 1 was Monday, the week number is exactly right, it can
635 * If it was Tuesday through Thursday, the weeknumber is one
636 * less than it should be, so we add one.
637 * Otherwise, Friday, Saturday or Sunday, the week number is
638 * OK, but if it is 0, it needs to be 52 or 53.
643 case 2: /* Tuesday */
644 case 3: /* Wednesday */
645 case 4: /* Thursday */
649 case 6: /* Saturday */
652 #ifdef USE_BROKEN_XPG4
653 /* XPG4 (as of March 1994) says 53 unconditionally */
656 /* get week number of last week of last year */
657 struct tm dec31ly; /* 12/31 last year */
661 dec31ly.tm_mday = 31;
662 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
663 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900);
664 weeknum = iso8601wknum(& dec31ly);
670 if (timeptr->tm_mon == 11) {
672 * The last week of the year
673 * can be in week 1 of next year.
676 * This can only happen if
684 wday = timeptr->tm_wday;
685 mday = timeptr->tm_mday;
686 if ( (wday == 1 && (mday >= 29 && mday <= 31))
687 || (wday == 2 && (mday == 30 || mday == 31))
688 || (wday == 3 && mday == 31))
695 /* weeknumber --- figure how many weeks into the year */
697 /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
700 weeknumber(const struct tm *timeptr, int firstweekday)
702 int wday = timeptr->tm_wday;
705 if (firstweekday == 1) {
706 if (wday == 0) /* sunday */
711 ret = ((timeptr->tm_yday + 7 - wday) / 7);
718 /* ADR --- I'm loathe to mess with ado's code ... */
720 Date: Wed, 24 Apr 91 20:54:08 MDT
721 From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
722 To: arnold@audiofax.com
725 in a process of fixing of strftime() in libraries on Atari ST I grabbed
726 some pieces of code from your own strftime. When doing that it came
727 to mind that your weeknumber() function compiles a little bit nicer
728 in the following form:
730 * firstweekday is 0 if starting in Sunday, non-zero if in Monday
733 return (timeptr->tm_yday - timeptr->tm_wday +
734 (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
736 How nicer it depends on a compiler, of course, but always a tiny bit.
740 ntomczak@vm.ucs.ualberta.ca
753 * "tst" is a test driver for the function "strftime".
760 * Control Data Systems, Inc.
761 * vogelke@c-17igp.wpafb.af.mil
767 * cc -o tst -DTEST_STRFTIME strftime.c
770 /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
775 #include <sys/time.h>
781 * Array of time formats.
784 static char *array[] =
786 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
787 "(%%B) full month name, var length (January..December) %B",
789 "(%%D) date (%%m/%%d/%%y) %D",
790 "(%%E) Locale extensions (ignored) %E",
791 "(%%F) full month name, var length (January..December) %F",
792 "(%%H) hour (24-hour clock, 00..23) %H",
793 "(%%I) hour (12-hour clock, 01..12) %I",
794 "(%%M) minute (00..59) %M",
795 "(%%N) Emporer/Era Name %N",
796 "(%%O) Locale extensions (ignored) %O",
797 "(%%R) time, 24-hour (%%H:%%M) %R",
798 "(%%S) second (00..60) %S",
799 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
800 "(%%U) week of year, Sunday as first day of week (00..53) %U",
801 "(%%V) week of year according to ISO 8601 %V",
802 "(%%W) week of year, Monday as first day of week (00..53) %W",
803 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
804 "(%%Y) year with century (1970...) %Y",
805 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
806 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
807 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
808 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
809 "(%%d) day of the month (01..31) %d",
810 "(%%e) day of the month, blank-padded ( 1..31) %e",
811 "(%%h) should be same as (%%b) %h",
812 "(%%j) day of the year (001..366) %j",
813 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
814 "(%%l) hour, 12-hour clock, blank pad ( 0..12) %l",
815 "(%%m) month (01..12) %m",
816 "(%%o) Emporer/Era Year %o",
817 "(%%p) locale's AM or PM based on 12-hour clock %p",
818 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
819 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
820 "(%%v) VMS date (dd-bbb-YYYY) %v",
821 "(%%w) day of week (0..6, Sunday == 0) %w",
822 "(%%x) appropriate locale date representation %x",
823 "(%%y) last two digits of year (00..99) %y",
824 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
838 char string[MAXTIME];
847 /* Call the function. */
849 clock = time((long *) 0);
850 tm = localtime(&clock);
852 for (k = 0; next = array[k]; k++) {
853 length = strftime(string, MAXTIME, next, tm);
854 printf("%s\n", string);
859 #endif /* TEST_STRFTIME */