tzinfo 1.2.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of tzinfo might be problematic. Click here for more details.

Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data.tar.gz.sig +0 -0
  4. data/.yardopts +6 -0
  5. data/CHANGES.md +786 -0
  6. data/LICENSE +19 -0
  7. data/README.md +151 -0
  8. data/Rakefile +107 -0
  9. data/lib/tzinfo.rb +40 -0
  10. data/lib/tzinfo/country.rb +196 -0
  11. data/lib/tzinfo/country_index_definition.rb +31 -0
  12. data/lib/tzinfo/country_info.rb +42 -0
  13. data/lib/tzinfo/country_timezone.rb +135 -0
  14. data/lib/tzinfo/data_source.rb +190 -0
  15. data/lib/tzinfo/data_timezone.rb +58 -0
  16. data/lib/tzinfo/data_timezone_info.rb +55 -0
  17. data/lib/tzinfo/info_timezone.rb +30 -0
  18. data/lib/tzinfo/linked_timezone.rb +63 -0
  19. data/lib/tzinfo/linked_timezone_info.rb +26 -0
  20. data/lib/tzinfo/offset_rationals.rb +77 -0
  21. data/lib/tzinfo/ruby_core_support.rb +146 -0
  22. data/lib/tzinfo/ruby_country_info.rb +74 -0
  23. data/lib/tzinfo/ruby_data_source.rb +136 -0
  24. data/lib/tzinfo/time_or_datetime.rb +340 -0
  25. data/lib/tzinfo/timezone.rb +669 -0
  26. data/lib/tzinfo/timezone_definition.rb +36 -0
  27. data/lib/tzinfo/timezone_index_definition.rb +54 -0
  28. data/lib/tzinfo/timezone_info.rb +30 -0
  29. data/lib/tzinfo/timezone_offset.rb +101 -0
  30. data/lib/tzinfo/timezone_period.rb +245 -0
  31. data/lib/tzinfo/timezone_proxy.rb +105 -0
  32. data/lib/tzinfo/timezone_transition.rb +130 -0
  33. data/lib/tzinfo/timezone_transition_definition.rb +104 -0
  34. data/lib/tzinfo/transition_data_timezone_info.rb +274 -0
  35. data/lib/tzinfo/zoneinfo_country_info.rb +37 -0
  36. data/lib/tzinfo/zoneinfo_data_source.rb +488 -0
  37. data/lib/tzinfo/zoneinfo_timezone_info.rb +296 -0
  38. data/test/tc_country.rb +234 -0
  39. data/test/tc_country_index_definition.rb +69 -0
  40. data/test/tc_country_info.rb +16 -0
  41. data/test/tc_country_timezone.rb +173 -0
  42. data/test/tc_data_source.rb +218 -0
  43. data/test/tc_data_timezone.rb +99 -0
  44. data/test/tc_data_timezone_info.rb +18 -0
  45. data/test/tc_info_timezone.rb +34 -0
  46. data/test/tc_linked_timezone.rb +155 -0
  47. data/test/tc_linked_timezone_info.rb +23 -0
  48. data/test/tc_offset_rationals.rb +23 -0
  49. data/test/tc_ruby_core_support.rb +168 -0
  50. data/test/tc_ruby_country_info.rb +110 -0
  51. data/test/tc_ruby_data_source.rb +143 -0
  52. data/test/tc_time_or_datetime.rb +654 -0
  53. data/test/tc_timezone.rb +1350 -0
  54. data/test/tc_timezone_definition.rb +113 -0
  55. data/test/tc_timezone_index_definition.rb +73 -0
  56. data/test/tc_timezone_info.rb +11 -0
  57. data/test/tc_timezone_london.rb +143 -0
  58. data/test/tc_timezone_melbourne.rb +142 -0
  59. data/test/tc_timezone_new_york.rb +142 -0
  60. data/test/tc_timezone_offset.rb +126 -0
  61. data/test/tc_timezone_period.rb +555 -0
  62. data/test/tc_timezone_proxy.rb +136 -0
  63. data/test/tc_timezone_transition.rb +366 -0
  64. data/test/tc_timezone_transition_definition.rb +295 -0
  65. data/test/tc_timezone_utc.rb +27 -0
  66. data/test/tc_transition_data_timezone_info.rb +423 -0
  67. data/test/tc_zoneinfo_country_info.rb +78 -0
  68. data/test/tc_zoneinfo_data_source.rb +1195 -0
  69. data/test/tc_zoneinfo_timezone_info.rb +1232 -0
  70. data/test/test_utils.rb +163 -0
  71. data/test/ts_all.rb +7 -0
  72. data/test/ts_all_ruby.rb +5 -0
  73. data/test/ts_all_zoneinfo.rb +7 -0
  74. data/test/tzinfo-data/tzinfo/data.rb +8 -0
  75. data/test/tzinfo-data/tzinfo/data/definitions/America/Argentina/Buenos_Aires.rb +89 -0
  76. data/test/tzinfo-data/tzinfo/data/definitions/America/New_York.rb +315 -0
  77. data/test/tzinfo-data/tzinfo/data/definitions/Australia/Melbourne.rb +218 -0
  78. data/test/tzinfo-data/tzinfo/data/definitions/EST.rb +19 -0
  79. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__m__1.rb +21 -0
  80. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__p__1.rb +21 -0
  81. data/test/tzinfo-data/tzinfo/data/definitions/Etc/UTC.rb +21 -0
  82. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Amsterdam.rb +261 -0
  83. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Andorra.rb +186 -0
  84. data/test/tzinfo-data/tzinfo/data/definitions/Europe/London.rb +321 -0
  85. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Paris.rb +265 -0
  86. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Prague.rb +220 -0
  87. data/test/tzinfo-data/tzinfo/data/definitions/UTC.rb +16 -0
  88. data/test/tzinfo-data/tzinfo/data/indexes/countries.rb +927 -0
  89. data/test/tzinfo-data/tzinfo/data/indexes/timezones.rb +596 -0
  90. data/test/tzinfo-data/tzinfo/data/version.rb +14 -0
  91. data/test/zoneinfo/America/Argentina/Buenos_Aires +0 -0
  92. data/test/zoneinfo/America/New_York +0 -0
  93. data/test/zoneinfo/Australia/Melbourne +0 -0
  94. data/test/zoneinfo/EST +0 -0
  95. data/test/zoneinfo/Etc/UTC +0 -0
  96. data/test/zoneinfo/Europe/Amsterdam +0 -0
  97. data/test/zoneinfo/Europe/Andorra +0 -0
  98. data/test/zoneinfo/Europe/London +0 -0
  99. data/test/zoneinfo/Europe/Paris +0 -0
  100. data/test/zoneinfo/Europe/Prague +0 -0
  101. data/test/zoneinfo/Factory +0 -0
  102. data/test/zoneinfo/iso3166.tab +275 -0
  103. data/test/zoneinfo/leapseconds +61 -0
  104. data/test/zoneinfo/posix/Europe/London +0 -0
  105. data/test/zoneinfo/posixrules +0 -0
  106. data/test/zoneinfo/right/Europe/London +0 -0
  107. data/test/zoneinfo/zone.tab +439 -0
  108. data/test/zoneinfo/zone1970.tab +369 -0
  109. data/tzinfo.gemspec +21 -0
  110. metadata +193 -0
  111. metadata.gz.sig +2 -0
@@ -0,0 +1,99 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+
3
+ include TZInfo
4
+
5
+ class TCDataTimezone < Minitest::Test
6
+
7
+ class TestTimezoneInfo < TimezoneInfo
8
+ attr_reader :utc
9
+ attr_reader :local
10
+ attr_reader :utc_to
11
+ attr_reader :utc_from
12
+
13
+ def initialize(identifier, utc_period, local_periods, transitions_up_to)
14
+ super(identifier)
15
+ @utc_period = utc_period
16
+ @local_periods = local_periods || []
17
+ @transitions_up_to = transitions_up_to
18
+ end
19
+
20
+ def period_for_utc(utc)
21
+ @utc = utc
22
+ @utc_period
23
+ end
24
+
25
+ def periods_for_local(local)
26
+ @local = local
27
+ @local_periods
28
+ end
29
+
30
+ def transitions_up_to(utc_to, utc_from = nil)
31
+ @utc_to = utc_to
32
+ @utc_from = utc_from
33
+ @transitions_up_to
34
+ end
35
+ end
36
+
37
+ def test_identifier
38
+ tz = DataTimezone.new(TestTimezoneInfo.new('Test/Zone', nil, [], []))
39
+ assert_equal('Test/Zone', tz.identifier)
40
+ end
41
+
42
+ def test_period_for_utc
43
+ # Don't need actual TimezonePeriods. DataTimezone isn't supposed to do
44
+ # anything with them apart from return them.
45
+ period = Object.new
46
+ tti = TestTimezoneInfo.new('Test/Zone', period, [], [])
47
+ tz = DataTimezone.new(tti)
48
+
49
+ t = Time.utc(2006, 6, 27, 22, 50, 12)
50
+ assert_same(period, tz.period_for_utc(t))
51
+ assert_same(t, tti.utc)
52
+ end
53
+
54
+ def test_periods_for_local
55
+ # Don't need actual TimezonePeriods. DataTimezone isn't supposed to do
56
+ # anything with them apart from return them.
57
+ periods = [Object.new, Object.new]
58
+ tti = TestTimezoneInfo.new('Test/Zone', nil, periods, [])
59
+ tz = DataTimezone.new(tti)
60
+
61
+ t = Time.utc(2006, 6, 27, 22, 50, 12)
62
+ assert_same(periods, tz.periods_for_local(t))
63
+ assert_same(t, tti.local)
64
+ end
65
+
66
+ def test_periods_for_local_not_found
67
+ periods = []
68
+ tti = TestTimezoneInfo.new('Test/Zone', nil, periods, [])
69
+ tz = DataTimezone.new(tti)
70
+
71
+ t = Time.utc(2006, 6, 27, 22, 50, 12)
72
+ assert_same(periods, tz.periods_for_local(t))
73
+ assert_same(t, tti.local)
74
+ end
75
+
76
+ def test_transitions_up_to
77
+ # Don't need actual TimezoneTransition instances. DataTimezone isn't
78
+ # supposed to do anything with them apart from return them.
79
+ transitions = [Object.new, Object.new]
80
+ tti = TestTimezoneInfo.new('Test/Zone', nil, nil, transitions)
81
+ tz = DataTimezone.new(tti)
82
+
83
+ utc_to = Time.utc(2013, 1, 1, 0, 0, 0)
84
+ utc_from = Time.utc(2012, 1, 1, 0, 0, 0)
85
+ assert_same(transitions, tz.transitions_up_to(utc_to, utc_from))
86
+ assert_same(utc_to, tti.utc_to)
87
+ assert_same(utc_from, tti.utc_from)
88
+ end
89
+
90
+ def test_canonical_identifier
91
+ tz = DataTimezone.new(TestTimezoneInfo.new('Test/Zone', nil, [], []))
92
+ assert_equal('Test/Zone', tz.canonical_identifier)
93
+ end
94
+
95
+ def test_canonical_zone
96
+ tz = DataTimezone.new(TestTimezoneInfo.new('Test/Zone', nil, [], []))
97
+ assert_same(tz, tz.canonical_zone)
98
+ end
99
+ end
@@ -0,0 +1,18 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+
3
+ include TZInfo
4
+
5
+ class TCDataTimezoneInfo < Minitest::Test
6
+
7
+ def test_identifier
8
+ ti = DataTimezoneInfo.new('Test/Zone')
9
+ assert_equal('Test/Zone', ti.identifier)
10
+ end
11
+
12
+ def test_construct_timezone
13
+ ti = DataTimezoneInfo.new('Test/Zone')
14
+ tz = ti.create_timezone
15
+ assert_kind_of(DataTimezone, tz)
16
+ assert_equal('Test/Zone', tz.identifier)
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+
3
+ include TZInfo
4
+
5
+ class TCInfoTimezone < Minitest::Test
6
+
7
+ class TestInfoTimezone < InfoTimezone
8
+ attr_reader :setup_info
9
+
10
+ protected
11
+ def setup(info)
12
+ super(info)
13
+ @setup_info = info
14
+ end
15
+ end
16
+
17
+ def test_identifier
18
+ tz = InfoTimezone.new(TimezoneInfo.new('Test/Identifier'))
19
+ assert_equal('Test/Identifier', tz.identifier)
20
+ end
21
+
22
+ def test_info
23
+ i = TimezoneInfo.new('Test/Identifier')
24
+ tz = InfoTimezone.new(i)
25
+ assert_same(i, tz.send(:info))
26
+ end
27
+
28
+ def test_setup
29
+ i = TimezoneInfo.new('Test/Identifier')
30
+ tz = TestInfoTimezone.new(i)
31
+ assert_same(i, tz.setup_info)
32
+ end
33
+ end
34
+
@@ -0,0 +1,155 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+
3
+ include TZInfo
4
+
5
+ class TCLinkedTimezone < Minitest::Test
6
+
7
+ class TestTimezone < Timezone
8
+ attr_reader :utc_period
9
+ attr_reader :local_periods
10
+ attr_reader :up_to_transitions
11
+ attr_reader :utc
12
+ attr_reader :local
13
+ attr_reader :utc_to
14
+ attr_reader :utc_from
15
+
16
+ def self.new(identifier, no_local_periods = false)
17
+ tz = super()
18
+ tz.send(:setup, identifier, no_local_periods)
19
+ tz
20
+ end
21
+
22
+ def identifier
23
+ @identifier
24
+ end
25
+
26
+ def period_for_utc(utc)
27
+ @utc = utc
28
+ @utc_period
29
+ end
30
+
31
+ def periods_for_local(local)
32
+ @local = local
33
+ raise PeriodNotFound if @no_local_periods
34
+ @local_periods
35
+ end
36
+
37
+ def transitions_up_to(utc_to, utc_from = nil)
38
+ @utc_to = utc_to
39
+ @utc_from = utc_from
40
+ @up_to_transitions
41
+ end
42
+
43
+ def canonical_zone
44
+ self
45
+ end
46
+
47
+ private
48
+ def setup(identifier, no_local_periods)
49
+ @identifier = identifier
50
+ @no_local_periods = no_local_periods
51
+
52
+ # Don't have to be real TimezonePeriod or TimezoneTransition objects
53
+ # (nothing will use them).
54
+ @utc_period = Object.new
55
+ @local_periods = [Object.new, Object.new]
56
+ @up_to_transitions = [Object.new, Object.new]
57
+ end
58
+ end
59
+
60
+
61
+ def setup
62
+ # Redefine Timezone.get to return a fake timezone.
63
+ # Use without_warnings to suppress redefined get method warning.
64
+ without_warnings do
65
+ def Timezone.get(identifier)
66
+ raise InvalidTimezoneIdentifier, 'Invalid identifier' if identifier == 'Invalid/Identifier'
67
+
68
+ @timezones ||= {}
69
+ @timezones[identifier] ||=
70
+ identifier == 'Test/Recursive/Linked' ?
71
+ LinkedTimezone.new(LinkedTimezoneInfo.new(identifier, 'Test/Recursive/Data')) :
72
+ TestTimezone.new(identifier, identifier == 'Test/No/Local')
73
+ end
74
+ end
75
+ end
76
+
77
+ def teardown
78
+ # Re-require timezone to reset.
79
+ # Suppress redefined method warnings.
80
+ without_warnings do
81
+ load 'tzinfo/timezone.rb'
82
+ end
83
+ end
84
+
85
+ def test_identifier
86
+ tz = LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Test/Linked'))
87
+ assert_equal('Test/Zone', tz.identifier)
88
+ end
89
+
90
+ def test_invalid_linked_identifier
91
+ assert_raises(InvalidTimezoneIdentifier) { LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Invalid/Identifier')) }
92
+ end
93
+
94
+ def test_period_for_utc
95
+ tz = LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Test/Linked'))
96
+ linked_tz = Timezone.get('Test/Linked')
97
+ t = Time.utc(2006, 6, 27, 23, 12, 28)
98
+ assert_same(linked_tz.utc_period, tz.period_for_utc(t))
99
+ assert_same(t, linked_tz.utc)
100
+ end
101
+
102
+ def test_periods_for_local
103
+ tz = LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Test/Linked'))
104
+ linked_tz = Timezone.get('Test/Linked')
105
+ t = Time.utc(2006, 6, 27, 23, 12, 28)
106
+ assert_same(linked_tz.local_periods, tz.periods_for_local(t))
107
+ assert_same(t, linked_tz.local)
108
+ end
109
+
110
+ def test_periods_for_local_not_found
111
+ tz = LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Test/No/Local'))
112
+ linked_tz = Timezone.get('Test/No/Local')
113
+ t = Time.utc(2006, 6, 27, 23, 12, 28)
114
+ assert_raises(PeriodNotFound) { tz.periods_for_local(t) }
115
+ assert_same(t, linked_tz.local)
116
+ end
117
+
118
+ def test_transitions_up_to
119
+ tz = LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Test/Linked'))
120
+ linked_tz = Timezone.get('Test/Linked')
121
+ utc_to = Time.utc(2013, 1, 1, 0, 0, 0)
122
+ utc_from = Time.utc(2012, 1, 1, 0, 0, 0)
123
+ assert_same(linked_tz.up_to_transitions, tz.transitions_up_to(utc_to, utc_from))
124
+ assert_same(utc_to, linked_tz.utc_to)
125
+ assert_same(utc_from, linked_tz.utc_from)
126
+ end
127
+
128
+ def test_canonical_identifier
129
+ tz = LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Test/Linked'))
130
+ assert_equal('Test/Linked', tz.canonical_identifier)
131
+ end
132
+
133
+ def test_canonical_identifier_recursive
134
+ # Recursive links are not currently used in the Time Zone database, but
135
+ # will be supported by TZInfo.
136
+
137
+ tz = LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Test/Recursive/Linked'))
138
+ assert_equal('Test/Recursive/Data', tz.canonical_identifier)
139
+ end
140
+
141
+ def test_canonical_zone
142
+ tz = LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Test/Linked'))
143
+ linked_tz = Timezone.get('Test/Linked')
144
+ assert_same(linked_tz, tz.canonical_zone)
145
+ end
146
+
147
+ def test_canonical_zone_recursive
148
+ # Recursive links are not currently used in the Time Zone database, but
149
+ # will be supported by TZInfo.
150
+
151
+ tz = LinkedTimezone.new(LinkedTimezoneInfo.new('Test/Zone', 'Test/Recursive/Linked'))
152
+ linked_tz = Timezone.get('Test/Recursive/Data')
153
+ assert_same(linked_tz, tz.canonical_zone)
154
+ end
155
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+
3
+ include TZInfo
4
+
5
+ class TCLinkedTimezoneInfo < Minitest::Test
6
+
7
+ def test_identifier
8
+ lti = LinkedTimezoneInfo.new('Test/Zone', 'Test/Linked')
9
+ assert_equal('Test/Zone', lti.identifier)
10
+ end
11
+
12
+ def test_link_to_identifier
13
+ lti = LinkedTimezoneInfo.new('Test/Zone', 'Test/Linked')
14
+ assert_equal('Test/Linked', lti.link_to_identifier)
15
+ end
16
+
17
+ def test_construct_timezone
18
+ lti = LinkedTimezoneInfo.new('Test/Zone', 'Europe/London')
19
+ tz = lti.create_timezone
20
+ assert_kind_of(LinkedTimezone, tz)
21
+ assert_equal('Test/Zone', tz.identifier)
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+
3
+ include TZInfo
4
+
5
+ class TCOffsetRationals < Minitest::Test
6
+ def test_rational_for_offset
7
+ [0,1,2,3,4,-1,-2,-3,-4,30*60,-30*60,61*60,-61*60,14*60*60,-14*60*60,20*60*60,-20*60*60].each {|seconds|
8
+ assert_equal(Rational(seconds, 86400), OffsetRationals.rational_for_offset(seconds))
9
+ }
10
+ end
11
+
12
+ def test_rational_for_offset_constant
13
+ -28.upto(28) {|i|
14
+ seconds = i * 30 * 60
15
+
16
+ r1 = OffsetRationals.rational_for_offset(seconds)
17
+ r2 = OffsetRationals.rational_for_offset(seconds)
18
+
19
+ assert_equal(Rational(seconds, 86400), r1)
20
+ assert_same(r1, r2)
21
+ }
22
+ end
23
+ end
@@ -0,0 +1,168 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
4
+
5
+ include TZInfo
6
+
7
+ class TCRubyCoreSupport < Minitest::Test
8
+ def test_rational_new!
9
+ assert_equal(Rational(3,4), RubyCoreSupport.rational_new!(3,4))
10
+ end
11
+
12
+ def test_datetime_new!
13
+ assert_equal(DateTime.new(2008,10,5,12,0,0, 0, Date::ITALY), RubyCoreSupport.datetime_new!(2454745,0,2299161))
14
+ assert_equal(DateTime.new(2008,10,5,13,0,0, Rational(1, 24), Date::ITALY), RubyCoreSupport.datetime_new!(2454745,Rational(1, 24),2299161))
15
+
16
+ assert_equal(DateTime.new(2008,10,5,20,30,0, 0, Date::ITALY), RubyCoreSupport.datetime_new!(Rational(117827777, 48), 0, 2299161))
17
+ assert_equal(DateTime.new(2008,10,5,21,30,0, Rational(1, 24), Date::ITALY), RubyCoreSupport.datetime_new!(Rational(117827777, 48), Rational(1, 24), 2299161))
18
+
19
+ assert_equal(DateTime.new(2008,10,6,6,26,21, 0, Date::ITALY), RubyCoreSupport.datetime_new!(Rational(70696678127,28800), 0, 2299161))
20
+ assert_equal(DateTime.new(2008,10,6,7,26,21, Rational(1, 24), Date::ITALY), RubyCoreSupport.datetime_new!(Rational(70696678127, 28800), Rational(1, 24), 2299161))
21
+
22
+ assert_equal(DateTime.new(-4712,1,1,12,0,0, 0, Date::ITALY), RubyCoreSupport.datetime_new!(0, 0, 2299161))
23
+ assert_equal(DateTime.new(-4712,1,1,13,0,0, Rational(1, 24), Date::ITALY), RubyCoreSupport.datetime_new!(0, Rational(1, 24), 2299161))
24
+
25
+ assert_equal(DateTime.new(-4713,12,31,23,58,59, 0, Date::ITALY), RubyCoreSupport.datetime_new!(Rational(-43261, 86400), 0, 2299161))
26
+ assert_equal(DateTime.new(-4712,1,1,0,58,59, Rational(1, 24), Date::ITALY), RubyCoreSupport.datetime_new!(Rational(-43261, 86400), Rational(1, 24), 2299161))
27
+
28
+ assert_equal(DateTime.new(-4713,12,30,23,58,59, 0, Date::ITALY), RubyCoreSupport.datetime_new!(Rational(-129661, 86400), 0, 2299161))
29
+ assert_equal(DateTime.new(-4713,12,31,0,58,59, Rational(1, 24), Date::ITALY), RubyCoreSupport.datetime_new!(Rational(-129661, 86400), Rational(1, 24), 2299161))
30
+ end
31
+
32
+ def test_datetime_new
33
+ assert_equal(DateTime.new(2012, 12, 31, 23, 59, 59, 0, Date::ITALY), RubyCoreSupport.datetime_new(2012, 12, 31, 23, 59, 59, 0, Date::ITALY))
34
+ assert_equal(DateTime.new(2013, 2, 6, 23, 2, 36, Rational(1, 24), Date::ITALY), RubyCoreSupport.datetime_new(2013, 2, 6, 23, 2, 36, Rational(1,24), Date::ITALY))
35
+
36
+ assert_equal(DateTime.new(2012, 12, 31, 23, 59, 59, 0, Date::ITALY) + Rational(1, 86400000), RubyCoreSupport.datetime_new(2012, 12, 31, 23, 59, 59 + Rational(1, 1000), 0, Date::ITALY))
37
+ assert_equal(DateTime.new(2001, 10, 12, 12, 22, 59, Rational(1, 24), Date::ITALY) + Rational(501, 86400000), RubyCoreSupport.datetime_new(2001, 10, 12, 12, 22, 59 + Rational(501, 1000), Rational(1, 24), Date::ITALY))
38
+ end
39
+
40
+ def test_time_supports_negative
41
+ if RubyCoreSupport.time_supports_negative
42
+ assert_equal(Time.utc(1969, 12, 31, 23, 59, 59), Time.at(-1).utc)
43
+ else
44
+ assert_raises(ArgumentError) do
45
+ Time.at(-1)
46
+ end
47
+ end
48
+ end
49
+
50
+ def test_time_supports_64_bit
51
+ if RubyCoreSupport.time_supports_64bit
52
+ assert_equal(Time.utc(1901, 12, 13, 20, 45, 51), Time.at(-2147483649).utc)
53
+ assert_equal(Time.utc(2038, 1, 19, 3, 14, 8), Time.at(2147483648).utc)
54
+ else
55
+ assert_raises(RangeError) do
56
+ Time.at(-2147483649)
57
+ end
58
+
59
+ assert_raises(RangeError) do
60
+ Time.at(2147483648)
61
+ end
62
+ end
63
+ end
64
+
65
+ def test_time_nsec
66
+ t = Time.utc(2013, 2, 6, 21, 56, 23, 567890 + Rational(123,1000))
67
+
68
+ if t.respond_to?(:nsec)
69
+ assert_equal(567890123, RubyCoreSupport.time_nsec(t))
70
+ else
71
+ assert_equal(567890000, RubyCoreSupport.time_nsec(t))
72
+ end
73
+ end
74
+
75
+ def test_force_encoding
76
+ s = [0xC2, 0xA9].pack('c2')
77
+
78
+ if s.respond_to?(:force_encoding)
79
+ # Ruby 1.9+ - should call String#force_encoding
80
+ assert_equal('ASCII-8BIT', s.encoding.name)
81
+ assert_equal(2, s.bytesize)
82
+ result = RubyCoreSupport.force_encoding(s, 'UTF-8')
83
+ assert_same(s, result)
84
+ assert_equal('UTF-8', s.encoding.name)
85
+ assert_equal(2, s.bytesize)
86
+ assert_equal(1, s.length)
87
+ assert_equal('©', s)
88
+ else
89
+ # Ruby 1.8 - no-op
90
+ result = RubyCoreSupport.force_encoding(s, 'UTF-8')
91
+ assert_same(s, result)
92
+ assert_equal('©', s)
93
+ end
94
+ end
95
+
96
+ begin
97
+ SUPPORTS_ENCODING = !!Encoding
98
+ rescue NameError
99
+ SUPPORTS_ENCODING = false
100
+ end
101
+
102
+ def check_open_file_test_file_bytes(test_file)
103
+ if SUPPORTS_ENCODING
104
+ File.open(test_file, 'r') do |file|
105
+ file.binmode
106
+ data = file.read(2)
107
+ refute_nil(data)
108
+ assert_equal(2, data.length)
109
+ bytes = data.unpack('C2')
110
+ assert_equal(0xC2, bytes[0])
111
+ assert_equal(0xA9, bytes[1])
112
+ end
113
+ end
114
+ end
115
+
116
+ def check_open_file_test_file_content(file)
117
+ content = file.gets
118
+ refute_nil(content)
119
+ content.chomp!
120
+
121
+ if SUPPORTS_ENCODING
122
+ assert_equal('UTF-8', content.encoding.name)
123
+ assert_equal(1, content.length)
124
+ assert_equal(2, content.bytesize)
125
+ assert_equal('©', content)
126
+ else
127
+ assert_equal('x', content)
128
+ end
129
+ end
130
+
131
+ def test_open_file
132
+ Dir.mktmpdir('tzinfo_test') do |dir|
133
+ test_file = File.join(dir, 'test.txt')
134
+
135
+ file = RubyCoreSupport.open_file(test_file, 'w', :external_encoding => 'UTF-8')
136
+ begin
137
+ file.puts(SUPPORTS_ENCODING ? '©' : 'x')
138
+ ensure
139
+ file.close
140
+ end
141
+
142
+ check_open_file_test_file_bytes(test_file)
143
+
144
+ file = RubyCoreSupport.open_file(test_file, 'r', :external_encoding => 'UTF-8', :internal_encoding => 'UTF-8')
145
+ begin
146
+ check_open_file_test_file_content(file)
147
+ ensure
148
+ file.close
149
+ end
150
+ end
151
+ end
152
+
153
+ def test_open_file_block
154
+ Dir.mktmpdir('tzinfo_test') do |dir|
155
+ test_file = File.join(dir, 'test.txt')
156
+
157
+ RubyCoreSupport.open_file(test_file, 'w', :external_encoding => 'UTF-8') do |file|
158
+ file.puts(SUPPORTS_ENCODING ? '©' : 'x')
159
+ end
160
+
161
+ check_open_file_test_file_bytes(test_file)
162
+
163
+ RubyCoreSupport.open_file(test_file, 'r', :external_encoding => 'UTF-8', :internal_encoding => 'UTF-8') do |file|
164
+ check_open_file_test_file_content(file)
165
+ end
166
+ end
167
+ end
168
+ end