60 #include <sys/types.h>
63 #if defined(TM_IN_SYS_TIME) || !defined(GAWK)
64 #include <sys/types.h>
76 #define MAILHEADER_EXT 1
77 #define ISO_DATE_EXT 1
79 #if defined(ISO_DATE_EXT)
80 #if ! defined(POSIX2_DATE)
85 #if defined(POSIX2_DATE)
86 #if ! defined(SYSV_EXT)
89 #if ! defined(SUNOS_EXT)
94 #if defined(POSIX2_DATE)
95 #define adddecl(stuff) stuff
97 #define adddecl(stuff)
102 #if !defined __STDC__ && !defined _WIN32
104 static int weeknumber();
106 static int weeknumber_v();
107 adddecl(
static int iso8601wknum_v();)
109 static int weeknumber(
const struct tm *timeptr,
int firstweekday);
110 adddecl(
static int iso8601wknum(
const struct tm *timeptr);)
111 static int weeknumber_v(
const struct vtm *vtm,
int firstweekday);
112 adddecl(
static int iso8601wknum_v(
const struct vtm *vtm);)
125 #define range(low, item, hi) max((low), min((item), (hi)))
134 return (a < b ? a : b);
144 return (a > b ? a : b);
147 #ifdef NO_STRING_LITERAL_CONCATENATION
148 #error No string literal concatenation
151 #define add(x,y) (rb_funcall((x), '+', 1, (y)))
152 #define sub(x,y) (rb_funcall((x), '-', 1, (y)))
153 #define mul(x,y) (rb_funcall((x), '*', 1, (y)))
154 #define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
155 #define div(x,y) (rb_funcall((x), rb_intern("div"), 1, (y)))
156 #define mod(x,y) (rb_funcall((x), '%', 1, (y)))
161 #define BIT_OF(n) (1U<<(n))
164 resize_buffer(
VALUE ftime,
char *s,
const char **start,
const char **endp,
167 size_t len = s - *start;
168 size_t nlen =
len +
n * 2;
170 if (nlen < len || nlen > maxsize) {
182 buffer_size_check(
const char *s,
183 const char *format_end,
size_t format_len,
187 const char *format = format_end-format_len;
215 format_value(
VALUE val,
int base)
228 rb_strftime_with_timespec(
VALUE ftime,
const char *format,
size_t format_len,
234 const char *start = s;
236 const char *
const format_end = format + format_len;
244 int precision, flags, colons;
246 #ifdef MAILHEADER_EXT
252 static const char days_l[][10] = {
253 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
254 "Thursday",
"Friday",
"Saturday",
256 static const char months_l[][10] = {
257 "January",
"February",
"March",
"April",
258 "May",
"June",
"July",
"August",
"September",
259 "October",
"November",
"December",
261 static const char ampm[][3] = {
"AM",
"PM", };
263 if (format ==
NULL || format_len == 0 || vtm ==
NULL) {
276 for (; format < format_end; format++) {
277 #define FLAG_FOUND() do { \
281 #define NEEDS(n) do { \
282 if (s >= endp || (n) >= endp - s - 1) { \
283 s = resize_buffer(ftime, s, &start, &endp, (n), maxsize); \
284 buffer_size_check(s, format_end, format_len, enc); \
287 #define FILL_PADDING(i) do { \
288 if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \
290 memset(s, padding ? padding : ' ', precision - (i)); \
291 s += precision - (i); \
297 #define FMT_PADDING(fmt, def_pad) \
298 (&"%*"fmt"\0""%0*"fmt[\
299 (padding == '0' || (!padding && (def_pad) == '0')) ? \
300 rb_strlen_lit("%*"fmt)+1 : 0])
301 #define FMT_PRECISION(def_prec) \
302 ((flags & BIT_OF(LEFT)) ? (1) : \
303 (precision <= 0) ? (def_prec) : (precision))
304 #define FMT(def_pad, def_prec, fmt, val) \
306 precision = FMT_PRECISION(def_prec); \
309 rb_str_set_len(ftime, len); \
310 rb_str_catf(ftime, FMT_PADDING(fmt, def_pad), \
312 RSTRING_GETMEM(ftime, s, len); \
313 endp = (start = s) + rb_str_capacity(ftime); \
316 #define STRFTIME(fmt) \
319 rb_str_set_len(ftime, len); \
320 if (!rb_strftime_with_timespec(ftime, (fmt), rb_strlen_lit(fmt), \
321 enc, time, vtm, timev, ts, gmt, maxsize)) \
323 s = RSTRING_PTR(ftime); \
324 i = RSTRING_LEN(ftime) - len; \
325 endp = (start = s) + rb_str_capacity(ftime); \
327 if (i > 0) case_conv(s, i, flags); \
328 if (precision > i) {\
330 memmove(s + precision - i, s, i);\
331 memset(s, padding ? padding : ' ', precision - i); \
336 #define FMTV(def_pad, def_prec, fmt, val) \
339 if (FIXNUM_P(tmp)) { \
340 FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
343 const int base = ((fmt[0] == 'x') ? 16 : \
344 (fmt[0] == 'o') ? 8 : \
346 precision = FMT_PRECISION(def_prec); \
347 if (!padding) padding = (def_pad); \
348 tmp = format_value(tmp, base); \
349 i = RSTRING_LEN(tmp); \
351 rb_str_set_len(ftime, s-start); \
352 rb_str_append(ftime, tmp); \
353 RSTRING_GETMEM(ftime, s, len); \
354 endp = (start = s) + rb_str_capacity(ftime); \
359 tp =
memchr(format,
'%', format_end - format);
360 if (!tp) tp = format_end;
362 memcpy(s, format, tp - format);
365 if (format == format_end)
break;
374 if (++format >= format_end)
goto unknown;
386 if (vtm->wday < 0 || vtm->wday > 6)
389 i = 3, tp = days_l[vtm->wday];
397 if (vtm->wday < 0 || vtm->wday > 6)
400 i =
strlen(tp = days_l[vtm->wday]);
411 if (vtm->mon < 1 || vtm->mon > 12)
414 i = 3, tp = months_l[vtm->mon-1];
422 if (vtm->mon < 1 || vtm->mon > 12)
425 i =
strlen(tp = months_l[vtm->mon-1]);
433 i =
range(1, vtm->mday, 31);
434 FMT(
'0', 2,
"d", (
int)
i);
438 i =
range(0, vtm->hour, 23);
439 FMT(
'0', 2,
"d", (
int)
i);
443 i =
range(0, vtm->hour, 23);
448 FMT(
'0', 2,
"d", (
int)
i);
452 i =
range(1, vtm->yday, 366);
453 FMT(
'0', 3,
"d", (
int)
i);
457 i =
range(1, vtm->mon, 12);
458 FMT(
'0', 2,
"d", (
int)
i);
462 i =
range(0, vtm->min, 59);
463 FMT(
'0', 2,
"d", (
int)
i);
473 i =
range(0, vtm->hour, 23);
491 FMTV(
'0', 1,
"d", sec);
496 i =
range(0, vtm->sec, 60);
497 FMT(
'0', 2,
"d", (
int)
i);
501 FMT(
'0', 2,
"d", weeknumber_v(vtm, 0));
505 i =
range(0, vtm->wday, 6);
506 FMT(
'0', 1,
"d", (
int)
i);
510 FMT(
'0', 2,
"d", weeknumber_v(vtm, 1));
523 FMT(
'0', 2,
"d", (
int)
i);
529 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
532 FMTV(
'0', 4,
"d", vtm->year);
536 #ifdef MAILHEADER_EXT
553 precision = precision <= 5 ? 2 : precision-3;
554 NEEDS(precision + 3);
558 precision = precision <= 6 ? 2 : precision-4;
559 NEEDS(precision + 4);
563 precision = precision <= 9 ? 2 : precision-7;
564 NEEDS(precision + 7);
568 if (off % 3600 == 0) {
569 precision = precision <= 3 ? 2 : precision-1;
570 NEEDS(precision + 3);
572 else if (off % 60 == 0) {
573 precision = precision <= 6 ? 2 : precision-4;
574 NEEDS(precision + 4);
577 precision = precision <= 9 ? 2 : precision-7;
578 NEEDS(precision + 9);
586 i =
snprintf(s, endp - s, (padding ==
' ' ?
"%+*ld" :
"%+.*ld"),
587 precision + (padding ==
' '), sign * (off / 3600));
589 if (sign < 0 && off < 3600) {
590 *(padding ==
' ' ? s +
i - 2 : s) =
'-';
594 if (colons == 3 && off == 0)
598 i =
snprintf(s, endp - s,
"%02d", (
int)(off / 60));
602 if (colons == 3 && off == 0)
606 i =
snprintf(s, endp - s,
"%02d", (
int)off);
623 if (
NIL_P(vtm->zone)) {
633 if ((
unsigned char)tp[
i] > 0x7F) {
662 FMT(
' ', 2,
"d",
range(1, vtm->mday, 31));
680 i =
range(0, vtm->hour, 23);
681 FMT(
' ', 2,
"d", (
int)
i);
685 i =
range(0, vtm->hour, 23);
690 FMT(
' ', 2,
"d", (
int)
i);
709 if (!format[1] || !
strchr(
"cCxXyY", format[1]))
714 if (!format[1] || !
strchr(
"deHkIlmMSuUVwWy", format[1]))
719 FMT(
'0', 2,
"d", iso8601wknum_v(vtm));
724 FMT(
'0', 1,
"d", vtm->wday == 0 ? 7 : vtm->wday);
741 VALUE yv = vtm->year;
742 w = iso8601wknum_v(vtm);
743 if (vtm->mon == 12 && w == 1)
745 else if (vtm->mon == 1 && w >= 52)
748 if (*format ==
'G') {
751 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
754 FMTV(
'0', 4,
"d", yv);
760 FMT(
'0', 2,
"ld", y);
783 if (precision <= 0) {
791 snprintf(s, endp - s,
"%09ld", subsec);
792 memset(s+9,
'0', precision-9);
797 for (
i = 0;
i < 9-precision;
i++)
799 snprintf(s, endp - s,
"%0*ld", precision, subsec);
825 VALUE args[2], result;
843 padding = precision = 0;
862 for (colons = 1; colons <= 3; ++colons) {
863 if (format+colons >= format_end)
goto unknown;
864 if (format[colons] ==
'z')
break;
865 if (format[colons] !=
':')
goto unknown;
867 format += colons - 1;
872 case '1':
case '2':
case '3':
case '4':
873 case '5':
case '6':
case '7':
case '8':
case '9':
878 if (ov || u >
INT_MAX)
goto unknown;
897 s = case_conv(s,
i, flags);
900 if (format != format_end) {
910 strftime_size_limit(
size_t format_len)
912 size_t limit = format_len * (1*1024*1024);
913 if (limit < format_len) limit = format_len;
914 else if (limit < 1024) limit = 1024;
923 return rb_strftime_with_timespec(result, format, format_len, enc,
925 strftime_size_limit(format_len));
933 return rb_strftime_with_timespec(result, format, format_len, enc,
935 strftime_size_limit(format_len));
940 rb_strftime_limit(
const char *format,
size_t format_len,
rb_encoding *enc,
942 int gmt,
size_t maxsize)
945 return rb_strftime_with_timespec(result, format, format_len, enc,
955 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
960 vtm2tm_noyear(
const struct vtm *vtm,
struct tm *result)
975 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
978 #if defined(HAVE_TM_ZONE)
988 iso8601wknum(
const struct tm *timeptr)
1004 int weeknum, jan1day;
1007 weeknum = weeknumber(timeptr, 1);
1051 #ifdef USE_BROKEN_XPG4
1059 dec31ly.tm_mon = 11;
1060 dec31ly.tm_mday = 31;
1061 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
1062 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900
L);
1063 weeknum = iso8601wknum(& dec31ly);
1069 if (timeptr->
tm_mon == 11) {
1085 if ( (wday == 1 && (mday >= 29 && mday <= 31))
1086 || (wday == 2 && (mday == 30 || mday == 31))
1087 || (wday == 3 && mday == 31))
1095 iso8601wknum_v(
const struct vtm *vtm)
1098 vtm2tm_noyear(vtm, &
tm);
1099 return iso8601wknum(&
tm);
1109 weeknumber(
const struct tm *timeptr,
int firstweekday)
1114 if (firstweekday == 1) {
1120 ret = ((timeptr->
tm_yday + 7 - wday) / 7);
1127 weeknumber_v(
const struct vtm *vtm,
int firstweekday)
1130 vtm2tm_noyear(vtm, &
tm);
1131 return weeknumber(&
tm, firstweekday);
1137 Date: Wed, 24 Apr 91 20:54:08 MDT
1138 From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
1139 To: arnold@audiofax.com
1142 in a process of fixing of
strftime() in libraries on Atari ST
I grabbed
1143 some pieces of code from your own
strftime. When doing that it came
1144 to mind that your weeknumber() function compiles a little bit nicer
1145 in the following form:
1151 (firstweekday ? (timeptr->
tm_wday ? 8 : 1) : 7)) / 7;
1153 How nicer it depends on a compiler, of course, but always a tiny bit.
1157 ntomczak@vm.ucs.ualberta.ca
1160 #ifdef TEST_STRFTIME
1192 #include <sys/time.h>
1201 static char *array[] =
1203 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
1204 "(%%B) full month name, var length (January..December) %B",
1206 "(%%D) date (%%m/%%d/%%y) %D",
1207 "(%%E) Locale extensions (ignored) %E",
1208 "(%%H) hour (24-hour clock, 00..23) %H",
1209 "(%%I) hour (12-hour clock, 01..12) %I",
1210 "(%%M) minute (00..59) %M",
1211 "(%%O) Locale extensions (ignored) %O",
1212 "(%%R) time, 24-hour (%%H:%%M) %R",
1213 "(%%S) second (00..60) %S",
1214 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
1215 "(%%U) week of year, Sunday as first day of week (00..53) %U",
1216 "(%%V) week of year according to ISO 8601 %V",
1217 "(%%W) week of year, Monday as first day of week (00..53) %W",
1218 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
1219 "(%%Y) year with century (1970...) %Y",
1220 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
1221 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
1222 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
1223 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
1224 "(%%d) day of the month (01..31) %d",
1225 "(%%e) day of the month, blank-padded ( 1..31) %e",
1226 "(%%h) should be same as (%%b) %h",
1227 "(%%j) day of the year (001..366) %j",
1228 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
1229 "(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
1230 "(%%m) month (01..12) %m",
1231 "(%%p) locale's AM or PM based on 12-hour clock %p",
1232 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
1233 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
1234 "(%%v) VMS date (dd-bbb-YYYY) %v",
1235 "(%%w) day of week (0..6, Sunday == 0) %w",
1236 "(%%x) appropriate locale date representation %x",
1237 "(%%y) last two digits of year (00..99) %y",
1238 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
1250 char string[MAXTIME];
1264 for (k = 0; next = array[k]; k++) {
1265 length =
strftime(
string, MAXTIME, next,
tm);