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
diff --git a/kernel/common/time.rb b/kernel/common/time.rb
index 05534db..174e5da 100644
--- a/kernel/common/time.rb
+++ b/kernel/common/time.rb
@@ -54,6 +54,10 @@ class Time
     :usec => 1,
   }
 
+  def initialize
+    gettimeofday
+  end
+
   #--
   # TODO: doesn't load nsec or ivars
   #++
@@ -91,6 +95,8 @@ class Time
   #++
 
   def _dump(limit = nil)
+    build_tm
+
     tm = @tm
     is_gmt = @is_gmt
 
@@ -148,7 +154,7 @@ class Time
 
     t = Time.allocate
     t.mktime(second, minute, hour, day, month, year, usec, isdst, false)
-    t.force_localtime
+    t
   end
 
   def self.gm(first, *args)
@@ -178,7 +184,7 @@ class Time
 
     t = Time.allocate
     t.mktime(second, minute, hour, day, month, year, usec, -1, true)
-    t.force_gmtime
+    t
   end
 
   def self.at(secs_or_time, msecs = nil)
@@ -190,10 +196,14 @@ class Time
   end
 
   def strftime(format)
+    build_tm
+
     __strftime__(@tm, format.to_str)
   end
 
   def inspect
+    build_tm
+
     if @is_gmt
       strftime("%a %b %d %H:%M:%S UTC %Y")
     else
@@ -251,30 +261,44 @@ class Time
   end
 
   def hour
+    build_tm
+
     @tm[2]
   end
 
   def min
+    build_tm
+
     @tm[1]
   end
 
   def sec
+    build_tm
+
     @tm[0]
   end
 
   def day
+    build_tm
+
     @tm[3]
   end
 
   def year
+    build_tm
+
     @tm[5] + 1900
   end
 
   def yday
+    build_tm
+
     @tm[7] + 1
   end
 
   def wday
+    build_tm
+
     @tm[6]
   end
 
@@ -283,6 +307,8 @@ class Time
   end
 
   def mon
+    build_tm
+
     @tm[4] + 1
   end
 
@@ -336,18 +362,26 @@ class Time
   end
 
   def localtime
-    force_localtime if @is_gmt
+    if @is_gmt
+      @is_gmt = false
+      @has_tm = false
+    end
 
     self
   end
 
   def gmtime
-    force_gmtime unless @is_gmt
+    unless @is_gmt
+      @is_gmt = true
+      @has_tm = false
+    end
 
     self
   end
 
   def dst?
+    build_tm
+
     !@tm[8].zero?
   end
 
@@ -363,20 +397,6 @@ class Time
     seconds ^ usec
   end
 
-  def force_localtime
-    time_switch false
-    @is_gmt = false
-
-    self
-  end
-
-  def force_gmtime
-    time_switch true
-    @is_gmt = true
-
-    self
-  end
-
   def mktime(sec, min, hour, mday, mon, year, usec, isdst, from_gmt)
     sec  = sec.to_i
     min  = min.to_i
@@ -398,6 +418,8 @@ class Time
     end
 
     @timeval = time_mktime(sec, min, hour, mday, mon, year, usec, isdst, from_gmt)
+    @is_gmt = from_gmt
+
     raise ArgumentError, "time out of range" if @timeval.first == -1
 
     self
@@ -422,11 +444,15 @@ class Time
     usec = usec % 1000000
 
     @timeval = [sec, usec]
+    @is_gmt = want_gmt
+    @has_tm = false
 
-    if want_gmt
-      force_gmtime
-    else
-      force_localtime
+    self
+  end
+
+  def build_tm
+    unless @has_tm
+      time_switch @is_gmt
     end
   end
 
diff --git a/vm/builtin/time.cpp b/vm/builtin/time.cpp
index 5cbca71..a5f83c1 100644
--- a/vm/builtin/time.cpp
+++ b/vm/builtin/time.cpp
@@ -22,8 +22,14 @@ namespace rubinius {
   Time* Time::create(STATE) {
     Time* tm = state->new_object<Time>(G(time_class));
 
-    tm->gettimeofday(state);
-    tm->time_switch(state, Qfalse);
+    /* update Time::TIMEVAL_FIELDS when changing order of fields */
+    Array* ary = Array::create(state, 2);
+    ary->set(state, 0, Integer::from(state, 0));
+    ary->set(state, 1, Integer::from(state, 0));
+
+    tm->timeval(state, ary);
+    tm->has_tm(state, Qfalse);
+    tm->is_gmt(state, Qfalse);
 
     return tm;
   }
@@ -44,11 +50,8 @@ namespace rubinius {
     ::gettimeofday(&tv, NULL);
 
     /* update Time::TIMEVAL_FIELDS when changing order of fields */
-    Array* ary = Array::create(state, 2);
-    ary->set(state, 0, Integer::from(state, tv.tv_sec));
-    ary->set(state, 1, Integer::from(state, tv.tv_usec));
-
-    this->timeval(state, ary);
+    timeval_->set(state, 0, Integer::from(state, tv.tv_sec));
+    timeval_->set(state, 1, Integer::from(state, tv.tv_usec));
 
     return this;
   }
@@ -88,7 +91,7 @@ namespace rubinius {
 #endif
 
     this->tm(state, ary);
-    this->is_gmt(state, gmt);
+    this->has_tm(state, Qtrue);
 
     return this;
   }
diff --git a/vm/builtin/time.hpp b/vm/builtin/time.hpp
index 4749671..bc0bb9e 100644
--- a/vm/builtin/time.hpp
+++ b/vm/builtin/time.hpp
@@ -21,6 +21,7 @@ namespace rubinius {
     Array* timeval_; // slot
     Array* tm_;      // slot
     Object* is_gmt_;  // slot
+    Object* has_tm_;  // slot
 
   public:
     /* accessors */
@@ -28,6 +29,7 @@ namespace rubinius {
     attr_accessor(timeval, Array);
     attr_accessor(tm, Array);
     attr_accessor(is_gmt, Object);
+    attr_accessor(has_tm, Object);
 
     /* interface */