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
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.
57 * Fixes from Tanaka Akira <akr@m17n.org>
67 #if defined(TM_IN_SYS_TIME)
68 #include <sys/types.h>
75 /* defaults: season to taste */
76 #define SUNOS_EXT 1 /* stuff in SunOS strftime routine */
77 #define VMS_EXT 1 /* include %v for VMS date format */
78 #define HPUX_EXT 1 /* non-conflicting stuff in HP-UX date */
79 #define POSIX_SEMANTICS 1 /* call tzset() if TZ changes */
80 #define POSIX_2008 1 /* flag and fw for C, F, G, Y formats */
82 #undef strchr /* avoid AIX weirdness */
89 extern char *get_string_value (const char *);
92 extern void tzset(void);
93 static int weeknumber(const struct tm
*timeptr
, int firstweekday
);
94 static int iso8601wknum(const struct tm
*timeptr
);
98 #define inline __inline__
104 #define range(low, item, hi) max(low, min(item, hi))
106 /* Whew! This stuff is a mess. */
107 #if !defined(OS2) && !defined(MSDOS) && !defined(__CYGWIN__) && defined(HAVE_TZNAME)
108 extern char *tzname
[2];
110 #if defined(SOLARIS) || defined(mips) || defined (M_UNIX)
111 extern long int timezone
, altzone
;
113 # if defined (HPUX) || defined(__hpux)
114 extern long int timezone
;
116 # if !defined(__CYGWIN__)
117 extern int timezone
, altzone
;
123 #undef min /* just in case */
125 /* min --- return minimum of two numbers */
130 return (a
< b
? a
: b
);
133 #undef max /* also, just in case */
135 /* max --- return maximum of two numbers */
140 return (a
> b
? a
: b
);
144 /* iso_8601_2000_year --- format a year per ISO 8601:2000 as in 1003.1 */
147 iso_8601_2000_year(char *buf
, int year
, size_t fw
)
152 if (year
>= -9999 && year
<= 9999) {
153 sprintf(buf
, "%0*d", (int) fw
, year
);
157 /* now things get weird */
165 extra
= year
/ 10000;
167 sprintf(buf
, "%c_%04d_%d", sign
, extra
, year
);
169 #endif /* POSIX_2008 */
171 /* strftime --- produce formatted time */
174 strftime(char *s
, size_t maxsize
, const char *format
, const struct tm
*timeptr
)
176 char *endp
= s
+ maxsize
;
182 static short first
= 1;
183 #ifdef POSIX_SEMANTICS
184 static char *savetz
= NULL
;
185 static int savetzlen
= 0;
187 #endif /* POSIX_SEMANTICS */
192 extern char *timezone();
194 struct timezone zone
;
195 #endif /* __CYGWIN__ */
196 #endif /* HAVE_TZNAME */
197 #endif /* HAVE_TM_NAME */
198 #endif /* HAVE_TM_ZONE */
203 #endif /* POSIX_2008 */
205 /* various tables, useful in North America */
206 static const char *days_a
[] = {
207 "Sun", "Mon", "Tue", "Wed",
210 static const char *days_l
[] = {
211 "Sunday", "Monday", "Tuesday", "Wednesday",
212 "Thursday", "Friday", "Saturday",
214 static const char *months_a
[] = {
215 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
216 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
218 static const char *months_l
[] = {
219 "January", "February", "March", "April",
220 "May", "June", "July", "August", "September",
221 "October", "November", "December",
223 static const char *ampm
[] = { "AM", "PM", };
227 if (s
== NULL
|| format
== NULL
|| timeptr
== NULL
|| maxsize
== 0)
230 /* quick check if we even need to bother */
231 if (strchr(format
, '%') == NULL
&& strlen(format
) + 1 >= maxsize
)
234 #ifndef POSIX_SEMANTICS
239 #else /* POSIX_SEMANTICS */
241 tz
= get_string_value ("TZ");
247 int tzlen
= strlen(tz
);
249 savetz
= (char *) malloc(tzlen
+ 1);
250 if (savetz
!= NULL
) {
251 savetzlen
= tzlen
+ 1;
258 /* if we have a saved TZ, and it is different, recapture and reset */
259 if (tz
&& savetz
&& (tz
[0] != savetz
[0] || strcmp(tz
, savetz
) != 0)) {
262 savetz
= (char *) realloc(savetz
, i
);
271 #endif /* POSIX_SEMANTICS */
273 for (; *format
&& s
< endp
- 1; format
++) {
275 if (*format
!= '%') {
307 for (; isdigit(*format
); format
++) {
308 fw
= fw
* 10 + (*format
- '0');
311 #endif /* POSIX_2008 */
323 case 'a': /* abbreviated weekday name */
324 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
> 6)
327 strcpy(tbuf
, days_a
[timeptr
->tm_wday
]);
330 case 'A': /* full weekday name */
331 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
> 6)
334 strcpy(tbuf
, days_l
[timeptr
->tm_wday
]);
337 case 'b': /* abbreviated month name */
339 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
> 11)
342 strcpy(tbuf
, months_a
[timeptr
->tm_mon
]);
345 case 'B': /* full month name */
346 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
> 11)
349 strcpy(tbuf
, months_l
[timeptr
->tm_mon
]);
352 case 'c': /* appropriate date and time representation */
356 * strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
358 * Now, per the ISO 1999 C standard, it this:
360 strftime(tbuf
, sizeof tbuf
, "%A %B %d %T %Y", timeptr
);
365 if (pad
!= '\0' && fw
> 0) {
366 size_t min_fw
= (flag
? 3 : 2);
368 fw
= max(fw
, min_fw
);
372 (timeptr
->tm_year
+ 1900L) / 100);
374 #endif /* POSIX_2008 */
376 sprintf(tbuf
, "%02ld", (timeptr
->tm_year
+ 1900L) / 100);
379 case 'd': /* day of the month, 01 - 31 */
380 i
= range(1, timeptr
->tm_mday
, 31);
381 sprintf(tbuf
, "%02d", i
);
384 case 'D': /* date as %m/%d/%y */
385 strftime(tbuf
, sizeof tbuf
, "%m/%d/%y", timeptr
);
388 case 'e': /* day of month, blank padded */
389 sprintf(tbuf
, "%2d", range(1, timeptr
->tm_mday
, 31));
393 /* POSIX (now C99) locale extensions, ignored for now */
396 case 'F': /* ISO 8601 date representation */
400 * Field width for %F is for the whole thing.
401 * It must be at least 10.
404 strftime(m_d
, sizeof m_d
, "-%m-%d", timeptr
);
407 if (pad
!= '\0' && fw
> 0) {
408 fw
= max(fw
, min_fw
);
413 fw
-= 6; /* -XX-XX at end are invariant */
415 iso_8601_2000_year(tbuf
, timeptr
->tm_year
+ 1900, fw
);
418 strftime(tbuf
, sizeof tbuf
, "%Y-%m-%d", timeptr
);
419 #endif /* POSIX_2008 */
428 * If it's December but the ISO week number is one,
429 * that week is in next year.
430 * If it's January but the ISO week number is 52 or
431 * 53, that week is in last year.
432 * Otherwise, it's this year.
434 w
= iso8601wknum(timeptr
);
435 if (timeptr
->tm_mon
== 11 && w
== 1)
436 y
= 1900L + timeptr
->tm_year
+ 1;
437 else if (timeptr
->tm_mon
== 0 && w
>= 52)
438 y
= 1900L + timeptr
->tm_year
- 1;
440 y
= 1900L + timeptr
->tm_year
;
442 if (*format
== 'G') {
444 if (pad
!= '\0' && fw
> 0) {
447 fw
= max(fw
, min_fw
);
453 #endif /* POSIX_2008 */
454 sprintf(tbuf
, "%ld", y
);
457 sprintf(tbuf
, "%02ld", y
% 100);
460 case 'h': /* abbreviated month name */
463 case 'H': /* hour, 24-hour clock, 00 - 23 */
464 i
= range(0, timeptr
->tm_hour
, 23);
465 sprintf(tbuf
, "%02d", i
);
468 case 'I': /* hour, 12-hour clock, 01 - 12 */
469 i
= range(0, timeptr
->tm_hour
, 23);
474 sprintf(tbuf
, "%02d", i
);
477 case 'j': /* day of the year, 001 - 366 */
478 sprintf(tbuf
, "%03d", timeptr
->tm_yday
+ 1);
481 case 'm': /* month, 01 - 12 */
482 i
= range(0, timeptr
->tm_mon
, 11);
483 sprintf(tbuf
, "%02d", i
+ 1);
486 case 'M': /* minute, 00 - 59 */
487 i
= range(0, timeptr
->tm_min
, 59);
488 sprintf(tbuf
, "%02d", i
);
491 case 'n': /* same as \n */
497 /* POSIX (now C99) locale extensions, ignored for now */
500 case 'p': /* am or pm based on 12-hour clock */
501 i
= range(0, timeptr
->tm_hour
, 23);
503 strcpy(tbuf
, ampm
[0]);
505 strcpy(tbuf
, ampm
[1]);
508 case 'r': /* time as %I:%M:%S %p */
509 strftime(tbuf
, sizeof tbuf
, "%I:%M:%S %p", timeptr
);
512 case 'R': /* time as %H:%M */
513 strftime(tbuf
, sizeof tbuf
, "%H:%M", timeptr
);
516 #if defined(HAVE_MKTIME)
517 case 's': /* time as seconds since the Epoch */
519 struct tm non_const_timeptr
;
521 non_const_timeptr
= *timeptr
;
522 sprintf(tbuf
, "%ld", mktime(& non_const_timeptr
));
525 #endif /* defined(HAVE_MKTIME) */
527 case 'S': /* second, 00 - 60 */
528 i
= range(0, timeptr
->tm_sec
, 60);
529 sprintf(tbuf
, "%02d", i
);
532 case 't': /* same as \t */
537 case 'T': /* time as %H:%M:%S */
539 strftime(tbuf
, sizeof tbuf
, "%H:%M:%S", timeptr
);
543 /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
544 sprintf(tbuf
, "%d", timeptr
->tm_wday
== 0 ? 7 :
548 case 'U': /* week of year, Sunday is first day of week */
549 sprintf(tbuf
, "%02d", weeknumber(timeptr
, 0));
552 case 'V': /* week of year according ISO 8601 */
553 sprintf(tbuf
, "%02d", iso8601wknum(timeptr
));
556 case 'w': /* weekday, Sunday == 0, 0 - 6 */
557 i
= range(0, timeptr
->tm_wday
, 6);
558 sprintf(tbuf
, "%d", i
);
561 case 'W': /* week of year, Monday is first day of week */
562 sprintf(tbuf
, "%02d", weeknumber(timeptr
, 1));
565 case 'x': /* appropriate date representation */
566 strftime(tbuf
, sizeof tbuf
, "%A %B %d %Y", timeptr
);
569 case 'X': /* appropriate time representation */
573 case 'y': /* year without a century, 00 - 99 */
575 i
= timeptr
->tm_year
% 100;
576 sprintf(tbuf
, "%02d", i
);
579 case 'Y': /* year with century */
581 if (pad
!= '\0' && fw
> 0) {
584 fw
= max(fw
, min_fw
);
588 1900L + timeptr
->tm_year
);
590 #endif /* POSIX_2008 */
591 sprintf(tbuf
, "%ld", 1900L + timeptr
->tm_year
);
595 * From: Chip Rosenthal <chip@chinacat.unicom.com>
596 * Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST)
598 * Warning: the %z [code] is implemented by inspecting the
599 * timezone name conditional compile settings, and
600 * inferring a method to get timezone offsets. I've tried
601 * this code on a couple of machines, but I don't doubt
602 * there is some system out there that won't like it.
603 * Maybe the easiest thing to do would be to bracket this
604 * with an #ifdef that can turn it off. The %z feature
605 * would be an admittedly obscure one that most folks can
606 * live without, but it would be a great help to those of
607 * us that muck around with various message processors.
609 case 'z': /* time zone offset east of GMT e.g. -0600 */
610 if (timeptr
->tm_isdst
< 0)
614 * Systems with tm_name probably have tm_tzadj as
615 * secs west of GMT. Convert to mins east of GMT.
617 off
= -timeptr
->tm_tzadj
/ 60;
618 #else /* !HAVE_TM_NAME */
621 * Systems with tm_zone probably have tm_gmtoff as
622 * secs east of GMT. Convert to mins east of GMT.
624 off
= timeptr
->tm_gmtoff
/ 60;
625 #else /* !HAVE_TM_ZONE */
628 * Systems with tzname[] probably have timezone as
629 * secs west of GMT. Convert to mins east of GMT.
631 # if defined(__hpux) || defined (HPUX) || defined(__CYGWIN__)
632 off
= -timezone
/ 60;
634 /* ADR: 4 August 2001, fixed this per gazelle@interaccess.com */
635 off
= -(daylight
? altzone
: timezone
) / 60;
637 #else /* !HAVE_TZNAME */
638 gettimeofday(& tv
, & zone
);
639 off
= -zone
.tz_minuteswest
;
640 #endif /* !HAVE_TZNAME */
641 #endif /* !HAVE_TM_ZONE */
642 #endif /* !HAVE_TM_NAME */
649 sprintf(tbuf
+1, "%02ld%02ld", off
/60, off
%60);
652 case 'Z': /* time zone name or abbreviation */
654 i
= (daylight
&& timeptr
->tm_isdst
> 0); /* 0 or 1 */
655 strcpy(tbuf
, tzname
[i
]);
658 strcpy(tbuf
, timeptr
->tm_zone
);
661 strcpy(tbuf
, timeptr
->tm_name
);
663 gettimeofday(& tv
, & zone
);
664 strcpy(tbuf
, timezone(zone
.tz_minuteswest
,
665 timeptr
->tm_isdst
> 0));
666 #endif /* HAVE_TM_NAME */
667 #endif /* HAVE_TM_ZONE */
668 #endif /* HAVE_TZNAME */
672 case 'k': /* hour, 24-hour clock, blank pad */
673 sprintf(tbuf
, "%2d", range(0, timeptr
->tm_hour
, 23));
676 case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
677 i
= range(0, timeptr
->tm_hour
, 23);
682 sprintf(tbuf
, "%2d", i
);
687 case 'N': /* Emperor/Era name */
688 /* this is essentially the same as the century */
689 goto century
; /* %C */
691 case 'o': /* Emperor/Era year */
693 #endif /* HPUX_EXT */
697 case 'v': /* date as dd-bbb-YYYY */
698 sprintf(tbuf
, "%2d-%3.3s-%4ld",
699 range(1, timeptr
->tm_mday
, 31),
700 months_a
[range(0, timeptr
->tm_mon
, 11)],
701 timeptr
->tm_year
+ 1900L);
702 for (i
= 3; i
< 6; i
++)
703 if (islower(tbuf
[i
]))
704 tbuf
[i
] = toupper(tbuf
[i
]);
716 if (s
+ i
< endp
- 1) {
724 if (s
< endp
&& *format
== '\0') {
733 /* isleap --- is a year a leap year? */
738 return ((year
% 4 == 0 && year
% 100 != 0) || year
% 400 == 0);
742 /* iso8601wknum --- compute week number according to ISO 8601 */
745 iso8601wknum(const struct tm
*timeptr
)
749 * If the week (Monday to Sunday) containing January 1
750 * has four or more days in the new year, then it is week 1;
751 * otherwise it is the highest numbered week of the previous
752 * year (52 or 53), and the next week is week 1.
754 * ADR: This means if Jan 1 was Monday through Thursday,
755 * it was week 1, otherwise week 52 or 53.
757 * XPG4 erroneously included POSIX.2 rationale text in the
758 * main body of the standard. Thus it requires week 53.
761 int weeknum
, jan1day
, diff
;
763 /* get week number, Monday as first day of the week */
764 weeknum
= weeknumber(timeptr
, 1);
767 * With thanks and tip of the hatlo to tml@tik.vtt.fi
769 * What day of the week does January 1 fall on?
771 * (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
772 * (timeptr->tm_wday - jan1.tm_wday) MOD 7
776 * timeptr->tm_wday MOD 7 == timeptr->tm_wday
777 * from which it follows that. . .
779 jan1day
= timeptr
->tm_wday
- (timeptr
->tm_yday
% 7);
784 * If Jan 1 was a Monday through Thursday, it was in
785 * week 1. Otherwise it was last year's highest week, which is
786 * this year's week 0.
788 * What does that mean?
789 * If Jan 1 was Monday, the week number is exactly right, it can
791 * If it was Tuesday through Thursday, the weeknumber is one
792 * less than it should be, so we add one.
793 * Otherwise, Friday, Saturday or Sunday, the week number is
794 * OK, but if it is 0, it needs to be 52 or 53.
799 case 2: /* Tuesday */
800 case 3: /* Wednesday */
801 case 4: /* Thursday */
805 case 6: /* Saturday */
808 #ifdef USE_BROKEN_XPG4
809 /* XPG4 (as of March 1994) says 53 unconditionally */
812 /* get week number of last week of last year */
813 struct tm dec31ly
; /* 12/31 last year */
817 dec31ly
.tm_mday
= 31;
818 dec31ly
.tm_wday
= (jan1day
== 0) ? 6 : jan1day
- 1;
819 dec31ly
.tm_yday
= 364 + isleap(dec31ly
.tm_year
+ 1900L);
820 weeknum
= iso8601wknum(& dec31ly
);
826 if (timeptr
->tm_mon
== 11) {
828 * The last week of the year
829 * can be in week 1 of next year.
832 * This can only happen if
840 wday
= timeptr
->tm_wday
;
841 mday
= timeptr
->tm_mday
;
842 if ( (wday
== 1 && (mday
>= 29 && mday
<= 31))
843 || (wday
== 2 && (mday
== 30 || mday
== 31))
844 || (wday
== 3 && mday
== 31))
851 /* weeknumber --- figure how many weeks into the year */
853 /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
856 weeknumber(const struct tm
*timeptr
, int firstweekday
)
858 int wday
= timeptr
->tm_wday
;
861 if (firstweekday
== 1) {
862 if (wday
== 0) /* sunday */
867 ret
= ((timeptr
->tm_yday
+ 7 - wday
) / 7);
874 /* ADR --- I'm loathe to mess with ado's code ... */
876 Date
: Wed
, 24 Apr
91 20:54:08 MDT
877 From
: Michal Jaegermann
<audfax
!emory
!vm
.ucs
.UAlberta
.CA
!NTOMCZAK
>
878 To
: arnold@audiofax
.com
881 in a process of fixing of
strftime() in libraries on Atari ST I grabbed
882 some pieces of code from your own strftime
. When doing that it came
883 to mind that your
weeknumber() function compiles a little bit nicer
884 in the following form
:
886 * firstweekday is 0 if starting in Sunday, non-zero if in Monday
889 return (timeptr
->tm_yday
- timeptr
->tm_wday
+
890 (firstweekday
? (timeptr
->tm_wday
? 8 : 1) : 7)) / 7;
892 How nicer it depends on a compiler
, of course
, but always a tiny bit
.
896 ntomczak@vm
.ucs
.ualberta
.ca
909 * "tst" is a test driver for the function "strftime".
916 * Control Data Systems, Inc.
917 * vogelke@c-17igp.wpafb.af.mil
923 * cc -o tst -DTEST_STRFTIME strftime.c
926 /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
931 #include <sys/time.h>
937 * Array of time formats.
940 static char *array
[] =
942 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
943 "(%%B) full month name, var length (January..December) %B",
945 "(%%D) date (%%m/%%d/%%y) %D",
946 "(%%E) Locale extensions (ignored) %E",
947 "(%%F) full month name, var length (January..December) %F",
948 "(%%H) hour (24-hour clock, 00..23) %H",
949 "(%%I) hour (12-hour clock, 01..12) %I",
950 "(%%M) minute (00..59) %M",
951 "(%%N) Emperor/Era Name %N",
952 "(%%O) Locale extensions (ignored) %O",
953 "(%%R) time, 24-hour (%%H:%%M) %R",
954 "(%%S) second (00..60) %S",
955 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
956 "(%%U) week of year, Sunday as first day of week (00..53) %U",
957 "(%%V) week of year according to ISO 8601 %V",
958 "(%%W) week of year, Monday as first day of week (00..53) %W",
959 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
960 "(%%Y) year with century (1970...) %Y",
961 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
962 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
963 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
964 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
965 "(%%d) day of the month (01..31) %d",
966 "(%%e) day of the month, blank-padded ( 1..31) %e",
967 "(%%h) should be same as (%%b) %h",
968 "(%%j) day of the year (001..366) %j",
969 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
970 "(%%l) hour, 12-hour clock, blank pad ( 0..12) %l",
971 "(%%m) month (01..12) %m",
972 "(%%o) Emperor/Era Year %o",
973 "(%%p) locale's AM or PM based on 12-hour clock %p",
974 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
975 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
976 "(%%v) VMS date (dd-bbb-YYYY) %v",
977 "(%%w) day of week (0..6, Sunday == 0) %w",
978 "(%%x) appropriate locale date representation %x",
979 "(%%y) last two digits of year (00..99) %y",
980 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
994 char string
[MAXTIME
];
1003 /* Call the function. */
1005 clock
= time((long *) 0);
1006 tm
= localtime(&clock
);
1008 for (k
= 0; next
= array
[k
]; k
++) {
1009 length
= strftime(string
, MAXTIME
, next
, tm
);
1010 printf("%s\n", string
);
1015 #endif /* TEST_STRFTIME */