58 #include <sys/types.h>
61 #if defined(TM_IN_SYS_TIME) || !defined(GAWK)
62 #include <sys/types.h>
74 #define MAILHEADER_EXT 1
75 #define ISO_DATE_EXT 1
77 #if defined(ISO_DATE_EXT)
78 #if ! defined(POSIX2_DATE)
83 #if defined(POSIX2_DATE)
84 #if ! defined(SYSV_EXT)
87 #if ! defined(SUNOS_EXT)
92 #if defined(POSIX2_DATE)
93 #define adddecl(stuff) stuff
95 #define adddecl(stuff)
100 #if !defined __STDC__ && !defined _WIN32
107 static int weeknumber(
const struct tm *timeptr,
int firstweekday);
123 #define range(low, item, hi) max((low), min((item), (hi)))
138 return (a < b ? a : b);
154 return (a > b ? a : b);
157 #ifdef NO_STRING_LITERAL_CONCATENATION
158 #error No string literal concatenation
161 #define add(x,y) (rb_funcall((x), '+', 1, (y)))
162 #define sub(x,y) (rb_funcall((x), '-', 1, (y)))
163 #define mul(x,y) (rb_funcall((x), '*', 1, (y)))
164 #define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
165 #define div(x,y) (rb_funcall((x), rb_intern("div"), 1, (y)))
166 #define mod(x,y) (rb_funcall((x), '%', 1, (y)))
173 const char *
const endp = s + maxsize;
174 const char *
const start = s;
181 int precision, flags, colons;
183 enum {LEFT, CHCASE, LOWER, UPPER, LOCALE_O, LOCALE_E};
184 #define BIT_OF(n) (1U<<(n))
185 #ifdef MAILHEADER_EXT
190 static const char days_l[][10] = {
191 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
192 "Thursday",
"Friday",
"Saturday",
194 static const char months_l[][10] = {
195 "January",
"February",
"March",
"April",
196 "May",
"June",
"July",
"August",
"September",
197 "October",
"November",
"December",
199 static const char ampm[][3] = {
"AM",
"PM", };
201 if (s ==
NULL || format ==
NULL || vtm ==
NULL || maxsize == 0)
211 for (; *format && s < endp - 1; format++) {
212 #define FLAG_FOUND() do { \
213 if (precision > 0 || flags & (BIT_OF(LOCALE_E)|BIT_OF(LOCALE_O))) \
216 #define NEEDS(n) do if (s >= endp || (n) >= endp - s - 1) goto err; while (0)
217 #define FILL_PADDING(i) do { \
218 if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \
220 memset(s, padding ? padding : ' ', precision - (i)); \
221 s += precision - (i); \
227 #define FMT(def_pad, def_prec, fmt, val) \
230 if (precision <= 0) precision = (def_prec); \
231 if (flags & BIT_OF(LEFT)) precision = 1; \
232 l = snprintf(s, endp - s, \
233 ((padding == '0' || (!padding && (def_pad) == '0')) ? "%0*"fmt : "%*"fmt), \
235 if (l < 0) goto err; \
238 #define STRFTIME(fmt) \
240 i = rb_strftime_with_timespec(s, endp - s, (fmt), vtm, timev, ts, gmt); \
242 if (precision > i) {\
244 memmove(s + precision - i, s, i);\
245 memset(s, padding ? padding : ' ', precision - i); \
250 #define FMTV(def_pad, def_prec, fmt, val) \
253 if (FIXNUM_P(tmp)) { \
254 FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
257 VALUE args[2], result; \
259 if (precision <= 0) precision = (def_prec); \
260 if (flags & BIT_OF(LEFT)) precision = 1; \
261 args[0] = INT2FIX(precision); \
263 if (padding == '0' || (!padding && (def_pad) == '0')) \
264 result = rb_str_format(2, args, rb_str_new2("%0*"fmt)); \
266 result = rb_str_format(2, args, rb_str_new2("%*"fmt)); \
267 l = strlcpy(s, StringValueCStr(result), endp-s); \
268 if ((size_t)(endp-s) <= l) \
274 if (*format !=
'%') {
296 if (flags &
BIT_OF(CHCASE)) {
303 i = 3, tp = days_l[vtm->
wday];
307 if (flags &
BIT_OF(CHCASE)) {
321 if (flags &
BIT_OF(CHCASE)) {
325 if (vtm->
mon < 1 || vtm->
mon > 12)
328 i = 3, tp = months_l[vtm->
mon-1];
332 if (flags &
BIT_OF(CHCASE)) {
336 if (vtm->
mon < 1 || vtm->
mon > 12)
348 FMT(
'0', 2,
"d", (
int)i);
353 FMT(
'0', 2,
"d", (
int)i);
362 FMT(
'0', 2,
"d", (
int)i);
371 FMT(
'0', 2,
"d", (
int)i);
376 FMT(
'0', 2,
"d", (
int)i);
381 if ((*format ==
'p' && (flags &
BIT_OF(CHCASE))) ||
382 (*format ==
'P' && !(flags & (
BIT_OF(CHCASE)|
BIT_OF(UPPER))))) {
404 FMTV(
'0', 1,
"d", sec);
410 FMT(
'0', 2,
"d", (
int)i);
419 FMT(
'0', 1,
"d", (
int)i);
436 FMT(
'0', 2,
"d", (
int)i);
442 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
449 #ifdef MAILHEADER_EXT
453 precision = precision <= 5 ? 2 : precision-3;
454 NEEDS(precision + 3);
458 precision = precision <= 6 ? 2 : precision-4;
459 NEEDS(precision + 4);
463 precision = precision <= 9 ? 2 : precision-7;
464 NEEDS(precision + 7);
483 i =
snprintf(s, endp - s, (padding ==
' ' ?
"%+*ld" :
"%+.*ld"),
484 precision + 1, sign * (off / 3600));
486 if (sign < 0 && off < 3600) {
487 *(padding ==
' ' ? s + i - 2 : s) =
'-';
493 i =
snprintf(s, endp - s,
"%02d", (
int)(off / 60));
499 i =
snprintf(s, endp - s,
"%02d", (
int)off);
507 if (flags &
BIT_OF(CHCASE)) {
558 FMT(
' ', 2,
"d", (
int)i);
567 FMT(
' ', 2,
"d", (
int)i);
586 flags |=
BIT_OF(LOCALE_E);
590 flags |=
BIT_OF(LOCALE_O);
618 if (vtm->
mon == 12 && w == 1)
620 else if (vtm->
mon == 1 && w >= 52)
623 if (*format ==
'G') {
626 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
629 FMTV(
'0', 4,
"d", yv);
635 FMT(
'0', 2,
"ld", y);
658 if (precision <= 0) {
666 snprintf(s, endp - s,
"%09ld", subsec);
667 memset(s+9,
'0', precision-9);
672 for (i = 0; i < 9-precision; i++)
674 snprintf(s, endp - s,
"%0*ld", precision, subsec);
717 padding = precision = 0;
742 case '1':
case '2':
case '3':
case '4':
743 case '5':
case '6':
case '7':
case '8':
case '9':
746 precision = (int)strtoul(format, &e, 10);
784 if (*format ==
'\0') {
792 rb_strftime(
char *s,
size_t maxsize,
const char *format,
const struct vtm *vtm,
VALUE timev,
int gmt)
814 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
826 tm.tm_mon = vtm->
mon-1;
827 tm.tm_mday = vtm->
mday;
828 tm.tm_hour = vtm->
hour;
829 tm.tm_min = vtm->
min;
830 tm.tm_sec = vtm->
sec;
831 tm.tm_wday = vtm->
wday;
832 tm.tm_yday = vtm->
yday-1;
833 tm.tm_isdst = vtm->
isdst;
834 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
837 #if defined(HAVE_TM_ZONE)
838 tm.tm_zone = (
char *)vtm->
zone;
849 const struct tm *timeptr;
869 int weeknum, jan1day;
887 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
916 #ifdef USE_BROKEN_XPG4
925 dec31ly.tm_mday = 31;
926 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
927 dec31ly.tm_yday = 364 +
isleap(dec31ly.tm_year + 1900L);
934 if (timeptr->tm_mon == 11) {
948 wday = timeptr->tm_wday;
949 mday = timeptr->tm_mday;
950 if ( (wday == 1 && (mday >= 29 && mday <= 31))
951 || (wday == 2 && (mday == 30 || mday == 31))
952 || (wday == 3 && mday == 31))
976 const struct tm *timeptr;
980 weeknumber(
const struct tm *timeptr,
int firstweekday)
983 int wday = timeptr->tm_wday;
986 if (firstweekday == 1) {
992 ret = ((timeptr->tm_yday + 7 - wday) / 7);
1009 Date: Wed, 24 Apr 91 20:54:08 MDT
1010 From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
1011 To: arnold@audiofax.com
1014 in a process of fixing of strftime() in libraries on Atari ST I grabbed
1015 some pieces of code from your own strftime. When doing that it came
1016 to mind that your
weeknumber() function compiles a little bit nicer
1017 in the following form:
1022 return (timeptr->tm_yday - timeptr->tm_wday +
1023 (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
1025 How nicer it depends on a compiler, of course, but always a tiny bit.
1029 ntomczak@vm.ucs.ualberta.ca
1032 #ifdef TEST_STRFTIME
1064 #include <sys/time.h>
1073 static char *array[] =
1075 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
1076 "(%%B) full month name, var length (January..December) %B",
1078 "(%%D) date (%%m/%%d/%%y) %D",
1079 "(%%E) Locale extensions (ignored) %E",
1080 "(%%H) hour (24-hour clock, 00..23) %H",
1081 "(%%I) hour (12-hour clock, 01..12) %I",
1082 "(%%M) minute (00..59) %M",
1083 "(%%O) Locale extensions (ignored) %O",
1084 "(%%R) time, 24-hour (%%H:%%M) %R",
1085 "(%%S) second (00..60) %S",
1086 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
1087 "(%%U) week of year, Sunday as first day of week (00..53) %U",
1088 "(%%V) week of year according to ISO 8601 %V",
1089 "(%%W) week of year, Monday as first day of week (00..53) %W",
1090 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
1091 "(%%Y) year with century (1970...) %Y",
1092 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
1093 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
1094 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
1095 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
1096 "(%%d) day of the month (01..31) %d",
1097 "(%%e) day of the month, blank-padded ( 1..31) %e",
1098 "(%%h) should be same as (%%b) %h",
1099 "(%%j) day of the year (001..366) %j",
1100 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
1101 "(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
1102 "(%%m) month (01..12) %m",
1103 "(%%p) locale's AM or PM based on 12-hour clock %p",
1104 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
1105 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
1106 "(%%v) VMS date (dd-bbb-YYYY) %v",
1107 "(%%w) day of week (0..6, Sunday == 0) %w",
1108 "(%%x) appropriate locale date representation %x",
1109 "(%%y) last two digits of year (00..99) %y",
1110 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
1124 char string[MAXTIME];
1135 clock = time((
long *) 0);
1136 tm = localtime(&clock);
1138 for (k = 0; next = array[k]; k++) {
1139 length = strftime(
string, MAXTIME, next, tm);
1140 printf(
"%s\n",
string);