forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.c
5007 lines (4480 loc) · 132 KB
/
time.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
/**********************************************************************
time.c -
$Author$
created at: Tue Dec 28 14:31:59 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#include "ruby/encoding.h"
#include "internal.h"
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <float.h>
#include <math.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif
#include "timev.h"
#include "id.h"
static ID id_divmod, id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
static ID id_quo, id_div;
static ID id_nanosecond, id_microsecond, id_millisecond, id_nsec, id_usec;
#define NDIV(x,y) (-(-((x)+1)/(y))-1)
#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
#define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d))
#define VTM_WDAY_INITVAL (7)
#define VTM_ISDST_INITVAL (3)
static int
eq(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
return x == y;
}
return RTEST(rb_funcall(x, idEq, 1, y));
}
static int
cmp(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
if ((long)x < (long)y)
return -1;
if ((long)x > (long)y)
return 1;
return 0;
}
if (RB_TYPE_P(x, T_BIGNUM)) return FIX2INT(rb_big_cmp(x, y));
return rb_cmpint(rb_funcall(x, idCmp, 1, y), x, y);
}
#define ne(x,y) (!eq((x),(y)))
#define lt(x,y) (cmp((x),(y)) < 0)
#define gt(x,y) (cmp((x),(y)) > 0)
#define le(x,y) (cmp((x),(y)) <= 0)
#define ge(x,y) (cmp((x),(y)) >= 0)
static VALUE
addv(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
return LONG2NUM(FIX2LONG(x) + FIX2LONG(y));
}
if (RB_TYPE_P(x, T_BIGNUM)) return rb_big_plus(x, y);
return rb_funcall(x, '+', 1, y);
}
static VALUE
subv(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
return LONG2NUM(FIX2LONG(x) - FIX2LONG(y));
}
if (RB_TYPE_P(x, T_BIGNUM)) return rb_big_minus(x, y);
return rb_funcall(x, '-', 1, y);
}
static VALUE
mulv(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
return rb_fix_mul_fix(x, y);
}
if (RB_TYPE_P(x, T_BIGNUM))
return rb_big_mul(x, y);
return rb_funcall(x, '*', 1, y);
}
static VALUE
divv(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
return rb_fix_div_fix(x, y);
}
if (RB_TYPE_P(x, T_BIGNUM))
return rb_big_div(x, y);
return rb_funcall(x, id_div, 1, y);
}
static VALUE
modv(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
if (FIX2LONG(y) == 0) rb_num_zerodiv();
if (FIXNUM_P(x)) return rb_fix_mod_fix(x, y);
}
if (RB_TYPE_P(x, T_BIGNUM)) return rb_big_modulo(x, y);
return rb_funcall(x, '%', 1, y);
}
#define neg(x) (subv(INT2FIX(0), (x)))
static VALUE
quov(VALUE x, VALUE y)
{
VALUE ret;
if (FIXNUM_P(x) && FIXNUM_P(y)) {
long a, b, c;
a = FIX2LONG(x);
b = FIX2LONG(y);
if (b == 0) rb_num_zerodiv();
if (a == FIXNUM_MIN && b == -1) return LONG2NUM(-a);
c = a / b;
if (c * b == a) {
return LONG2FIX(c);
}
}
ret = rb_numeric_quo(x, y);
if (RB_TYPE_P(ret, T_RATIONAL) &&
RRATIONAL(ret)->den == INT2FIX(1)) {
ret = RRATIONAL(ret)->num;
}
return ret;
}
#define mulquov(x,y,z) (((y) == (z)) ? (x) : quov(mulv((x),(y)),(z)))
static void
divmodv(VALUE n, VALUE d, VALUE *q, VALUE *r)
{
VALUE tmp, ary;
if (FIXNUM_P(d)) {
if (FIX2LONG(d) == 0) rb_num_zerodiv();
if (FIXNUM_P(n)) {
rb_fix_divmod_fix(n, d, q, r);
return;
}
}
tmp = rb_funcall(n, id_divmod, 1, d);
ary = rb_check_array_type(tmp);
if (NIL_P(ary)) {
rb_raise(rb_eTypeError, "unexpected divmod result: into %"PRIsVALUE,
rb_obj_class(tmp));
}
*q = rb_ary_entry(ary, 0);
*r = rb_ary_entry(ary, 1);
}
#if SIZEOF_LONG == 8
# define INT64toNUM(x) LONG2NUM(x)
#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
# define INT64toNUM(x) LL2NUM(x)
#endif
#if defined(HAVE_UINT64_T) && SIZEOF_LONG*2 <= SIZEOF_UINT64_T
typedef uint64_t uwideint_t;
typedef int64_t wideint_t;
typedef uint64_t WIDEVALUE;
typedef int64_t SIGNED_WIDEVALUE;
# define WIDEVALUE_IS_WIDER 1
# define UWIDEINT_MAX UINT64_MAX
# define WIDEINT_MAX INT64_MAX
# define WIDEINT_MIN INT64_MIN
# define FIXWINT_P(tv) ((tv) & 1)
# define FIXWVtoINT64(tv) RSHIFT((SIGNED_WIDEVALUE)(tv), 1)
# define INT64toFIXWV(wi) ((WIDEVALUE)((SIGNED_WIDEVALUE)(wi) << 1 | FIXNUM_FLAG))
# define FIXWV_MAX (((int64_t)1 << 62) - 1)
# define FIXWV_MIN (-((int64_t)1 << 62))
# define FIXWVABLE(wi) (POSFIXWVABLE(wi) && NEGFIXWVABLE(wi))
# define WINT2FIXWV(i) WIDEVAL_WRAP(INT64toFIXWV(i))
# define FIXWV2WINT(w) FIXWVtoINT64(WIDEVAL_GET(w))
#else
typedef unsigned long uwideint_t;
typedef long wideint_t;
typedef VALUE WIDEVALUE;
typedef SIGNED_VALUE SIGNED_WIDEVALUE;
# define WIDEVALUE_IS_WIDER 0
# define UWIDEINT_MAX ULONG_MAX
# define WIDEINT_MAX LONG_MAX
# define WIDEINT_MIN LONG_MIN
# define FIXWINT_P(v) FIXNUM_P(v)
# define FIXWV_MAX FIXNUM_MAX
# define FIXWV_MIN FIXNUM_MIN
# define FIXWVABLE(i) FIXABLE(i)
# define WINT2FIXWV(i) WIDEVAL_WRAP(LONG2FIX(i))
# define FIXWV2WINT(w) FIX2LONG(WIDEVAL_GET(w))
#endif
#define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
#define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
#define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
#define MUL_OVERFLOW_FIXWV_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXWV_MIN, FIXWV_MAX)
/* #define STRUCT_WIDEVAL */
#ifdef STRUCT_WIDEVAL
/* for type checking */
typedef struct {
WIDEVALUE value;
} wideval_t;
static inline wideval_t WIDEVAL_WRAP(WIDEVALUE v) { wideval_t w = { v }; return w; }
# define WIDEVAL_GET(w) ((w).value)
#else
typedef WIDEVALUE wideval_t;
# define WIDEVAL_WRAP(v) (v)
# define WIDEVAL_GET(w) (w)
#endif
#if WIDEVALUE_IS_WIDER
static inline wideval_t
wint2wv(wideint_t wi)
{
if (FIXWVABLE(wi))
return WINT2FIXWV(wi);
else
return WIDEVAL_WRAP(INT64toNUM(wi));
}
# define WINT2WV(wi) wint2wv(wi)
#else
# define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
#endif
static inline VALUE
w2v(wideval_t w)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(w))
return INT64toNUM(FIXWV2WINT(w));
return (VALUE)WIDEVAL_GET(w);
#else
return WIDEVAL_GET(w);
#endif
}
#if WIDEVALUE_IS_WIDER
static wideval_t
v2w_bignum(VALUE v)
{
int sign;
uwideint_t u;
sign = rb_integer_pack(v, &u, 1, sizeof(u), 0,
INTEGER_PACK_NATIVE_BYTE_ORDER);
if (sign == 0)
return WINT2FIXWV(0);
else if (sign == -1) {
if (u <= -FIXWV_MIN)
return WINT2FIXWV(-(wideint_t)u);
}
else if (sign == +1) {
if (u <= FIXWV_MAX)
return WINT2FIXWV((wideint_t)u);
}
return WIDEVAL_WRAP(v);
}
#endif
static inline wideval_t
v2w(VALUE v)
{
if (RB_TYPE_P(v, T_RATIONAL)) {
if (RRATIONAL(v)->den != LONG2FIX(1))
return v;
v = RRATIONAL(v)->num;
}
#if WIDEVALUE_IS_WIDER
if (FIXNUM_P(v)) {
return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(long)v);
}
else if (RB_TYPE_P(v, T_BIGNUM) &&
rb_absint_size(v, NULL) <= sizeof(WIDEVALUE)) {
return v2w_bignum(v);
}
#endif
return WIDEVAL_WRAP(v);
}
static int
weq(wideval_t wx, wideval_t wy)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
}
return RTEST(rb_funcall(w2v(wx), idEq, 1, w2v(wy)));
#else
return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
#endif
}
static int
wcmp(wideval_t wx, wideval_t wy)
{
VALUE x, y;
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
wideint_t a, b;
a = FIXWV2WINT(wx);
b = FIXWV2WINT(wy);
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
#endif
x = w2v(wx);
y = w2v(wy);
return cmp(x, y);
}
#define wne(x,y) (!weq((x),(y)))
#define wlt(x,y) (wcmp((x),(y)) < 0)
#define wgt(x,y) (wcmp((x),(y)) > 0)
#define wle(x,y) (wcmp((x),(y)) <= 0)
#define wge(x,y) (wcmp((x),(y)) >= 0)
static wideval_t
wadd(wideval_t wx, wideval_t wy)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
return WINT2WV(r);
}
#endif
return v2w(addv(w2v(wx), w2v(wy)));
}
static wideval_t
wsub(wideval_t wx, wideval_t wy)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
return WINT2WV(r);
}
#endif
return v2w(subv(w2v(wx), w2v(wy)));
}
static wideval_t
wmul(wideval_t wx, wideval_t wy)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
if (!MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy)))
return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
}
#endif
return v2w(mulv(w2v(wx), w2v(wy)));
}
static wideval_t
wquo(wideval_t wx, wideval_t wy)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
wideint_t a, b, c;
a = FIXWV2WINT(wx);
b = FIXWV2WINT(wy);
if (b == 0) rb_num_zerodiv();
c = a / b;
if (c * b == a) {
return WINT2WV(c);
}
}
#endif
return v2w(quov(w2v(wx), w2v(wy)));
}
#define wmulquo(x,y,z) ((WIDEVAL_GET(y) == WIDEVAL_GET(z)) ? (x) : wquo(wmul((x),(y)),(z)))
#define wmulquoll(x,y,z) (((y) == (z)) ? (x) : wquo(wmul((x),WINT2WV(y)),WINT2WV(z)))
#if WIDEVALUE_IS_WIDER
static int
wdivmod0(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
{
if (FIXWV_P(wn) && FIXWV_P(wd)) {
wideint_t n, d, q, r;
d = FIXWV2WINT(wd);
if (d == 0) rb_num_zerodiv();
if (d == 1) {
*wq = wn;
*wr = WINT2FIXWV(0);
return 1;
}
if (d == -1) {
wideint_t xneg = -FIXWV2WINT(wn);
*wq = WINT2WV(xneg);
*wr = WINT2FIXWV(0);
return 1;
}
n = FIXWV2WINT(wn);
if (n == 0) {
*wq = WINT2FIXWV(0);
*wr = WINT2FIXWV(0);
return 1;
}
q = n / d;
r = n % d;
if (d > 0 ? r < 0 : r > 0) {
q -= 1;
r += d;
}
*wq = WINT2FIXWV(q);
*wr = WINT2FIXWV(r);
return 1;
}
return 0;
}
#endif
static void
wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
{
VALUE vq, vr;
#if WIDEVALUE_IS_WIDER
if (wdivmod0(wn, wd, wq, wr)) return;
#endif
divmodv(w2v(wn), w2v(wd), &vq, &vr);
*wq = v2w(vq);
*wr = v2w(vr);
}
static void
wmuldivmod(wideval_t wx, wideval_t wy, wideval_t wz, wideval_t *wq, wideval_t *wr)
{
if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
*wq = wx;
*wr = WINT2FIXWV(0);
return;
}
wdivmod(wmul(wx,wy), wz, wq, wr);
}
static wideval_t
wdiv(wideval_t wx, wideval_t wy)
{
#if WIDEVALUE_IS_WIDER
wideval_t q, dmy;
if (wdivmod0(wx, wy, &q, &dmy)) return q;
#endif
return v2w(divv(w2v(wx), w2v(wy)));
}
static wideval_t
wmod(wideval_t wx, wideval_t wy)
{
#if WIDEVALUE_IS_WIDER
wideval_t r, dmy;
if (wdivmod0(wx, wy, &dmy, &r)) return r;
#endif
return v2w(modv(w2v(wx), w2v(wy)));
}
static VALUE
num_exact(VALUE v)
{
VALUE tmp;
if (NIL_P(v)) {
rb_raise(rb_eTypeError, "can't convert nil into an exact number");
}
else if (RB_INTEGER_TYPE_P(v)) {
return v;
}
else if (RB_TYPE_P(v, T_RATIONAL)) {
goto rational;
}
else if (RB_TYPE_P(v, T_STRING)) {
goto typeerror;
}
else {
if ((tmp = rb_check_funcall(v, idTo_r, 0, NULL)) != Qundef) {
/* test to_int method availability to reject non-Numeric
* objects such as String, Time, etc which have to_r method. */
if (!rb_respond_to(v, idTo_int)) goto typeerror;
}
else if (!NIL_P(tmp = rb_check_to_int(v))) {
return tmp;
}
else {
goto typeerror;
}
}
if (RB_INTEGER_TYPE_P(tmp)) {
v = tmp;
}
else if (RB_TYPE_P(tmp, T_RATIONAL)) {
v = tmp;
rational:
if (RRATIONAL(v)->den == INT2FIX(1))
v = RRATIONAL(v)->num;
}
else {
typeerror:
rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into an exact number",
rb_obj_class(v));
}
return v;
}
/* time_t */
static wideval_t
rb_time_magnify(wideval_t w)
{
return wmul(w, WINT2FIXWV(TIME_SCALE));
}
static wideval_t
rb_time_unmagnify(wideval_t w)
{
return wquo(w, WINT2FIXWV(TIME_SCALE));
}
static VALUE
rb_time_unmagnify_to_float(wideval_t w)
{
VALUE v;
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(w)) {
wideint_t a, b, c;
a = FIXWV2WINT(w);
b = TIME_SCALE;
c = a / b;
if (c * b == a) {
return DBL2NUM((double)c);
}
v = DBL2NUM((double)FIXWV2WINT(w));
return quov(v, DBL2NUM(TIME_SCALE));
}
#endif
v = w2v(w);
if (RB_TYPE_P(v, T_RATIONAL))
return rb_Float(quov(v, INT2FIX(TIME_SCALE)));
else
return quov(v, DBL2NUM(TIME_SCALE));
}
static void
split_second(wideval_t timew, wideval_t *timew_p, VALUE *subsecx_p)
{
wideval_t q, r;
wdivmod(timew, WINT2FIXWV(TIME_SCALE), &q, &r);
*timew_p = q;
*subsecx_p = w2v(r);
}
static wideval_t
timet2wv(time_t t)
{
#if WIDEVALUE_IS_WIDER
if (TIMET_MIN == 0) {
uwideint_t wi = (uwideint_t)t;
if (wi <= FIXWV_MAX) {
return WINT2FIXWV(wi);
}
}
else {
wideint_t wi = (wideint_t)t;
if (FIXWV_MIN <= wi && wi <= FIXWV_MAX) {
return WINT2FIXWV(wi);
}
}
#endif
return v2w(TIMET2NUM(t));
}
#define TIMET2WV(t) timet2wv(t)
static time_t
wv2timet(wideval_t w)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(w)) {
wideint_t wi = FIXWV2WINT(w);
if (TIMET_MIN == 0) {
if (wi < 0)
rb_raise(rb_eRangeError, "negative value to convert into `time_t'");
if (TIMET_MAX < (uwideint_t)wi)
rb_raise(rb_eRangeError, "too big to convert into `time_t'");
}
else {
if (wi < TIMET_MIN || TIMET_MAX < wi)
rb_raise(rb_eRangeError, "too big to convert into `time_t'");
}
return (time_t)wi;
}
#endif
return NUM2TIMET(w2v(w));
}
#define WV2TIMET(t) wv2timet(t)
VALUE rb_cTime;
static int obj2int(VALUE obj);
static uint32_t obj2ubits(VALUE obj, size_t bits);
static VALUE obj2vint(VALUE obj);
static uint32_t month_arg(VALUE arg);
static VALUE validate_utc_offset(VALUE utc_offset);
static VALUE validate_zone_name(VALUE zone_name);
static void validate_vtm(struct vtm *vtm);
static uint32_t obj2subsecx(VALUE obj, VALUE *subsecx);
static VALUE time_gmtime(VALUE);
static VALUE time_localtime(VALUE);
static VALUE time_fixoff(VALUE);
static time_t timegm_noleapsecond(struct tm *tm);
static int tmcmp(struct tm *a, struct tm *b);
static int vtmcmp(struct vtm *a, struct vtm *b);
static const char *find_time_t(struct tm *tptr, int utc_p, time_t *tp);
static struct vtm *localtimew(wideval_t timew, struct vtm *result);
static int leap_year_p(long y);
#define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
bool ruby_tz_uptodate_p;
static struct tm *
rb_localtime_r(const time_t *t, struct tm *result)
{
#if defined __APPLE__ && defined __LP64__
if (*t != (time_t)(int)*t) return NULL;
#endif
if (!ruby_tz_uptodate_p) {
ruby_tz_uptodate_p = 1;
tzset();
}
#ifdef HAVE_GMTIME_R
result = localtime_r(t, result);
#else
{
struct tm *tmp = localtime(t);
if (tmp) *result = *tmp;
}
#endif
#if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
if (result) {
long gmtoff1 = 0;
long gmtoff2 = 0;
struct tm tmp = *result;
time_t t2;
t2 = mktime(&tmp);
# if defined(HAVE_STRUCT_TM_TM_GMTOFF)
gmtoff1 = result->tm_gmtoff;
gmtoff2 = tmp.tm_gmtoff;
# endif
if (*t + gmtoff1 != t2 + gmtoff2)
result = NULL;
}
#endif
return result;
}
#define LOCALTIME(tm, result) rb_localtime_r((tm), &(result))
#ifndef HAVE_STRUCT_TM_TM_GMTOFF
static struct tm *
rb_gmtime_r(const time_t *t, struct tm *result)
{
#ifdef HAVE_GMTIME_R
result = gmtime_r(t, result);
#else
struct tm *tmp = gmtime(t);
if (tmp) *result = *tmp;
#endif
#if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
if (result && *t != timegm(result)) {
return NULL;
}
#endif
return result;
}
# define GMTIME(tm, result) rb_gmtime_r((tm), &(result))
#endif
static const int common_year_yday_offset[] = {
-1,
-1 + 31,
-1 + 31 + 28,
-1 + 31 + 28 + 31,
-1 + 31 + 28 + 31 + 30,
-1 + 31 + 28 + 31 + 30 + 31,
-1 + 31 + 28 + 31 + 30 + 31 + 30,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
/* 1 2 3 4 5 6 7 8 9 10 11 */
};
static const int leap_year_yday_offset[] = {
-1,
-1 + 31,
-1 + 31 + 29,
-1 + 31 + 29 + 31,
-1 + 31 + 29 + 31 + 30,
-1 + 31 + 29 + 31 + 30 + 31,
-1 + 31 + 29 + 31 + 30 + 31 + 30,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
/* 1 2 3 4 5 6 7 8 9 10 11 */
};
static const int common_year_days_in_month[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static const int leap_year_days_in_month[] = {
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
#define M28(m) \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
(m),(m),(m),(m),(m),(m),(m),(m)
#define M29(m) \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
(m),(m),(m),(m),(m),(m),(m),(m),(m)
#define M30(m) \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m)
#define M31(m) \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
(m),(m),(m),(m),(m),(m),(m),(m),(m),(m), (m)
static const uint8_t common_year_mon_of_yday[] = {
M31(1), M28(2), M31(3), M30(4), M31(5), M30(6),
M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
};
static const uint8_t leap_year_mon_of_yday[] = {
M31(1), M29(2), M31(3), M30(4), M31(5), M30(6),
M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
};
#undef M28
#undef M29
#undef M30
#undef M31
#define D28 \
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
#define D29 \
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
#define D30 \
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
#define D31 \
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
static const uint8_t common_year_mday_of_yday[] = {
/* 1 2 3 4 5 6 7 8 9 10 11 12 */
D31, D28, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
};
static const uint8_t leap_year_mday_of_yday[] = {
D31, D29, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
};
#undef D28
#undef D29
#undef D30
#undef D31
static int
calc_tm_yday(long tm_year, int tm_mon, int tm_mday)
{
int tm_year_mod400 = (int)MOD(tm_year, 400);
int tm_yday = tm_mday;
if (leap_year_p(tm_year_mod400 + 1900))
tm_yday += leap_year_yday_offset[tm_mon];
else
tm_yday += common_year_yday_offset[tm_mon];
return tm_yday;
}
static wideval_t
timegmw_noleapsecond(struct vtm *vtm)
{
VALUE year1900;
VALUE q400, r400;
int year_mod400;
int yday;
long days_in400;
VALUE vdays, ret;
wideval_t wret;
year1900 = subv(vtm->year, INT2FIX(1900));
divmodv(year1900, INT2FIX(400), &q400, &r400);
year_mod400 = NUM2INT(r400);
yday = calc_tm_yday(year_mod400, vtm->mon-1, vtm->mday);
/*
* `Seconds Since the Epoch' in SUSv3:
* tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
* (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
* ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
*/
ret = LONG2NUM(vtm->sec
+ vtm->min*60
+ vtm->hour*3600);
days_in400 = yday
- 70*365
+ DIV(year_mod400 - 69, 4)
- DIV(year_mod400 - 1, 100)
+ (year_mod400 + 299) / 400;
vdays = LONG2NUM(days_in400);
vdays = addv(vdays, mulv(q400, INT2FIX(97)));
vdays = addv(vdays, mulv(year1900, INT2FIX(365)));
wret = wadd(rb_time_magnify(v2w(ret)), wmul(rb_time_magnify(v2w(vdays)), WINT2FIXWV(86400)));
wret = wadd(wret, v2w(vtm->subsecx));
return wret;
}
#define rb_fstring_usascii(str) rb_fstring_enc_cstr((str), rb_usascii_encoding())
static VALUE
zone_str(const char *zone)
{
const char *p;
int ascii_only = 1;
if (zone == NULL) {
return rb_fstring_usascii("(NO-TIMEZONE-ABBREVIATION)");
}
for (p = zone; *p; p++)
if (!ISASCII(*p)) {
ascii_only = 0;
break;
}
if (ascii_only) {
return rb_fstring_usascii(zone);
}
else {
return rb_fstring_enc_cstr(zone, rb_locale_encoding());
}
}
static void
gmtimew_noleapsecond(wideval_t timew, struct vtm *vtm)
{
VALUE v;
int n, x, y;
int wday;
VALUE timev;
wideval_t timew2, w, w2;
VALUE subsecx;
vtm->isdst = 0;
split_second(timew, &timew2, &subsecx);
vtm->subsecx = subsecx;
wdivmod(timew2, WINT2FIXWV(86400), &w2, &w);
timev = w2v(w2);
v = w2v(w);
wday = NUM2INT(modv(timev, INT2FIX(7)));
vtm->wday = (wday + 4) % 7;
n = NUM2INT(v);
vtm->sec = n % 60; n = n / 60;
vtm->min = n % 60; n = n / 60;
vtm->hour = n;
/* 97 leap days in the 400 year cycle */
divmodv(timev, INT2FIX(400*365 + 97), &timev, &v);
vtm->year = mulv(timev, INT2FIX(400));
/* n is the days in the 400 year cycle.
* the start of the cycle is 1970-01-01. */
n = NUM2INT(v);
y = 1970;
/* 30 years including 7 leap days (1972, 1976, ... 1996),
* 31 days in January 2000 and
* 29 days in February 2000
* from 1970-01-01 to 2000-02-29 */
if (30*365+7+31+29-1 <= n) {
/* 2000-02-29 or after */
if (n < 31*365+8) {
/* 2000-02-29 to 2000-12-31 */
y += 30;
n -= 30*365+7;
goto found;
}
else {
/* 2001-01-01 or after */
n -= 1;
}
}
x = n / (365*100 + 24);
n = n % (365*100 + 24);
y += x * 100;
if (30*365+7+31+29-1 <= n) {
if (n < 31*365+7) {
y += 30;
n -= 30*365+7;
goto found;
}
else
n += 1;
}
x = n / (365*4 + 1);
n = n % (365*4 + 1);
y += x * 4;
if (365*2+31+29-1 <= n) {
if (n < 365*2+366) {
y += 2;
n -= 365*2;
goto found;
}
else
n -= 1;
}
x = n / 365;
n = n % 365;
y += x;
found:
vtm->yday = n+1;
vtm->year = addv(vtm->year, INT2NUM(y));
if (leap_year_p(y)) {
vtm->mon = leap_year_mon_of_yday[n];
vtm->mday = leap_year_mday_of_yday[n];
}
else {
vtm->mon = common_year_mon_of_yday[n];
vtm->mday = common_year_mday_of_yday[n];
}
vtm->utc_offset = INT2FIX(0);
vtm->zone = rb_fstring_usascii("UTC");
}
static struct tm *
gmtime_with_leapsecond(const time_t *timep, struct tm *result)
{
#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
/* 4.4BSD counts leap seconds only with localtime, not with gmtime. */
struct tm *t;
int sign;
int gmtoff_sec, gmtoff_min, gmtoff_hour, gmtoff_day;
long gmtoff;
t = LOCALTIME(timep, *result);
if (t == NULL)
return NULL;