Module: RubyRunJs::JsDateMethods

Extended by:
Helper
Defined in:
lib/ruby_run_js/object_methods/js_date.rb

Constant Summary collapse

CUM =
[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]

Class Method Summary collapse

Methods included from Helper

check_object, get_member, get_member_dot, is_accessor_descriptor, is_callable, is_data_descriptor, is_generic_descriptor, is_primitive, make_error, strict_equality

Methods included from ConversionHelper

#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32

Class Method Details

._date_from_time(t) ⇒ Object



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
380
381
382
383
384
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 354

def _date_from_time(t)
  month = _month_from_time(t)
  day = _day_within_year(t)
  leap_year = _in_leap_year(t)
  case month
  when 0
    day + 1
  when 1
    day - 30
  when 2
    day - 58 - leap_year
  when 3
    day - 89 - leap_year
  when 4
    day - 119 - leap_year
  when 5
    day - 150 - leap_year
  when 6
    day - 180 - leap_year
  when 7
    day - 211 - leap_year
  when 8
    day - 242 - leap_year
  when 9
    day - 272 - leap_year
  when 10
    day - 303 - leap_year
  when 11
    day - 333 - leap_year
  end
end

._day(t) ⇒ Object



288
289
290
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 288

def _day(t)
  t / _ms_per_day
end

._day_from_year(y) ⇒ Object



299
300
301
302
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 299

def _day_from_year(y)
  y = y.to_i
  365 * (y - 1970) + (y - 1969) / 4 - (y - 1901) / 100 + (y - 1601) / 400
end

._day_within_year(t) ⇒ Object



331
332
333
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 331

def _day_within_year(t)
  _day(t) - _day_from_year(_year_from_time(t))
end

._days_in_year(y) ⇒ Object



292
293
294
295
296
297
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 292

def _days_in_year(y)
  return 365 if y % 4 != 0
  return 366 if y % 4 == 0 && y % 100 != 0
  return 365 if y % 100 == 0 && y % 400 != 0
  366
end

._hour_from_time(t) ⇒ Object



390
391
392
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 390

def _hour_from_time(t)
  (t / _ms_per_hour) % 24
end

._in_leap_year(t) ⇒ Object



326
327
328
329
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 326

def _in_leap_year(t)
  year = _year_from_time(t)
  _days_in_year(year) == 366 ? 1 : 0
end

._local_to_utc(t) ⇒ Object

DaylightSavingTime is ignored



450
451
452
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 450

def _local_to_utc(t)
  t - localTZA
end

._make_date(day, time) ⇒ Object



431
432
433
434
435
436
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 431

def _make_date(day, time)
  if !day.finite? || !time.finite?
    return Float::NAN
  end
  day * 24 * 60 * 60 * 1000 + time
end

._make_day(year, month, date) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 419

def _make_day(year, month, date)
  if !year.finite? || !month.finite? || !date.finite?
    return Float::NAN
  end
  y = to_integer(year)
  m = to_integer(month)
  dt = to_integer(date)
  ym = y + (m / 12).floor
  mn = m % 12
  _day_from_year(ym) + CUM[mn] + dt - 1 + ((_days_in_year(ym) == 366 && mn >= 2) ? 1 : 0)
end

._make_time(hour, min, sec, ms) ⇒ Object



406
407
408
409
410
411
412
413
414
415
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 406

def _make_time(hour, min, sec, ms)
  if !hour.finite? || !min.finite? || !sec.finite? || !ms.finite?
    return Float::NAN
  end
  h = to_integer(hour)
  m = to_integer(min)
  s = to_integer(sec)
  milli = to_integer(ms)
  h * 60 * 60 * 1000 + m * 60 * 1000 + s * 1000 + milli
end

._min_from_time(t) ⇒ Object



394
395
396
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 394

def _min_from_time(t)
  (t / _ms_per_minute) % 60
end

._month_from_time(t) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 335

def _month_from_time(t)
  day_within_year = _day_within_year(t)
  leap_year = _in_leap_year(t)

  return 0 if day_within_year < 31
  day_within_year -= leap_year
  return 1 if day_within_year < 59
  return 2 if day_within_year < 90
  return 3 if day_within_year < 120
  return 4 if day_within_year < 151
  return 5 if day_within_year < 181
  return 6 if day_within_year < 212
  return 7 if day_within_year < 243
  return 8 if day_within_year < 273
  return 9 if day_within_year < 304
  return 10 if day_within_year < 334
  return 11
end

._ms_from_time(t) ⇒ Object



402
403
404
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 402

def _ms_from_time(t)
  t % 1000
end

._ms_per_dayObject



272
273
274
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 272

def _ms_per_day
  24 * 60 * 60 * 1000
end

._ms_per_hourObject



276
277
278
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 276

def _ms_per_hour
  60 * 60 * 1000
end

._ms_per_minuteObject



280
281
282
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 280

def _ms_per_minute
  60 * 1000
end

._ms_per_secondObject



284
285
286
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 284

def _ms_per_second
  1000
end

._sec_from_time(t) ⇒ Object



398
399
400
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 398

def _sec_from_time(t)
  (t / _ms_per_second) % 60
end

._time_clip(time) ⇒ Object



438
439
440
441
442
443
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 438

def _time_clip(time)
  if !time.finite? || time.abs > 8.64e15
    return Float::NAN
  end
  to_integer(time)
end

._time_from_year(y) ⇒ Object



304
305
306
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 304

def _time_from_year(y)
  _day_from_year(y) * _ms_per_day
end

._utc_to_local(t) ⇒ Object

DaylightSavingTime is ignored



455
456
457
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 455

def _utc_to_local(t)
  t + localTZA
end

._week_day(t) ⇒ Object



386
387
388
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 386

def _week_day(t)
  (_day(t) + 4 ) % 7
end

._year_from_time(t) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 308

def _year_from_time(t)
  guess = 1970 + t / 31556908800  # msPerYear
  gt = _time_from_year(guess)
  if gt <= t
    while gt <= t
      guess += 1
      gt = _time_from_year(guess)
    end
    return guess - 1
  else
    while gt > t
      guess -= 1
      gt = _time_from_year(guess)
    end
    return guess
  end
end

.check_date(obj) ⇒ Object



73
74
75
76
77
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 73

def check_date(obj)
  if obj.js_class != 'Date'
    raise make_error('TypeError', 'this is not a Date object')
  end
end

.constructor(builtin, this, *args) ⇒ Object



10
11
12
13
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 10

def constructor(builtin, this, *args)
  obj = constructor_new(builtin, this, )
  obj.get('toString').call(obj)
end

.constructor_new(builtin, this, *args) ⇒ Object



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
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 15

def constructor_new(builtin, this, *args)
  if args.length == 0
    return builtin.new_date_by_ruby_time(Time.now)
  end

  if args.length >= 2
    y = to_number(args[0])
    m = to_number(args[1])
    dt = args[2].nil? ? 1.0 : to_number(args[2]) 
    h = args[3].nil? ? 0.0 : to_number(args[3])
    min = args[4].nil? ? 0.0 : to_number(args[4])
    s = args[5].nil? ? 0.0 : to_number(args[5])
    milli = args[6].nil? ? 0.0 : to_number(args[6])
    y_int = to_integer(y)
    if y == y && y_int >= 0 && y_int <= 99
      yr = y_int + 1900
    else
      yr = y
    end
    finalDate = _make_date(_make_day(yr, m, dt), _make_time(h, min, s, milli))
    return builtin.new_date(_time_clip(_local_to_utc(finalDate)))
  end

  v = to_primitive(args[0])
  if v.js_type == :String
    time_value = constructor_parse(builtin, this, v).value
  else
    time_value = to_number(v)
  end
  builtin.new_date(_time_clip(time_value))
end

.constructor_now(builtin, this) ⇒ Object



69
70
71
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 69

def constructor_now(builtin, this)
  Time.now.to_f.round(3) * 1000
end

.constructor_parse(builtin, this, str) ⇒ Object



47
48
49
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 47

def constructor_parse(builtin, this, str)
  builtin.new_date_by_ruby_time(Time.parse(str))
end

.constructor_UTC(builtin, this, *args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 51

def constructor_UTC(builtin, this, *args)
  y = to_number(args[0])
  m = to_number(args[1])
  dt = args[2].nil? ? 1.0 : to_number(args[2]) 
  h = args[3].nil? ? 0.0 : to_number(args[3])
  min = args[4].nil? ? 0.0 : to_number(args[4])
  s = args[5].nil? ? 0.0 : to_number(args[5])
  milli = args[6].nil? ? 0.0 : to_number(args[6])
  y_int = to_integer(y)
  if y == y && y_int >= 0 && y_int <= 99
    yr = y_int + 1900
  else
    yr = y
  end
  finalDate = _make_date(_make_day(yr, m, dt), _make_time(h, min, s, milli))
  _time_clip(finalDate).to_f
end

.localTZAObject



445
446
447
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 445

def localTZA
  @localTZA ||= ((Time.local(2000) - Time.utc(2000)).to_f * 1000).to_i
end

.property_get_from_time(name, t) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 119

def property_get_from_time(name, t)
  r = case name
  when :FullYear
    _year_from_time(t)
  when :Month
    _month_from_time(t)
  when :Date
    _date_from_time(t)
  when :Day
    _week_day(t)
  when :Hours
    _hour_from_time(t)
  when :Minutes
    _min_from_time(t)
  when :Seconds
    _sec_from_time(t)
  when :Milliseconds
    _ms_from_time(t)
  end
  r.to_f
end

.property_set_from_time(name, t, args) ⇒ Object



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
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 172

def property_set_from_time(name, t, args)
  hour = _hour_from_time(t)
  min = _min_from_time(t)
  second = _sec_from_time(t)
  ms = _ms_from_time(t)

  year = _year_from_time(t)
  month = _month_from_time(t)
  date = _date_from_time(t)
  
  case name
  when :FullYear
    year = to_number(args[0])
    if args.length > 1
      month = to_number(args[1])
    end
    if args.length > 2
      date = to_number(args[2])
    end
  when :Month
    month = to_number(args[0])
    if args.length > 1
      date = to_number(args[1])
    end
  when :Date
    date = to_number(args[0])
  when :Day
    _week_day(t)
  when :Hours
    hour = to_number(args[0])
    if args.length > 1
      min = to_number(args[1])
    end
    if args.length > 2
      second = to_number(args[2])
    end
    if args.length > 3
      ms = to_number(args[3])
    end
  when :Minutes
    min = to_number(args[0])
    if args.length > 1
      second = to_number(args[1])
    end
    if args.length > 2
      ms = to_number(args[2])
    end
  when :Seconds
    second = to_number(args[0])
    if args.length > 1
      ms = to_number(args[1])
    end
  when :Milliseconds
    ms = to_number(args[0])
  end
  time = _make_time(hour, min, second, ms)
  _make_date(_make_day(year, month, date), time)
end

.prototype_getTime(builtin, this) ⇒ Object



114
115
116
117
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 114

def prototype_getTime(builtin, this)
  check_date(this)
  this.value.to_f
end

.prototype_getTimezoneOffset(builtin, this) ⇒ Object



158
159
160
161
162
163
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 158

def prototype_getTimezoneOffset(builtin, this)
  check_date(this)
  t = this.value
  return t if t != t
  (t - _utc_to_local(t)) / 60000.0
end

.prototype_setTime(builtin, this, time) ⇒ Object



165
166
167
168
169
170
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 165

def prototype_setTime(builtin, this, time)
  check_date(this)
  v = _time_clip(to_number(time))
  this.set_value(v)
  v.to_f
end

.prototype_toDateString(builtin, this) ⇒ Object



84
85
86
87
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 84

def prototype_toDateString(builtin, this)
  check_date(this)
  this.rb_time.strftime('%d %B %Y')
end

.prototype_toISOString(builtin, this) ⇒ Object



256
257
258
259
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 256

def prototype_toISOString(builtin, this)
  check_date(this)
  this.rb_time.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
end

.prototype_toJSON(builtin, this, key) ⇒ Object



261
262
263
264
265
266
267
268
269
270
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 261

def prototype_toJSON(builtin, this, key)
  obj = to_object(this, builtin)
  tv = to_primitive(obj, 'Number')
  return null if tv.js_type == :Number && !tv.finite?
  toISO = obj.get('toISOString')
  unless is_callable(toISO)
    raise make_error('TypeError', 'toISOString is not callable')
  end
  toISO.call(obj, [])
end

.prototype_toLocaleDateString(builtin, this) ⇒ Object



99
100
101
102
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 99

def prototype_toLocaleDateString(builtin, this)
  check_date(this)
  this.rb_time.strftime('%d %B %Y')
end

.prototype_toLocaleString(builtin, this) ⇒ Object



94
95
96
97
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 94

def prototype_toLocaleString(builtin, this)
  check_date(this)
  this.rb_time.strftime('%d %B %Y %H:%M:%S')
end

.prototype_toLocaleTimeString(builtin, this) ⇒ Object



104
105
106
107
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 104

def prototype_toLocaleTimeString(builtin, this)
  check_date(this)
  this.rb_time.strftime('%H:%M:%S')
end

.prototype_toString(builtin, this) ⇒ Object



79
80
81
82
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 79

def prototype_toString(builtin, this)
  check_date(this)
  this.rb_time.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
end

.prototype_toTimeString(builtin, this) ⇒ Object



89
90
91
92
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 89

def prototype_toTimeString(builtin, this)
  check_date(this)
  this.rb_time.strftime('%H:%M:%S')
end

.prototype_toUTCString(builtin, this) ⇒ Object



251
252
253
254
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 251

def prototype_toUTCString(builtin, this)
  check_date(this)
  this.rb_time.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
end

.prototype_valueOf(builtin, this) ⇒ Object



109
110
111
112
# File 'lib/ruby_run_js/object_methods/js_date.rb', line 109

def prototype_valueOf(builtin, this)
  check_date(this)
  this.value.to_f
end