-
Notifications
You must be signed in to change notification settings - Fork 49
/
DateTime.xs
308 lines (241 loc) · 6.74 KB
/
DateTime.xs
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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include <stdlib.h>
/* This file is generated by tools/leap_seconds_header.pl */
#include "leap_seconds.h"
# if defined(WIN32) || defined(PERL_DARWIN)
# if PERL_VERSION_GE(5, 22, 0)
# define dt_isfinite Perl_isfinite
# endif
# else
# define dt_isfinite Perl_isfinite
# endif
#define DAYS_PER_400_YEARS 146097
#define DAYS_PER_4_YEARS 1461
#define MARCH_1 306
#define SECONDS_PER_DAY 86400
const int PREVIOUS_MONTH_DOY[12] = { 0,
31,
59,
90,
120,
151,
181,
212,
243,
273,
304,
334 };
const int PREVIOUS_MONTH_DOLY[12] = { 0,
31,
60,
91,
121,
152,
182,
213,
244,
274,
305,
335 };
IV
_real_is_leap_year(IV y) {
/* See http://www.perlmonks.org/?node_id=274247 for where this silliness
comes from */
return (y % 4) ? 0 : (y % 100) ? 1 : (y % 400) ? 0 : 1;
}
MODULE = DateTime PACKAGE = DateTime
PROTOTYPES: ENABLE
void
_rd2ymd(self, d, extra = 0)
IV d;
IV extra;
PREINIT:
IV y, m;
IV c;
IV quarter;
IV yadj = 0;
IV dow, doy, doq;
IV rd_days;
PPCODE:
rd_days = d;
d += MARCH_1;
if (d <= 0) {
yadj = -1 * (((-1 * d) / DAYS_PER_400_YEARS) + 1);
d -= yadj * DAYS_PER_400_YEARS;
}
/* c is century */
c = ((d * 4) - 1) / DAYS_PER_400_YEARS;
d -= c * DAYS_PER_400_YEARS / 4;
y = ((d * 4) - 1) / DAYS_PER_4_YEARS;
d -= y * DAYS_PER_4_YEARS / 4;
m = ((d * 12) + 1093) / 367;
d -= ((m * 367) - 1094) / 12;
y += (c * 100) + (yadj * 400);
if (m > 12) {
++y;
m -= 12;
}
EXTEND(SP, extra ? 7 : 3);
mPUSHi(y);
mPUSHi(m);
mPUSHi(d);
if (extra) {
quarter = ( ( 1.0 / 3.1 ) * m ) + 1;
dow = rd_days % 7;
if ( dow <= 0 ) {
dow += 7;
}
mPUSHi(dow);
if (_real_is_leap_year(y)) {
doy = PREVIOUS_MONTH_DOLY[m - 1] + d;
doq = doy - PREVIOUS_MONTH_DOLY[ (3 * quarter) - 3 ];
} else {
doy = PREVIOUS_MONTH_DOY[m - 1] + d;
doq = doy - PREVIOUS_MONTH_DOY[ (3 * quarter ) - 3 ];
}
mPUSHi(doy);
mPUSHi(quarter);
mPUSHi(doq);
}
void
_ymd2rd(self, y, m, d)
IV y;
IV m;
IV d;
PREINIT:
IV adj;
PPCODE:
if (m <= 2) {
adj = (14 - m) / 12;
y -= adj;
m += 12 * adj;
} else if (m > 14) {
adj = (m - 3) / 12;
y += adj;
m -= 12 * adj;
}
if (y < 0) {
adj = (399 - y) / 400;
d -= DAYS_PER_400_YEARS * adj;
y += 400 * adj;
}
d += (m * 367 - 1094) /
12 + y % 100 * DAYS_PER_4_YEARS /
4 + (y / 100 * 36524 + y / 400) - MARCH_1;
EXTEND(SP, 1);
mPUSHi(d);
void
_seconds_as_components(self, secs, utc_secs = 0, secs_modifier = 0)
IV secs;
IV utc_secs;
IV secs_modifier;
PREINIT:
IV h, m, s;
PPCODE:
secs -= secs_modifier;
h = secs / 3600;
secs -= h * 3600;
m = secs / 60;
s = secs - (m * 60);
if (utc_secs >= SECONDS_PER_DAY) {
if (utc_secs >= SECONDS_PER_DAY + 1) {
/* If we just use %d and the IV, we get a warning that IV is
not an int. */
croak("Invalid UTC RD seconds value: %s", SvPV_nolen(newSViv(utc_secs)));
}
s += (utc_secs - SECONDS_PER_DAY) + 60;
m = 59;
h--;
if (h < 0) {
h = 23;
}
}
EXTEND(SP, 3);
mPUSHi(h);
mPUSHi(m);
mPUSHi(s);
#ifdef dt_isfinite
void
_normalize_tai_seconds(self, days, secs)
SV* days;
SV* secs;
PPCODE:
if (dt_isfinite(SvNV(days)) && dt_isfinite(SvNV(secs))) {
IV d = SvIV(days);
IV s = SvIV(secs);
IV adj;
if (s < 0) {
adj = (s - (SECONDS_PER_DAY - 1)) / SECONDS_PER_DAY;
} else {
adj = s / SECONDS_PER_DAY;
}
d += adj;
s -= adj * SECONDS_PER_DAY;
sv_setiv(days, (IV) d);
sv_setiv(secs, (IV) s);
}
void
_normalize_leap_seconds(self, days, secs)
SV* days;
SV* secs;
PPCODE:
if (dt_isfinite(SvNV(days)) && dt_isfinite(SvNV(secs))) {
IV d = SvIV(days);
IV s = SvIV(secs);
IV day_length;
while (s < 0) {
SET_DAY_LENGTH(d - 1, day_length);
s += day_length;
d--;
}
SET_DAY_LENGTH(d, day_length);
while (s > day_length - 1) {
s -= day_length;
d++;
SET_DAY_LENGTH(d, day_length);
}
sv_setiv(days, (IV) d);
sv_setiv(secs, (IV) s);
}
#endif /* ifdef dt_isfinite */
void
_time_as_seconds(self, h, m, s)
IV h;
IV m;
IV s;
PPCODE:
EXTEND(SP, 1);
mPUSHi(h * 3600 + m * 60 + s);
void
_is_leap_year(self, y)
IV y;
PPCODE:
EXTEND(SP, 1);
mPUSHi(_real_is_leap_year(y));
void
_day_length(self, utc_rd)
IV utc_rd;
PPCODE:
IV day_length;
SET_DAY_LENGTH(utc_rd, day_length);
EXTEND(SP, 1);
mPUSHi(day_length);
void
_day_has_leap_second(self, utc_rd)
IV utc_rd;
PPCODE:
IV day_length;
SET_DAY_LENGTH(utc_rd, day_length);
EXTEND(SP, 1);
mPUSHi(day_length > 86400 ? 1 : 0);
void
_accumulated_leap_seconds(self, utc_rd)
IV utc_rd;
PPCODE:
IV leap_seconds;
SET_LEAP_SECONDS(utc_rd, leap_seconds);
EXTEND(SP, 1);
mPUSHi(leap_seconds);