Ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e0ba6b95ab71a441357ed5484e33498)
strftime.c
1/* -*- c-file-style: "linux" -*- */
2
3/*
4 * strftime.c
5 *
6 * Public-domain implementation of ANSI C library routine.
7 *
8 * It's written in old-style C for maximal portability.
9 * However, since I'm used to prototypes, I've included them too.
10 *
11 * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
12 * For extensions from SunOS, add SUNOS_EXT.
13 * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
14 * For VMS dates, add VMS_EXT.
15 * For a an RFC822 time format, add MAILHEADER_EXT.
16 * For ISO week years, add ISO_DATE_EXT.
17 * For complete POSIX semantics, add POSIX_SEMANTICS.
18 *
19 * The code for %c, %x, and %X now follows the 1003.2 specification for
20 * the POSIX locale.
21 * This version ignores LOCALE information.
22 * It also doesn't worry about multi-byte characters.
23 * So there.
24 *
25 * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
26 * code are included if GAWK is defined.
27 *
28 * Arnold Robbins
29 * January, February, March, 1991
30 * Updated March, April 1992
31 * Updated April, 1993
32 * Updated February, 1994
33 * Updated May, 1994
34 * Updated January, 1995
35 * Updated September, 1995
36 * Updated January, 1996
37 *
38 * Fixes from ado@elsie.nci.nih.gov
39 * February 1991, May 1992
40 * Fixes from Tor Lillqvist tml@tik.vtt.fi
41 * May, 1993
42 * Further fixes from ado@elsie.nci.nih.gov
43 * February 1994
44 * %z code from chip@chinacat.unicom.com
45 * Applied September 1995
46 * %V code fixed (again) and %G, %g added,
47 * January 1996
48 */
49
50#include "ruby/internal/config.h"
51
52#ifndef GAWK
53#include <stdio.h>
54#include <ctype.h>
55#include <string.h>
56#include <time.h>
57#include <sys/types.h>
58#include <errno.h>
59#endif
60#if defined(TM_IN_SYS_TIME) || !defined(GAWK)
61#include <sys/types.h>
62#ifdef HAVE_SYS_TIME_H
63#include <sys/time.h>
64#endif
65#endif
66#include <math.h>
67
68#include "internal.h"
69#include "internal/string.h"
70#include "internal/vm.h"
71#include "ruby/encoding.h"
72#include "ruby/ruby.h"
73#include "ruby/util.h"
74#include "timev.h"
75
76/* defaults: season to taste */
77#define SYSV_EXT 1 /* stuff in System V ascftime routine */
78#define SUNOS_EXT 1 /* stuff in SunOS strftime routine */
79#define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */
80#define VMS_EXT 1 /* include %v for VMS date format */
81#define MAILHEADER_EXT 1 /* add %z for HHMM format */
82#define ISO_DATE_EXT 1 /* %G and %g for year of ISO week */
83
84#if defined(ISO_DATE_EXT)
85#if ! defined(POSIX2_DATE)
86#define POSIX2_DATE 1
87#endif
88#endif
89
90#if defined(POSIX2_DATE)
91#if ! defined(SYSV_EXT)
92#define SYSV_EXT 1
93#endif
94#if ! defined(SUNOS_EXT)
95#define SUNOS_EXT 1
96#endif
97#endif
98
99#if defined(POSIX2_DATE)
100#define adddecl(stuff) stuff
101#else
102#define adddecl(stuff)
103#endif
104
105#undef strchr /* avoid AIX weirdness */
106
107#if !defined __STDC__ && !defined _WIN32
108#define const
109static int weeknumber();
110adddecl(static int iso8601wknum();)
111static int weeknumber_v();
112adddecl(static int iso8601wknum_v();)
113#else
114static int weeknumber(const struct tm *timeptr, int firstweekday);
115adddecl(static int iso8601wknum(const struct tm *timeptr);)
116static int weeknumber_v(const struct vtm *vtm, int firstweekday);
117adddecl(static int iso8601wknum_v(const struct vtm *vtm);)
118#endif
119
120#ifdef STDC_HEADERS
121#include <stdlib.h>
122#include <string.h>
123#else
124extern void *malloc();
125extern void *realloc();
126extern char *getenv();
127extern char *strchr();
128#endif
129
130#define range(low, item, hi) max((low), min((item), (hi)))
131
132#undef min /* just in case */
133
134/* min --- return minimum of two numbers */
135
136static inline int
137min(int a, int b)
138{
139 return (a < b ? a : b);
140}
141
142#undef max /* also, just in case */
143
144/* max --- return maximum of two numbers */
145
146static inline int
147max(int a, int b)
148{
149 return (a > b ? a : b);
150}
151
152#ifdef NO_STRING_LITERAL_CONCATENATION
153#error No string literal concatenation
154#endif
155
156#define add(x,y) (rb_funcall((x), '+', 1, (y)))
157#define sub(x,y) (rb_funcall((x), '-', 1, (y)))
158#define mul(x,y) (rb_funcall((x), '*', 1, (y)))
159#define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
160#define div(x,y) (rb_funcall((x), rb_intern("div"), 1, (y)))
161#define mod(x,y) (rb_funcall((x), '%', 1, (y)))
162
163/* strftime --- produce formatted time */
164
165enum {LEFT, CHCASE, LOWER, UPPER};
166#define BIT_OF(n) (1U<<(n))
167
168static char *
169resize_buffer(VALUE ftime, char *s, const char **start, const char **endp,
170 ptrdiff_t n, size_t maxsize)
171{
172 size_t len = s - *start;
173 size_t nlen = len + n * 2;
174
175 if (nlen < len || nlen > maxsize) {
176 return 0;
177 }
178 rb_str_set_len(ftime, len);
179 rb_str_modify_expand(ftime, nlen-len);
180 s = RSTRING_PTR(ftime);
181 *endp = s + nlen;
182 *start = s;
183 return s += len;
184}
185
186static void
187buffer_size_check(const char *s,
188 const char *format_end, size_t format_len,
189 rb_encoding *enc)
190{
191 if (!s) {
192 const char *format = format_end-format_len;
193 VALUE fmt = rb_enc_str_new(format, format_len, enc);
194 rb_syserr_fail_str(ERANGE, fmt);
195 }
196}
197
198static char *
199case_conv(char *s, ptrdiff_t i, int flags)
200{
201 switch (flags & (BIT_OF(UPPER)|BIT_OF(LOWER))) {
202 case BIT_OF(UPPER):
203 do {
204 if (ISLOWER(*s)) *s = TOUPPER(*s);
205 } while (s++, --i);
206 break;
207 case BIT_OF(LOWER):
208 do {
209 if (ISUPPER(*s)) *s = TOLOWER(*s);
210 } while (s++, --i);
211 break;
212 default:
213 s += i;
214 break;
215 }
216 return s;
217}
218
219static VALUE
220format_value(VALUE val, int base)
221{
222 if (!RB_BIGNUM_TYPE_P(val))
223 val = rb_Integer(val);
224 return rb_big2str(val, base);
225}
226
227/*
228 * enc is the encoding of the format. It is used as the encoding of resulted
229 * string, but the name of the month and weekday are always US-ASCII. So it
230 * is only used for the timezone name on Windows.
231 */
232static VALUE
233rb_strftime_with_timespec(VALUE ftime, const char *format, size_t format_len,
234 rb_encoding *enc, VALUE time, const struct vtm *vtm,
235 VALUE timev, struct timespec *ts, int gmt, size_t maxsize)
236{
237 size_t len = RSTRING_LEN(ftime);
238 char *s = RSTRING_PTR(ftime);
239 const char *start = s;
240 const char *endp = start + rb_str_capacity(ftime);
241 const char *const format_end = format + format_len;
242 const char *sp, *tp;
243#define TBUFSIZE 100
244 auto char tbuf[TBUFSIZE];
245 long off;
246 ptrdiff_t i;
247 int w;
248 long y;
249 int precision, flags, colons;
250 char padding;
251#ifdef MAILHEADER_EXT
252 int sign;
253#endif
254 VALUE zone = Qnil;
255
256 /* various tables, useful in North America */
257 static const char days_l[][10] = {
258 "Sunday", "Monday", "Tuesday", "Wednesday",
259 "Thursday", "Friday", "Saturday",
260 };
261 static const char months_l[][10] = {
262 "January", "February", "March", "April",
263 "May", "June", "July", "August", "September",
264 "October", "November", "December",
265 };
266 static const char ampm[][3] = { "AM", "PM", };
267
268 if (format == NULL || format_len == 0 || vtm == NULL) {
269 goto err;
270 }
271
272 if (enc &&
273 (enc == rb_usascii_encoding() ||
274 enc == rb_ascii8bit_encoding() ||
275 enc == rb_locale_encoding())) {
276 enc = NULL;
277 }
278
279 s += len;
280 for (; format < format_end; format++) {
281#define FLAG_FOUND() do { \
282 if (precision > 0) \
283 goto unknown; \
284 } while (0)
285#define NEEDS(n) do { \
286 if (s >= endp || (n) >= endp - s - 1) { \
287 s = resize_buffer(ftime, s, &start, &endp, (n), maxsize); \
288 buffer_size_check(s, format_end, format_len, enc); \
289 } \
290 } while (0)
291#define FILL_PADDING(i) do { \
292 if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \
293 NEEDS(precision); \
294 memset(s, padding ? padding : ' ', precision - (i)); \
295 s += precision - (i); \
296 } \
297 else { \
298 NEEDS(i); \
299 } \
300} while (0);
301#define FMT_PADDING(fmt, def_pad) \
302 (&"%*"fmt"\0""%0*"fmt[\
303 (padding == '0' || (!padding && (def_pad) == '0')) ? \
304 rb_strlen_lit("%*"fmt)+1 : 0])
305#define FMT_PRECISION(def_prec) \
306 ((flags & BIT_OF(LEFT)) ? (1) : \
307 (precision <= 0) ? (def_prec) : (precision))
308#define FMT(def_pad, def_prec, fmt, val) \
309 do { \
310 precision = FMT_PRECISION(def_prec); \
311 len = s - start; \
312 NEEDS(precision); \
313 rb_str_set_len(ftime, len); \
314 rb_str_catf(ftime, FMT_PADDING(fmt, def_pad), \
315 precision, (val)); \
316 RSTRING_GETMEM(ftime, s, len); \
317 endp = (start = s) + rb_str_capacity(ftime); \
318 s += len; \
319 } while (0)
320#define STRFTIME(fmt) \
321 do { \
322 len = s - start; \
323 rb_str_set_len(ftime, len); \
324 if (!rb_strftime_with_timespec(ftime, (fmt), rb_strlen_lit(fmt), \
325 enc, time, vtm, timev, ts, gmt, maxsize)) \
326 return 0; \
327 s = RSTRING_PTR(ftime); \
328 i = RSTRING_LEN(ftime) - len; \
329 endp = (start = s) + rb_str_capacity(ftime); \
330 s += len; \
331 if (i > 0) case_conv(s, i, flags); \
332 if (precision > i) {\
333 s += i; \
334 NEEDS(precision); \
335 s -= i; \
336 memmove(s + precision - i, s, i);\
337 memset(s, padding ? padding : ' ', precision - i); \
338 s += precision; \
339 } \
340 else s += i; \
341 } while (0)
342#define FMTV(def_pad, def_prec, fmt, val) \
343 do { \
344 VALUE tmp = (val); \
345 if (FIXNUM_P(tmp)) { \
346 FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
347 } \
348 else { \
349 const int base = ((fmt[0] == 'x') ? 16 : \
350 (fmt[0] == 'o') ? 8 : \
351 10); \
352 precision = FMT_PRECISION(def_prec); \
353 if (!padding) padding = (def_pad); \
354 tmp = format_value(tmp, base); \
355 i = RSTRING_LEN(tmp); \
356 FILL_PADDING(i); \
357 rb_str_set_len(ftime, s-start); \
358 rb_str_append(ftime, tmp); \
359 RSTRING_GETMEM(ftime, s, len); \
360 endp = (start = s) + rb_str_capacity(ftime); \
361 s += len; \
362 } \
363 } while (0)
364
365 tp = memchr(format, '%', format_end - format);
366 if (!tp) tp = format_end;
367 NEEDS(tp - format);
368 memcpy(s, format, tp - format);
369 s += tp - format;
370 format = tp;
371 if (format == format_end) break;
372
373 tp = tbuf;
374 sp = format;
375 precision = -1;
376 flags = 0;
377 padding = 0;
378 colons = 0;
379 again:
380 if (++format >= format_end) goto unknown;
381 switch (*format) {
382 case '%':
383 FILL_PADDING(1);
384 *s++ = '%';
385 continue;
386
387 case 'a': /* abbreviated weekday name */
388 if (flags & BIT_OF(CHCASE)) {
389 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
390 flags |= BIT_OF(UPPER);
391 }
392 if (vtm->wday > 6)
393 i = 1, tp = "?";
394 else
395 i = 3, tp = days_l[vtm->wday];
396 break;
397
398 case 'A': /* full weekday name */
399 if (flags & BIT_OF(CHCASE)) {
400 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
401 flags |= BIT_OF(UPPER);
402 }
403 if (vtm->wday > 6)
404 i = 1, tp = "?";
405 else
406 i = strlen(tp = days_l[vtm->wday]);
407 break;
408
409#ifdef SYSV_EXT
410 case 'h': /* abbreviated month name */
411#endif
412 case 'b': /* abbreviated month name */
413 if (flags & BIT_OF(CHCASE)) {
414 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
415 flags |= BIT_OF(UPPER);
416 }
417 if (vtm->mon < 1 || vtm->mon > 12)
418 i = 1, tp = "?";
419 else
420 i = 3, tp = months_l[vtm->mon-1];
421 break;
422
423 case 'B': /* full month name */
424 if (flags & BIT_OF(CHCASE)) {
425 flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
426 flags |= BIT_OF(UPPER);
427 }
428 if (vtm->mon < 1 || vtm->mon > 12)
429 i = 1, tp = "?";
430 else
431 i = strlen(tp = months_l[vtm->mon-1]);
432 break;
433
434 case 'c': /* appropriate date and time representation */
435 STRFTIME("%a %b %e %H:%M:%S %Y");
436 continue;
437
438 case 'd': /* day of the month, 01 - 31 */
439 i = range(1, vtm->mday, 31);
440 FMT('0', 2, "d", (int)i);
441 continue;
442
443 case 'H': /* hour, 24-hour clock, 00 - 23 */
444 i = range(0, vtm->hour, 23);
445 FMT('0', 2, "d", (int)i);
446 continue;
447
448 case 'I': /* hour, 12-hour clock, 01 - 12 */
449 i = range(0, vtm->hour, 23);
450 if (i == 0)
451 i = 12;
452 else if (i > 12)
453 i -= 12;
454 FMT('0', 2, "d", (int)i);
455 continue;
456
457 case 'j': /* day of the year, 001 - 366 */
458 i = range(1, vtm->yday, 366);
459 FMT('0', 3, "d", (int)i);
460 continue;
461
462 case 'm': /* month, 01 - 12 */
463 i = range(1, vtm->mon, 12);
464 FMT('0', 2, "d", (int)i);
465 continue;
466
467 case 'M': /* minute, 00 - 59 */
468 i = range(0, vtm->min, 59);
469 FMT('0', 2, "d", (int)i);
470 continue;
471
472 case 'p': /* AM or PM based on 12-hour clock */
473 case 'P': /* am or pm based on 12-hour clock */
474 if ((*format == 'p' && (flags & BIT_OF(CHCASE))) ||
475 (*format == 'P' && !(flags & (BIT_OF(CHCASE)|BIT_OF(UPPER))))) {
476 flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
477 flags |= BIT_OF(LOWER);
478 }
479 i = range(0, vtm->hour, 23);
480 if (i < 12)
481 tp = ampm[0];
482 else
483 tp = ampm[1];
484 i = 2;
485 break;
486
487 case 's':
488 if (ts) {
489 time_t sec = ts->tv_sec;
490 if (~(time_t)0 <= 0)
491 FMT('0', 1, PRI_TIMET_PREFIX"d", sec);
492 else
493 FMT('0', 1, PRI_TIMET_PREFIX"u", sec);
494 }
495 else {
496 VALUE sec = div(timev, INT2FIX(1));
497 FMTV('0', 1, "d", sec);
498 }
499 continue;
500
501 case 'S': /* second, 00 - 60 */
502 i = range(0, vtm->sec, 60);
503 FMT('0', 2, "d", (int)i);
504 continue;
505
506 case 'U': /* week of year, Sunday is first day of week */
507 FMT('0', 2, "d", weeknumber_v(vtm, 0));
508 continue;
509
510 case 'w': /* weekday, Sunday == 0, 0 - 6 */
511 i = range(0, vtm->wday, 6);
512 FMT('0', 1, "d", (int)i);
513 continue;
514
515 case 'W': /* week of year, Monday is first day of week */
516 FMT('0', 2, "d", weeknumber_v(vtm, 1));
517 continue;
518
519 case 'x': /* appropriate date representation */
520 STRFTIME("%m/%d/%y");
521 continue;
522
523 case 'X': /* appropriate time representation */
524 STRFTIME("%H:%M:%S");
525 continue;
526
527 case 'y': /* year without a century, 00 - 99 */
528 i = NUM2INT(mod(vtm->year, INT2FIX(100)));
529 FMT('0', 2, "d", (int)i);
530 continue;
531
532 case 'Y': /* year with century */
533 if (FIXNUM_P(vtm->year)) {
534 long y = FIX2LONG(vtm->year);
535 FMT('0', 0 <= y ? 4 : 5, "ld", y);
536 }
537 else {
538 FMTV('0', 4, "d", vtm->year);
539 }
540 continue;
541
542#ifdef MAILHEADER_EXT
543 case 'z': /* time zone offset east of GMT e.g. -0600 */
544 if (gmt) {
545 off = 0;
546 }
547 else {
548 off = NUM2LONG(rb_funcall(vtm->utc_offset, rb_intern("round"), 0));
549 }
550 if (off < 0 || (gmt && (flags & BIT_OF(LEFT)))) {
551 off = -off;
552 sign = -1;
553 }
554 else {
555 sign = +1;
556 }
557 switch (colons) {
558 case 0: /* %z -> +hhmm */
559 precision = precision <= 5 ? 2 : precision-3;
560 NEEDS(precision + 3);
561 break;
562
563 case 1: /* %:z -> +hh:mm */
564 precision = precision <= 6 ? 2 : precision-4;
565 NEEDS(precision + 4);
566 break;
567
568 case 2: /* %::z -> +hh:mm:ss */
569 precision = precision <= 9 ? 2 : precision-7;
570 NEEDS(precision + 7);
571 break;
572
573 case 3: /* %:::z -> +hh[:mm[:ss]] */
574 if (off % 3600 == 0) {
575 precision = precision <= 3 ? 2 : precision-1;
576 NEEDS(precision + 3);
577 }
578 else if (off % 60 == 0) {
579 precision = precision <= 6 ? 2 : precision-4;
580 NEEDS(precision + 4);
581 }
582 else {
583 precision = precision <= 9 ? 2 : precision-7;
584 NEEDS(precision + 9);
585 }
586 break;
587
588 default:
589 format--;
590 goto unknown;
591 }
592 i = snprintf(s, endp - s, (padding == ' ' ? "%+*ld" : "%+.*ld"),
593 precision + (padding == ' '), sign * (off / 3600));
594 if (i < 0) goto err;
595 if (sign < 0 && off < 3600) {
596 *(padding == ' ' ? s + i - 2 : s) = '-';
597 }
598 s += i;
599 off = off % 3600;
600 if (colons == 3 && off == 0)
601 continue;
602 if (1 <= colons)
603 *s++ = ':';
604 i = snprintf(s, endp - s, "%02d", (int)(off / 60));
605 if (i < 0) goto err;
606 s += i;
607 off = off % 60;
608 if (colons == 3 && off == 0)
609 continue;
610 if (2 <= colons) {
611 *s++ = ':';
612 i = snprintf(s, endp - s, "%02d", (int)off);
613 if (i < 0) goto err;
614 s += i;
615 }
616 continue;
617#endif /* MAILHEADER_EXT */
618
619 case 'Z': /* time zone name or abbreviation */
620 if (flags & BIT_OF(CHCASE)) {
621 flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
622 flags |= BIT_OF(LOWER);
623 }
624 if (gmt) {
625 i = 3;
626 tp = "UTC";
627 break;
628 }
629 if (NIL_P(vtm->zone)) {
630 i = 0;
631 }
632 else {
633 if (NIL_P(zone)) {
634 zone = rb_time_zone_abbreviation(vtm->zone, time);
635 }
636 tp = RSTRING_PTR(zone);
637 if (enc) {
638 for (i = 0; i < TBUFSIZE && tp[i]; i++) {
639 if ((unsigned char)tp[i] > 0x7F) {
641 i = strlcpy(tbuf, RSTRING_PTR(str), TBUFSIZE);
642 tp = tbuf;
643 break;
644 }
645 }
646 }
647 else
648 i = strlen(tp);
649 }
650 break;
651
652#ifdef SYSV_EXT
653 case 'n': /* same as \n */
654 FILL_PADDING(1);
655 *s++ = '\n';
656 continue;
657
658 case 't': /* same as \t */
659 FILL_PADDING(1);
660 *s++ = '\t';
661 continue;
662
663 case 'D': /* date as %m/%d/%y */
664 STRFTIME("%m/%d/%y");
665 continue;
666
667 case 'e': /* day of month, blank padded */
668 FMT(' ', 2, "d", range(1, vtm->mday, 31));
669 continue;
670
671 case 'r': /* time as %I:%M:%S %p */
672 STRFTIME("%I:%M:%S %p");
673 continue;
674
675 case 'R': /* time as %H:%M */
676 STRFTIME("%H:%M");
677 continue;
678
679 case 'T': /* time as %H:%M:%S */
680 STRFTIME("%H:%M:%S");
681 continue;
682#endif
683
684#ifdef SUNOS_EXT
685 case 'k': /* hour, 24-hour clock, blank pad */
686 i = range(0, vtm->hour, 23);
687 FMT(' ', 2, "d", (int)i);
688 continue;
689
690 case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
691 i = range(0, vtm->hour, 23);
692 if (i == 0)
693 i = 12;
694 else if (i > 12)
695 i -= 12;
696 FMT(' ', 2, "d", (int)i);
697 continue;
698#endif
699
700
701#ifdef VMS_EXT
702 case 'v': /* date as dd-bbb-YYYY */
703 STRFTIME("%e-%^b-%4Y");
704 continue;
705#endif
706
707
708#ifdef POSIX2_DATE
709 case 'C':
710 FMTV('0', 2, "d", div(vtm->year, INT2FIX(100)));
711 continue;
712
713 case 'E':
714 /* POSIX locale extensions, ignored for now */
715 if (!format[1] || !strchr("cCxXyY", format[1]))
716 goto unknown;
717 goto again;
718 case 'O':
719 /* POSIX locale extensions, ignored for now */
720 if (!format[1] || !strchr("deHkIlmMSuUVwWy", format[1]))
721 goto unknown;
722 goto again;
723
724 case 'V': /* week of year according ISO 8601 */
725 FMT('0', 2, "d", iso8601wknum_v(vtm));
726 continue;
727
728 case 'u':
729 /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
730 FMT('0', 1, "d", vtm->wday == 0 ? 7 : vtm->wday);
731 continue;
732#endif /* POSIX2_DATE */
733
734#ifdef ISO_DATE_EXT
735 case 'G':
736 case 'g':
737 /*
738 * Year of ISO week.
739 *
740 * If it's December but the ISO week number is one,
741 * that week is in next year.
742 * If it's January but the ISO week number is 52 or
743 * 53, that week is in last year.
744 * Otherwise, it's this year.
745 */
746 {
747 VALUE yv = vtm->year;
748 w = iso8601wknum_v(vtm);
749 if (vtm->mon == 12 && w == 1)
750 yv = add(yv, INT2FIX(1));
751 else if (vtm->mon == 1 && w >= 52)
752 yv = sub(yv, INT2FIX(1));
753
754 if (*format == 'G') {
755 if (FIXNUM_P(yv)) {
756 const long y = FIX2LONG(yv);
757 FMT('0', 0 <= y ? 4 : 5, "ld", y);
758 }
759 else {
760 FMTV('0', 4, "d", yv);
761 }
762 }
763 else {
764 yv = mod(yv, INT2FIX(100));
765 y = FIX2LONG(yv);
766 FMT('0', 2, "ld", y);
767 }
768 continue;
769 }
770
771#endif /* ISO_DATE_EXT */
772
773
774 case 'L':
775 w = 3;
776 goto subsec;
777
778 case 'N':
779 /*
780 * fractional second digits. default is 9 digits
781 * (nanosecond).
782 *
783 * %3N millisecond (3 digits)
784 * %6N microsecond (6 digits)
785 * %9N nanosecond (9 digits)
786 */
787 w = 9;
788 subsec:
789 if (precision <= 0) {
790 precision = w;
791 }
792 NEEDS(precision);
793
794 if (ts) {
795 long subsec = ts->tv_nsec;
796 if (9 < precision) {
797 snprintf(s, endp - s, "%09ld", subsec);
798 memset(s+9, '0', precision-9);
799 s += precision;
800 }
801 else {
802 int i;
803 for (i = 0; i < 9-precision; i++)
804 subsec /= 10;
805 snprintf(s, endp - s, "%0*ld", precision, subsec);
806 s += precision;
807 }
808 }
809 else {
810 VALUE subsec = mod(timev, INT2FIX(1));
811 int ww;
812 long n;
813
814 ww = precision;
815 while (9 <= ww) {
816 subsec = mul(subsec, INT2FIX(1000000000));
817 ww -= 9;
818 }
819 n = 1;
820 for (; 0 < ww; ww--)
821 n *= 10;
822 if (n != 1)
823 subsec = mul(subsec, INT2FIX(n));
824 subsec = div(subsec, INT2FIX(1));
825
826 if (FIXNUM_P(subsec)) {
827 (void)snprintf(s, endp - s, "%0*ld", precision, FIX2LONG(subsec));
828 s += precision;
829 }
830 else {
831 VALUE args[2], result;
832 args[0] = INT2FIX(precision);
833 args[1] = subsec;
834 result = rb_str_format(2, args,
835 rb_fstring_lit("%0*d"));
836 (void)strlcpy(s, StringValueCStr(result), endp-s);
837 s += precision;
838 }
839 }
840 continue;
841
842 case 'F': /* Equivalent to %Y-%m-%d */
843 STRFTIME("%Y-%m-%d");
844 continue;
845
846 case '-':
847 FLAG_FOUND();
848 flags |= BIT_OF(LEFT);
849 padding = precision = 0;
850 goto again;
851
852 case '^':
853 FLAG_FOUND();
854 flags |= BIT_OF(UPPER);
855 goto again;
856
857 case '#':
858 FLAG_FOUND();
859 flags |= BIT_OF(CHCASE);
860 goto again;
861
862 case '_':
863 FLAG_FOUND();
864 padding = ' ';
865 goto again;
866
867 case ':':
868 for (colons = 1; colons <= 3; ++colons) {
869 if (format+colons >= format_end) goto unknown;
870 if (format[colons] == 'z') break;
871 if (format[colons] != ':') goto unknown;
872 }
873 format += colons - 1;
874 goto again;
875
876 case '0':
877 padding = '0';
878 case '1': case '2': case '3': case '4':
879 case '5': case '6': case '7': case '8': case '9':
880 {
881 size_t n;
882 int ov;
883 unsigned long u = ruby_scan_digits(format, format_end-format, 10, &n, &ov);
884 if (ov || u > INT_MAX) goto unknown;
885 precision = (int)u;
886 format += n - 1;
887 goto again;
888 }
889
890 default:
891 unknown:
892 i = format - sp + 1;
893 tp = sp;
894 precision = -1;
895 flags = 0;
896 padding = 0;
897 colons = 0;
898 break;
899 }
900 if (i) {
901 FILL_PADDING(i);
902 memcpy(s, tp, i);
903 s = case_conv(s, i, flags);
904 }
905 }
906 if (format != format_end) {
907 return 0;
908 }
909 len = s - start;
910 rb_str_set_len(ftime, len);
911 rb_str_resize(ftime, len);
912 return ftime;
913
914err:
915 return 0;
916}
917
918static size_t
919strftime_size_limit(size_t format_len)
920{
921 size_t limit = format_len * (1*1024*1024);
922 if (limit < format_len) limit = format_len;
923 else if (limit < 1024) limit = 1024;
924 return limit;
925}
926
927VALUE
928rb_strftime(const char *format, size_t format_len, rb_encoding *enc,
929 VALUE time, const struct vtm *vtm, VALUE timev, int gmt)
930{
931 VALUE result = rb_enc_str_new(0, 0, enc);
932 return rb_strftime_with_timespec(result, format, format_len, enc,
933 time, vtm, timev, NULL, gmt,
934 strftime_size_limit(format_len));
935}
936
937VALUE
938rb_strftime_timespec(const char *format, size_t format_len, rb_encoding *enc,
939 VALUE time, const struct vtm *vtm, struct timespec *ts, int gmt)
940{
941 VALUE result = rb_enc_str_new(0, 0, enc);
942 return rb_strftime_with_timespec(result, format, format_len, enc,
943 time, vtm, Qnil, ts, gmt,
944 strftime_size_limit(format_len));
945}
946
947#if 0
948VALUE
949rb_strftime_limit(const char *format, size_t format_len, rb_encoding *enc,
950 VALUE time, const struct vtm *vtm, struct timespec *ts,
951 int gmt, size_t maxsize)
952{
953 VALUE result = rb_enc_str_new(0, 0, enc);
954 return rb_strftime_with_timespec(result, format, format_len, enc,
955 time, vtm, Qnil, ts, gmt, maxsize);
956}
957#endif
958
959/* isleap --- is a year a leap year? */
960
961static int
962isleap(long year)
963{
964 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
965}
966
967
968static void
969vtm2tm_noyear(const struct vtm *vtm, struct tm *result)
970{
971 struct tm tm;
972
973 /* for isleap() in iso8601wknum. +100 is -1900 (mod 400). */
974 tm.tm_year = FIX2INT(mod(vtm->year, INT2FIX(400))) + 100;
975
976 tm.tm_mon = vtm->mon-1;
977 tm.tm_mday = vtm->mday;
978 tm.tm_hour = vtm->hour;
979 tm.tm_min = vtm->min;
980 tm.tm_sec = vtm->sec;
981 tm.tm_wday = vtm->wday;
982 tm.tm_yday = vtm->yday-1;
983 tm.tm_isdst = vtm->isdst;
984#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
985 tm.tm_gmtoff = NUM2LONG(vtm->utc_offset);
986#endif
987#if defined(HAVE_TM_ZONE)
988 tm.tm_zone = (char *)vtm->zone;
989#endif
990 *result = tm;
991}
992
993#ifdef POSIX2_DATE
994/* iso8601wknum --- compute week number according to ISO 8601 */
995
996static int
997iso8601wknum(const struct tm *timeptr)
998{
999 /*
1000 * From 1003.2:
1001 * If the week (Monday to Sunday) containing January 1
1002 * has four or more days in the new year, then it is week 1;
1003 * otherwise it is the highest numbered week of the previous
1004 * year (52 or 53), and the next week is week 1.
1005 *
1006 * ADR: This means if Jan 1 was Monday through Thursday,
1007 * it was week 1, otherwise week 52 or 53.
1008 *
1009 * XPG4 erroneously included POSIX.2 rationale text in the
1010 * main body of the standard. Thus it requires week 53.
1011 */
1012
1013 int weeknum, jan1day;
1014
1015 /* get week number, Monday as first day of the week */
1016 weeknum = weeknumber(timeptr, 1);
1017
1018 /*
1019 * With thanks and tip of the hatlo to tml@tik.vtt.fi
1020 *
1021 * What day of the week does January 1 fall on?
1022 * We know that
1023 * (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
1024 * (timeptr->tm_wday - jan1.tm_wday) MOD 7
1025 * and that
1026 * jan1.tm_yday == 0
1027 * and that
1028 * timeptr->tm_wday MOD 7 == timeptr->tm_wday
1029 * from which it follows that. . .
1030 */
1031 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
1032 if (jan1day < 0)
1033 jan1day += 7;
1034
1035 /*
1036 * If Jan 1 was a Monday through Thursday, it was in
1037 * week 1. Otherwise it was last year's highest week, which is
1038 * this year's week 0.
1039 *
1040 * What does that mean?
1041 * If Jan 1 was Monday, the week number is exactly right, it can
1042 * never be 0.
1043 * If it was Tuesday through Thursday, the weeknumber is one
1044 * less than it should be, so we add one.
1045 * Otherwise, Friday, Saturday or Sunday, the week number is
1046 * OK, but if it is 0, it needs to be 52 or 53.
1047 */
1048 switch (jan1day) {
1049 case 1: /* Monday */
1050 break;
1051 case 2: /* Tuesday */
1052 case 3: /* Wednesday */
1053 case 4: /* Thursday */
1054 weeknum++;
1055 break;
1056 case 5: /* Friday */
1057 case 6: /* Saturday */
1058 case 0: /* Sunday */
1059 if (weeknum == 0) {
1060#ifdef USE_BROKEN_XPG4
1061 /* XPG4 (as of March 1994) says 53 unconditionally */
1062 weeknum = 53;
1063#else
1064 /* get week number of last week of last year */
1065 struct tm dec31ly; /* 12/31 last year */
1066 dec31ly = *timeptr;
1067 dec31ly.tm_year--;
1068 dec31ly.tm_mon = 11;
1069 dec31ly.tm_mday = 31;
1070 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
1071 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
1072 weeknum = iso8601wknum(& dec31ly);
1073#endif
1074 }
1075 break;
1076 }
1077
1078 if (timeptr->tm_mon == 11) {
1079 /*
1080 * The last week of the year
1081 * can be in week 1 of next year.
1082 * Sigh.
1083 *
1084 * This can only happen if
1085 * M T W
1086 * 29 30 31
1087 * 30 31
1088 * 31
1089 */
1090 int wday, mday;
1091
1092 wday = timeptr->tm_wday;
1093 mday = timeptr->tm_mday;
1094 if ( (wday == 1 && (mday >= 29 && mday <= 31))
1095 || (wday == 2 && (mday == 30 || mday == 31))
1096 || (wday == 3 && mday == 31))
1097 weeknum = 1;
1098 }
1099
1100 return weeknum;
1101}
1102
1103static int
1104iso8601wknum_v(const struct vtm *vtm)
1105{
1106 struct tm tm;
1107 vtm2tm_noyear(vtm, &tm);
1108 return iso8601wknum(&tm);
1109}
1110
1111#endif
1112
1113/* weeknumber --- figure how many weeks into the year */
1114
1115/* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
1116
1117static int
1118weeknumber(const struct tm *timeptr, int firstweekday)
1119{
1120 int wday = timeptr->tm_wday;
1121 int ret;
1122
1123 if (firstweekday == 1) {
1124 if (wday == 0) /* sunday */
1125 wday = 6;
1126 else
1127 wday--;
1128 }
1129 ret = ((timeptr->tm_yday + 7 - wday) / 7);
1130 if (ret < 0)
1131 ret = 0;
1132 return ret;
1133}
1134
1135static int
1136weeknumber_v(const struct vtm *vtm, int firstweekday)
1137{
1138 struct tm tm;
1139 vtm2tm_noyear(vtm, &tm);
1140 return weeknumber(&tm, firstweekday);
1141}
1142
1143#if 0
1144/* ADR --- I'm loathe to mess with ado's code ... */
1145
1146Date: Wed, 24 Apr 91 20:54:08 MDT
1147From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
1148To: arnold@audiofax.com
1149
1150Hi Arnold,
1151in a process of fixing of strftime() in libraries on Atari ST I grabbed
1152some pieces of code from your own strftime. When doing that it came
1153to mind that your weeknumber() function compiles a little bit nicer
1154in the following form:
1155/*
1156 * firstweekday is 0 if starting in Sunday, non-zero if in Monday
1157 */
1158{
1159 return (timeptr->tm_yday - timeptr->tm_wday +
1160 (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
1161}
1162How nicer it depends on a compiler, of course, but always a tiny bit.
1163
1164 Cheers,
1165 Michal
1166 ntomczak@vm.ucs.ualberta.ca
1167#endif
1168
1169#ifdef TEST_STRFTIME
1170
1171/*
1172 * NAME:
1173 * tst
1174 *
1175 * SYNOPSIS:
1176 * tst
1177 *
1178 * DESCRIPTION:
1179 * "tst" is a test driver for the function "strftime".
1180 *
1181 * OPTIONS:
1182 * None.
1183 *
1184 * AUTHOR:
1185 * Karl Vogel
1186 * Control Data Systems, Inc.
1187 * vogelke@c-17igp.wpafb.af.mil
1188 *
1189 * BUGS:
1190 * None noticed yet.
1191 *
1192 * COMPILE:
1193 * cc -o tst -DTEST_STRFTIME strftime.c
1194 */
1195
1196/* ADR: I reformatted this to my liking, and deleted some unneeded code. */
1197
1198#ifndef NULL
1199#include <stdio.h>
1200#endif
1201#include <sys/time.h>
1202#include <string.h>
1203
1204#define MAXTIME 132
1205
1206/*
1207 * Array of time formats.
1208 */
1209
1210static char *array[] =
1211{
1212 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
1213 "(%%B) full month name, var length (January..December) %B",
1214 "(%%C) Century %C",
1215 "(%%D) date (%%m/%%d/%%y) %D",
1216 "(%%E) Locale extensions (ignored) %E",
1217 "(%%H) hour (24-hour clock, 00..23) %H",
1218 "(%%I) hour (12-hour clock, 01..12) %I",
1219 "(%%M) minute (00..59) %M",
1220 "(%%O) Locale extensions (ignored) %O",
1221 "(%%R) time, 24-hour (%%H:%%M) %R",
1222 "(%%S) second (00..60) %S",
1223 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
1224 "(%%U) week of year, Sunday as first day of week (00..53) %U",
1225 "(%%V) week of year according to ISO 8601 %V",
1226 "(%%W) week of year, Monday as first day of week (00..53) %W",
1227 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
1228 "(%%Y) year with century (1970...) %Y",
1229 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
1230 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
1231 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
1232 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
1233 "(%%d) day of the month (01..31) %d",
1234 "(%%e) day of the month, blank-padded ( 1..31) %e",
1235 "(%%h) should be same as (%%b) %h",
1236 "(%%j) day of the year (001..366) %j",
1237 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
1238 "(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
1239 "(%%m) month (01..12) %m",
1240 "(%%p) locale's AM or PM based on 12-hour clock %p",
1241 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
1242 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
1243 "(%%v) VMS date (dd-bbb-YYYY) %v",
1244 "(%%w) day of week (0..6, Sunday == 0) %w",
1245 "(%%x) appropriate locale date representation %x",
1246 "(%%y) last two digits of year (00..99) %y",
1247 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
1248 (char *) NULL
1249};
1250
1251/* main routine. */
1252
1253int
1254main(int argc, char **argv)
1255{
1256 long time();
1257
1258 char *next;
1259 char string[MAXTIME];
1260
1261 int k;
1262 int length;
1263
1264 struct tm *tm;
1265
1266 long clock;
1267
1268 /* Call the function. */
1269
1270 clock = time((long *) 0);
1271 tm = localtime(&clock);
1272
1273 for (k = 0; next = array[k]; k++) {
1274 length = strftime(string, MAXTIME, next, tm);
1275 printf("%s\n", string);
1276 }
1277
1278 exit(0);
1279}
1280#endif /* TEST_STRFTIME */
#define INT2FIX
Old name of RB_INT2FIX.
Definition: long.h:48
#define ISUPPER
Old name of rb_isupper.
Definition: ctype.h:89
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
Definition: transcode.h:523
#define FIX2INT
Old name of RB_FIX2INT.
Definition: int.h:41
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
Definition: transcode.h:521
#define TOUPPER
Old name of rb_toupper.
Definition: ctype.h:100
#define ISLOWER
Old name of rb_islower.
Definition: ctype.h:90
#define TOLOWER
Old name of rb_tolower.
Definition: ctype.h:101
#define NUM2INT
Old name of RB_NUM2INT.
Definition: int.h:44
#define Qnil
Old name of RUBY_Qnil.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition: long.h:46
#define NIL_P
Old name of RB_NIL_P.
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition: long.h:51
#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.
Definition: error.c:3139
Encoding relates APIs.
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
Definition: encoding.c:1515
rb_encoding * rb_locale_encoding(void)
Queries the encoding that represents the current locale.
Definition: encoding.c:1573
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
Definition: encoding.c:1539
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.
Definition: string.c:1067
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it additionally takes an encoding.
Definition: string.c:940
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition: vm_eval.c:1102
VALUE rb_big2str(VALUE x, int base)
Generates a place-value representation of the passed integer.
Definition: bignum.c:5096
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition: string.c:828
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition: string.c:3039
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to a C string.
Definition: string.c:952
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
Definition: string.c:3056
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition: string.c:2467
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:782
unsigned long ruby_scan_digits(const char *str, ssize_t len, int base, size_t *retlen, int *overflow)
Scans the passed string, assuming the string is a textual representation of an integer.
Definition: util.c:98
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Formats a string.
Definition: sprintf.c:214
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition: rstring.h:483
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition: rstring.h:497
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition: rstring.h:95
Definition: timev.h:21