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 need =
len + n * 2;
176 while (nlen < need) nlen <<= 1;
178 if (nlen < len || nlen > maxsize) {
183 s = RSTRING_PTR(ftime);
190buffer_size_check(
const char *s,
191 const char *format_end,
size_t format_len,
195 const char *format = format_end-format_len;
196 VALUE fmt = rb_enc_str_new(format, format_len, enc);
202case_conv(
char *s, ptrdiff_t i,
int flags)
204 switch (flags & (BIT_OF(UPPER)|BIT_OF(LOWER))) {
223format_value(
VALUE val,
int base)
225 if (!RB_BIGNUM_TYPE_P(val))
227 return rb_big2str(val, base);
236rb_strftime_with_timespec(
VALUE ftime,
const char *format,
size_t format_len,
240 size_t len = RSTRING_LEN(ftime);
241 char *s = RSTRING_PTR(ftime);
242 const char *start = s;
244 const char *
const format_end = format + format_len;
247 auto char tbuf[TBUFSIZE];
252 int precision, flags, colons;
260 static const char days_l[][10] = {
261 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
262 "Thursday",
"Friday",
"Saturday",
264 static const char months_l[][10] = {
265 "January",
"February",
"March",
"April",
266 "May",
"June",
"July",
"August",
"September",
267 "October",
"November",
"December",
269 static const char ampm[][3] = {
"AM",
"PM", };
271 if (format == NULL || format_len == 0 ||
vtm == NULL) {
276 (rb_is_usascii_enc(enc) ||
277 rb_is_ascii8bit_enc(enc) ||
278 rb_is_locale_enc(enc))) {
283 for (; format < format_end; format++) {
284#define FLAG_FOUND() do { \
288#define NEEDS(n) do { \
289 if (s >= endp || (n) >= endp - s - 1) { \
290 s = resize_buffer(ftime, s, &start, &endp, (n), maxsize); \
291 buffer_size_check(s, format_end, format_len, enc); \
294#define FILL_PADDING(i) do { \
295 if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \
297 memset(s, padding ? padding : ' ', precision - (i)); \
298 s += precision - (i); \
304#define FMT_PADDING(fmt, def_pad) \
305 (&"%*"fmt"\0""%0*"fmt[\
306 (padding == '0' || (!padding && (def_pad) == '0')) ? \
307 rb_strlen_lit("%*"fmt)+1 : 0])
308#define FMT_PRECISION(def_prec) \
309 ((flags & BIT_OF(LEFT)) ? (1) : \
310 (precision <= 0) ? (def_prec) : (precision))
311#define FMT(def_pad, def_prec, fmt, val) \
313 precision = FMT_PRECISION(def_prec); \
316 rb_str_set_len(ftime, len); \
317 rb_str_catf(ftime, FMT_PADDING(fmt, def_pad), \
319 RSTRING_GETMEM(ftime, s, len); \
320 endp = (start = s) + rb_str_capacity(ftime); \
323#define STRFTIME(fmt) \
326 rb_str_set_len(ftime, len); \
327 if (!rb_strftime_with_timespec(ftime, (fmt), rb_strlen_lit(fmt), \
328 enc, time, vtm, timev, ts, gmt, maxsize)) \
330 s = RSTRING_PTR(ftime); \
331 i = RSTRING_LEN(ftime) - len; \
332 endp = (start = s) + rb_str_capacity(ftime); \
334 if (i > 0) case_conv(s, i, flags); \
335 if (precision > i) {\
339 memmove(s + precision - i, s, i);\
340 memset(s, padding ? padding : ' ', precision - i); \
345#define FMTV(def_pad, def_prec, fmt, val) \
348 if (FIXNUM_P(tmp)) { \
349 FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
352 const int base = ((fmt[0] == 'x') ? 16 : \
353 (fmt[0] == 'o') ? 8 : \
355 precision = FMT_PRECISION(def_prec); \
356 if (!padding) padding = (def_pad); \
357 tmp = format_value(tmp, base); \
358 i = RSTRING_LEN(tmp); \
360 rb_str_set_len(ftime, s-start); \
361 rb_str_append(ftime, tmp); \
362 RSTRING_GETMEM(ftime, s, len); \
363 endp = (start = s) + rb_str_capacity(ftime); \
368 tp = memchr(format,
'%', format_end - format);
369 if (!tp) tp = format_end;
371 memcpy(s, format, tp - format);
374 if (format == format_end)
break;
383 if (++format >= format_end)
goto unknown;
391 if (flags & BIT_OF(CHCASE)) {
392 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
393 flags |= BIT_OF(UPPER);
398 i = 3, tp = days_l[
vtm->wday];
402 if (flags & BIT_OF(CHCASE)) {
403 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
404 flags |= BIT_OF(UPPER);
409 i = strlen(tp = days_l[
vtm->wday]);
416 if (flags & BIT_OF(CHCASE)) {
417 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
418 flags |= BIT_OF(UPPER);
420 if (
vtm->mon < 1 ||
vtm->mon > 12)
423 i = 3, tp = months_l[
vtm->mon-1];
427 if (flags & BIT_OF(CHCASE)) {
428 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
429 flags |= BIT_OF(UPPER);
431 if (
vtm->mon < 1 ||
vtm->mon > 12)
434 i = strlen(tp = months_l[
vtm->mon-1]);
438 STRFTIME(
"%a %b %e %H:%M:%S %Y");
442 i = range(1,
vtm->mday, 31);
443 FMT(
'0', 2,
"d", (
int)i);
447 i = range(0,
vtm->hour, 23);
448 FMT(
'0', 2,
"d", (
int)i);
452 i = range(0,
vtm->hour, 23);
457 FMT(
'0', 2,
"d", (
int)i);
461 i = range(1,
vtm->yday, 366);
462 FMT(
'0', 3,
"d", (
int)i);
466 i = range(1,
vtm->mon, 12);
467 FMT(
'0', 2,
"d", (
int)i);
471 i = range(0,
vtm->min, 59);
472 FMT(
'0', 2,
"d", (
int)i);
477 if ((*format ==
'p' && (flags & BIT_OF(CHCASE))) ||
478 (*format ==
'P' && !(flags & (BIT_OF(CHCASE)|BIT_OF(UPPER))))) {
479 flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
480 flags |= BIT_OF(LOWER);
482 i = range(0,
vtm->hour, 23);
492 time_t sec = ts->tv_sec;
494 FMT(
'0', 1, PRI_TIMET_PREFIX
"d", sec);
496 FMT(
'0', 1, PRI_TIMET_PREFIX
"u", sec);
500 FMTV(
'0', 1,
"d", sec);
505 i = range(0,
vtm->sec, 60);
506 FMT(
'0', 2,
"d", (
int)i);
510 FMT(
'0', 2,
"d", weeknumber_v(
vtm, 0));
514 i = range(0,
vtm->wday, 6);
515 FMT(
'0', 1,
"d", (
int)i);
519 FMT(
'0', 2,
"d", weeknumber_v(
vtm, 1));
523 STRFTIME(
"%m/%d/%y");
527 STRFTIME(
"%H:%M:%S");
532 FMT(
'0', 2,
"d", (
int)i);
538 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
541 FMTV(
'0', 4,
"d",
vtm->year);
553 if (
off < 0 || (gmt && (flags & BIT_OF(LEFT)))) {
562 precision = precision <= 5 ? 2 : precision-3;
563 NEEDS(precision + 3);
567 precision = precision <= 6 ? 2 : precision-4;
568 NEEDS(precision + 4);
572 precision = precision <= 9 ? 2 : precision-7;
573 NEEDS(precision + 7);
577 if (
off % 3600 == 0) {
578 precision = precision <= 3 ? 2 : precision-1;
579 NEEDS(precision + 3);
581 else if (
off % 60 == 0) {
582 precision = precision <= 6 ? 2 : precision-4;
583 NEEDS(precision + 4);
586 precision = precision <= 9 ? 2 : precision-7;
587 NEEDS(precision + 9);
595 i = snprintf(s, endp - s, (padding ==
' ' ?
"%+*ld" :
"%+.*ld"),
596 precision + (padding ==
' '), sign * (
off / 3600));
598 if (sign < 0 &&
off < 3600) {
599 *(padding ==
' ' ? s + i - 2 : s) =
'-';
603 if (colons == 3 &&
off == 0)
607 i = snprintf(s, endp - s,
"%02d", (
int)(
off / 60));
611 if (colons == 3 &&
off == 0)
615 i = snprintf(s, endp - s,
"%02d", (
int)
off);
623 if (flags & BIT_OF(CHCASE)) {
624 flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
625 flags |= BIT_OF(LOWER);
637 zone = rb_time_zone_abbreviation(
vtm->zone, time);
639 tp = RSTRING_PTR(zone);
641 for (i = 0; i < TBUFSIZE && tp[i]; i++) {
642 if ((
unsigned char)tp[i] > 0x7F) {
644 i = strlcpy(tbuf, RSTRING_PTR(str), TBUFSIZE);
645 if (i >= TBUFSIZE) i = TBUFSIZE - 1;
668 STRFTIME(
"%m/%d/%y");
672 FMT(
' ', 2,
"d", range(1,
vtm->mday, 31));
676 STRFTIME(
"%I:%M:%S %p");
684 STRFTIME(
"%H:%M:%S");
690 i = range(0,
vtm->hour, 23);
691 FMT(
' ', 2,
"d", (
int)i);
695 i = range(0,
vtm->hour, 23);
700 FMT(
' ', 2,
"d", (
int)i);
707 STRFTIME(
"%e-%^b-%4Y");
714 FMTV(
'0', 2,
"d", div(
vtm->year,
INT2FIX(100)));
719 if (!format[1] || !strchr(
"cCxXyY", format[1]))
724 if (!format[1] || !strchr(
"deHkIlmMSuUVwWy", format[1]))
729 FMT(
'0', 2,
"d", iso8601wknum_v(
vtm));
734 FMT(
'0', 1,
"d",
vtm->wday == 0 ? 7 :
vtm->wday);
752 w = iso8601wknum_v(
vtm);
753 if (
vtm->mon == 12 && w == 1)
755 else if (
vtm->mon == 1 && w >= 52)
758 if (*format ==
'G') {
761 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
764 FMTV(
'0', 4,
"d", yv);
770 FMT(
'0', 2,
"ld", y);
793 if (precision <= 0) {
799 long subsec = ts->tv_nsec;
801 snprintf(s, endp - s,
"%09ld", subsec);
802 memset(s+9,
'0', precision-9);
807 for (i = 0; i < 9-precision; i++)
809 snprintf(s, endp - s,
"%0*ld", precision, subsec);
820 subsec = mul(subsec,
INT2FIX(1000000000));
827 subsec = mul(subsec,
INT2FIX(n));
828 subsec = div(subsec,
INT2FIX(1));
831 (void)snprintf(s, endp - s,
"%0*ld", precision,
FIX2LONG(subsec));
835 VALUE args[2], result;
839 rb_fstring_lit(
"%0*d"));
847 STRFTIME(
"%Y-%m-%d");
852 flags |= BIT_OF(LEFT);
853 padding = precision = 0;
858 flags |= BIT_OF(UPPER);
863 flags |= BIT_OF(CHCASE);
872 for (colons = 1; colons <= 3; ++colons) {
873 if (format+colons >= format_end)
goto unknown;
874 if (format[colons] ==
'z')
break;
875 if (format[colons] !=
':')
goto unknown;
877 format += colons - 1;
882 case '1':
case '2':
case '3':
case '4':
883 case '5':
case '6':
case '7':
case '8':
case '9':
887 unsigned long u = ruby_scan_digits(format, format_end-format, 10, &n, &ov);
888 if (ov || u > INT_MAX)
goto unknown;
907 s = case_conv(s, i, flags);
910 if (format != format_end) {
915 rb_str_resize(ftime,
len);
923strftime_size_limit(
size_t format_len)
925 size_t limit = format_len * (1*1024*1024);
926 if (limit < format_len) limit = format_len;
927 else if (limit < 1024) limit = 1024;
932rb_strftime(
const char *format,
size_t format_len,
rb_encoding *enc,
935 VALUE result = rb_enc_str_new(0, 0, enc);
937 return rb_strftime_with_timespec(result, format, format_len, enc,
938 time,
vtm, timev, NULL, gmt,
939 strftime_size_limit(format_len));
943rb_strftime_timespec(
const char *format,
size_t format_len,
rb_encoding *enc,
946 VALUE result = rb_enc_str_new(0, 0, enc);
948 return rb_strftime_with_timespec(result, format, format_len, enc,
950 strftime_size_limit(format_len));
955rb_strftime_limit(
const char *format,
size_t format_len,
rb_encoding *enc,
957 int gmt,
size_t maxsize)
959 VALUE result = rb_enc_str_new(0, 0, enc);
960 return rb_strftime_with_timespec(result, format, format_len, enc,
961 time,
vtm,
Qnil, ts, gmt, maxsize);
970 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
975vtm2tm_noyear(
const struct vtm *
vtm,
struct tm *result)
982 tm.tm_mon =
vtm->mon-1;
983 tm.tm_mday =
vtm->mday;
984 tm.tm_hour =
vtm->hour;
985 tm.tm_min =
vtm->min;
986 tm.tm_sec =
vtm->sec;
987 tm.tm_wday =
vtm->wday;
988 tm.tm_yday =
vtm->yday-1;
989 tm.tm_isdst =
vtm->isdst;
990#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
993#if defined(HAVE_TM_ZONE)
1003iso8601wknum(
const struct tm *timeptr)
1019 int weeknum, jan1day;
1022 weeknum = weeknumber(timeptr, 1);
1037 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
1066#ifdef USE_BROKEN_XPG4
1074 dec31ly.tm_mon = 11;
1075 dec31ly.tm_mday = 31;
1076 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
1077 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
1078 weeknum = iso8601wknum(& dec31ly);
1084 if (timeptr->tm_mon == 11) {
1098 wday = timeptr->tm_wday;
1099 mday = timeptr->tm_mday;
1100 if ( (wday == 1 && (mday >= 29 && mday <= 31))
1101 || (wday == 2 && (mday == 30 || mday == 31))
1102 || (wday == 3 && mday == 31))
1110iso8601wknum_v(
const struct vtm *
vtm)
1113 vtm2tm_noyear(
vtm, &tm);
1114 return iso8601wknum(&tm);
1124weeknumber(
const struct tm *timeptr,
int firstweekday)
1126 int wday = timeptr->tm_wday;
1129 if (firstweekday == 1) {
1135 ret = ((timeptr->tm_yday + 7 - wday) / 7);
1142weeknumber_v(
const struct vtm *
vtm,
int firstweekday)
1145 vtm2tm_noyear(
vtm, &tm);
1146 return weeknumber(&tm, firstweekday);
1152Date: Wed, 24 Apr 91 20:54:08 MDT
1153From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
1154To: arnold@audiofax.com
1157in a process of fixing of strftime() in libraries on Atari ST I grabbed
1158some pieces of code from your own strftime. When doing that it came
1159to mind that your weeknumber() function compiles a little bit nicer
1160in the following form:
1165 return (timeptr->tm_yday - timeptr->tm_wday +
1166 (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
1168How nicer it depends on a compiler, of course, but always a tiny bit.
1172 ntomczak@vm.ucs.ualberta.ca
1207#include <sys/time.h>
1216static char *array[] =
1218 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
1219 "(%%B) full month name, var length (January..December) %B",
1221 "(%%D) date (%%m/%%d/%%y) %D",
1222 "(%%E) Locale extensions (ignored) %E",
1223 "(%%H) hour (24-hour clock, 00..23) %H",
1224 "(%%I) hour (12-hour clock, 01..12) %I",
1225 "(%%M) minute (00..59) %M",
1226 "(%%O) Locale extensions (ignored) %O",
1227 "(%%R) time, 24-hour (%%H:%%M) %R",
1228 "(%%S) second (00..60) %S",
1229 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
1230 "(%%U) week of year, Sunday as first day of week (00..53) %U",
1231 "(%%V) week of year according to ISO 8601 %V",
1232 "(%%W) week of year, Monday as first day of week (00..53) %W",
1233 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
1234 "(%%Y) year with century (1970...) %Y",
1235 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
1236 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
1237 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
1238 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
1239 "(%%d) day of the month (01..31) %d",
1240 "(%%e) day of the month, blank-padded ( 1..31) %e",
1241 "(%%h) should be same as (%%b) %h",
1242 "(%%j) day of the year (001..366) %j",
1243 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
1244 "(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
1245 "(%%m) month (01..12) %m",
1246 "(%%p) locale's AM or PM based on 12-hour clock %p",
1247 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
1248 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
1249 "(%%v) VMS date (dd-bbb-YYYY) %v",
1250 "(%%w) day of week (0..6, Sunday == 0) %w",
1251 "(%%x) appropriate locale date representation %x",
1252 "(%%y) last two digits of year (00..99) %y",
1253 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
1260main(
int argc,
char **argv)
1265 char string[MAXTIME];
1276 clock = time((
long *) 0);
1277 tm = localtime(&clock);
1279 for (k = 0; next = array[k]; k++) {
1280 length = strftime(
string, MAXTIME, next, tm);
1281 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 ENC_CODERANGE_CLEAR(obj)
Old name of RB_ENC_CODERANGE_CLEAR.
#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_set_len(VALUE str, long len)
Overwrites the length of the 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.