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.
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 */
84 extern char *get_string_value (const char *);
87 extern void tzset(void);
88 static int weeknumber(const struct tm
*timeptr
, int firstweekday
);
89 static int iso8601wknum(const struct tm
*timeptr
);
93 #define inline __inline__
99 #define range(low, item, hi) max(low, min(item, hi))
101 #if !defined(OS2) && !defined(MSDOS) && defined(HAVE_TZNAME)
102 extern char *tzname
[2];
104 #if defined(SOLARIS) || defined(mips) || defined (M_UNIX)
105 extern long int timezone
, altzone
;
108 extern long int timezone
;
110 extern int timezone
, altzone
;
112 #endif /* !SOLARIS && !mips && !M_UNIX */
115 #undef min /* just in case */
117 /* min --- return minimum of two numbers */
122 return (a
< b
? a
: b
);
125 #undef max /* also, just in case */
127 /* max --- return maximum of two numbers */
132 return (a
> b
? a
: b
);
135 /* strftime --- produce formatted time */
138 strftime(char *s
, size_t maxsize
, const char *format
, const struct tm
*timeptr
)
140 char *endp
= s
+ maxsize
;
145 static short first
= 1;
146 #ifdef POSIX_SEMANTICS
147 static char *savetz
= NULL
;
148 static int savetzlen
= 0;
150 #endif /* POSIX_SEMANTICS */
154 extern char *timezone();
156 struct timezone zone
;
157 #endif /* HAVE_TZNAME */
158 #endif /* HAVE_TM_NAME */
159 #endif /* HAVE_TM_ZONE */
161 /* various tables, useful in North America */
162 static const char *days_a
[] = {
163 "Sun", "Mon", "Tue", "Wed",
166 static const char *days_l
[] = {
167 "Sunday", "Monday", "Tuesday", "Wednesday",
168 "Thursday", "Friday", "Saturday",
170 static const char *months_a
[] = {
171 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
172 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
174 static const char *months_l
[] = {
175 "January", "February", "March", "April",
176 "May", "June", "July", "August", "September",
177 "October", "November", "December",
179 static const char *ampm
[] = { "AM", "PM", };
181 if (s
== NULL
|| format
== NULL
|| timeptr
== NULL
|| maxsize
== 0)
184 /* quick check if we even need to bother */
185 if (strchr(format
, '%') == NULL
&& strlen(format
) + 1 >= maxsize
)
188 #ifndef POSIX_SEMANTICS
193 #else /* POSIX_SEMANTICS */
195 tz
= get_string_value ("TZ");
201 int tzlen
= strlen(tz
);
203 savetz
= (char *) malloc(tzlen
+ 1);
204 if (savetz
!= NULL
) {
205 savetzlen
= tzlen
+ 1;
212 /* if we have a saved TZ, and it is different, recapture and reset */
213 if (tz
&& savetz
&& (tz
[0] != savetz
[0] || strcmp(tz
, savetz
) != 0)) {
216 savetz
= (char *) realloc(savetz
, i
);
225 #endif /* POSIX_SEMANTICS */
227 for (; *format
&& s
< endp
- 1; format
++) {
229 if (*format
!= '%') {
243 case 'a': /* abbreviated weekday name */
244 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
> 6)
247 strcpy(tbuf
, days_a
[timeptr
->tm_wday
]);
250 case 'A': /* full weekday name */
251 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
> 6)
254 strcpy(tbuf
, days_l
[timeptr
->tm_wday
]);
257 case 'b': /* abbreviated month name */
259 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
> 11)
262 strcpy(tbuf
, months_a
[timeptr
->tm_mon
]);
265 case 'B': /* full month name */
266 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
> 11)
269 strcpy(tbuf
, months_l
[timeptr
->tm_mon
]);
272 case 'c': /* appropriate date and time representation */
276 * strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
278 * Now, per the ISO 1999 C standard, it this:
280 strftime(tbuf
, sizeof tbuf
, "%A %B %d %T %Y", timeptr
);
285 sprintf(tbuf
, "%02d", (timeptr
->tm_year
+ 1900) / 100);
288 case 'd': /* day of the month, 01 - 31 */
289 i
= range(1, timeptr
->tm_mday
, 31);
290 sprintf(tbuf
, "%02d", i
);
293 case 'D': /* date as %m/%d/%y */
294 strftime(tbuf
, sizeof tbuf
, "%m/%d/%y", timeptr
);
297 case 'e': /* day of month, blank padded */
298 sprintf(tbuf
, "%2d", range(1, timeptr
->tm_mday
, 31));
302 /* POSIX (now C99) locale extensions, ignored for now */
305 case 'F': /* ISO 8601 date representation */
306 strftime(tbuf
, sizeof tbuf
, "%Y-%m-%d", timeptr
);
314 * If it's December but the ISO week number is one,
315 * that week is in next year.
316 * If it's January but the ISO week number is 52 or
317 * 53, that week is in last year.
318 * Otherwise, it's this year.
320 w
= iso8601wknum(timeptr
);
321 if (timeptr
->tm_mon
== 11 && w
== 1)
322 y
= 1900 + timeptr
->tm_year
+ 1;
323 else if (timeptr
->tm_mon
== 0 && w
>= 52)
324 y
= 1900 + timeptr
->tm_year
- 1;
326 y
= 1900 + timeptr
->tm_year
;
329 sprintf(tbuf
, "%d", y
);
331 sprintf(tbuf
, "%02d", y
% 100);
334 case 'h': /* abbreviated month name */
337 case 'H': /* hour, 24-hour clock, 00 - 23 */
338 i
= range(0, timeptr
->tm_hour
, 23);
339 sprintf(tbuf
, "%02d", i
);
342 case 'I': /* hour, 12-hour clock, 01 - 12 */
343 i
= range(0, timeptr
->tm_hour
, 23);
348 sprintf(tbuf
, "%02d", i
);
351 case 'j': /* day of the year, 001 - 366 */
352 sprintf(tbuf
, "%03d", timeptr
->tm_yday
+ 1);
355 case 'm': /* month, 01 - 12 */
356 i
= range(0, timeptr
->tm_mon
, 11);
357 sprintf(tbuf
, "%02d", i
+ 1);
360 case 'M': /* minute, 00 - 59 */
361 i
= range(0, timeptr
->tm_min
, 59);
362 sprintf(tbuf
, "%02d", i
);
365 case 'n': /* same as \n */
371 /* POSIX (now C99) locale extensions, ignored for now */
374 case 'p': /* am or pm based on 12-hour clock */
375 i
= range(0, timeptr
->tm_hour
, 23);
377 strcpy(tbuf
, ampm
[0]);
379 strcpy(tbuf
, ampm
[1]);
382 case 'r': /* time as %I:%M:%S %p */
383 strftime(tbuf
, sizeof tbuf
, "%I:%M:%S %p", timeptr
);
386 case 'R': /* time as %H:%M */
387 strftime(tbuf
, sizeof tbuf
, "%H:%M", timeptr
);
390 #if defined(HAVE_MKTIME) || defined(GAWK)
391 case 's': /* time as seconds since the Epoch */
393 struct tm non_const_timeptr
;
395 non_const_timeptr
= *timeptr
;
396 sprintf(tbuf
, "%ld", mktime(& non_const_timeptr
));
399 #endif /* defined(HAVE_MKTIME) || defined(GAWK) */
401 case 'S': /* second, 00 - 60 */
402 i
= range(0, timeptr
->tm_sec
, 60);
403 sprintf(tbuf
, "%02d", i
);
406 case 't': /* same as \t */
411 case 'T': /* time as %H:%M:%S */
413 strftime(tbuf
, sizeof tbuf
, "%H:%M:%S", timeptr
);
417 /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
418 sprintf(tbuf
, "%d", timeptr
->tm_wday
== 0 ? 7 :
422 case 'U': /* week of year, Sunday is first day of week */
423 sprintf(tbuf
, "%02d", weeknumber(timeptr
, 0));
426 case 'V': /* week of year according ISO 8601 */
427 sprintf(tbuf
, "%02d", iso8601wknum(timeptr
));
430 case 'w': /* weekday, Sunday == 0, 0 - 6 */
431 i
= range(0, timeptr
->tm_wday
, 6);
432 sprintf(tbuf
, "%d", i
);
435 case 'W': /* week of year, Monday is first day of week */
436 sprintf(tbuf
, "%02d", weeknumber(timeptr
, 1));
439 case 'x': /* appropriate date representation */
440 strftime(tbuf
, sizeof tbuf
, "%A %B %d %Y", timeptr
);
443 case 'X': /* appropriate time representation */
447 case 'y': /* year without a century, 00 - 99 */
449 i
= timeptr
->tm_year
% 100;
450 sprintf(tbuf
, "%02d", i
);
453 case 'Y': /* year with century */
455 sprintf(tbuf
, "%d", 1900 + timeptr
->tm_year
);
459 * From: Chip Rosenthal <chip@chinacat.unicom.com>
460 * Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST)
462 * Warning: the %z [code] is implemented by inspecting the
463 * timezone name conditional compile settings, and
464 * inferring a method to get timezone offsets. I've tried
465 * this code on a couple of machines, but I don't doubt
466 * there is some system out there that won't like it.
467 * Maybe the easiest thing to do would be to bracket this
468 * with an #ifdef that can turn it off. The %z feature
469 * would be an admittedly obscure one that most folks can
470 * live without, but it would be a great help to those of
471 * us that muck around with various message processors.
473 case 'z': /* time zone offset east of GMT e.g. -0600 */
474 if (timeptr
->tm_isdst
< 0)
478 * Systems with tm_name probably have tm_tzadj as
479 * secs west of GMT. Convert to mins east of GMT.
481 off
= -timeptr
->tm_tzadj
/ 60;
482 #else /* !HAVE_TM_NAME */
485 * Systems with tm_zone probably have tm_gmtoff as
486 * secs east of GMT. Convert to mins east of GMT.
488 off
= timeptr
->tm_gmtoff
/ 60;
489 #else /* !HAVE_TM_ZONE */
492 * Systems with tzname[] probably have timezone as
493 * secs west of GMT. Convert to mins east of GMT.
496 off
= -timezone
/ 60;
498 off
= -(daylight
? altzone
: timezone
) / 60;
500 #else /* !HAVE_TZNAME */
501 gettimeofday(& tv
, & zone
);
502 off
= -zone
.tz_minuteswest
;
503 #endif /* !HAVE_TZNAME */
504 #endif /* !HAVE_TM_ZONE */
505 #endif /* !HAVE_TM_NAME */
512 sprintf(tbuf
+1, "%02d%02d", off
/60, off
%60);
515 case 'Z': /* time zone name or abbrevation */
517 i
= (daylight
&& timeptr
->tm_isdst
> 0); /* 0 or 1 */
518 strcpy(tbuf
, tzname
[i
]);
521 strcpy(tbuf
, timeptr
->tm_zone
);
524 strcpy(tbuf
, timeptr
->tm_name
);
526 gettimeofday(& tv
, & zone
);
527 strcpy(tbuf
, timezone(zone
.tz_minuteswest
,
528 timeptr
->tm_isdst
> 0));
529 #endif /* HAVE_TM_NAME */
530 #endif /* HAVE_TM_ZONE */
531 #endif /* HAVE_TZNAME */
535 case 'k': /* hour, 24-hour clock, blank pad */
536 sprintf(tbuf
, "%2d", range(0, timeptr
->tm_hour
, 23));
539 case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
540 i
= range(0, timeptr
->tm_hour
, 23);
545 sprintf(tbuf
, "%2d", i
);
550 case 'N': /* Emperor/Era name */
551 /* this is essentially the same as the century */
552 goto century
; /* %C */
554 case 'o': /* Emperor/Era year */
556 #endif /* HPUX_EXT */
560 case 'v': /* date as dd-bbb-YYYY */
561 sprintf(tbuf
, "%2d-%3.3s-%4d",
562 range(1, timeptr
->tm_mday
, 31),
563 months_a
[range(0, timeptr
->tm_mon
, 11)],
564 timeptr
->tm_year
+ 1900);
565 for (i
= 3; i
< 6; i
++)
566 if (islower(tbuf
[i
]))
567 tbuf
[i
] = toupper(tbuf
[i
]);
579 if (s
+ i
< endp
- 1) {
587 if (s
< endp
&& *format
== '\0') {
594 /* isleap --- is a year a leap year? */
599 return ((year
% 4 == 0 && year
% 100 != 0) || year
% 400 == 0);
603 /* iso8601wknum --- compute week number according to ISO 8601 */
606 iso8601wknum(const struct tm
*timeptr
)
610 * If the week (Monday to Sunday) containing January 1
611 * has four or more days in the new year, then it is week 1;
612 * otherwise it is the highest numbered week of the previous
613 * year (52 or 53), and the next week is week 1.
615 * ADR: This means if Jan 1 was Monday through Thursday,
616 * it was week 1, otherwise week 52 or 53.
618 * XPG4 erroneously included POSIX.2 rationale text in the
619 * main body of the standard. Thus it requires week 53.
622 int weeknum
, jan1day
, diff
;
624 /* get week number, Monday as first day of the week */
625 weeknum
= weeknumber(timeptr
, 1);
628 * With thanks and tip of the hatlo to tml@tik.vtt.fi
630 * What day of the week does January 1 fall on?
632 * (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
633 * (timeptr->tm_wday - jan1.tm_wday) MOD 7
637 * timeptr->tm_wday MOD 7 == timeptr->tm_wday
638 * from which it follows that. . .
640 jan1day
= timeptr
->tm_wday
- (timeptr
->tm_yday
% 7);
645 * If Jan 1 was a Monday through Thursday, it was in
646 * week 1. Otherwise it was last year's highest week, which is
647 * this year's week 0.
649 * What does that mean?
650 * If Jan 1 was Monday, the week number is exactly right, it can
652 * If it was Tuesday through Thursday, the weeknumber is one
653 * less than it should be, so we add one.
654 * Otherwise, Friday, Saturday or Sunday, the week number is
655 * OK, but if it is 0, it needs to be 52 or 53.
660 case 2: /* Tuesday */
661 case 3: /* Wednesday */
662 case 4: /* Thursday */
666 case 6: /* Saturday */
669 #ifdef USE_BROKEN_XPG4
670 /* XPG4 (as of March 1994) says 53 unconditionally */
673 /* get week number of last week of last year */
674 struct tm dec31ly
; /* 12/31 last year */
678 dec31ly
.tm_mday
= 31;
679 dec31ly
.tm_wday
= (jan1day
== 0) ? 6 : jan1day
- 1;
680 dec31ly
.tm_yday
= 364 + isleap(dec31ly
.tm_year
+ 1900);
681 weeknum
= iso8601wknum(& dec31ly
);
687 if (timeptr
->tm_mon
== 11) {
689 * The last week of the year
690 * can be in week 1 of next year.
693 * This can only happen if
701 wday
= timeptr
->tm_wday
;
702 mday
= timeptr
->tm_mday
;
703 if ( (wday
== 1 && (mday
>= 29 && mday
<= 31))
704 || (wday
== 2 && (mday
== 30 || mday
== 31))
705 || (wday
== 3 && mday
== 31))
712 /* weeknumber --- figure how many weeks into the year */
714 /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
717 weeknumber(const struct tm
*timeptr
, int firstweekday
)
719 int wday
= timeptr
->tm_wday
;
722 if (firstweekday
== 1) {
723 if (wday
== 0) /* sunday */
728 ret
= ((timeptr
->tm_yday
+ 7 - wday
) / 7);
735 /* ADR --- I'm loathe to mess with ado's code ... */
737 Date
: Wed
, 24 Apr
91 20:54:08 MDT
738 From
: Michal Jaegermann
<audfax
!emory
!vm
.ucs
.UAlberta
.CA
!NTOMCZAK
>
739 To
: arnold@audiofax
.com
742 in a process of fixing of
strftime() in libraries on Atari ST I grabbed
743 some pieces of code from your own strftime
. When doing that it came
744 to mind that your
weeknumber() function compiles a little bit nicer
745 in the following form
:
747 * firstweekday is 0 if starting in Sunday, non-zero if in Monday
750 return (timeptr
->tm_yday
- timeptr
->tm_wday
+
751 (firstweekday
? (timeptr
->tm_wday
? 8 : 1) : 7)) / 7;
753 How nicer it depends on a compiler
, of course
, but always a tiny bit
.
757 ntomczak@vm
.ucs
.ualberta
.ca
770 * "tst" is a test driver for the function "strftime".
777 * Control Data Systems, Inc.
778 * vogelke@c-17igp.wpafb.af.mil
784 * cc -o tst -DTEST_STRFTIME strftime.c
787 /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
792 #include <sys/time.h>
798 * Array of time formats.
801 static char *array
[] =
803 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
804 "(%%B) full month name, var length (January..December) %B",
806 "(%%D) date (%%m/%%d/%%y) %D",
807 "(%%E) Locale extensions (ignored) %E",
808 "(%%F) full month name, var length (January..December) %F",
809 "(%%H) hour (24-hour clock, 00..23) %H",
810 "(%%I) hour (12-hour clock, 01..12) %I",
811 "(%%M) minute (00..59) %M",
812 "(%%N) Emporer/Era Name %N",
813 "(%%O) Locale extensions (ignored) %O",
814 "(%%R) time, 24-hour (%%H:%%M) %R",
815 "(%%S) second (00..60) %S",
816 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
817 "(%%U) week of year, Sunday as first day of week (00..53) %U",
818 "(%%V) week of year according to ISO 8601 %V",
819 "(%%W) week of year, Monday as first day of week (00..53) %W",
820 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
821 "(%%Y) year with century (1970...) %Y",
822 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
823 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
824 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
825 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
826 "(%%d) day of the month (01..31) %d",
827 "(%%e) day of the month, blank-padded ( 1..31) %e",
828 "(%%h) should be same as (%%b) %h",
829 "(%%j) day of the year (001..366) %j",
830 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
831 "(%%l) hour, 12-hour clock, blank pad ( 0..12) %l",
832 "(%%m) month (01..12) %m",
833 "(%%o) Emporer/Era Year %o",
834 "(%%p) locale's AM or PM based on 12-hour clock %p",
835 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
836 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
837 "(%%v) VMS date (dd-bbb-YYYY) %v",
838 "(%%w) day of week (0..6, Sunday == 0) %w",
839 "(%%x) appropriate locale date representation %x",
840 "(%%y) last two digits of year (00..99) %y",
841 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
855 char string
[MAXTIME
];
864 /* Call the function. */
866 clock
= time((long *) 0);
867 tm
= localtime(&clock
);
869 for (k
= 0; next
= array
[k
]; k
++) {
870 length
= strftime(string
, MAXTIME
, next
, tm
);
871 printf("%s\n", string
);
876 #endif /* TEST_STRFTIME */