63#if defined(TM_IN_SYS_TIME) || !defined(GAWK)
76#define MAILHEADER_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
102#if !defined __STDC__ && !defined _WIN32
104static int weeknumber();
106static int weeknumber_v();
107adddecl(
static int iso8601wknum_v();)
109static int weeknumber(
const struct tm *timeptr,
int firstweekday);
110adddecl(
static int iso8601wknum(
const struct tm *timeptr);)
111static int weeknumber_v(
const struct vtm *vtm,
int firstweekday);
112adddecl(
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))
164resize_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) {
182buffer_size_check(
const char *s,
183 const char *format_end,
size_t format_len,
187 const char *format = format_end-format_len;
215format_value(
VALUE val,
int base)
228rb_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;
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) {\
332 memmove(s + precision - i, s, i);\
333 memset(s, padding ? padding : ' ', precision - i); \
338#define FMTV(def_pad, def_prec, fmt, val) \
341 if (FIXNUM_P(tmp)) { \
342 FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
345 const int base = ((fmt[0] == 'x') ? 16 : \
346 (fmt[0] == 'o') ? 8 : \
348 precision = FMT_PRECISION(def_prec); \
349 if (!padding) padding = (def_pad); \
350 tmp = format_value(tmp, base); \
351 i = RSTRING_LEN(tmp); \
353 rb_str_set_len(ftime, s-start); \
354 rb_str_append(ftime, tmp); \
355 RSTRING_GETMEM(ftime, s, len); \
356 endp = (start = s) + rb_str_capacity(ftime); \
361 tp =
memchr(format,
'%', format_end - format);
362 if (!tp) tp = format_end;
364 memcpy(s, format, tp - format);
367 if (format == format_end)
break;
376 if (++format >= format_end)
goto unknown;
388 if (vtm->wday < 0 || vtm->wday > 6)
391 i = 3, tp = days_l[vtm->wday];
399 if (vtm->wday < 0 || vtm->wday > 6)
402 i =
strlen(tp = days_l[vtm->wday]);
413 if (vtm->mon < 1 || vtm->mon > 12)
416 i = 3, tp = months_l[vtm->mon-1];
424 if (vtm->mon < 1 || vtm->mon > 12)
427 i =
strlen(tp = months_l[vtm->mon-1]);
435 i =
range(1, vtm->mday, 31);
436 FMT(
'0', 2,
"d", (
int)
i);
440 i =
range(0, vtm->hour, 23);
441 FMT(
'0', 2,
"d", (
int)
i);
445 i =
range(0, vtm->hour, 23);
450 FMT(
'0', 2,
"d", (
int)
i);
454 i =
range(1, vtm->yday, 366);
455 FMT(
'0', 3,
"d", (
int)
i);
459 i =
range(1, vtm->mon, 12);
460 FMT(
'0', 2,
"d", (
int)
i);
464 i =
range(0, vtm->min, 59);
465 FMT(
'0', 2,
"d", (
int)
i);
475 i =
range(0, vtm->hour, 23);
493 FMTV(
'0', 1,
"d", sec);
498 i =
range(0, vtm->sec, 60);
499 FMT(
'0', 2,
"d", (
int)
i);
503 FMT(
'0', 2,
"d", weeknumber_v(vtm, 0));
507 i =
range(0, vtm->wday, 6);
508 FMT(
'0', 1,
"d", (
int)
i);
512 FMT(
'0', 2,
"d", weeknumber_v(vtm, 1));
525 FMT(
'0', 2,
"d", (
int)
i);
531 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
534 FMTV(
'0', 4,
"d", vtm->year);
555 precision = precision <= 5 ? 2 : precision-3;
556 NEEDS(precision + 3);
560 precision = precision <= 6 ? 2 : precision-4;
561 NEEDS(precision + 4);
565 precision = precision <= 9 ? 2 : precision-7;
566 NEEDS(precision + 7);
570 if (off % 3600 == 0) {
571 precision = precision <= 3 ? 2 : precision-1;
572 NEEDS(precision + 3);
574 else if (off % 60 == 0) {
575 precision = precision <= 6 ? 2 : precision-4;
576 NEEDS(precision + 4);
579 precision = precision <= 9 ? 2 : precision-7;
580 NEEDS(precision + 9);
588 i =
snprintf(s, endp - s, (padding ==
' ' ?
"%+*ld" :
"%+.*ld"),
589 precision + (padding ==
' '), sign * (off / 3600));
591 if (sign < 0 && off < 3600) {
592 *(padding ==
' ' ? s +
i - 2 : s) =
'-';
596 if (colons == 3 && off == 0)
600 i =
snprintf(s, endp - s,
"%02d", (
int)(off / 60));
604 if (colons == 3 && off == 0)
608 i =
snprintf(s, endp - s,
"%02d", (
int)off);
625 if (
NIL_P(vtm->zone)) {
635 if ((
unsigned char)tp[
i] > 0x7F) {
664 FMT(
' ', 2,
"d",
range(1, vtm->mday, 31));
682 i =
range(0, vtm->hour, 23);
683 FMT(
' ', 2,
"d", (
int)
i);
687 i =
range(0, vtm->hour, 23);
692 FMT(
' ', 2,
"d", (
int)
i);
711 if (!format[1] || !
strchr(
"cCxXyY", format[1]))
716 if (!format[1] || !
strchr(
"deHkIlmMSuUVwWy", format[1]))
721 FMT(
'0', 2,
"d", iso8601wknum_v(vtm));
726 FMT(
'0', 1,
"d", vtm->wday == 0 ? 7 : vtm->wday);
743 VALUE yv = vtm->year;
744 w = iso8601wknum_v(vtm);
745 if (vtm->mon == 12 && w == 1)
747 else if (vtm->mon == 1 && w >= 52)
750 if (*format ==
'G') {
753 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
756 FMTV(
'0', 4,
"d", yv);
762 FMT(
'0', 2,
"ld", y);
785 if (precision <= 0) {
793 snprintf(s, endp - s,
"%09ld", subsec);
794 memset(s+9,
'0', precision-9);
799 for (
i = 0;
i < 9-precision;
i++)
801 snprintf(s, endp - s,
"%0*ld", precision, subsec);
827 VALUE args[2], result;
845 padding = precision = 0;
864 for (colons = 1; colons <= 3; ++colons) {
865 if (format+colons >= format_end)
goto unknown;
866 if (format[colons] ==
'z')
break;
867 if (format[colons] !=
':')
goto unknown;
869 format += colons - 1;
874 case '1':
case '2':
case '3':
case '4':
875 case '5':
case '6':
case '7':
case '8':
case '9':
880 if (ov || u >
INT_MAX)
goto unknown;
899 s = case_conv(s,
i, flags);
902 if (format != format_end) {
912strftime_size_limit(
size_t format_len)
914 size_t limit = format_len * (1*1024*1024);
915 if (limit < format_len) limit = format_len;
916 else if (limit < 1024) limit = 1024;
925 return rb_strftime_with_timespec(result, format, format_len, enc,
927 strftime_size_limit(format_len));
935 return rb_strftime_with_timespec(result, format, format_len, enc,
937 strftime_size_limit(format_len));
942rb_strftime_limit(
const char *format,
size_t format_len,
rb_encoding *enc,
944 int gmt,
size_t maxsize)
947 return rb_strftime_with_timespec(result, format, format_len, enc,
957 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
962vtm2tm_noyear(
const struct vtm *vtm,
struct tm *result)
977#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
980#if defined(HAVE_TM_ZONE)
990iso8601wknum(
const struct tm *timeptr)
1006 int weeknum, jan1day;
1009 weeknum = weeknumber(timeptr, 1);
1053#ifdef USE_BROKEN_XPG4
1061 dec31ly.tm_mon = 11;
1062 dec31ly.tm_mday = 31;
1063 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
1064 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
1065 weeknum = iso8601wknum(& dec31ly);
1071 if (timeptr->
tm_mon == 11) {
1087 if ( (wday == 1 && (mday >= 29 && mday <= 31))
1088 || (wday == 2 && (mday == 30 || mday == 31))
1089 || (wday == 3 && mday == 31))
1097iso8601wknum_v(
const struct vtm *vtm)
1100 vtm2tm_noyear(vtm, &
tm);
1101 return iso8601wknum(&
tm);
1111weeknumber(
const struct tm *timeptr,
int firstweekday)
1116 if (firstweekday == 1) {
1122 ret = ((timeptr->
tm_yday + 7 - wday) / 7);
1129weeknumber_v(
const struct vtm *vtm,
int firstweekday)
1132 vtm2tm_noyear(vtm, &
tm);
1133 return weeknumber(&
tm, firstweekday);
1139Date: Wed, 24 Apr 91 20:54:08 MDT
1140From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
1141To: arnold@audiofax.com
1144in a process of fixing of
strftime() in libraries on Atari ST
I grabbed
1145some pieces of code from your own
strftime. When doing that it came
1146to mind that your weeknumber() function compiles a little bit nicer
1147in the following form:
1153 (firstweekday ? (timeptr->
tm_wday ? 8 : 1) : 7)) / 7;
1155How nicer it depends on a compiler, of course, but always a tiny bit.
1159 ntomczak@vm.ucs.ualberta.ca
1194#include <sys/time.h>
1203static char *array[] =
1205 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
1206 "(%%B) full month name, var length (January..December) %B",
1208 "(%%D) date (%%m/%%d/%%y) %D",
1209 "(%%E) Locale extensions (ignored) %E",
1210 "(%%H) hour (24-hour clock, 00..23) %H",
1211 "(%%I) hour (12-hour clock, 01..12) %I",
1212 "(%%M) minute (00..59) %M",
1213 "(%%O) Locale extensions (ignored) %O",
1214 "(%%R) time, 24-hour (%%H:%%M) %R",
1215 "(%%S) second (00..60) %S",
1216 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
1217 "(%%U) week of year, Sunday as first day of week (00..53) %U",
1218 "(%%V) week of year according to ISO 8601 %V",
1219 "(%%W) week of year, Monday as first day of week (00..53) %W",
1220 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
1221 "(%%Y) year with century (1970...) %Y",
1222 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
1223 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
1224 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
1225 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
1226 "(%%d) day of the month (01..31) %d",
1227 "(%%e) day of the month, blank-padded ( 1..31) %e",
1228 "(%%h) should be same as (%%b) %h",
1229 "(%%j) day of the year (001..366) %j",
1230 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
1231 "(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
1232 "(%%m) month (01..12) %m",
1233 "(%%p) locale's AM or PM based on 12-hour clock %p",
1234 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
1235 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
1236 "(%%v) VMS date (dd-bbb-YYYY) %v",
1237 "(%%w) day of week (0..6, Sunday == 0) %w",
1238 "(%%x) appropriate locale date representation %x",
1239 "(%%y) last two digits of year (00..99) %y",
1240 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
1252 char string[MAXTIME];
1266 for (k = 0; next = array[k]; k++) {
1267 length =
strftime(
string, MAXTIME, next,
tm);
rb_encoding * rb_ascii8bit_encoding(void)
rb_encoding * rb_locale_encoding(void)
rb_encoding * rb_usascii_encoding(void)
#define ECONV_UNDEF_REPLACE
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
#define ECONV_INVALID_REPLACE
char str[HTML_ESCAPE_MAX_LEN+1]
void rb_syserr_fail_str(int e, VALUE mesg)
VALUE rb_Integer(VALUE)
Equivalent to Kernel#Integer in Ruby.
VALUE rb_strftime_timespec(const char *format, size_t format_len, rb_encoding *enc, VALUE time, const struct vtm *vtm, struct timespec *ts, int gmt)
#define FMTV(def_pad, def_prec, fmt, val)
#define range(low, item, hi)
VALUE rb_strftime(const char *format, size_t format_len, rb_encoding *enc, VALUE time, const struct vtm *vtm, VALUE timev, int gmt)
#define FMT(def_pad, def_prec, fmt, val)
size_t rb_str_capacity(VALUE str)
VALUE rb_time_zone_abbreviation(VALUE zone, VALUE time)