37
37
class Time
38
38
include Comparable
39
39
40
- MonthValue = {
41
- 'JAN' => 1 , 'FEB' => 2 , 'MAR' => 3 , 'APR' => 4 , 'MAY' => 5 , 'JUN' => 6 ,
42
- 'JUL' => 7 , 'AUG' => 8 , 'SEP' => 9 , 'OCT' => 10 , 'NOV' => 11 , 'DEC' => 12
43
- }
44
-
45
40
def inspect
46
41
str = strftime ( '%Y-%m-%d %H:%M:%S' )
47
42
@@ -371,103 +366,6 @@ def at(sec, sub_sec = undefined, unit = undefined, **kwargs)
371
366
time
372
367
end
373
368
374
- def from_array ( sec , min , hour , mday , month , year , nsec , is_dst , is_utc , utc_offset )
375
- # Ensure all the user provided numeric values fit into int type.
376
- # sec and nsec are handled separately.
377
- Primitive . rb_num2int ( min )
378
- Primitive . rb_num2int ( hour )
379
- Primitive . rb_num2int ( mday )
380
- Primitive . rb_num2int ( month )
381
- Primitive . rb_num2int ( year )
382
-
383
- # handle sec and nsec
384
- if Primitive . is_a? ( sec , String )
385
- sec = sec . to_i
386
- elsif nsec
387
- sec = Truffle ::Type . coerce_to ( sec || 0 , Integer , :to_int )
388
- else
389
- s = Truffle ::Type . coerce_to_exact_num ( sec || 0 )
390
-
391
- sec = s . to_i
392
- nsec_frac = s % 1.0
393
-
394
- if s < 0 && nsec_frac > 0
395
- sec -= 1
396
- end
397
-
398
- nsec = ( nsec_frac * 1_000_000_000 + 0.5 ) . to_i
399
- end
400
-
401
- nsec ||= 0
402
-
403
- Primitive . time_s_from_array ( self , sec , min , hour , mday , month , year , nsec , is_dst , is_utc , utc_offset )
404
- end
405
- private :from_array
406
-
407
- def compose ( offset , p1 , p2 = nil , p3 = nil , p4 = nil , p5 = nil , p6 = nil , p7 = nil ,
408
- yday = undefined , is_dst = undefined , tz = undefined )
409
- if Primitive . undefined? ( tz )
410
- unless Primitive . undefined? ( is_dst )
411
- raise ArgumentError , 'wrong number of arguments (9 for 1..8)'
412
- end
413
-
414
- y = p1
415
- m = p2
416
- d = p3
417
- hr = p4
418
- min = p5
419
- sec = p6
420
- usec = p7
421
- is_dst = -1
422
- else
423
- y = p6
424
- m = p5
425
- d = p4
426
- hr = p3
427
- min = p2
428
- sec = p1
429
- usec = 0
430
- is_dst = is_dst ? 1 : 0
431
- end
432
-
433
- if Primitive . is_a? ( m , String ) or m . respond_to? ( :to_str )
434
- m = StringValue ( m )
435
- m = MonthValue [ m . upcase ] || m . to_i
436
-
437
- raise ArgumentError , 'month argument out of range' unless m
438
- else
439
- m = Truffle ::Type . coerce_to ( m || 1 , Integer , :to_int )
440
- end
441
-
442
- y = Primitive . is_a? ( y , String ) ? y . to_i : Truffle ::Type . coerce_to ( y , Integer , :to_int )
443
- d = Primitive . is_a? ( d , String ) ? d . to_i : Truffle ::Type . coerce_to ( d || 1 , Integer , :to_int )
444
- hr = Primitive . is_a? ( hr , String ) ? hr . to_i : Truffle ::Type . coerce_to ( hr || 0 , Integer , :to_int )
445
- min = Primitive . is_a? ( min , String ) ? min . to_i : Truffle ::Type . coerce_to ( min || 0 , Integer , :to_int )
446
-
447
- nsec = nil
448
- if Primitive . is_a? ( usec , String )
449
- nsec = usec . to_i * 1000
450
- elsif usec
451
- nsec = ( usec * 1000 ) . to_i
452
- end
453
-
454
- case offset
455
- when :utc
456
- is_dst = -1
457
- is_utc = true
458
- offset = nil
459
- when :local
460
- is_utc = false
461
- offset = nil
462
- else
463
- is_dst = -1
464
- is_utc = false
465
- end
466
-
467
- from_array ( sec , min , hr , d , m , y , nsec , is_dst , is_utc , offset )
468
- end
469
- private :compose
470
-
471
369
def new ( year = undefined , month = nil , day = nil , hour = nil , minute = nil , second = nil , utc_offset = nil , **options )
472
370
if utc_offset && options [ :in ]
473
371
raise ArgumentError , 'timezone argument given as positional and keyword arguments'
@@ -478,18 +376,18 @@ def new(year = undefined, month = nil, day = nil, hour = nil, minute = nil, seco
478
376
if Primitive . undefined? ( year )
479
377
utc_offset ? self . now . getlocal ( utc_offset ) : self . now
480
378
elsif Primitive . nil? utc_offset
481
- compose ( :local , year , month , day , hour , minute , second )
379
+ Truffle :: TimeOperations . compose ( self , :local , year , month , day , hour , minute , second )
482
380
elsif utc_offset == :std
483
- compose ( :local , second , minute , hour , day , month , year , nil , nil , false , nil )
381
+ Truffle :: TimeOperations . compose ( self , :local , second , minute , hour , day , month , year , nil , nil , false , nil )
484
382
elsif utc_offset == :dst
485
- compose ( :local , second , minute , hour , day , month , year , nil , nil , true , nil )
383
+ Truffle :: TimeOperations . compose ( self , :local , second , minute , hour , day , month , year , nil , nil , true , nil )
486
384
else
487
385
if utc_offset_in_utc? ( utc_offset )
488
386
utc_offset = :utc
489
387
else
490
388
utc_offset = Truffle ::Type . coerce_to_utc_offset ( utc_offset )
491
389
end
492
- compose ( utc_offset , year , month , day , hour , minute , second )
390
+ Truffle :: TimeOperations . compose ( self , utc_offset , year , month , day , hour , minute , second )
493
391
end
494
392
end
495
393
@@ -512,12 +410,12 @@ def now(**options)
512
410
end
513
411
514
412
def local ( *args )
515
- compose ( :local , *args )
413
+ Truffle :: TimeOperations . compose ( self , :local , *args )
516
414
end
517
415
alias_method :mktime , :local
518
416
519
417
def gm ( *args )
520
- compose ( :utc , *args )
418
+ Truffle :: TimeOperations . compose ( self , :utc , *args )
521
419
end
522
420
alias_method :utc , :gm
523
421
end
0 commit comments