/
ruby_time.c
380 lines (348 loc) · 8.87 KB
/
ruby_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
/**********************************************************************
time.c -
$Author$
created at: Tue Dec 28 14:31:59 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
#include "ruby.h"
#include <time.h>
#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif
/* rbtime_timespec_new */
typedef uint64_t WIDEVALUE;
typedef WIDEVALUE wideval_t;
#ifdef PACKED_STRUCT_UNALIGNED
PACKED_STRUCT_UNALIGNED(struct vtm {
VALUE year; /* 2000 for example. Integer. */
VALUE subsecx; /* 0 <= subsecx < TIME_SCALE. possibly Rational. */
VALUE utc_offset; /* -3600 as -01:00 for example. possibly Rational. */
const char *zone; /* "JST", "EST", "EDT", etc. */
uint16_t yday : 9; /* 1..366 */
uint8_t mon : 4; /* 1..12 */
uint8_t mday : 5; /* 1..31 */
uint8_t hour : 5; /* 0..23 */
uint8_t min : 6; /* 0..59 */
uint8_t sec : 6; /* 0..60 */
uint8_t wday : 3; /* 0:Sunday, 1:Monday, ..., 6:Saturday 7:init */
uint8_t isdst : 2; /* 0:StandardTime 1:DayLightSavingTime 3:init */
});
PACKED_STRUCT_UNALIGNED(struct time_object {
wideval_t timew; /* time_t value * TIME_SCALE. possibly Rational. */
struct vtm vtm;
uint8_t gmt : 3; /* 0:utc 1:localtime 2:fixoff 3:init */
uint8_t tm_got : 1;
});
#else
struct vtm {
VALUE year; /* 2000 for example. Integer. */
int mon; /* 1..12 */
int mday; /* 1..31 */
int hour; /* 0..23 */
int min; /* 0..59 */
int sec; /* 0..60 */
VALUE subsecx; /* 0 <= subsecx < TIME_SCALE. possibly Rational. */
VALUE utc_offset; /* -3600 as -01:00 for example. possibly Rational. */
int wday; /* 0:Sunday, 1:Monday, ..., 6:Saturday */
int yday; /* 1..366 */
int isdst; /* 0:StandardTime 1:DayLightSavingTime */
const char *zone; /* "JST", "EST", "EDT", etc. */
};
struct time_object {
wideval_t timew; /* time_t value * TIME_SCALE. possibly Rational. */
struct vtm vtm;
int gmt; /* 0:utc 1:localtime 2:fixoff */
int tm_got;
};
#endif
VALUE
rb_time_succ(VALUE time);
VALUE
rbtime_timespec_new(const struct timespec *ts, int offset)
{
VALUE obj = rb_time_nano_new(ts->tv_sec, ts->tv_nsec);
if (offset) {
struct time_object *tobj;
tobj = DATA_PTR(obj);
tobj->tm_got = 0;
tobj->gmt = 2;
tobj->vtm.utc_offset = INT2FIX(offset);
tobj->vtm.zone = NULL;
}
return obj;
}
/* timespec_now */
void
timespec_now(struct timespec *ts)
{
#ifdef HAVE_CLOCK_GETTIME
if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
rb_sys_fail("clock_gettime");
}
#else
{
struct timeval tv;
if (gettimeofday(&tv, 0) < 0) {
rb_sys_fail("gettimeofday");
}
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
}
#endif
}
/* localtime_with_gmtoff_zone */
#ifdef HAVE_GMTIME_R
#define rb_gmtime_r(t, tm) gmtime_r((t), (tm))
#define rb_localtime_r(t, tm) localtime_r((t), (tm))
#else
static inline struct tm *
rb_gmtime_r(const time_t *tp, struct tm *result)
{
struct tm *t = gmtime(tp);
if (t) *result = *t;
return t;
}
static inline struct tm *
rb_localtime_r(const time_t *tp, struct tm *result)
{
struct tm *t = localtime(tp);
if (t) *result = *t;
return t;
}
#endif
static struct tm *
rb_localtime_r2(const time_t *t, struct tm *result)
{
#if defined __APPLE__ && defined __LP64__
if (*t != (time_t)(int)*t) return NULL;
#endif
result = rb_localtime_r(t, result);
#if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
if (result) {
long gmtoff1 = 0;
long gmtoff2 = 0;
struct tm tmp = *result;
time_t t2;
#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
gmtoff1 = result->tm_gmtoff;
#endif
t2 = mktime(&tmp);
#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
gmtoff2 = tmp.tm_gmtoff;
#endif
if (*t + gmtoff1 != t2 + gmtoff2) result = NULL;
}
#endif
return result;
}
#define LOCALTIME(tm, result) (tzset(), rb_localtime_r2((tm), &(result)))
#if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
static struct tm *
rb_gmtime_r2(const time_t *t, struct tm *result)
{
result = rb_gmtime_r(t, result);
#if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
if (result) {
struct tm tmp = *result;
time_t t2 = timegm(&tmp);
if (*t != t2) result = NULL;
}
#endif
return result;
}
#define GMTIME(tm, result) rb_gmtime_r2((tm), &(result))
#endif
struct tm *
localtime_with_gmtoff_zone(const time_t *t, struct tm *result, long *gmtoff,
const char **zone)
{
struct tm tm;
if (LOCALTIME(t, tm)) {
#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
*gmtoff = tm.tm_gmtoff;
#else
struct tm *u, *l;
long off;
struct tm tmbuf;
l = &tm;
u = GMTIME(t, tmbuf);
if (!u) return NULL;
if (l->tm_year != u->tm_year)
off = l->tm_year < u->tm_year ? -1 : 1;
else if (l->tm_mon != u->tm_mon)
off = l->tm_mon < u->tm_mon ? -1 : 1;
else if (l->tm_mday != u->tm_mday)
off = l->tm_mday < u->tm_mday ? -1 : 1;
else
off = 0;
off = off * 24 + l->tm_hour - u->tm_hour;
off = off * 60 + l->tm_min - u->tm_min;
off = off * 60 + l->tm_sec - u->tm_sec;
*gmtoff = off;
#endif
*result = tm;
return result;
}
return NULL;
}
#define NDIV(x,y) (-(-((x)+1)/(y))-1)
#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
static int
leap_year_p(int y)
{
return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0);
}
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
};
time_t
timegm_noleapsecond(struct tm *tm)
{
long tm_year = tm->tm_year;
int tm_yday = tm->tm_mday;
if (leap_year_p(tm_year + 1900))
tm_yday += leap_year_yday_offset[tm->tm_mon];
else
tm_yday += common_year_yday_offset[tm->tm_mon];
/*
* `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
*/
return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
(time_t)(tm_yday +
(tm_year-70)*365 +
DIV(tm_year-69,4) -
DIV(tm_year-1,100) +
DIV(tm_year+299,400))*86400;
}
void
tm_add_offset(struct tm *tm, long diff)
{
int sign, tsec, tmin, thour, tday;
if (diff < 0) {
sign = -1;
diff = -diff;
}
else {
sign = 1;
}
tsec = diff % 60;
diff = diff / 60;
tmin = diff % 60;
diff = diff / 60;
thour = diff % 24;
diff = diff / 24;
if (sign < 0) {
tsec = -tsec;
tmin = -tmin;
thour = -thour;
}
tday = 0;
if (tsec) {
tsec += tm->tm_sec;
if (tsec < 0) {
tsec += 60;
tmin -= 1;
}
if (60 <= tsec) {
tsec -= 60;
tmin += 1;
}
tm->tm_sec = tsec;
}
if (tmin) {
tmin += tm->tm_min;
if (tmin < 0) {
tmin += 60;
thour -= 1;
}
if (60 <= tmin) {
tmin -= 60;
thour += 1;
}
tm->tm_min = tmin;
}
if (thour) {
thour += tm->tm_hour;
if (thour < 0) {
thour += 24;
tday = -1;
}
if (24 <= thour) {
thour -= 24;
tday = 1;
}
tm->tm_hour = thour;
}
if (tday) {
if (tday < 0) {
if (tm->tm_mon == 1 && tm->tm_mday == 1) {
tm->tm_mday = 31;
tm->tm_mon = 12; /* December */
tm->tm_year = tm->tm_year - 1;
}
else if (tm->tm_mday == 1) {
const int *days_in_month = leap_year_p(tm->tm_year)
? leap_year_days_in_month
: common_year_days_in_month;
tm->tm_mon--;
tm->tm_mday = days_in_month[tm->tm_mon - 1];
}
else {
tm->tm_mday--;
}
}
else {
int leap = leap_year_p(tm->tm_year);
if (tm->tm_mon == 12 && tm->tm_mday == 31) {
tm->tm_year = tm->tm_year + 1;
tm->tm_mon = 1; /* January */
tm->tm_mday = 1;
}
else if (tm->tm_mday ==
(leap ? leap_year_days_in_month
: common_year_days_in_month)[tm->tm_mon - 1]) {
tm->tm_mon++;
tm->tm_mday = 1;
}
else {
tm->tm_mday++;
}
}
}
}