50#include "ruby/internal/config.h"
60#if defined(TM_IN_SYS_TIME) || !defined(GAWK)
69#include "internal/encoding.h"
70#include "internal/string.h"
71#include "internal/vm.h"
82#define MAILHEADER_EXT 1
85#if defined(ISO_DATE_EXT)
86#if ! defined(POSIX2_DATE)
91#if defined(POSIX2_DATE)
92#if ! defined(SYSV_EXT)
95#if ! defined(SUNOS_EXT)
100#if defined(POSIX2_DATE)
101#define adddecl(stuff) stuff
103#define adddecl(stuff)
108#if !defined __STDC__ && !defined _WIN32
110static int weeknumber();
111adddecl(
static int iso8601wknum();)
112static int weeknumber_v();
113adddecl(
static int iso8601wknum_v();)
115static int weeknumber(
const struct tm *timeptr,
int firstweekday);
116adddecl(
static int iso8601wknum(
const struct tm *timeptr);)
117static int weeknumber_v(
const struct vtm *
vtm,
int firstweekday);
118adddecl(
static int iso8601wknum_v(
const struct vtm *
vtm);)
125extern void *malloc();
126extern void *realloc();
127extern char *getenv();
128extern char *strchr();
131#define range(low, item, hi) max((low), min((item), (hi)))
140 return (a < b ? a : b);
150 return (a > b ? a : b);
153#ifdef NO_STRING_LITERAL_CONCATENATION
154#error No string literal concatenation
157#define add(x,y) (rb_funcall((x), '+', 1, (y)))
158#define sub(x,y) (rb_funcall((x), '-', 1, (y)))
159#define mul(x,y) (rb_funcall((x), '*', 1, (y)))
160#define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
161#define div(x,y) (rb_funcall((x), rb_intern("div"), 1, (y)))
162#define mod(x,y) (rb_funcall((x), '%', 1, (y)))
166enum {LEFT, CHCASE, LOWER, UPPER};
167#define BIT_OF(n) (1U<<(n))
170resize_buffer(
VALUE ftime,
char *s,
const char **start,
const char **endp,
171 ptrdiff_t n,
size_t maxsize)
173 size_t len = s - *start;
174 size_t nlen =
len + n * 2;
176 if (nlen < len || nlen > maxsize) {
179 rb_str_set_len(ftime,
len);
181 s = RSTRING_PTR(ftime);
188buffer_size_check(
const char *s,
189 const char *format_end,
size_t format_len,
193 const char *format = format_end-format_len;
194 VALUE fmt = rb_enc_str_new(format, format_len, enc);
200case_conv(
char *s, ptrdiff_t i,
int flags)
202 switch (flags & (BIT_OF(UPPER)|BIT_OF(LOWER))) {
221format_value(
VALUE val,
int base)
223 if (!RB_BIGNUM_TYPE_P(val))
225 return rb_big2str(val, base);
234rb_strftime_with_timespec(
VALUE ftime,
const char *format,
size_t format_len,
235 rb_encoding *enc,
VALUE time,
const struct vtm *
vtm,
238 size_t len = RSTRING_LEN(ftime);
239 char *s = RSTRING_PTR(ftime);
240 const char *start = s;
242 const char *
const format_end = format + format_len;
245 auto char tbuf[TBUFSIZE];
250 int precision, flags, colons;
258 static const char days_l[][10] = {
259 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
260 "Thursday",
"Friday",
"Saturday",
262 static const char months_l[][10] = {
263 "January",
"February",
"March",
"April",
264 "May",
"June",
"July",
"August",
"September",
265 "October",
"November",
"December",
267 static const char ampm[][3] = {
"AM",
"PM", };
269 if (format == NULL || format_len == 0 ||
vtm == NULL) {
274 (rb_is_usascii_enc(enc) ||
275 rb_is_ascii8bit_enc(enc) ||
276 rb_is_locale_enc(enc))) {
281 for (; format < format_end; format++) {
282#define FLAG_FOUND() do { \
286#define NEEDS(n) do { \
287 if (s >= endp || (n) >= endp - s - 1) { \
288 s = resize_buffer(ftime, s, &start, &endp, (n), maxsize); \
289 buffer_size_check(s, format_end, format_len, enc); \
292#define FILL_PADDING(i) do { \
293 if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \
295 memset(s, padding ? padding : ' ', precision - (i)); \
296 s += precision - (i); \
302#define FMT_PADDING(fmt, def_pad) \
303 (&"%*"fmt"\0""%0*"fmt[\
304 (padding == '0' || (!padding && (def_pad) == '0')) ? \
305 rb_strlen_lit("%*"fmt)+1 : 0])
306#define FMT_PRECISION(def_prec) \
307 ((flags & BIT_OF(LEFT)) ? (1) : \
308 (precision <= 0) ? (def_prec) : (precision))
309#define FMT(def_pad, def_prec, fmt, val) \
311 precision = FMT_PRECISION(def_prec); \
314 rb_str_set_len(ftime, len); \
315 rb_str_catf(ftime, FMT_PADDING(fmt, def_pad), \
317 RSTRING_GETMEM(ftime, s, len); \
318 endp = (start = s) + rb_str_capacity(ftime); \
321#define STRFTIME(fmt) \
324 rb_str_set_len(ftime, len); \
325 if (!rb_strftime_with_timespec(ftime, (fmt), rb_strlen_lit(fmt), \
326 enc, time, vtm, timev, ts, gmt, maxsize)) \
328 s = RSTRING_PTR(ftime); \
329 i = RSTRING_LEN(ftime) - len; \
330 endp = (start = s) + rb_str_capacity(ftime); \
332 if (i > 0) case_conv(s, i, flags); \
333 if (precision > i) {\
337 memmove(s + precision - i, s, i);\
338 memset(s, padding ? padding : ' ', precision - i); \
343#define FMTV(def_pad, def_prec, fmt, val) \
346 if (FIXNUM_P(tmp)) { \
347 FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
350 const int base = ((fmt[0] == 'x') ? 16 : \
351 (fmt[0] == 'o') ? 8 : \
353 precision = FMT_PRECISION(def_prec); \
354 if (!padding) padding = (def_pad); \
355 tmp = format_value(tmp, base); \
356 i = RSTRING_LEN(tmp); \
358 rb_str_set_len(ftime, s-start); \
359 rb_str_append(ftime, tmp); \
360 RSTRING_GETMEM(ftime, s, len); \
361 endp = (start = s) + rb_str_capacity(ftime); \
366 tp = memchr(format,
'%', format_end - format);
367 if (!tp) tp = format_end;
369 memcpy(s, format, tp - format);
372 if (format == format_end)
break;
381 if (++format >= format_end)
goto unknown;
389 if (flags & BIT_OF(CHCASE)) {
390 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
391 flags |= BIT_OF(UPPER);
396 i = 3, tp = days_l[
vtm->wday];
400 if (flags & BIT_OF(CHCASE)) {
401 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
402 flags |= BIT_OF(UPPER);
407 i = strlen(tp = days_l[
vtm->wday]);
414 if (flags & BIT_OF(CHCASE)) {
415 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
416 flags |= BIT_OF(UPPER);
418 if (
vtm->mon < 1 ||
vtm->mon > 12)
421 i = 3, tp = months_l[
vtm->mon-1];
425 if (flags & BIT_OF(CHCASE)) {
426 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
427 flags |= BIT_OF(UPPER);
429 if (
vtm->mon < 1 ||
vtm->mon > 12)
432 i = strlen(tp = months_l[
vtm->mon-1]);
436 STRFTIME(
"%a %b %e %H:%M:%S %Y");
440 i = range(1,
vtm->mday, 31);
441 FMT(
'0', 2,
"d", (
int)i);
445 i = range(0,
vtm->hour, 23);
446 FMT(
'0', 2,
"d", (
int)i);
450 i = range(0,
vtm->hour, 23);
455 FMT(
'0', 2,
"d", (
int)i);
459 i = range(1,
vtm->yday, 366);
460 FMT(
'0', 3,
"d", (
int)i);
464 i = range(1,
vtm->mon, 12);
465 FMT(
'0', 2,
"d", (
int)i);
469 i = range(0,
vtm->min, 59);
470 FMT(
'0', 2,
"d", (
int)i);
475 if ((*format ==
'p' && (flags & BIT_OF(CHCASE))) ||
476 (*format ==
'P' && !(flags & (BIT_OF(CHCASE)|BIT_OF(UPPER))))) {
477 flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
478 flags |= BIT_OF(LOWER);
480 i = range(0,
vtm->hour, 23);
490 time_t sec = ts->tv_sec;
492 FMT(
'0', 1, PRI_TIMET_PREFIX
"d", sec);
494 FMT(
'0', 1, PRI_TIMET_PREFIX
"u", sec);
498 FMTV(
'0', 1,
"d", sec);
503 i = range(0,
vtm->sec, 60);
504 FMT(
'0', 2,
"d", (
int)i);
508 FMT(
'0', 2,
"d", weeknumber_v(
vtm, 0));
512 i = range(0,
vtm->wday, 6);
513 FMT(
'0', 1,
"d", (
int)i);
517 FMT(
'0', 2,
"d", weeknumber_v(
vtm, 1));
521 STRFTIME(
"%m/%d/%y");
525 STRFTIME(
"%H:%M:%S");
530 FMT(
'0', 2,
"d", (
int)i);
536 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
539 FMTV(
'0', 4,
"d",
vtm->year);
551 if (
off < 0 || (gmt && (flags & BIT_OF(LEFT)))) {
560 precision = precision <= 5 ? 2 : precision-3;
561 NEEDS(precision + 3);
565 precision = precision <= 6 ? 2 : precision-4;
566 NEEDS(precision + 4);
570 precision = precision <= 9 ? 2 : precision-7;
571 NEEDS(precision + 7);
575 if (
off % 3600 == 0) {
576 precision = precision <= 3 ? 2 : precision-1;
577 NEEDS(precision + 3);
579 else if (
off % 60 == 0) {
580 precision = precision <= 6 ? 2 : precision-4;
581 NEEDS(precision + 4);
584 precision = precision <= 9 ? 2 : precision-7;
585 NEEDS(precision + 9);
593 i = snprintf(s, endp - s, (padding ==
' ' ?
"%+*ld" :
"%+.*ld"),
594 precision + (padding ==
' '), sign * (
off / 3600));
596 if (sign < 0 &&
off < 3600) {
597 *(padding ==
' ' ? s + i - 2 : s) =
'-';
601 if (colons == 3 &&
off == 0)
605 i = snprintf(s, endp - s,
"%02d", (
int)(
off / 60));
609 if (colons == 3 &&
off == 0)
613 i = snprintf(s, endp - s,
"%02d", (
int)
off);
621 if (flags & BIT_OF(CHCASE)) {
622 flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
623 flags |= BIT_OF(LOWER);
635 zone = rb_time_zone_abbreviation(
vtm->zone, time);
637 tp = RSTRING_PTR(zone);
639 for (i = 0; i < TBUFSIZE && tp[i]; i++) {
640 if ((
unsigned char)tp[i] > 0x7F) {
642 i = strlcpy(tbuf, RSTRING_PTR(str), TBUFSIZE);
665 STRFTIME(
"%m/%d/%y");
669 FMT(
' ', 2,
"d", range(1,
vtm->mday, 31));
673 STRFTIME(
"%I:%M:%S %p");
681 STRFTIME(
"%H:%M:%S");
687 i = range(0,
vtm->hour, 23);
688 FMT(
' ', 2,
"d", (
int)i);
692 i = range(0,
vtm->hour, 23);
697 FMT(
' ', 2,
"d", (
int)i);
704 STRFTIME(
"%e-%^b-%4Y");
711 FMTV(
'0', 2,
"d", div(
vtm->year,
INT2FIX(100)));
716 if (!format[1] || !strchr(
"cCxXyY", format[1]))
721 if (!format[1] || !strchr(
"deHkIlmMSuUVwWy", format[1]))
726 FMT(
'0', 2,
"d", iso8601wknum_v(
vtm));
731 FMT(
'0', 1,
"d",
vtm->wday == 0 ? 7 :
vtm->wday);
749 w = iso8601wknum_v(
vtm);
750 if (
vtm->mon == 12 && w == 1)
752 else if (
vtm->mon == 1 && w >= 52)
755 if (*format ==
'G') {
758 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
761 FMTV(
'0', 4,
"d", yv);
767 FMT(
'0', 2,
"ld", y);
790 if (precision <= 0) {
796 long subsec = ts->tv_nsec;
798 snprintf(s, endp - s,
"%09ld", subsec);
799 memset(s+9,
'0', precision-9);
804 for (i = 0; i < 9-precision; i++)
806 snprintf(s, endp - s,
"%0*ld", precision, subsec);
817 subsec = mul(subsec,
INT2FIX(1000000000));
824 subsec = mul(subsec,
INT2FIX(n));
825 subsec = div(subsec,
INT2FIX(1));
828 (void)snprintf(s, endp - s,
"%0*ld", precision,
FIX2LONG(subsec));
832 VALUE args[2], result;
836 rb_fstring_lit(
"%0*d"));
844 STRFTIME(
"%Y-%m-%d");
849 flags |= BIT_OF(LEFT);
850 padding = precision = 0;
855 flags |= BIT_OF(UPPER);
860 flags |= BIT_OF(CHCASE);
869 for (colons = 1; colons <= 3; ++colons) {
870 if (format+colons >= format_end)
goto unknown;
871 if (format[colons] ==
'z')
break;
872 if (format[colons] !=
':')
goto unknown;
874 format += colons - 1;
879 case '1':
case '2':
case '3':
case '4':
880 case '5':
case '6':
case '7':
case '8':
case '9':
884 unsigned long u = ruby_scan_digits(format, format_end-format, 10, &n, &ov);
885 if (ov || u > INT_MAX)
goto unknown;
904 s = case_conv(s, i, flags);
907 if (format != format_end) {
911 rb_str_set_len(ftime,
len);
912 rb_str_resize(ftime,
len);
920strftime_size_limit(
size_t format_len)
922 size_t limit = format_len * (1*1024*1024);
923 if (limit < format_len) limit = format_len;
924 else if (limit < 1024) limit = 1024;
929rb_strftime(
const char *format,
size_t format_len, rb_encoding *enc,
932 VALUE result = rb_enc_str_new(0, 0, enc);
933 return rb_strftime_with_timespec(result, format, format_len, enc,
934 time,
vtm, timev, NULL, gmt,
935 strftime_size_limit(format_len));
939rb_strftime_timespec(
const char *format,
size_t format_len, rb_encoding *enc,
942 VALUE result = rb_enc_str_new(0, 0, enc);
943 return rb_strftime_with_timespec(result, format, format_len, enc,
945 strftime_size_limit(format_len));
950rb_strftime_limit(
const char *format,
size_t format_len, rb_encoding *enc,
952 int gmt,
size_t maxsize)
954 VALUE result = rb_enc_str_new(0, 0, enc);
955 return rb_strftime_with_timespec(result, format, format_len, enc,
956 time,
vtm,
Qnil, ts, gmt, maxsize);
965 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
970vtm2tm_noyear(
const struct vtm *
vtm,
struct tm *result)
977 tm.tm_mon =
vtm->mon-1;
978 tm.tm_mday =
vtm->mday;
979 tm.tm_hour =
vtm->hour;
980 tm.tm_min =
vtm->min;
981 tm.tm_sec =
vtm->sec;
982 tm.tm_wday =
vtm->wday;
983 tm.tm_yday =
vtm->yday-1;
984 tm.tm_isdst =
vtm->isdst;
985#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
988#if defined(HAVE_TM_ZONE)
989 tm.tm_zone = (
char *)
vtm->zone;
998iso8601wknum(
const struct tm *timeptr)
1014 int weeknum, jan1day;
1017 weeknum = weeknumber(timeptr, 1);
1032 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
1061#ifdef USE_BROKEN_XPG4
1069 dec31ly.tm_mon = 11;
1070 dec31ly.tm_mday = 31;
1071 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
1072 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
1073 weeknum = iso8601wknum(& dec31ly);
1079 if (timeptr->tm_mon == 11) {
1093 wday = timeptr->tm_wday;
1094 mday = timeptr->tm_mday;
1095 if ( (wday == 1 && (mday >= 29 && mday <= 31))
1096 || (wday == 2 && (mday == 30 || mday == 31))
1097 || (wday == 3 && mday == 31))
1105iso8601wknum_v(
const struct vtm *
vtm)
1108 vtm2tm_noyear(
vtm, &tm);
1109 return iso8601wknum(&tm);
1119weeknumber(
const struct tm *timeptr,
int firstweekday)
1121 int wday = timeptr->tm_wday;
1124 if (firstweekday == 1) {
1130 ret = ((timeptr->tm_yday + 7 - wday) / 7);
1137weeknumber_v(
const struct vtm *
vtm,
int firstweekday)
1140 vtm2tm_noyear(
vtm, &tm);
1141 return weeknumber(&tm, firstweekday);
1147Date: Wed, 24 Apr 91 20:54:08 MDT
1148From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
1149To: arnold@audiofax.com
1152in a process of fixing of strftime() in libraries on Atari ST I grabbed
1153some pieces of code from your own strftime. When doing that it came
1154to mind that your weeknumber() function compiles a little bit nicer
1155in the following form:
1160 return (timeptr->tm_yday - timeptr->tm_wday +
1161 (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
1163How nicer it depends on a compiler, of course, but always a tiny bit.
1167 ntomczak@vm.ucs.ualberta.ca
1202#include <sys/time.h>
1211static char *array[] =
1213 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
1214 "(%%B) full month name, var length (January..December) %B",
1216 "(%%D) date (%%m/%%d/%%y) %D",
1217 "(%%E) Locale extensions (ignored) %E",
1218 "(%%H) hour (24-hour clock, 00..23) %H",
1219 "(%%I) hour (12-hour clock, 01..12) %I",
1220 "(%%M) minute (00..59) %M",
1221 "(%%O) Locale extensions (ignored) %O",
1222 "(%%R) time, 24-hour (%%H:%%M) %R",
1223 "(%%S) second (00..60) %S",
1224 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
1225 "(%%U) week of year, Sunday as first day of week (00..53) %U",
1226 "(%%V) week of year according to ISO 8601 %V",
1227 "(%%W) week of year, Monday as first day of week (00..53) %W",
1228 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
1229 "(%%Y) year with century (1970...) %Y",
1230 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
1231 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
1232 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
1233 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
1234 "(%%d) day of the month (01..31) %d",
1235 "(%%e) day of the month, blank-padded ( 1..31) %e",
1236 "(%%h) should be same as (%%b) %h",
1237 "(%%j) day of the year (001..366) %j",
1238 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
1239 "(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
1240 "(%%m) month (01..12) %m",
1241 "(%%p) locale's AM or PM based on 12-hour clock %p",
1242 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
1243 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
1244 "(%%v) VMS date (dd-bbb-YYYY) %v",
1245 "(%%w) day of week (0..6, Sunday == 0) %w",
1246 "(%%x) appropriate locale date representation %x",
1247 "(%%y) last two digits of year (00..99) %y",
1248 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
1255main(
int argc,
char **argv)
1260 char string[MAXTIME];
1271 clock = time((
long *) 0);
1272 tm = localtime(&clock);
1274 for (k = 0; next = array[k]; k++) {
1275 length = strftime(
string, MAXTIME, next, tm);
1276 printf(
"%s\n",
string);
#define INT2FIX
Old name of RB_INT2FIX.
#define ISUPPER
Old name of rb_isupper.
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
#define FIX2INT
Old name of RB_FIX2INT.
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
#define TOUPPER
Old name of rb_toupper.
#define ISLOWER
Old name of rb_islower.
#define TOLOWER
Old name of rb_tolower.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
void rb_syserr_fail_str(int e, VALUE mesg)
Identical to rb_syserr_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_Integer(VALUE val)
This is the logic behind Kernel#Integer.
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
Identical to rb_str_conv_enc(), except it additionally takes IO encoder options.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
int off
Offset inside of ptr.
int len
Length of the buffer.
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Formats a string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
uintptr_t VALUE
Type that represents a Ruby object.