-
Notifications
You must be signed in to change notification settings - Fork 0
/
strftime.c
1280 lines (1138 loc) · 33.2 KB
/
strftime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- c-file-style: "linux" -*- */
/*
* strftime.c
*
* Public-domain implementation of ANSI C library routine.
*
* It's written in old-style C for maximal portability.
* However, since I'm used to prototypes, I've included them too.
*
* If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
* For extensions from SunOS, add SUNOS_EXT.
* For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
* For VMS dates, add VMS_EXT.
* For a an RFC822 time format, add MAILHEADER_EXT.
* For ISO week years, add ISO_DATE_EXT.
* For complete POSIX semantics, add POSIX_SEMANTICS.
*
* The code for %c, %x, and %X now follows the 1003.2 specification for
* the POSIX locale.
* This version ignores LOCALE information.
* It also doesn't worry about multi-byte characters.
* So there.
*
* This file is also shipped with GAWK (GNU Awk), gawk specific bits of
* code are included if GAWK is defined.
*
* Arnold Robbins
* January, February, March, 1991
* Updated March, April 1992
* Updated April, 1993
* Updated February, 1994
* Updated May, 1994
* Updated January, 1995
* Updated September, 1995
* Updated January, 1996
*
* Fixes from [email protected]
* February 1991, May 1992
* Fixes from Tor Lillqvist [email protected]
* May, 1993
* Further fixes from [email protected]
* February 1994
* %z code from [email protected]
* Applied September 1995
* %V code fixed (again) and %G, %g added,
* January 1996
*/
#include "ruby/internal/config.h"
#ifndef GAWK
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <errno.h>
#endif
#if defined(TM_IN_SYS_TIME) || !defined(GAWK)
#include <sys/types.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#endif
#include <math.h>
#include "internal.h"
#include "internal/string.h"
#include "internal/vm.h"
#include "ruby/encoding.h"
#include "ruby/ruby.h"
#include "ruby/util.h"
#include "timev.h"
/* defaults: season to taste */
#define SYSV_EXT 1 /* stuff in System V ascftime routine */
#define SUNOS_EXT 1 /* stuff in SunOS strftime routine */
#define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */
#define VMS_EXT 1 /* include %v for VMS date format */
#define MAILHEADER_EXT 1 /* add %z for HHMM format */
#define ISO_DATE_EXT 1 /* %G and %g for year of ISO week */
#if defined(ISO_DATE_EXT)
#if ! defined(POSIX2_DATE)
#define POSIX2_DATE 1
#endif
#endif
#if defined(POSIX2_DATE)
#if ! defined(SYSV_EXT)
#define SYSV_EXT 1
#endif
#if ! defined(SUNOS_EXT)
#define SUNOS_EXT 1
#endif
#endif
#if defined(POSIX2_DATE)
#define adddecl(stuff) stuff
#else
#define adddecl(stuff)
#endif
#undef strchr /* avoid AIX weirdness */
#if !defined __STDC__ && !defined _WIN32
#define const /**/
static int weeknumber();
adddecl(static int iso8601wknum();)
static int weeknumber_v();
adddecl(static int iso8601wknum_v();)
#else
static int weeknumber(const struct tm *timeptr, int firstweekday);
adddecl(static int iso8601wknum(const struct tm *timeptr);)
static int weeknumber_v(const struct vtm *vtm, int firstweekday);
adddecl(static int iso8601wknum_v(const struct vtm *vtm);)
#endif
#ifdef STDC_HEADERS
#include <stdlib.h>
#include <string.h>
#else
extern void *malloc();
extern void *realloc();
extern char *getenv();
extern char *strchr();
#endif
#define range(low, item, hi) max((low), min((item), (hi)))
#undef min /* just in case */
/* min --- return minimum of two numbers */
static inline int
min(int a, int b)
{
return (a < b ? a : b);
}
#undef max /* also, just in case */
/* max --- return maximum of two numbers */
static inline int
max(int a, int b)
{
return (a > b ? a : b);
}
#ifdef NO_STRING_LITERAL_CONCATENATION
#error No string literal concatenation
#endif
#define add(x,y) (rb_funcall((x), '+', 1, (y)))
#define sub(x,y) (rb_funcall((x), '-', 1, (y)))
#define mul(x,y) (rb_funcall((x), '*', 1, (y)))
#define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
#define div(x,y) (rb_funcall((x), rb_intern("div"), 1, (y)))
#define mod(x,y) (rb_funcall((x), '%', 1, (y)))
/* strftime --- produce formatted time */
enum {LEFT, CHCASE, LOWER, UPPER};
#define BIT_OF(n) (1U<<(n))
static char *
resize_buffer(VALUE ftime, char *s, const char **start, const char **endp,
ptrdiff_t n, size_t maxsize)
{
size_t len = s - *start;
size_t nlen = len + n * 2;
if (nlen < len || nlen > maxsize) {
return 0;
}
rb_str_set_len(ftime, len);
rb_str_modify_expand(ftime, nlen-len);
s = RSTRING_PTR(ftime);
*endp = s + nlen;
*start = s;
return s += len;
}
static void
buffer_size_check(const char *s,
const char *format_end, size_t format_len,
rb_encoding *enc)
{
if (!s) {
const char *format = format_end-format_len;
VALUE fmt = rb_enc_str_new(format, format_len, enc);
rb_syserr_fail_str(ERANGE, fmt);
}
}
static char *
case_conv(char *s, ptrdiff_t i, int flags)
{
switch (flags & (BIT_OF(UPPER)|BIT_OF(LOWER))) {
case BIT_OF(UPPER):
do {
if (ISLOWER(*s)) *s = TOUPPER(*s);
} while (s++, --i);
break;
case BIT_OF(LOWER):
do {
if (ISUPPER(*s)) *s = TOLOWER(*s);
} while (s++, --i);
break;
default:
s += i;
break;
}
return s;
}
static VALUE
format_value(VALUE val, int base)
{
if (!RB_BIGNUM_TYPE_P(val))
val = rb_Integer(val);
return rb_big2str(val, base);
}
/*
* enc is the encoding of the format. It is used as the encoding of resulted
* string, but the name of the month and weekday are always US-ASCII. So it
* is only used for the timezone name on Windows.
*/
static VALUE
rb_strftime_with_timespec(VALUE ftime, const char *format, size_t format_len,
rb_encoding *enc, VALUE time, const struct vtm *vtm,
VALUE timev, struct timespec *ts, int gmt, size_t maxsize)
{
size_t len = RSTRING_LEN(ftime);
char *s = RSTRING_PTR(ftime);
const char *start = s;
const char *endp = start + rb_str_capacity(ftime);
const char *const format_end = format + format_len;
const char *sp, *tp;
#define TBUFSIZE 100
auto char tbuf[TBUFSIZE];
long off;
ptrdiff_t i;
int w;
long y;
int precision, flags, colons;
char padding;
#ifdef MAILHEADER_EXT
int sign;
#endif
VALUE zone = Qnil;
/* various tables, useful in North America */
static const char days_l[][10] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday",
};
static const char months_l[][10] = {
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December",
};
static const char ampm[][3] = { "AM", "PM", };
if (format == NULL || format_len == 0 || vtm == NULL) {
goto err;
}
if (enc &&
(enc == rb_usascii_encoding() ||
enc == rb_ascii8bit_encoding() ||
enc == rb_locale_encoding())) {
enc = NULL;
}
s += len;
for (; format < format_end; format++) {
#define FLAG_FOUND() do { \
if (precision > 0) \
goto unknown; \
} while (0)
#define NEEDS(n) do { \
if (s >= endp || (n) >= endp - s - 1) { \
s = resize_buffer(ftime, s, &start, &endp, (n), maxsize); \
buffer_size_check(s, format_end, format_len, enc); \
} \
} while (0)
#define FILL_PADDING(i) do { \
if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \
NEEDS(precision); \
memset(s, padding ? padding : ' ', precision - (i)); \
s += precision - (i); \
} \
else { \
NEEDS(i); \
} \
} while (0);
#define FMT_PADDING(fmt, def_pad) \
(&"%*"fmt"\0""%0*"fmt[\
(padding == '0' || (!padding && (def_pad) == '0')) ? \
rb_strlen_lit("%*"fmt)+1 : 0])
#define FMT_PRECISION(def_prec) \
((flags & BIT_OF(LEFT)) ? (1) : \
(precision <= 0) ? (def_prec) : (precision))
#define FMT(def_pad, def_prec, fmt, val) \
do { \
precision = FMT_PRECISION(def_prec); \
len = s - start; \
NEEDS(precision); \
rb_str_set_len(ftime, len); \
rb_str_catf(ftime, FMT_PADDING(fmt, def_pad), \
precision, (val)); \
RSTRING_GETMEM(ftime, s, len); \
endp = (start = s) + rb_str_capacity(ftime); \
s += len; \
} while (0)
#define STRFTIME(fmt) \
do { \
len = s - start; \
rb_str_set_len(ftime, len); \
if (!rb_strftime_with_timespec(ftime, (fmt), rb_strlen_lit(fmt), \
enc, time, vtm, timev, ts, gmt, maxsize)) \
return 0; \
s = RSTRING_PTR(ftime); \
i = RSTRING_LEN(ftime) - len; \
endp = (start = s) + rb_str_capacity(ftime); \
s += len; \
if (i > 0) case_conv(s, i, flags); \
if (precision > i) {\
s += i; \
NEEDS(precision); \
s -= i; \
memmove(s + precision - i, s, i);\
memset(s, padding ? padding : ' ', precision - i); \
s += precision; \
} \
else s += i; \
} while (0)
#define FMTV(def_pad, def_prec, fmt, val) \
do { \
VALUE tmp = (val); \
if (FIXNUM_P(tmp)) { \
FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
} \
else { \
const int base = ((fmt[0] == 'x') ? 16 : \
(fmt[0] == 'o') ? 8 : \
10); \
precision = FMT_PRECISION(def_prec); \
if (!padding) padding = (def_pad); \
tmp = format_value(tmp, base); \
i = RSTRING_LEN(tmp); \
FILL_PADDING(i); \
rb_str_set_len(ftime, s-start); \
rb_str_append(ftime, tmp); \
RSTRING_GETMEM(ftime, s, len); \
endp = (start = s) + rb_str_capacity(ftime); \
s += len; \
} \
} while (0)
tp = memchr(format, '%', format_end - format);
if (!tp) tp = format_end;
NEEDS(tp - format);
memcpy(s, format, tp - format);
s += tp - format;
format = tp;
if (format == format_end) break;
tp = tbuf;
sp = format;
precision = -1;
flags = 0;
padding = 0;
colons = 0;
again:
if (++format >= format_end) goto unknown;
switch (*format) {
case '%':
FILL_PADDING(1);
*s++ = '%';
continue;
case 'a': /* abbreviated weekday name */
if (flags & BIT_OF(CHCASE)) {
flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
flags |= BIT_OF(UPPER);
}
if (vtm->wday > 6)
i = 1, tp = "?";
else
i = 3, tp = days_l[vtm->wday];
break;
case 'A': /* full weekday name */
if (flags & BIT_OF(CHCASE)) {
flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
flags |= BIT_OF(UPPER);
}
if (vtm->wday > 6)
i = 1, tp = "?";
else
i = strlen(tp = days_l[vtm->wday]);
break;
#ifdef SYSV_EXT
case 'h': /* abbreviated month name */
#endif
case 'b': /* abbreviated month name */
if (flags & BIT_OF(CHCASE)) {
flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
flags |= BIT_OF(UPPER);
}
if (vtm->mon < 1 || vtm->mon > 12)
i = 1, tp = "?";
else
i = 3, tp = months_l[vtm->mon-1];
break;
case 'B': /* full month name */
if (flags & BIT_OF(CHCASE)) {
flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
flags |= BIT_OF(UPPER);
}
if (vtm->mon < 1 || vtm->mon > 12)
i = 1, tp = "?";
else
i = strlen(tp = months_l[vtm->mon-1]);
break;
case 'c': /* appropriate date and time representation */
STRFTIME("%a %b %e %H:%M:%S %Y");
continue;
case 'd': /* day of the month, 01 - 31 */
i = range(1, vtm->mday, 31);
FMT('0', 2, "d", (int)i);
continue;
case 'H': /* hour, 24-hour clock, 00 - 23 */
i = range(0, vtm->hour, 23);
FMT('0', 2, "d", (int)i);
continue;
case 'I': /* hour, 12-hour clock, 01 - 12 */
i = range(0, vtm->hour, 23);
if (i == 0)
i = 12;
else if (i > 12)
i -= 12;
FMT('0', 2, "d", (int)i);
continue;
case 'j': /* day of the year, 001 - 366 */
i = range(1, vtm->yday, 366);
FMT('0', 3, "d", (int)i);
continue;
case 'm': /* month, 01 - 12 */
i = range(1, vtm->mon, 12);
FMT('0', 2, "d", (int)i);
continue;
case 'M': /* minute, 00 - 59 */
i = range(0, vtm->min, 59);
FMT('0', 2, "d", (int)i);
continue;
case 'p': /* AM or PM based on 12-hour clock */
case 'P': /* am or pm based on 12-hour clock */
if ((*format == 'p' && (flags & BIT_OF(CHCASE))) ||
(*format == 'P' && !(flags & (BIT_OF(CHCASE)|BIT_OF(UPPER))))) {
flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
flags |= BIT_OF(LOWER);
}
i = range(0, vtm->hour, 23);
if (i < 12)
tp = ampm[0];
else
tp = ampm[1];
i = 2;
break;
case 's':
if (ts) {
time_t sec = ts->tv_sec;
if (~(time_t)0 <= 0)
FMT('0', 1, PRI_TIMET_PREFIX"d", sec);
else
FMT('0', 1, PRI_TIMET_PREFIX"u", sec);
}
else {
VALUE sec = div(timev, INT2FIX(1));
FMTV('0', 1, "d", sec);
}
continue;
case 'S': /* second, 00 - 60 */
i = range(0, vtm->sec, 60);
FMT('0', 2, "d", (int)i);
continue;
case 'U': /* week of year, Sunday is first day of week */
FMT('0', 2, "d", weeknumber_v(vtm, 0));
continue;
case 'w': /* weekday, Sunday == 0, 0 - 6 */
i = range(0, vtm->wday, 6);
FMT('0', 1, "d", (int)i);
continue;
case 'W': /* week of year, Monday is first day of week */
FMT('0', 2, "d", weeknumber_v(vtm, 1));
continue;
case 'x': /* appropriate date representation */
STRFTIME("%m/%d/%y");
continue;
case 'X': /* appropriate time representation */
STRFTIME("%H:%M:%S");
continue;
case 'y': /* year without a century, 00 - 99 */
i = NUM2INT(mod(vtm->year, INT2FIX(100)));
FMT('0', 2, "d", (int)i);
continue;
case 'Y': /* year with century */
if (FIXNUM_P(vtm->year)) {
long y = FIX2LONG(vtm->year);
FMT('0', 0 <= y ? 4 : 5, "ld", y);
}
else {
FMTV('0', 4, "d", vtm->year);
}
continue;
#ifdef MAILHEADER_EXT
case 'z': /* time zone offset east of GMT e.g. -0600 */
if (gmt) {
off = 0;
}
else {
off = NUM2LONG(rb_funcall(vtm->utc_offset, rb_intern("round"), 0));
}
if (off < 0 || (gmt && (flags & BIT_OF(LEFT)))) {
off = -off;
sign = -1;
}
else {
sign = +1;
}
switch (colons) {
case 0: /* %z -> +hhmm */
precision = precision <= 5 ? 2 : precision-3;
NEEDS(precision + 3);
break;
case 1: /* %:z -> +hh:mm */
precision = precision <= 6 ? 2 : precision-4;
NEEDS(precision + 4);
break;
case 2: /* %::z -> +hh:mm:ss */
precision = precision <= 9 ? 2 : precision-7;
NEEDS(precision + 7);
break;
case 3: /* %:::z -> +hh[:mm[:ss]] */
if (off % 3600 == 0) {
precision = precision <= 3 ? 2 : precision-1;
NEEDS(precision + 3);
}
else if (off % 60 == 0) {
precision = precision <= 6 ? 2 : precision-4;
NEEDS(precision + 4);
}
else {
precision = precision <= 9 ? 2 : precision-7;
NEEDS(precision + 9);
}
break;
default:
format--;
goto unknown;
}
i = snprintf(s, endp - s, (padding == ' ' ? "%+*ld" : "%+.*ld"),
precision + (padding == ' '), sign * (off / 3600));
if (i < 0) goto err;
if (sign < 0 && off < 3600) {
*(padding == ' ' ? s + i - 2 : s) = '-';
}
s += i;
off = off % 3600;
if (colons == 3 && off == 0)
continue;
if (1 <= colons)
*s++ = ':';
i = snprintf(s, endp - s, "%02d", (int)(off / 60));
if (i < 0) goto err;
s += i;
off = off % 60;
if (colons == 3 && off == 0)
continue;
if (2 <= colons) {
*s++ = ':';
i = snprintf(s, endp - s, "%02d", (int)off);
if (i < 0) goto err;
s += i;
}
continue;
#endif /* MAILHEADER_EXT */
case 'Z': /* time zone name or abbreviation */
if (flags & BIT_OF(CHCASE)) {
flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
flags |= BIT_OF(LOWER);
}
if (gmt) {
i = 3;
tp = "UTC";
break;
}
if (NIL_P(vtm->zone)) {
i = 0;
}
else {
if (NIL_P(zone)) {
zone = rb_time_zone_abbreviation(vtm->zone, time);
}
tp = RSTRING_PTR(zone);
if (enc) {
for (i = 0; i < TBUFSIZE && tp[i]; i++) {
if ((unsigned char)tp[i] > 0x7F) {
VALUE str = rb_str_conv_enc_opts(rb_str_new_cstr(tp), rb_locale_encoding(), enc, ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE, Qnil);
i = strlcpy(tbuf, RSTRING_PTR(str), TBUFSIZE);
tp = tbuf;
break;
}
}
}
else
i = strlen(tp);
}
break;
#ifdef SYSV_EXT
case 'n': /* same as \n */
FILL_PADDING(1);
*s++ = '\n';
continue;
case 't': /* same as \t */
FILL_PADDING(1);
*s++ = '\t';
continue;
case 'D': /* date as %m/%d/%y */
STRFTIME("%m/%d/%y");
continue;
case 'e': /* day of month, blank padded */
FMT(' ', 2, "d", range(1, vtm->mday, 31));
continue;
case 'r': /* time as %I:%M:%S %p */
STRFTIME("%I:%M:%S %p");
continue;
case 'R': /* time as %H:%M */
STRFTIME("%H:%M");
continue;
case 'T': /* time as %H:%M:%S */
STRFTIME("%H:%M:%S");
continue;
#endif
#ifdef SUNOS_EXT
case 'k': /* hour, 24-hour clock, blank pad */
i = range(0, vtm->hour, 23);
FMT(' ', 2, "d", (int)i);
continue;
case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
i = range(0, vtm->hour, 23);
if (i == 0)
i = 12;
else if (i > 12)
i -= 12;
FMT(' ', 2, "d", (int)i);
continue;
#endif
#ifdef VMS_EXT
case 'v': /* date as dd-bbb-YYYY */
STRFTIME("%e-%^b-%4Y");
continue;
#endif
#ifdef POSIX2_DATE
case 'C':
FMTV('0', 2, "d", div(vtm->year, INT2FIX(100)));
continue;
case 'E':
/* POSIX locale extensions, ignored for now */
if (!format[1] || !strchr("cCxXyY", format[1]))
goto unknown;
goto again;
case 'O':
/* POSIX locale extensions, ignored for now */
if (!format[1] || !strchr("deHkIlmMSuUVwWy", format[1]))
goto unknown;
goto again;
case 'V': /* week of year according ISO 8601 */
FMT('0', 2, "d", iso8601wknum_v(vtm));
continue;
case 'u':
/* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
FMT('0', 1, "d", vtm->wday == 0 ? 7 : vtm->wday);
continue;
#endif /* POSIX2_DATE */
#ifdef ISO_DATE_EXT
case 'G':
case 'g':
/*
* Year of ISO week.
*
* If it's December but the ISO week number is one,
* that week is in next year.
* If it's January but the ISO week number is 52 or
* 53, that week is in last year.
* Otherwise, it's this year.
*/
{
VALUE yv = vtm->year;
w = iso8601wknum_v(vtm);
if (vtm->mon == 12 && w == 1)
yv = add(yv, INT2FIX(1));
else if (vtm->mon == 1 && w >= 52)
yv = sub(yv, INT2FIX(1));
if (*format == 'G') {
if (FIXNUM_P(yv)) {
const long y = FIX2LONG(yv);
FMT('0', 0 <= y ? 4 : 5, "ld", y);
}
else {
FMTV('0', 4, "d", yv);
}
}
else {
yv = mod(yv, INT2FIX(100));
y = FIX2LONG(yv);
FMT('0', 2, "ld", y);
}
continue;
}
#endif /* ISO_DATE_EXT */
case 'L':
w = 3;
goto subsec;
case 'N':
/*
* fractional second digits. default is 9 digits
* (nanosecond).
*
* %3N millisecond (3 digits)
* %6N microsecond (6 digits)
* %9N nanosecond (9 digits)
*/
w = 9;
subsec:
if (precision <= 0) {
precision = w;
}
NEEDS(precision);
if (ts) {
long subsec = ts->tv_nsec;
if (9 < precision) {
snprintf(s, endp - s, "%09ld", subsec);
memset(s+9, '0', precision-9);
s += precision;
}
else {
int i;
for (i = 0; i < 9-precision; i++)
subsec /= 10;
snprintf(s, endp - s, "%0*ld", precision, subsec);
s += precision;
}
}
else {
VALUE subsec = mod(timev, INT2FIX(1));
int ww;
long n;
ww = precision;
while (9 <= ww) {
subsec = mul(subsec, INT2FIX(1000000000));
ww -= 9;
}
n = 1;
for (; 0 < ww; ww--)
n *= 10;
if (n != 1)
subsec = mul(subsec, INT2FIX(n));
subsec = div(subsec, INT2FIX(1));
if (FIXNUM_P(subsec)) {
(void)snprintf(s, endp - s, "%0*ld", precision, FIX2LONG(subsec));
s += precision;
}
else {
VALUE args[2], result;
args[0] = INT2FIX(precision);
args[1] = subsec;
result = rb_str_format(2, args,
rb_fstring_lit("%0*d"));
(void)strlcpy(s, StringValueCStr(result), endp-s);
s += precision;
}
}
continue;
case 'F': /* Equivalent to %Y-%m-%d */
STRFTIME("%Y-%m-%d");
continue;
case '-':
FLAG_FOUND();
flags |= BIT_OF(LEFT);
padding = precision = 0;
goto again;
case '^':
FLAG_FOUND();
flags |= BIT_OF(UPPER);
goto again;
case '#':
FLAG_FOUND();
flags |= BIT_OF(CHCASE);
goto again;
case '_':
FLAG_FOUND();
padding = ' ';
goto again;
case ':':
for (colons = 1; colons <= 3; ++colons) {
if (format+colons >= format_end) goto unknown;
if (format[colons] == 'z') break;
if (format[colons] != ':') goto unknown;
}
format += colons - 1;
goto again;
case '0':
padding = '0';
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
size_t n;
int ov;
unsigned long u = ruby_scan_digits(format, format_end-format, 10, &n, &ov);
if (ov || u > INT_MAX) goto unknown;
precision = (int)u;
format += n - 1;
goto again;
}
default:
unknown:
i = format - sp + 1;
tp = sp;
precision = -1;
flags = 0;
padding = 0;
colons = 0;
break;
}
if (i) {
FILL_PADDING(i);
memcpy(s, tp, i);
s = case_conv(s, i, flags);
}
}
if (format != format_end) {
return 0;
}
len = s - start;
rb_str_set_len(ftime, len);
rb_str_resize(ftime, len);
return ftime;
err:
return 0;
}
static size_t
strftime_size_limit(size_t format_len)
{
size_t limit = format_len * (1*1024*1024);
if (limit < format_len) limit = format_len;
else if (limit < 1024) limit = 1024;
return limit;
}
VALUE
rb_strftime(const char *format, size_t format_len, rb_encoding *enc,
VALUE time, const struct vtm *vtm, VALUE timev, int gmt)
{
VALUE result = rb_enc_str_new(0, 0, enc);
return rb_strftime_with_timespec(result, format, format_len, enc,
time, vtm, timev, NULL, gmt,
strftime_size_limit(format_len));
}
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)
{
VALUE result = rb_enc_str_new(0, 0, enc);
return rb_strftime_with_timespec(result, format, format_len, enc,
time, vtm, Qnil, ts, gmt,
strftime_size_limit(format_len));
}
#if 0
VALUE
rb_strftime_limit(const char *format, size_t format_len, rb_encoding *enc,
VALUE time, const struct vtm *vtm, struct timespec *ts,
int gmt, size_t maxsize)
{
VALUE result = rb_enc_str_new(0, 0, enc);
return rb_strftime_with_timespec(result, format, format_len, enc,
time, vtm, Qnil, ts, gmt, maxsize);
}
#endif
/* isleap --- is a year a leap year? */
static int
isleap(long year)
{
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
static void
vtm2tm_noyear(const struct vtm *vtm, struct tm *result)
{
struct tm tm;
/* for isleap() in iso8601wknum. +100 is -1900 (mod 400). */
tm.tm_year = FIX2INT(mod(vtm->year, INT2FIX(400))) + 100;
tm.tm_mon = vtm->mon-1;
tm.tm_mday = vtm->mday;
tm.tm_hour = vtm->hour;
tm.tm_min = vtm->min;
tm.tm_sec = vtm->sec;
tm.tm_wday = vtm->wday;
tm.tm_yday = vtm->yday-1;
tm.tm_isdst = vtm->isdst;
#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
tm.tm_gmtoff = NUM2LONG(vtm->utc_offset);
#endif
#if defined(HAVE_TM_ZONE)
tm.tm_zone = (char *)vtm->zone;
#endif
*result = tm;
}
#ifdef POSIX2_DATE
/* iso8601wknum --- compute week number according to ISO 8601 */
static int
iso8601wknum(const struct tm *timeptr)
{
/*
* From 1003.2: