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);
667 STRFTIME(
"%m/%d/%y");
671 FMT(
' ', 2,
"d", range(1,
vtm->mday, 31));
675 STRFTIME(
"%I:%M:%S %p");
683 STRFTIME(
"%H:%M:%S");
689 i = range(0,
vtm->hour, 23);
690 FMT(
' ', 2,
"d", (
int)i);
694 i = range(0,
vtm->hour, 23);
699 FMT(
' ', 2,
"d", (
int)i);
706 STRFTIME(
"%e-%^b-%4Y");
713 FMTV(
'0', 2,
"d", div(
vtm->year,
INT2FIX(100)));
718 if (!format[1] || !strchr(
"cCxXyY", format[1]))
723 if (!format[1] || !strchr(
"deHkIlmMSuUVwWy", format[1]))
728 FMT(
'0', 2,
"d", iso8601wknum_v(
vtm));
733 FMT(
'0', 1,
"d",
vtm->wday == 0 ? 7 :
vtm->wday);
751 w = iso8601wknum_v(
vtm);
752 if (
vtm->mon == 12 && w == 1)
754 else if (
vtm->mon == 1 && w >= 52)
757 if (*format ==
'G') {
760 FMT(
'0', 0 <= y ? 4 : 5,
"ld", y);
763 FMTV(
'0', 4,
"d", yv);
769 FMT(
'0', 2,
"ld", y);
792 if (precision <= 0) {
798 long subsec = ts->tv_nsec;
800 snprintf(s, endp - s,
"%09ld", subsec);
801 memset(s+9,
'0', precision-9);
806 for (i = 0; i < 9-precision; i++)
808 snprintf(s, endp - s,
"%0*ld", precision, subsec);
819 subsec = mul(subsec,
INT2FIX(1000000000));
826 subsec = mul(subsec,
INT2FIX(n));
827 subsec = div(subsec,
INT2FIX(1));
830 (void)snprintf(s, endp - s,
"%0*ld", precision,
FIX2LONG(subsec));
834 VALUE args[2], result;
838 rb_fstring_lit(
"%0*d"));
846 STRFTIME(
"%Y-%m-%d");
851 flags |= BIT_OF(LEFT);
852 padding = precision = 0;
857 flags |= BIT_OF(UPPER);
862 flags |= BIT_OF(CHCASE);
871 for (colons = 1; colons <= 3; ++colons) {
872 if (format+colons >= format_end)
goto unknown;
873 if (format[colons] ==
'z')
break;
874 if (format[colons] !=
':')
goto unknown;
876 format += colons - 1;
881 case '1':
case '2':
case '3':
case '4':
882 case '5':
case '6':
case '7':
case '8':
case '9':
886 unsigned long u = ruby_scan_digits(format, format_end-format, 10, &n, &ov);
887 if (ov || u > INT_MAX)
goto unknown;
906 s = case_conv(s, i, flags);
909 if (format != format_end) {
914 rb_str_resize(ftime,
len);
922strftime_size_limit(
size_t format_len)
924 size_t limit = format_len * (1*1024*1024);
925 if (limit < format_len) limit = format_len;
926 else if (limit < 1024) limit = 1024;
931rb_strftime(
const char *format,
size_t format_len,
rb_encoding *enc,
934 VALUE result = rb_enc_str_new(0, 0, enc);
936 return rb_strftime_with_timespec(result, format, format_len, enc,
937 time,
vtm, timev, NULL, gmt,
938 strftime_size_limit(format_len));
942rb_strftime_timespec(
const char *format,
size_t format_len,
rb_encoding *enc,
945 VALUE result = rb_enc_str_new(0, 0, enc);
947 return rb_strftime_with_timespec(result, format, format_len, enc,
949 strftime_size_limit(format_len));
954rb_strftime_limit(
const char *format,
size_t format_len,
rb_encoding *enc,
956 int gmt,
size_t maxsize)
958 VALUE result = rb_enc_str_new(0, 0, enc);
959 return rb_strftime_with_timespec(result, format, format_len, enc,
960 time,
vtm,
Qnil, ts, gmt, maxsize);
969 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
974vtm2tm_noyear(
const struct vtm *
vtm,
struct tm *result)
981 tm.tm_mon =
vtm->mon-1;
982 tm.tm_mday =
vtm->mday;
983 tm.tm_hour =
vtm->hour;
984 tm.tm_min =
vtm->min;
985 tm.tm_sec =
vtm->sec;
986 tm.tm_wday =
vtm->wday;
987 tm.tm_yday =
vtm->yday-1;
988 tm.tm_isdst =
vtm->isdst;
989#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
992#if defined(HAVE_TM_ZONE)
1002iso8601wknum(
const struct tm *timeptr)
1018 int weeknum, jan1day;
1021 weeknum = weeknumber(timeptr, 1);
1036 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
1065#ifdef USE_BROKEN_XPG4
1073 dec31ly.tm_mon = 11;
1074 dec31ly.tm_mday = 31;
1075 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
1076 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
1077 weeknum = iso8601wknum(& dec31ly);
1083 if (timeptr->tm_mon == 11) {
1097 wday = timeptr->tm_wday;
1098 mday = timeptr->tm_mday;
1099 if ( (wday == 1 && (mday >= 29 && mday <= 31))
1100 || (wday == 2 && (mday == 30 || mday == 31))
1101 || (wday == 3 && mday == 31))
1109iso8601wknum_v(
const struct vtm *
vtm)
1112 vtm2tm_noyear(
vtm, &tm);
1113 return iso8601wknum(&tm);
1123weeknumber(
const struct tm *timeptr,
int firstweekday)
1125 int wday = timeptr->tm_wday;
1128 if (firstweekday == 1) {
1134 ret = ((timeptr->tm_yday + 7 - wday) / 7);
1141weeknumber_v(
const struct vtm *
vtm,
int firstweekday)
1144 vtm2tm_noyear(
vtm, &tm);
1145 return weeknumber(&tm, firstweekday);
1151Date: Wed, 24 Apr 91 20:54:08 MDT
1152From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
1153To: arnold@audiofax.com
1156in a process of fixing of strftime() in libraries on Atari ST I grabbed
1157some pieces of code from your own strftime. When doing that it came
1158to mind that your weeknumber() function compiles a little bit nicer
1159in the following form:
1164 return (timeptr->tm_yday - timeptr->tm_wday +
1165 (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
1167How nicer it depends on a compiler, of course, but always a tiny bit.
1171 ntomczak@vm.ucs.ualberta.ca
1206#include <sys/time.h>
1215static char *array[] =
1217 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
1218 "(%%B) full month name, var length (January..December) %B",
1220 "(%%D) date (%%m/%%d/%%y) %D",
1221 "(%%E) Locale extensions (ignored) %E",
1222 "(%%H) hour (24-hour clock, 00..23) %H",
1223 "(%%I) hour (12-hour clock, 01..12) %I",
1224 "(%%M) minute (00..59) %M",
1225 "(%%O) Locale extensions (ignored) %O",
1226 "(%%R) time, 24-hour (%%H:%%M) %R",
1227 "(%%S) second (00..60) %S",
1228 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
1229 "(%%U) week of year, Sunday as first day of week (00..53) %U",
1230 "(%%V) week of year according to ISO 8601 %V",
1231 "(%%W) week of year, Monday as first day of week (00..53) %W",
1232 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
1233 "(%%Y) year with century (1970...) %Y",
1234 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
1235 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
1236 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
1237 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
1238 "(%%d) day of the month (01..31) %d",
1239 "(%%e) day of the month, blank-padded ( 1..31) %e",
1240 "(%%h) should be same as (%%b) %h",
1241 "(%%j) day of the year (001..366) %j",
1242 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
1243 "(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
1244 "(%%m) month (01..12) %m",
1245 "(%%p) locale's AM or PM based on 12-hour clock %p",
1246 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
1247 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
1248 "(%%v) VMS date (dd-bbb-YYYY) %v",
1249 "(%%w) day of week (0..6, Sunday == 0) %w",
1250 "(%%x) appropriate locale date representation %x",
1251 "(%%y) last two digits of year (00..99) %y",
1252 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
1259main(
int argc,
char **argv)
1264 char string[MAXTIME];
1275 clock = time((
long *) 0);
1276 tm = localtime(&clock);
1278 for (k = 0; next = array[k]; k++) {
1279 length = strftime(
string, MAXTIME, next, tm);
1280 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.