forked from rubinius/rubinius
/
time.cpp
214 lines (166 loc) · 5.42 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
#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 "util/time.h"
#include <sys/time.h>
#include <time.h>
#include "windows_compat.h"
namespace rubinius {
void Time::init(STATE) {
GO(time_class).set(state->new_class("Time", G(object)));
G(time_class)->set_object_type(state, TimeType);
}
Time* Time::now(STATE, Object* self) {
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);
Time* tm = state->new_object<Time>(as<Class>(self));
tm->seconds_ = tv.tv_sec;
tm->microseconds_ = tv.tv_usec;
tm->is_gmt(state, Qfalse);
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* usec,
Object* gmt)
{
Time* tm = state->new_object<Time>(as<Class>(self));
if(sizeof(time_t) == sizeof(long long)) {
tm->seconds_ = sec->to_long_long();
tm->microseconds_ = usec->to_long_long();
} else {
tm->seconds_ = sec->to_native();
tm->microseconds_ = usec->to_native();
}
// Do a little overflow cleanup.
if(tm->microseconds_ >= 1000000) {
tm->seconds_ += tm->microseconds_ / 1000000;
tm->microseconds_ %= 1000000;
}
if(tm->microseconds_ < 0) {
tm->seconds_ += NDIV(tm->microseconds_,1000000);
tm->microseconds_ = NMOD(tm->microseconds_, 1000000);
}
tm->is_gmt(state, RTEST(gmt) ? Qtrue : Qfalse);
return tm;
}
Time* Time::from_array(STATE, Object* self,
Fixnum* sec, Fixnum* min, Fixnum* hour,
Fixnum* mday, Fixnum* mon, Fixnum* year, Fixnum* usec,
Fixnum* isdst, Object* from_gmt) {
struct tm 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;
#ifndef RBX_WINDOWS
tm.tm_gmtoff = 0;
tm.tm_zone = 0;
#endif
tm.tm_year = year->to_native() - 1900;
tm.tm_isdst = isdst->to_native();
time_t seconds = -1;
if(RTEST(from_gmt)) {
seconds = ::timegm(&tm);
} else {
tzset();
seconds = ::mktime(&tm);
}
int err = 0;
if(seconds == -1) {
int utc_p = from_gmt->true_p() ? 1 : 0;
seconds = mktime_extended(&tm, utc_p, &err);
}
if(err) return (Time*)Primitives::failure();
Time* obj = state->new_object<Time>(as<Class>(self));
obj->seconds_ = seconds;
obj->microseconds_ = usec->to_native();
obj->is_gmt(state, RTEST(from_gmt) ? Qtrue : Qfalse);
return obj;
}
Time* Time::dup(STATE) {
Time* tm = state->new_object<Time>(class_object(state));
tm->seconds_ = seconds_;
tm->microseconds_ = microseconds_;
tm->is_gmt(state, is_gmt_);
return tm;
}
Array* Time::calculate_decompose(STATE, Object* use_gmt) {
if(!decomposed_->nil_p()) return decomposed_;
time_t seconds = seconds_;
struct tm tm = {0};
if(RTEST(use_gmt)) {
gmtime_r(&seconds, &tm);
} else {
tzset();
localtime_r(&seconds, &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 + 1900));
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, tm.tm_isdst ? Qtrue : Qfalse);
#ifdef HAVE_STRUCT_TM_TM_ZONE
ary->set(state, 9, String::create(state, tm.tm_zone));
#else
ary->set(state, 9, Qnil);
#endif
// Cache it.
decomposed(state, ary);
return ary;
}
#define MAX_STRFTIME_OUTPUT 128
String* Time::strftime(STATE, String* format) {
struct tm tm;
char str[MAX_STRFTIME_OUTPUT];
int is_gmt = 0;
time_t seconds = seconds_;
if(RTEST(is_gmt_)) {
is_gmt = 1;
gmtime_r(&seconds, &tm);
} else {
tzset();
localtime_r(&seconds, &tm);
}
struct timespec ts = { seconds, 0 };
size_t chars = ::strftime_extended(str, MAX_STRFTIME_OUTPUT,
format->c_str(state), &tm, &ts, is_gmt);
str[MAX_STRFTIME_OUTPUT-1] = 0;
return String::create(state, str, chars);
}
}