4 * Public-domain implementation of ANSI C library routine.
6 * It's written in old-style C for maximal portability.
7 * However, since I'm used to prototypes, I've included them too.
9 * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
10 * For extensions from SunOS, add SUNOS_EXT.
11 * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
12 * For VMS dates, add VMS_EXT.
13 * For a an RFC822 time format, add MAILHEADER_EXT.
14 * For ISO week years, add ISO_DATE_EXT.
15 * For complete POSIX semantics, add POSIX_SEMANTICS.
17 * The code for %c, %x, and %X now follows the 1003.2 specification for
19 * This version ignores LOCALE information.
20 * It also doesn't worry about multi-byte characters.
23 * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
24 * code are included if GAWK is defined.
27 * January, February, March, 1991
28 * Updated March, April 1992
30 * Updated February, 1994
32 * Updated January, 1995
33 * Updated September, 1995
34 * Updated January, 1996
36 * Fixes from ado@elsie.nci.nih.gov
37 * February 1991, May 1992
38 * Fixes from Tor Lillqvist tml@tik.vtt.fi
40 * Further fixes from ado@elsie.nci.nih.gov
42 * %z code from chip@chinacat.unicom.com
43 * Applied September 1995
44 * %V code fixed (again) and %G, %g added,
48 #include "ruby/config.h"
56 #if defined(TM_IN_SYS_TIME) || !defined(GAWK) && !defined(_WIN32_WCE)
57 #include <sys/types.h>
61 /* defaults: season to taste */
62 #define SYSV_EXT 1 /* stuff in System V ascftime routine */
63 #define SUNOS_EXT 1 /* stuff in SunOS strftime routine */
64 #define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */
65 #define VMS_EXT 1 /* include %v for VMS date format */
66 #define MAILHEADER_EXT 1 /* add %z for HHMM format */
67 #define ISO_DATE_EXT 1 /* %G and %g for year of ISO week */
69 #define POSIX_SEMANTICS 1 /* call tzset() if TZ changes */
72 #if defined(ISO_DATE_EXT)
73 #if ! defined(POSIX2_DATE)
78 #if defined(POSIX2_DATE)
79 #if ! defined(SYSV_EXT)
82 #if ! defined(SUNOS_EXT)
87 #if defined(POSIX2_DATE)
88 #define adddecl(stuff) stuff
90 #define adddecl(stuff)
93 #undef strchr /* avoid AIX weirdness */
98 static int weeknumber();
99 adddecl(static int iso8601wknum();)
101 extern void tzset(void);
102 static int weeknumber(const struct tm
*timeptr
, int firstweekday
);
103 adddecl(static int iso8601wknum(const struct tm
*timeptr
);)
110 extern void *malloc();
111 extern void *realloc();
112 extern char *getenv();
113 extern char *strchr();
116 #define range(low, item, hi) max(low, min(item, hi))
119 #define DLL_IMPORT __declspec(dllimport)
122 #define DLL_IMPORT __declspec(dllimport)
124 #if !defined(OS2) && !defined(MSDOS) && defined(HAVE_TZNAME)
125 extern DLL_IMPORT
char *tzname
[2];
127 extern DLL_IMPORT
int daylight
;
129 #ifdef HAVE_VAR_TIMEZONE
130 extern DLL_IMPORT TYPEOF_VAR_TIMEZONE timezone
;
132 #ifdef HAVE_VAR_ALTZONE
133 extern DLL_IMPORT TYPEOF_VAR_ALTZONE altzone
;
137 #undef min /* just in case */
139 /* min --- return minimum of two numbers */
150 return (a
< b
? a
: b
);
153 #undef max /* also, just in case */
155 /* max --- return maximum of two numbers */
166 return (a
> b
? a
: b
);
169 /* strftime --- produce formatted time */
173 strftime(s
, maxsize
, format
, timeptr
)
177 const struct tm
*timeptr
;
180 strftime(char *s
, size_t maxsize
, const char *format
, const struct tm
*timeptr
)
183 char *endp
= s
+ maxsize
;
189 static short first
= 1;
190 #ifdef POSIX_SEMANTICS
191 static char *savetz
= NULL
;
192 static int savetzlen
= 0;
194 #endif /* POSIX_SEMANTICS */
198 struct timezone zone
;
199 #endif /* HAVE_TM_NAME */
200 #endif /* HAVE_TM_ZONE */
202 /* various tables, useful in North America */
203 static const char *days_a
[] = {
204 "Sun", "Mon", "Tue", "Wed",
207 static const char *days_l
[] = {
208 "Sunday", "Monday", "Tuesday", "Wednesday",
209 "Thursday", "Friday", "Saturday",
211 static const char *months_a
[] = {
212 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
213 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
215 static const char *months_l
[] = {
216 "January", "February", "March", "April",
217 "May", "June", "July", "August", "September",
218 "October", "November", "December",
220 static const char *ampm
[] = { "AM", "PM", };
222 if (s
== NULL
|| format
== NULL
|| timeptr
== NULL
|| maxsize
== 0)
225 /* quick check if we even need to bother */
226 if (strchr(format
, '%') == NULL
&& strlen(format
) + 1 >= maxsize
)
229 #ifndef POSIX_SEMANTICS
234 #else /* POSIX_SEMANTICS */
238 int tzlen
= strlen(tz
);
240 savetz
= (char *) malloc(tzlen
+ 1);
241 if (savetz
!= NULL
) {
242 savetzlen
= tzlen
+ 1;
249 /* if we have a saved TZ, and it is different, recapture and reset */
250 if (tz
&& savetz
&& (tz
[0] != savetz
[0] || strcmp(tz
, savetz
) != 0)) {
253 savetz
= (char *) realloc(savetz
, i
);
262 #endif /* POSIX_SEMANTICS */
264 for (; *format
&& s
< endp
- 1; format
++) {
266 if (*format
!= '%') {
280 case 'a': /* abbreviated weekday name */
281 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
> 6)
284 strcpy(tbuf
, days_a
[timeptr
->tm_wday
]);
287 case 'A': /* full weekday name */
288 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
> 6)
291 strcpy(tbuf
, days_l
[timeptr
->tm_wday
]);
295 case 'h': /* abbreviated month name */
297 case 'b': /* abbreviated month name */
298 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
> 11)
301 strcpy(tbuf
, months_a
[timeptr
->tm_mon
]);
304 case 'B': /* full month name */
305 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
> 11)
308 strcpy(tbuf
, months_l
[timeptr
->tm_mon
]);
311 case 'c': /* appropriate date and time representation */
312 strftime(tbuf
, sizeof tbuf
, "%a %b %e %H:%M:%S %Y", timeptr
);
315 case 'd': /* day of the month, 01 - 31 */
316 i
= range(1, timeptr
->tm_mday
, 31);
317 sprintf(tbuf
, "%02d", i
);
320 case 'H': /* hour, 24-hour clock, 00 - 23 */
321 i
= range(0, timeptr
->tm_hour
, 23);
322 sprintf(tbuf
, "%02d", i
);
325 case 'I': /* hour, 12-hour clock, 01 - 12 */
326 i
= range(0, timeptr
->tm_hour
, 23);
331 sprintf(tbuf
, "%02d", i
);
334 case 'j': /* day of the year, 001 - 366 */
335 sprintf(tbuf
, "%03d", timeptr
->tm_yday
+ 1);
338 case 'm': /* month, 01 - 12 */
339 i
= range(0, timeptr
->tm_mon
, 11);
340 sprintf(tbuf
, "%02d", i
+ 1);
343 case 'M': /* minute, 00 - 59 */
344 i
= range(0, timeptr
->tm_min
, 59);
345 sprintf(tbuf
, "%02d", i
);
348 case 'p': /* am or pm based on 12-hour clock */
349 i
= range(0, timeptr
->tm_hour
, 23);
351 strcpy(tbuf
, ampm
[0]);
353 strcpy(tbuf
, ampm
[1]);
356 case 'S': /* second, 00 - 60 */
357 i
= range(0, timeptr
->tm_sec
, 60);
358 sprintf(tbuf
, "%02d", i
);
361 case 'U': /* week of year, Sunday is first day of week */
362 sprintf(tbuf
, "%02d", weeknumber(timeptr
, 0));
365 case 'w': /* weekday, Sunday == 0, 0 - 6 */
366 i
= range(0, timeptr
->tm_wday
, 6);
367 sprintf(tbuf
, "%d", i
);
370 case 'W': /* week of year, Monday is first day of week */
371 sprintf(tbuf
, "%02d", weeknumber(timeptr
, 1));
374 case 'x': /* appropriate date representation */
375 strftime(tbuf
, sizeof tbuf
, "%m/%d/%y", timeptr
);
378 case 'X': /* appropriate time representation */
379 strftime(tbuf
, sizeof tbuf
, "%H:%M:%S", timeptr
);
382 case 'y': /* year without a century, 00 - 99 */
383 i
= timeptr
->tm_year
% 100;
384 sprintf(tbuf
, "%02d", i
);
387 case 'Y': /* year with century */
388 sprintf(tbuf
, "%ld", 1900L + timeptr
->tm_year
);
391 #ifdef MAILHEADER_EXT
393 * From: Chip Rosenthal <chip@chinacat.unicom.com>
394 * Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST)
396 * Warning: the %z [code] is implemented by inspecting the
397 * timezone name conditional compile settings, and
398 * inferring a method to get timezone offsets. I've tried
399 * this code on a couple of machines, but I don't doubt
400 * there is some system out there that won't like it.
401 * Maybe the easiest thing to do would be to bracket this
402 * with an #ifdef that can turn it off. The %z feature
403 * would be an admittedly obscure one that most folks can
404 * live without, but it would be a great help to those of
405 * us that muck around with various message processors.
407 case 'z': /* time zone offset east of GMT e.g. -0600 */
410 * Systems with tm_name probably have tm_tzadj as
411 * secs west of GMT. Convert to mins east of GMT.
413 off
= -timeptr
->tm_tzadj
/ 60;
414 #else /* !HAVE_TM_NAME */
417 * Systems with tm_zone probably have tm_gmtoff as
418 * secs east of GMT. Convert to mins east of GMT.
420 off
= timeptr
->tm_gmtoff
/ 60;
421 #else /* !HAVE_TM_ZONE */
422 #ifdef HAVE_GETTIMEOFDAY
423 gettimeofday(&tv
, &zone
);
424 off
= -zone
.tz_minuteswest
;
426 #if HAVE_VAR_TIMEZONE
428 off
= -(daylight
? timezone
: altzone
) / 60;
430 off
= -timezone
/ 60;
434 #endif /* !HAVE_TM_ZONE */
435 #endif /* !HAVE_TM_NAME */
442 sprintf(tbuf
+1, "%02u%02u", (unsigned)off
/60, (unsigned)off
%60);
444 #endif /* MAILHEADER_EXT */
446 case 'Z': /* time zone name or abbrevation */
448 i
= (daylight
&& timeptr
->tm_isdst
> 0); /* 0 or 1 */
449 strcpy(tbuf
, tzname
[i
]);
452 strcpy(tbuf
, timeptr
->tm_zone
);
455 strcpy(tbuf
, timeptr
->tm_name
);
458 gettimeofday(& tv
, & zone
);
460 strcpy(tbuf
, timezone());
462 strcpy(tbuf
, timezone(zone
.tz_minuteswest
,
463 timeptr
->tm_isdst
> 0));
464 #endif /* TIMEZONE_VOID */
465 #endif /* HAVE_TIMEZONE */
466 #endif /* HAVE_TM_NAME */
467 #endif /* HAVE_TM_ZONE */
468 #endif /* HAVE_TZNAME */
472 case 'n': /* same as \n */
477 case 't': /* same as \t */
482 case 'D': /* date as %m/%d/%y */
483 strftime(tbuf
, sizeof tbuf
, "%m/%d/%y", timeptr
);
486 case 'e': /* day of month, blank padded */
487 sprintf(tbuf
, "%2d", range(1, timeptr
->tm_mday
, 31));
490 case 'r': /* time as %I:%M:%S %p */
491 strftime(tbuf
, sizeof tbuf
, "%I:%M:%S %p", timeptr
);
494 case 'R': /* time as %H:%M */
495 strftime(tbuf
, sizeof tbuf
, "%H:%M", timeptr
);
498 case 'T': /* time as %H:%M:%S */
499 strftime(tbuf
, sizeof tbuf
, "%H:%M:%S", timeptr
);
504 case 'k': /* hour, 24-hour clock, blank pad */
505 sprintf(tbuf
, "%2d", range(0, timeptr
->tm_hour
, 23));
508 case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
509 i
= range(0, timeptr
->tm_hour
, 23);
514 sprintf(tbuf
, "%2d", i
);
520 case 'v': /* date as dd-bbb-YYYY */
521 sprintf(tbuf
, "%2d-%3.3s-%4ld",
522 range(1, timeptr
->tm_mday
, 31),
523 months_a
[range(0, timeptr
->tm_mon
, 11)],
524 timeptr
->tm_year
+ 1900L);
525 for (i
= 3; i
< 6; i
++)
526 if (islower(tbuf
[i
]))
527 tbuf
[i
] = toupper(tbuf
[i
]);
534 sprintf(tbuf
, "%02ld", (timeptr
->tm_year
+ 1900L) / 100);
540 /* POSIX locale extensions, ignored for now */
543 case 'V': /* week of year according ISO 8601 */
544 sprintf(tbuf
, "%02d", iso8601wknum(timeptr
));
548 /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
549 sprintf(tbuf
, "%d", timeptr
->tm_wday
== 0 ? 7 :
552 #endif /* POSIX2_DATE */
560 * If it's December but the ISO week number is one,
561 * that week is in next year.
562 * If it's January but the ISO week number is 52 or
563 * 53, that week is in last year.
564 * Otherwise, it's this year.
566 w
= iso8601wknum(timeptr
);
567 if (timeptr
->tm_mon
== 11 && w
== 1)
568 y
= 1900L + timeptr
->tm_year
+ 1;
569 else if (timeptr
->tm_mon
== 0 && w
>= 52)
570 y
= 1900L + timeptr
->tm_year
- 1;
572 y
= 1900L + timeptr
->tm_year
;
575 sprintf(tbuf
, "%ld", y
);
577 sprintf(tbuf
, "%02ld", y
% 100);
579 #endif /* ISO_DATE_EXT */
588 if (s
+ i
< endp
- 1) {
596 if (s
< endp
&& *format
== '\0') {
603 /* isleap --- is a year a leap year? */
614 return ((year
% 4 == 0 && year
% 100 != 0) || year
% 400 == 0);
619 /* iso8601wknum --- compute week number according to ISO 8601 */
623 iso8601wknum(timeptr
)
624 const struct tm
*timeptr
;
627 iso8601wknum(const struct tm
*timeptr
)
632 * If the week (Monday to Sunday) containing January 1
633 * has four or more days in the new year, then it is week 1;
634 * otherwise it is the highest numbered week of the previous
635 * year (52 or 53), and the next week is week 1.
637 * ADR: This means if Jan 1 was Monday through Thursday,
638 * it was week 1, otherwise week 52 or 53.
640 * XPG4 erroneously included POSIX.2 rationale text in the
641 * main body of the standard. Thus it requires week 53.
644 int weeknum
, jan1day
;
646 /* get week number, Monday as first day of the week */
647 weeknum
= weeknumber(timeptr
, 1);
650 * With thanks and tip of the hatlo to tml@tik.vtt.fi
652 * What day of the week does January 1 fall on?
654 * (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
655 * (timeptr->tm_wday - jan1.tm_wday) MOD 7
659 * timeptr->tm_wday MOD 7 == timeptr->tm_wday
660 * from which it follows that. . .
662 jan1day
= timeptr
->tm_wday
- (timeptr
->tm_yday
% 7);
667 * If Jan 1 was a Monday through Thursday, it was in
668 * week 1. Otherwise it was last year's highest week, which is
669 * this year's week 0.
671 * What does that mean?
672 * If Jan 1 was Monday, the week number is exactly right, it can
674 * If it was Tuesday through Thursday, the weeknumber is one
675 * less than it should be, so we add one.
676 * Otherwise, Friday, Saturday or Sunday, the week number is
677 * OK, but if it is 0, it needs to be 52 or 53.
682 case 2: /* Tuesday */
683 case 3: /* Wednesday */
684 case 4: /* Thursday */
688 case 6: /* Saturday */
691 #ifdef USE_BROKEN_XPG4
692 /* XPG4 (as of March 1994) says 53 unconditionally */
695 /* get week number of last week of last year */
696 struct tm dec31ly
; /* 12/31 last year */
700 dec31ly
.tm_mday
= 31;
701 dec31ly
.tm_wday
= (jan1day
== 0) ? 6 : jan1day
- 1;
702 dec31ly
.tm_yday
= 364 + isleap(dec31ly
.tm_year
+ 1900L);
703 weeknum
= iso8601wknum(& dec31ly
);
709 if (timeptr
->tm_mon
== 11) {
711 * The last week of the year
712 * can be in week 1 of next year.
715 * This can only happen if
723 wday
= timeptr
->tm_wday
;
724 mday
= timeptr
->tm_mday
;
725 if ( (wday
== 1 && (mday
>= 29 && mday
<= 31))
726 || (wday
== 2 && (mday
== 30 || mday
== 31))
727 || (wday
== 3 && mday
== 31))
735 /* weeknumber --- figure how many weeks into the year */
737 /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
741 weeknumber(timeptr
, firstweekday
)
742 const struct tm
*timeptr
;
746 weeknumber(const struct tm
*timeptr
, int firstweekday
)
749 int wday
= timeptr
->tm_wday
;
752 if (firstweekday
== 1) {
753 if (wday
== 0) /* sunday */
758 ret
= ((timeptr
->tm_yday
+ 7 - wday
) / 7);
765 /* ADR --- I'm loathe to mess with ado's code ... */
767 Date
: Wed
, 24 Apr
91 20:54:08 MDT
768 From
: Michal Jaegermann
<audfax
!emory
!vm
.ucs
.UAlberta
.CA
!NTOMCZAK
>
769 To
: arnold@audiofax
.com
772 in a process of fixing of
strftime() in libraries on Atari ST I grabbed
773 some pieces of code from your own strftime
. When doing that it came
774 to mind that your
weeknumber() function compiles a little bit nicer
775 in the following form
:
777 * firstweekday is 0 if starting in Sunday, non-zero if in Monday
780 return (timeptr
->tm_yday
- timeptr
->tm_wday
+
781 (firstweekday
? (timeptr
->tm_wday
? 8 : 1) : 7)) / 7;
783 How nicer it depends on a compiler
, of course
, but always a tiny bit
.
787 ntomczak@vm
.ucs
.ualberta
.ca
800 * "tst" is a test driver for the function "strftime".
807 * Control Data Systems, Inc.
808 * vogelke@c-17igp.wpafb.af.mil
814 * cc -o tst -DTEST_STRFTIME strftime.c
817 /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
822 #include <sys/time.h>
828 * Array of time formats.
831 static char *array
[] =
833 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
834 "(%%B) full month name, var length (January..December) %B",
836 "(%%D) date (%%m/%%d/%%y) %D",
837 "(%%E) Locale extensions (ignored) %E",
838 "(%%H) hour (24-hour clock, 00..23) %H",
839 "(%%I) hour (12-hour clock, 01..12) %I",
840 "(%%M) minute (00..59) %M",
841 "(%%O) Locale extensions (ignored) %O",
842 "(%%R) time, 24-hour (%%H:%%M) %R",
843 "(%%S) second (00..60) %S",
844 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
845 "(%%U) week of year, Sunday as first day of week (00..53) %U",
846 "(%%V) week of year according to ISO 8601 %V",
847 "(%%W) week of year, Monday as first day of week (00..53) %W",
848 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
849 "(%%Y) year with century (1970...) %Y",
850 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
851 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
852 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
853 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
854 "(%%d) day of the month (01..31) %d",
855 "(%%e) day of the month, blank-padded ( 1..31) %e",
856 "(%%h) should be same as (%%b) %h",
857 "(%%j) day of the year (001..366) %j",
858 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
859 "(%%l) hour, 12-hour clock, blank pad ( 0..12) %l",
860 "(%%m) month (01..12) %m",
861 "(%%p) locale's AM or PM based on 12-hour clock %p",
862 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
863 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
864 "(%%v) VMS date (dd-bbb-YYYY) %v",
865 "(%%w) day of week (0..6, Sunday == 0) %w",
866 "(%%x) appropriate locale date representation %x",
867 "(%%y) last two digits of year (00..99) %y",
868 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
882 char string
[MAXTIME
];
891 /* Call the function. */
893 clock
= time((long *) 0);
894 tm
= localtime(&clock
);
896 for (k
= 0; next
= array
[k
]; k
++) {
897 length
= strftime(string
, MAXTIME
, next
, tm
);
898 printf("%s\n", string
);
903 #endif /* TEST_STRFTIME */