File: tc_timezone_offset.rb

package info (click to toggle)
ruby-tzinfo 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,132 kB
  • sloc: ruby: 12,421; makefile: 4
file content (126 lines) | stat: -rw-r--r-- 5,326 bytes parent folder | download | duplicates (3)
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
require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')

include TZInfo

class TCTimezoneOffset < Minitest::Test
  
  def test_utc_offset
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(-3600, 3600, :TEST2)
    
    assert_equal(18000, o1.utc_offset)
    assert_equal(-3600, o2.utc_offset)
  end
  
  def test_std_offset
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(-3600, 3600, :TEST2)
    
    assert_equal(0, o1.std_offset)
    assert_equal(3600, o2.std_offset)
  end
  
  def test_utc_total_offset
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(-3600, 3600, :TEST2)
    
    assert_equal(18000, o1.utc_total_offset)
    assert_equal(0, o2.utc_total_offset)
  end
  
  def test_abbreviation
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(-3600, 3600, :TEST2)
    
    assert_equal(:TEST, o1.abbreviation)
    assert_equal(:TEST2, o2.abbreviation)
  end
  
  def test_dst
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(-3600, 3600, :TEST2)
    
    assert_equal(false, o1.dst?)
    assert_equal(true, o2.dst?)
  end
  
  def test_to_local
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(-3600, 3600, :TEST2)
    
    assert_equal(1148949080, o1.to_local(1148931080))
    assert_equal(Time.utc(2006, 5, 30, 0, 31, 20), o1.to_local(Time.utc(2006, 5, 29, 19, 31, 20)))
    assert_equal(Time.utc(2006, 5, 30, 0, 31, 20, 782000), o1.to_local(Time.utc(2006, 5, 29, 19, 31, 20, 782000)))
    assert_equal(DateTime.new(2006, 5, 30, 0, 31, 20), o1.to_local(DateTime.new(2006, 5, 29, 19, 31, 20)))
    assert_equal(DateTime.new(2006, 5, 30, 0, 31, 20 + Rational(782, 1000)), o1.to_local(DateTime.new(2006, 5, 29, 19, 31, 20 + Rational(782, 1000))))
    assert_equal(1148949080, o1.to_local(1148931080))
    assert(TimeOrDateTime.new(1148949080).eql?(o1.to_local(TimeOrDateTime.new(1148931080))))
    
    assert_equal(1148931080, o2.to_local(1148931080))
    assert_equal(Time.utc(2006, 5, 29, 19, 31, 20), o2.to_local(Time.utc(2006, 5, 29, 19, 31, 20)))
    assert_equal(Time.utc(2006, 5, 29, 19, 31, 20, 123000), o2.to_local(Time.utc(2006, 5, 29, 19, 31, 20, 123000)))
    assert_equal(DateTime.new(2006, 5, 29, 19, 31, 20), o2.to_local(DateTime.new(2006, 5, 29, 19, 31, 20)))
    assert_equal(DateTime.new(2006, 5, 29, 19, 31, 20 + Rational(123, 1000)), o2.to_local(DateTime.new(2006, 5, 29, 19, 31, 20 + Rational(123, 1000))))
    assert_equal(1148931080, o2.to_local(1148931080))
    assert(TimeOrDateTime.new(1148931080).eql?(o2.to_local(TimeOrDateTime.new(1148931080))))
  end
  
  def test_to_utc
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(-3600, 3600, :TEST2)
    
    assert_equal(1148913080, o1.to_utc(1148931080))
    assert_equal(Time.utc(2006, 5, 29, 14, 31, 20), o1.to_utc(Time.utc(2006, 5, 29, 19, 31, 20)))
    assert_equal(Time.utc(2006, 5, 29, 14, 31, 20, 913000), o1.to_utc(Time.utc(2006, 5, 29, 19, 31, 20, 913000)))
    assert_equal(DateTime.new(2006, 5, 29, 14, 31, 20), o1.to_utc(DateTime.new(2006, 5, 29, 19, 31, 20)))
    assert_equal(DateTime.new(2006, 5, 29, 14, 31, 20 + Rational(913,1000)), o1.to_utc(DateTime.new(2006, 5, 29, 19, 31, 20 + Rational(913,1000))))
    assert_equal(1148913080, o1.to_utc(1148931080))
    assert(TimeOrDateTime.new(1148913080).eql?(o1.to_utc(TimeOrDateTime.new(1148931080))))
    
    assert_equal(1148931080, o2.to_local(1148931080))
    assert_equal(Time.utc(2006, 5, 29, 19, 31, 20), o2.to_local(Time.utc(2006, 5, 29, 19, 31, 20)))
    assert_equal(Time.utc(2006, 5, 29, 19, 31, 20, 323000), o2.to_local(Time.utc(2006, 5, 29, 19, 31, 20, 323000)))
    assert_equal(DateTime.new(2006, 5, 29, 19, 31, 20), o2.to_local(DateTime.new(2006, 5, 29, 19, 31, 20)))
    assert_equal(DateTime.new(2006, 5, 29, 19, 31, 20 + Rational(323, 1000)), o2.to_local(DateTime.new(2006, 5, 29, 19, 31, 20 + Rational(323, 1000))))
    assert_equal(1148931080, o2.to_utc(1148931080))
    assert(TimeOrDateTime.new(1148931080).eql?(o2.to_local(TimeOrDateTime.new(1148931080))))    
  end
  
  def test_equality
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(18000, 0, :TEST)
    o3 = TimezoneOffset.new(18001, 0, :TEST)
    o4 = TimezoneOffset.new(18000, 1, :TEST)
    o5 = TimezoneOffset.new(18000, 0, :TEST2)
    
    assert_equal(true, o1 == o1)
    assert_equal(true, o1 == o2)
    assert_equal(false, o1 == o3)
    assert_equal(false, o1 == o4)
    assert_equal(false, o1 == o5)
    assert_equal(false, o1 == Object.new)
  end
  
  def test_eql
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(18000, 0, :TEST)
    o3 = TimezoneOffset.new(18001, 0, :TEST)
    o4 = TimezoneOffset.new(18000, 1, :TEST)
    o5 = TimezoneOffset.new(18000, 0, :TEST2)
    
    assert_equal(true, o1.eql?(o1))
    assert_equal(true, o1.eql?(o2))
    assert_equal(false, o1.eql?(o3))
    assert_equal(false, o1.eql?(o4))
    assert_equal(false, o1.eql?(o5))
    assert_equal(false, o1.eql?(Object.new))
  end
  
  def test_hash
    o1 = TimezoneOffset.new(18000, 0, :TEST)
    o2 = TimezoneOffset.new(-3600, 3600, :TEST2)
    
    assert_equal(18000.hash ^ 0.hash ^ :TEST.hash, o1.hash)
    assert_equal(-3600.hash ^ 3600.hash ^ :TEST2.hash, o2.hash)
  end
end