/
time.cpp
318 lines (251 loc) · 8.12 KB
/
time.cpp
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
#include "config.h"
#include "vm.hpp"
#include "vm/object_utils.hpp"
#include "objectmemory.hpp"
#include "primitives.hpp"
#include "builtin/array.hpp"
#include "builtin/class.hpp"
#include "builtin/exception.hpp"
#include "builtin/integer.hpp"
#include "builtin/string.hpp"
#include "builtin/time.hpp"
#include "ontology.hpp"
#include "util/time64.h"
#include "util/strftime.h"
#include <sys/time.h>
#include <time.h>
#include "windows_compat.h"
#include "configuration.hpp"
namespace rubinius {
void Time::init(STATE) {
GO(time_class).set(ontology::new_class(state, "Time", G(object)));
G(time_class)->set_object_type(state, TimeType);
}
Time* Time::now(STATE, Object* self) {
Time* tm = state->new_object_dirty<Time>(as<Class>(self));
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;
::clock_gettime(CLOCK_REALTIME, &ts);
tm->seconds_ = ts.tv_sec;
tm->nanoseconds_ = ts.tv_nsec;
#else
struct timeval tv;
/* don't fill in the 2nd argument here. getting the timezone here
* this way is not portable and broken anyway.
*/
::gettimeofday(&tv, NULL);
tm->seconds_ = tv.tv_sec;
tm->nanoseconds_ = tv.tv_usec * 1000;
#endif
tm->decomposed(state, nil<Array>());
tm->is_gmt(state, cFalse);
tm->offset(state, nil<Fixnum>());
return tm;
}
// Taken from MRI
#define NDIV(x,y) (-(-((x)+1)/(y))-1)
#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
Time* Time::specific(STATE, Object* self, Integer* sec, Integer* nsec,
Object* gmt, Object* offset)
{
Time* tm = state->new_object_dirty<Time>(as<Class>(self));
tm->seconds_ = sec->to_long_long();
tm->nanoseconds_ = nsec->to_native();
// Do a little overflow cleanup.
if(tm->nanoseconds_ >= 1000000000) {
tm->seconds_ += tm->nanoseconds_ / 1000000000;
tm->nanoseconds_ %= 1000000000;
}
if(tm->nanoseconds_ < 0) {
tm->seconds_ += NDIV(tm->nanoseconds_, 1000000000);
tm->nanoseconds_ = NMOD(tm->nanoseconds_, 1000000000);
}
tm->decomposed(state, nil<Array>());
tm->is_gmt(state, RBOOL(CBOOL(gmt)));
tm->offset(state, offset);
return tm;
}
Time* Time::from_array(STATE, Object* self,
Fixnum* sec, Fixnum* min, Fixnum* hour,
Fixnum* mday, Fixnum* mon, Fixnum* year, Fixnum* nsec,
Fixnum* isdst, Object* from_gmt, Object* offset,
Fixnum* offset_sec, Fixnum* offset_nsec) {
struct tm64 tm;
tm.tm_sec = sec->to_native();
if(tm.tm_sec < 0 || tm.tm_sec > 60) {
Exception::argument_error(state, "sec must be in 0..60");
}
tm.tm_min = min->to_native();
if(tm.tm_min < 0 || tm.tm_min > 60) {
Exception::argument_error(state, "min must be in 0..60");
}
tm.tm_hour = hour->to_native();
if(tm.tm_hour < 0 || tm.tm_hour > 24) {
Exception::argument_error(state, "hour must be in 0..24");
}
tm.tm_mday = mday->to_native();
if(tm.tm_mday < 1 || tm.tm_mday > 31) {
Exception::argument_error(state, "mday must be in 1..31");
}
tm.tm_mon = mon->to_native() - 1;
if(tm.tm_mon < 0 || tm.tm_mon > 11) {
Exception::argument_error(state, "mon must be in 0..11");
}
tm.tm_wday = -1;
tm.tm_gmtoff = 0;
tm.tm_zone = 0;
tm.tm_year = year->to_long_long();
tm.tm_isdst = isdst->to_native();
time64_t seconds = -1;
if(CBOOL(from_gmt) || !offset->nil_p()) {
seconds = ::timegm64(&tm);
} else {
tzset();
seconds = ::timelocal64(&tm);
}
Time* obj = state->new_object_dirty<Time>(as<Class>(self));
obj->seconds_ = seconds;
obj->nanoseconds_ = nsec->to_native();
obj->is_gmt(state, RBOOL(CBOOL(from_gmt)));
if(!offset->nil_p()) {
obj->seconds_ -= offset_sec->to_long_long();
obj->nanoseconds_ -= offset_nsec->to_native();
// Deal with underflow wrapping
if(obj->nanoseconds_ < 0) {
obj->seconds_ += NDIV(obj->nanoseconds_, 1000000000);
obj->nanoseconds_ = NMOD(obj->nanoseconds_, 1000000000);
}
obj->offset(state, offset);
} else {
obj->offset(state, nil<Fixnum>());
}
obj->decomposed(state, nil<Array>());
return obj;
}
Time* Time::dup(STATE, Object* self, Time* other) {
Time* tm = state->new_object_dirty<Time>(as<Class>(self));
tm->seconds_ = other->seconds_;
tm->nanoseconds_ = other->nanoseconds_;
tm->decomposed(state, other->decomposed_);
tm->is_gmt(state, other->is_gmt_);
tm->offset(state, other->offset_);
return tm;
}
struct tm64 Time::get_tm() {
time64_t seconds = seconds_;
struct tm64 tm = {0};
if(Fixnum* off = try_as<Fixnum>(offset_)) {
seconds += off->to_long_long();
gmtime64_r(&seconds, &tm);
} else if(CBOOL(is_gmt_)) {
gmtime64_r(&seconds, &tm);
} else {
tzset();
localtime64_r(&seconds, &tm);
}
return tm;
}
Object* Time::utc_offset(STATE) {
if(CBOOL(is_gmt_)) {
return Fixnum::from(0);
} else if(!offset_->nil_p()) {
return offset_;
}
native_int off;
#ifdef HAVE_TM_NAME
struct tm tm = get_tm();
off = -tm.tm_tzadj;
#else /* !HAVE_TM_NAME */
#ifdef HAVE_TM_ZONE
#ifdef HAVE_TM_GMTOFF
struct tm64 tm = get_tm();
off = tm.tm_gmtoff;
#else
off = _timezone;
#endif
#else /* !HAVE_TM_ZONE */
#if HAVE_VAR_TIMEZONE
#if HAVE_VAR_ALTZONE
off = -(daylight ? timezone : altzone);
#else
off = -timezone;
#endif
#else /* !HAVE_VAR_TIMEZONE */
#ifdef HAVE_GETTIMEOFDAY
gettimeofday(&tv, &zone);
off = -zone.tz_minuteswest * 60;
#else
/* no timezone info, then calc by myself */
{
struct tm utc;
time_t now;
time(&now);
utc = *gmtime(&now);
off = (now - mktime(&utc));
}
#endif
#endif /* !HAVE_VAR_TIMEZONE */
#endif /* !HAVE_TM_ZONE */
#endif /* !HAVE_TM_NAME */
return Fixnum::from(off);
}
Array* Time::calculate_decompose(STATE) {
if(!decomposed_->nil_p()) return decomposed_;
struct tm64 tm = get_tm();
/* update Time::TM_FIELDS when changing order of fields */
Array* ary = Array::create(state, 11);
ary->set(state, 0, Integer::from(state, tm.tm_sec));
ary->set(state, 1, Integer::from(state, tm.tm_min));
ary->set(state, 2, Integer::from(state, tm.tm_hour));
ary->set(state, 3, Integer::from(state, tm.tm_mday));
ary->set(state, 4, Integer::from(state, tm.tm_mon + 1));
ary->set(state, 5, Integer::from(state, tm.tm_year));
ary->set(state, 6, Integer::from(state, tm.tm_wday));
ary->set(state, 7, Integer::from(state, tm.tm_yday + 1));
ary->set(state, 8, RBOOL(tm.tm_isdst));
if(offset_->nil_p() && tm.tm_zone) {
ary->set(state, 9, String::create(state, tm.tm_zone));
} else {
ary->set(state, 9, cNil);
}
// Cache it.
decomposed(state, ary);
return ary;
}
#define STRFTIME_STACK_BUF 128
String* Time::strftime(STATE, String* format) {
struct tm64 tm = get_tm();
struct timespec64 ts;
ts.tv_sec = seconds_;
ts.tv_nsec = nanoseconds_;
int off = 0;
if(Fixnum* offset = try_as<Fixnum>(utc_offset(state))) {
off = offset->to_int();
}
char stack_str[STRFTIME_STACK_BUF];
char* malloc_str = 0;
size_t chars = ::strftime_extended(stack_str, STRFTIME_STACK_BUF,
format->c_str(state), &tm, &ts, CBOOL(is_gmt_) ? 1 : 0,
off);
size_t buf_size = format->byte_size() * 2;
String* result = 0;
if(chars == 0 && format->byte_size() > 0) {
malloc_str = (char*)malloc(buf_size);
chars = ::strftime_extended(malloc_str, buf_size,
format->c_str(state), &tm, &ts, CBOOL(is_gmt_) ? 1 : 0,
off);
while (chars == 0 && format->byte_size() > 0) {
buf_size *= 2;
malloc_str = (char*)realloc(malloc_str, buf_size);
chars = ::strftime_extended(malloc_str, buf_size,
format->c_str(state), &tm, &ts, CBOOL(is_gmt_) ? 1 : 0,
off);
}
result = String::create(state, malloc_str, chars);
free(malloc_str);
} else {
result = String::create(state, stack_str, chars);
}
return result;
}
}