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,110 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+
3
+ include TZInfo
4
+
5
+ class TCRubyCountryInfo < Minitest::Test
6
+
7
+ def test_code
8
+ ci = RubyCountryInfo.new('ZZ', 'Zzz') {|c| }
9
+ assert_equal('ZZ', ci.code)
10
+ end
11
+
12
+ def test_name
13
+ ci = RubyCountryInfo.new('ZZ', 'Zzz') {|c| }
14
+ assert_equal('Zzz', ci.name)
15
+ end
16
+
17
+ def test_zone_identifiers_empty
18
+ ci = RubyCountryInfo.new('ZZ', 'Zzz') {|c| }
19
+ assert(ci.zone_identifiers.empty?)
20
+ assert(ci.zone_identifiers.frozen?)
21
+ end
22
+
23
+ def test_zone_identifiers_no_block
24
+ ci = RubyCountryInfo.new('ZZ', 'Zzz')
25
+ assert(ci.zone_identifiers.empty?)
26
+ assert(ci.zone_identifiers.frozen?)
27
+ end
28
+
29
+ def test_zone_identifiers
30
+ ci = RubyCountryInfo.new('ZZ', 'Zzz') do |c|
31
+ c.timezone('ZZ/TimezoneB', 1, 2, 1, 2, 'Timezone B')
32
+ c.timezone('ZZ/TimezoneA', 1, 4, 1, 4, 'Timezone A')
33
+ c.timezone('ZZ/TimezoneC', -10, 3, -20, 7, 'C')
34
+ c.timezone('ZZ/TimezoneD', -10, 3, -20, 7)
35
+ end
36
+
37
+ assert_equal(['ZZ/TimezoneB', 'ZZ/TimezoneA', 'ZZ/TimezoneC', 'ZZ/TimezoneD'], ci.zone_identifiers)
38
+ assert(ci.zone_identifiers.frozen?)
39
+ end
40
+
41
+ def test_zone_identifiers_after_freeze
42
+ ci = RubyCountryInfo.new('ZZ', 'Zzz') do |c|
43
+ c.timezone('ZZ/TimezoneB', 1, 2, 1, 2, 'Timezone B')
44
+ c.timezone('ZZ/TimezoneA', 1, 4, 1, 4, 'Timezone A')
45
+ c.timezone('ZZ/TimezoneC', -10, 3, -20, 7, 'C')
46
+ c.timezone('ZZ/TimezoneD', -10, 3, -20, 7)
47
+ end
48
+
49
+ ci.freeze
50
+
51
+ assert_equal(['ZZ/TimezoneB', 'ZZ/TimezoneA', 'ZZ/TimezoneC', 'ZZ/TimezoneD'], ci.zone_identifiers)
52
+ end
53
+
54
+ def test_zones_empty
55
+ ci = RubyCountryInfo.new('ZZ', 'Zzz') {|c| }
56
+ assert(ci.zones.empty?)
57
+ assert(ci.zones.frozen?)
58
+ end
59
+
60
+ def test_zones_no_block
61
+ ci = RubyCountryInfo.new('ZZ', 'Zzz')
62
+ assert(ci.zones.empty?)
63
+ assert(ci.zones.frozen?)
64
+ end
65
+
66
+ def test_zones
67
+ ci = RubyCountryInfo.new('ZZ', 'Zzz') do |c|
68
+ c.timezone('ZZ/TimezoneB', 1, 2, 1, 2, 'Timezone B')
69
+ c.timezone('ZZ/TimezoneA', 1, 4, 1, 4, 'Timezone A')
70
+ c.timezone('ZZ/TimezoneC', -10, 3, -20, 7, 'C')
71
+ c.timezone('ZZ/TimezoneD', -10, 3, -20, 7)
72
+ end
73
+
74
+ assert_equal([CountryTimezone.new!('ZZ/TimezoneB', 1, 2, 1, 2, 'Timezone B'),
75
+ CountryTimezone.new!('ZZ/TimezoneA', 1, 4, 1, 4, 'Timezone A'),
76
+ CountryTimezone.new!('ZZ/TimezoneC', -10, 3, -20, 7, 'C'),
77
+ CountryTimezone.new!('ZZ/TimezoneD', -10, 3, -20, 7)],
78
+ ci.zones)
79
+ assert(ci.zones.frozen?)
80
+ end
81
+
82
+ def test_zones_after_freeze
83
+ ci = RubyCountryInfo.new('ZZ', 'Zzz') do |c|
84
+ c.timezone('ZZ/TimezoneB', 1, 2, 1, 2, 'Timezone B')
85
+ c.timezone('ZZ/TimezoneA', 1, 4, 1, 4, 'Timezone A')
86
+ c.timezone('ZZ/TimezoneC', -10, 3, -20, 7, 'C')
87
+ c.timezone('ZZ/TimezoneD', -10, 3, -20, 7)
88
+ end
89
+
90
+ ci.freeze
91
+
92
+ assert_equal([CountryTimezone.new!('ZZ/TimezoneB', 1, 2, 1, 2, 'Timezone B'),
93
+ CountryTimezone.new!('ZZ/TimezoneA', 1, 4, 1, 4, 'Timezone A'),
94
+ CountryTimezone.new!('ZZ/TimezoneC', -10, 3, -20, 7, 'C'),
95
+ CountryTimezone.new!('ZZ/TimezoneD', -10, 3, -20, 7)],
96
+ ci.zones)
97
+ end
98
+
99
+ def test_deferred_evaluate
100
+ block_called = false
101
+
102
+ ci = RubyCountryInfo.new('ZZ', 'Zzz') do |c|
103
+ block_called = true
104
+ end
105
+
106
+ assert_equal(false, block_called)
107
+ ci.zones
108
+ assert_equal(true, block_called)
109
+ end
110
+ end
@@ -0,0 +1,143 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+
3
+ include TZInfo
4
+
5
+ class TCRubyDataSource < Minitest::Test
6
+ def setup
7
+ @data_source = RubyDataSource.new
8
+ end
9
+
10
+ def test_load_timezone_info_data
11
+ info = @data_source.load_timezone_info('Europe/London')
12
+ assert_kind_of(DataTimezoneInfo, info)
13
+ assert_equal('Europe/London', info.identifier)
14
+ end
15
+
16
+ def test_load_timezone_info_linked
17
+ info = @data_source.load_timezone_info('UTC')
18
+ assert_kind_of(LinkedTimezoneInfo, info)
19
+ assert_equal('UTC', info.identifier)
20
+ assert_equal('Etc/UTC', info.link_to_identifier)
21
+ end
22
+
23
+ def test_load_timezone_info_does_not_exist
24
+ assert_raises(InvalidTimezoneIdentifier) do
25
+ @data_source.load_timezone_info('Nowhere/Special')
26
+ end
27
+ end
28
+
29
+ def test_load_timezone_info_invalid
30
+ assert_raises(InvalidTimezoneIdentifier) do
31
+ @data_source.load_timezone_info('../Definitions/UTC')
32
+ end
33
+ end
34
+
35
+ def test_load_timezone_info_nil
36
+ assert_raises(InvalidTimezoneIdentifier) do
37
+ @data_source.load_timezone_info(nil)
38
+ end
39
+ end
40
+
41
+ def test_load_timezone_info_case
42
+ assert_raises(InvalidTimezoneIdentifier) do
43
+ @data_source.load_timezone_info('europe/london')
44
+ end
45
+ end
46
+
47
+ def test_load_timezone_info_plus
48
+ info = @data_source.load_timezone_info('Etc/GMT+1')
49
+ assert_equal('Etc/GMT+1', info.identifier)
50
+ end
51
+
52
+ def test_load_timezone_info_minus
53
+ info = @data_source.load_timezone_info('Etc/GMT-1')
54
+ assert_equal('Etc/GMT-1', info.identifier)
55
+ end
56
+
57
+ def test_load_timezone_info_tainted
58
+ safe_test do
59
+ identifier = 'Europe/Amsterdam'.dup.taint
60
+ assert(identifier.tainted?)
61
+ info = @data_source.load_timezone_info(identifier)
62
+ assert_equal('Europe/Amsterdam', info.identifier)
63
+ assert(identifier.tainted?)
64
+ end
65
+ end
66
+
67
+ def test_load_timezone_info_tainted_and_frozen
68
+ safe_test do
69
+ info = @data_source.load_timezone_info('Europe/Amsterdam'.dup.taint.freeze)
70
+ assert_equal('Europe/Amsterdam', info.identifier)
71
+ end
72
+ end
73
+
74
+ def test_timezone_identifiers
75
+ all = @data_source.timezone_identifiers
76
+ assert_equal(TZInfo::Data::Indexes::Timezones.timezones, all)
77
+ assert_equal(true, all.frozen?)
78
+ end
79
+
80
+ def test_data_timezone_identifiers
81
+ all_data = @data_source.data_timezone_identifiers
82
+ assert_equal(TZInfo::Data::Indexes::Timezones.data_timezones, all_data)
83
+ assert_equal(true, all_data.frozen?)
84
+ end
85
+
86
+ def test_linked_timezone_identifiers
87
+ all_linked = @data_source.linked_timezone_identifiers
88
+ assert_equal(TZInfo::Data::Indexes::Timezones.linked_timezones, all_linked)
89
+ assert_equal(true, all_linked.frozen?)
90
+ end
91
+
92
+ def test_load_country_info
93
+ info = @data_source.load_country_info('GB')
94
+ assert_equal('GB', info.code)
95
+ end
96
+
97
+ def test_load_country_info_not_exist
98
+ assert_raises(InvalidCountryCode) do
99
+ @data_source.load_country_info('ZZ')
100
+ end
101
+ end
102
+
103
+ def test_load_country_info_invalid
104
+ assert_raises(InvalidCountryCode) do
105
+ @data_source.load_country_info('../Countries/GB')
106
+ end
107
+ end
108
+
109
+ def test_load_country_info_nil
110
+ assert_raises(InvalidCountryCode) do
111
+ @data_source.load_country_info(nil)
112
+ end
113
+ end
114
+
115
+ def test_load_country_info_case
116
+ assert_raises(InvalidCountryCode) do
117
+ @data_source.load_country_info('gb')
118
+ end
119
+ end
120
+
121
+ def test_load_country_info_tainted
122
+ safe_test do
123
+ code = 'NL'.dup.taint
124
+ assert(code.tainted?)
125
+ info = @data_source.load_country_info(code)
126
+ assert_equal('NL', info.code)
127
+ assert(code.tainted?)
128
+ end
129
+ end
130
+
131
+ def test_load_country_info_tainted_and_frozen
132
+ safe_test do
133
+ info = @data_source.load_country_info('NL'.dup.taint.freeze)
134
+ assert_equal('NL', info.code)
135
+ end
136
+ end
137
+
138
+ def test_country_codes
139
+ codes = @data_source.country_codes
140
+ assert_equal(TZInfo::Data::Indexes::Countries.countries.keys, codes)
141
+ assert_equal(true, codes.frozen?)
142
+ end
143
+ end
@@ -0,0 +1,654 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
+ require 'rational' unless defined?(Rational)
3
+
4
+ include TZInfo
5
+
6
+ class TCTimeOrDateTime < Minitest::Test
7
+ def test_initialize_time
8
+ assert_nothing_raised do
9
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000))
10
+ end
11
+ end
12
+
13
+ def test_initialize_time_local
14
+ tdt = TimeOrDateTime.new(Time.local(2006, 3, 24, 15, 32, 3))
15
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), tdt.to_time)
16
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), tdt.to_orig)
17
+ assert(tdt.to_time.utc?)
18
+ assert(tdt.to_orig.utc?)
19
+ end
20
+
21
+ def test_intialize_time_local_usec
22
+ tdt = TimeOrDateTime.new(Time.local(2006, 3, 24, 15, 32, 3, 721123))
23
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721123), tdt.to_time)
24
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721123), tdt.to_orig)
25
+ assert(tdt.to_time.utc?)
26
+ assert(tdt.to_orig.utc?)
27
+ end
28
+
29
+ if Time.utc(2013, 1, 1).respond_to?(:nsec)
30
+ def test_initialize_time_local_nsec
31
+ tdt = TimeOrDateTime.new(Time.local(2006, 3, 24, 15, 32, 3, 721123 + Rational(456,1000)))
32
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721123 + Rational(456,1000)), tdt.to_time)
33
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721123 + Rational(456,1000)), tdt.to_orig)
34
+ assert(tdt.to_time.utc?)
35
+ assert(tdt.to_orig.utc?)
36
+ end
37
+ end
38
+
39
+ def test_initialize_time_utc_local
40
+ # Check that local Time instances on systems using UTC as the system
41
+ # time zone are still converted to UTC Time instances.
42
+
43
+ # Note that this will only test will only work correctly on platforms where
44
+ # setting the TZ environment variable has an effect. If setting TZ has no
45
+ # effect, then this test will still pass.
46
+
47
+ old_tz = ENV['TZ']
48
+ begin
49
+ ENV['TZ'] = 'UTC'
50
+ tdt = TimeOrDateTime.new(Time.local(2014, 1, 11, 17, 18, 41))
51
+ assert_equal(Time.utc(2014, 1, 11, 17, 18, 41), tdt.to_time)
52
+ assert_equal(Time.utc(2014, 1, 11, 17, 18, 41), tdt.to_orig)
53
+ assert(tdt.to_time.utc?)
54
+ assert(tdt.to_orig.utc?)
55
+ ensure
56
+ ENV['TZ'] = old_tz
57
+ end
58
+ end
59
+
60
+ def test_initialize_datetime_offset
61
+ tdt = TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3).new_offset(Rational(5, 24)))
62
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), tdt.to_datetime)
63
+ assert_equal(0, tdt.to_datetime.offset)
64
+ end
65
+
66
+ def test_initialize_datetime
67
+ assert_nothing_raised do
68
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3))
69
+ end
70
+ end
71
+
72
+ def test_initialize_timestamp
73
+ assert_nothing_raised do
74
+ TimeOrDateTime.new(1143214323)
75
+ end
76
+ end
77
+
78
+ def test_initialize_timestamp_string
79
+ assert_nothing_raised do
80
+ TimeOrDateTime.new('1143214323')
81
+ end
82
+ end
83
+
84
+ unless RubyCoreSupport.time_supports_64bit
85
+ # Only define this test for non-64bit platforms. Some 64-bit Rubies support
86
+ # greater than 64-bit, others support less than the full range. In either
87
+ # case, times at the far ends of the range are so far in the future or past
88
+ # that they are not going to turn up in timezone data.
89
+ def test_initialize_timestamp_supported_range
90
+ assert_equal((2 ** 31) - 1, TimeOrDateTime.new((2 ** 31) - 1).to_orig)
91
+
92
+ assert_raises(RangeError) do
93
+ TimeOrDateTime.new(2 ** 31)
94
+ end
95
+
96
+ if RubyCoreSupport.time_supports_negative
97
+ assert_equal(-(2 ** 31), TimeOrDateTime.new(-(2 ** 31)).to_orig)
98
+
99
+ assert_raises(RangeError) do
100
+ TimeOrDateTime.new(-(2 ** 31) - 1)
101
+ end
102
+ else
103
+ assert_equal(0, TimeOrDateTime.new(0).to_orig)
104
+
105
+ assert_raises(RangeError) do
106
+ TimeOrDateTime.new(-1)
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ def test_to_time
113
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3),
114
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).to_time)
115
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721123),
116
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721123)).to_time)
117
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3),
118
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).to_time)
119
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721123),
120
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721123, 1000000))).to_time)
121
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3),
122
+ TimeOrDateTime.new(1143214323).to_time)
123
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3),
124
+ TimeOrDateTime.new('1143214323').to_time)
125
+ end
126
+
127
+ def test_to_time_trunc_to_usec
128
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721123),
129
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(7211239, 10000000))).to_time)
130
+ end
131
+
132
+ def test_to_time_after_freeze
133
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).freeze.to_time)
134
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), TimeOrDateTime.new(1143214323).freeze.to_time)
135
+ end
136
+
137
+ def test_to_datetime
138
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3),
139
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).to_datetime)
140
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721123, 1000000)),
141
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721123)).to_datetime)
142
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721123, 1000000)),
143
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721123, 1000000))).to_datetime)
144
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3),
145
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).to_datetime)
146
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3),
147
+ TimeOrDateTime.new(1143214323).to_datetime)
148
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3),
149
+ TimeOrDateTime.new('1143214323').to_datetime)
150
+ end
151
+
152
+ def test_to_datetime_ruby186_bug
153
+ # DateTime.new in Ruby 1.8.6 won't allow a time to be specified using
154
+ # fractions of a second that is within the 60th second of a minute.
155
+
156
+ # TimeOrDateTime has a workaround for this issue.
157
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 59) + Rational(721123, 86400000000),
158
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 59, 721123)).to_datetime)
159
+ end
160
+
161
+ def test_to_datetime_trunc_to_usec
162
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721123, 1000000)),
163
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721123 + Rational(9, 10))).to_datetime)
164
+ end
165
+
166
+ def test_to_datetime_after_freeze
167
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).freeze.to_datetime)
168
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), TimeOrDateTime.new(1143214323).freeze.to_datetime)
169
+ end
170
+
171
+ def test_to_i
172
+ assert_equal(1143214323,
173
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).to_i)
174
+ assert_equal(1143214323,
175
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)).to_i)
176
+ assert_equal(1143214323,
177
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).to_i)
178
+ assert_equal(1143214323,
179
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))).to_i)
180
+ assert_equal(1143214323,
181
+ TimeOrDateTime.new(1143214323).to_i)
182
+ assert_equal(1143214323,
183
+ TimeOrDateTime.new('1143214323').to_i)
184
+ end
185
+
186
+ def test_to_i_after_freeze
187
+ assert_equal(1143214323, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).freeze.to_i)
188
+ assert_equal(1143214323, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).freeze.to_i)
189
+ end
190
+
191
+ def test_to_orig
192
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3),
193
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).to_orig)
194
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721000),
195
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)).to_orig)
196
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3),
197
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).to_orig)
198
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000)),
199
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))).to_orig)
200
+ assert_equal(1143214323,
201
+ TimeOrDateTime.new(1143214323).to_orig)
202
+ assert_equal(1143214323,
203
+ TimeOrDateTime.new('1143214323').to_orig)
204
+ end
205
+
206
+ def test_to_s
207
+ assert_equal("Time: #{Time.utc(2006, 3, 24, 15, 32, 3).to_s}",
208
+ TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).to_s)
209
+ assert_equal("DateTime: #{DateTime.new(2006, 3, 24, 15, 32, 3)}",
210
+ TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).to_s)
211
+ assert_equal('Timestamp: 1143214323',
212
+ TimeOrDateTime.new(1143214323).to_s)
213
+ assert_equal('Timestamp: 1143214323',
214
+ TimeOrDateTime.new('1143214323').to_s)
215
+ end
216
+
217
+ def test_year
218
+ assert_equal(2006, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).year)
219
+ assert_equal(2006, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).year)
220
+ assert_equal(2006, TimeOrDateTime.new(1143214323).year)
221
+ end
222
+
223
+ def test_mon
224
+ assert_equal(3, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).mon)
225
+ assert_equal(3, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).mon)
226
+ assert_equal(3, TimeOrDateTime.new(1143214323).mon)
227
+ end
228
+
229
+ def test_month
230
+ assert_equal(3, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).month)
231
+ assert_equal(3, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).month)
232
+ assert_equal(3, TimeOrDateTime.new(1143214323).month)
233
+ end
234
+
235
+ def test_mday
236
+ assert_equal(24, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).mday)
237
+ assert_equal(24, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).mday)
238
+ assert_equal(24, TimeOrDateTime.new(1143214323).mday)
239
+ end
240
+
241
+ def test_day
242
+ assert_equal(24, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).day)
243
+ assert_equal(24, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).day)
244
+ assert_equal(24, TimeOrDateTime.new(1143214323).day)
245
+ end
246
+
247
+ def test_hour
248
+ assert_equal(15, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).hour)
249
+ assert_equal(15, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).hour)
250
+ assert_equal(15, TimeOrDateTime.new(1143214323).hour)
251
+ end
252
+
253
+ def test_min
254
+ assert_equal(32, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).min)
255
+ assert_equal(32, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).min)
256
+ assert_equal(32, TimeOrDateTime.new(1143214323).min)
257
+ end
258
+
259
+ def test_sec
260
+ assert_equal(3, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).sec)
261
+ assert_equal(3, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).sec)
262
+ assert_equal(3, TimeOrDateTime.new(1143214323).sec)
263
+ end
264
+
265
+ def test_usec
266
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).usec)
267
+ assert_equal(721123, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721123)).usec)
268
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).usec)
269
+ assert_equal(721123, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721123,1000000))).usec)
270
+ assert_equal(0, TimeOrDateTime.new(1143214323).usec)
271
+ end
272
+
273
+ def test_usec_after_to_i
274
+ val = TimeOrDateTime.new(Time.utc(2013, 2, 4, 22, 10, 33, 598000))
275
+ assert_equal(Time.utc(2013, 2, 4, 22, 10, 33).to_i, val.to_i)
276
+ assert_equal(598000, val.usec)
277
+ end
278
+
279
+ def test_compare_timeordatetime_time
280
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 4)))
281
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2007, 3, 24, 15, 32, 3)))
282
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)))
283
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 2)))
284
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2005, 3, 24, 15, 32, 3)))
285
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 4)))
286
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2007, 3, 24, 15, 32, 3)))
287
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)))
288
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 2)))
289
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2005, 3, 24, 15, 32, 3)))
290
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)))
291
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)))
292
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 4)))
293
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(Time.utc(2007, 3, 24, 15, 32, 3)))
294
+ assert_equal(0, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)))
295
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 2)))
296
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(Time.utc(2005, 3, 24, 15, 32, 3)))
297
+
298
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500001)))
299
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)))
300
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 499999)))
301
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000 + DATETIME_RESOLUTION)))
302
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)))
303
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000 - DATETIME_RESOLUTION)))
304
+ end
305
+
306
+ def test_compare_timeordatetime_datetime
307
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 4)))
308
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)))
309
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)))
310
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 2)))
311
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)))
312
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 4)))
313
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)))
314
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)))
315
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 2)))
316
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)))
317
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)))
318
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)))
319
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 4)))
320
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)))
321
+ assert_equal(0, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)))
322
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 2)))
323
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)))
324
+
325
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000 + DATETIME_RESOLUTION, 1000000))))
326
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))))
327
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000 - DATETIME_RESOLUTION, 1000000))))
328
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000 + DATETIME_RESOLUTION, 1000000))))
329
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))))
330
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000 - DATETIME_RESOLUTION, 1000000))))
331
+ end
332
+
333
+ def test_compare_timeordatetime_timestamp
334
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1143214324))
335
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1174750323))
336
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1143214323))
337
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1143214322))
338
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1111678323))
339
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1143214324))
340
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1174750323))
341
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1143214323))
342
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1143214322))
343
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1111678323))
344
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1143214323))
345
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)) <=> TimeOrDateTime.new(1143214323))
346
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(1143214324))
347
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(1174750323))
348
+ assert_equal(0, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(1143214323))
349
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(1143214322))
350
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> TimeOrDateTime.new(1111678323))
351
+ end
352
+
353
+ def test_compare_time
354
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2006, 3, 24, 15, 32, 4))
355
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2007, 3, 24, 15, 32, 3))
356
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2006, 3, 24, 15, 32, 3))
357
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2006, 3, 24, 15, 32, 2))
358
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2005, 3, 24, 15, 32, 3))
359
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2006, 3, 24, 15, 32, 4))
360
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2007, 3, 24, 15, 32, 3))
361
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2006, 3, 24, 15, 32, 3))
362
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2006, 3, 24, 15, 32, 2))
363
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> Time.utc(2005, 3, 24, 15, 32, 3))
364
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)) <=> Time.utc(2006, 3, 24, 15, 32, 3))
365
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)) <=> Time.utc(2006, 3, 24, 15, 32, 3))
366
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> Time.utc(2006, 3, 24, 15, 32, 4))
367
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> Time.utc(2007, 3, 24, 15, 32, 3))
368
+ assert_equal(0, TimeOrDateTime.new(1143214323) <=> Time.utc(2006, 3, 24, 15, 32, 3))
369
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> Time.utc(2006, 3, 24, 15, 32, 2))
370
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> Time.utc(2005, 3, 24, 15, 32, 3))
371
+
372
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> Time.utc(2006, 3, 24, 15, 32, 3, 500001))
373
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> Time.utc(2006, 3, 24, 15, 32, 3, 500000))
374
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> Time.utc(2006, 3, 24, 15, 32, 3, 499999))
375
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> Time.utc(2006, 3, 24, 15, 32, 3, 500000 + DATETIME_RESOLUTION))
376
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> Time.utc(2006, 3, 24, 15, 32, 3, 500000))
377
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> Time.utc(2006, 3, 24, 15, 32, 3, 500000 - DATETIME_RESOLUTION))
378
+ end
379
+
380
+ def test_compare_datetime
381
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(2006, 3, 24, 15, 32, 4))
382
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(2040, 3, 24, 15, 32, 3))
383
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(2006, 3, 24, 15, 32, 3))
384
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(2006, 3, 24, 15, 32, 2))
385
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(1960, 3, 24, 15, 32, 3))
386
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(2006, 3, 24, 15, 32, 4))
387
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(2040, 3, 24, 15, 32, 3))
388
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(2006, 3, 24, 15, 32, 3))
389
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(2006, 3, 24, 15, 32, 2))
390
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> DateTime.new(1960, 3, 24, 15, 32, 3))
391
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)) <=> DateTime.new(2006, 3, 24, 15, 32, 3))
392
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)) <=> DateTime.new(2006, 3, 24, 15, 32, 3))
393
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> DateTime.new(2006, 3, 24, 15, 32, 4))
394
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> DateTime.new(2040, 3, 24, 15, 32, 3))
395
+ assert_equal(0, TimeOrDateTime.new(1143214323) <=> DateTime.new(2006, 3, 24, 15, 32, 3))
396
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> DateTime.new(2006, 3, 24, 15, 32, 2))
397
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> DateTime.new(1960, 3, 24, 15, 32, 3))
398
+
399
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000 + DATETIME_RESOLUTION, 1000000)))
400
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000)))
401
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 500000)) <=> DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000 - DATETIME_RESOLUTION, 1000000)))
402
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000 + DATETIME_RESOLUTION, 1000000)))
403
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000)))
404
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000, 1000000))) <=> DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(500000 - DATETIME_RESOLUTION, 1000000)))
405
+ end
406
+
407
+ def test_compare_timestamp
408
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> 1143214324)
409
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> 1174750323)
410
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> 1143214323)
411
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> 1143214322)
412
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> 1111678323)
413
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> 1143214324)
414
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> 1174750323)
415
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> 1143214323)
416
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> 1143214322)
417
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> 1111678323)
418
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)) <=> 1143214323)
419
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)) <=> 1143214323)
420
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> 1143214324)
421
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> 1174750323)
422
+ assert_equal(0, TimeOrDateTime.new(1143214323) <=> 1143214323)
423
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> 1143214322)
424
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> 1111678323)
425
+ end
426
+
427
+ def test_compare_timestamp_str
428
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> '1143214324')
429
+ assert_equal(-1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> '1174750323')
430
+ assert_equal(0, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> '1143214323')
431
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> '1143214322')
432
+ assert_equal(1, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> '1111678323')
433
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> '1143214324')
434
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> '1174750323')
435
+ assert_equal(0, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> '1143214323')
436
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> '1143214322')
437
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> '1111678323')
438
+ assert_equal(-1, TimeOrDateTime.new(DateTime.new(1960, 3, 24, 15, 32, 3)) <=> '1143214323')
439
+ assert_equal(1, TimeOrDateTime.new(DateTime.new(2040, 3, 24, 15, 32, 3)) <=> '1143214323')
440
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> '1143214324')
441
+ assert_equal(-1, TimeOrDateTime.new(1143214323) <=> '1174750323')
442
+ assert_equal(0, TimeOrDateTime.new(1143214323) <=> '1143214323')
443
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> '1143214322')
444
+ assert_equal(1, TimeOrDateTime.new(1143214323) <=> '1111678323')
445
+ end
446
+
447
+ def test_compare_non_comparable
448
+ assert_nil(TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) <=> Object.new)
449
+ assert_nil(TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) <=> Object.new)
450
+ assert_nil(TimeOrDateTime.new(1143214323) <=> Object.new)
451
+ end
452
+
453
+ def test_eql
454
+ assert_equal(true, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3))))
455
+ assert_equal(false, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3))))
456
+ assert_equal(false, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new(1143214323)))
457
+ assert_equal(false, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new('1143214323')))
458
+ assert_equal(false, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 4))))
459
+ assert_equal(false, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).eql?(Object.new))
460
+
461
+ assert_equal(true, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)).eql?(TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000))))
462
+ assert_equal(false, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)).eql?(TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000)))))
463
+ assert_equal(false, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)).eql?(TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 722000))))
464
+ assert_equal(false, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)).eql?(Object.new))
465
+
466
+ assert_equal(false, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3))))
467
+ assert_equal(true, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3))))
468
+ assert_equal(false, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new(1143214323)))
469
+ assert_equal(false, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new('1143214323')))
470
+ assert_equal(false, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).eql?(TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 4))))
471
+ assert_equal(false, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).eql?(Object.new))
472
+
473
+ assert_equal(false, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))).eql?(TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000))))
474
+ assert_equal(true, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))).eql?(TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000)))))
475
+ assert_equal(false, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))).eql?(TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(722, 1000)))))
476
+ assert_equal(false, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))).eql?(Object.new))
477
+
478
+
479
+ assert_equal(false, TimeOrDateTime.new(1143214323).eql?(TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3))))
480
+ assert_equal(false, TimeOrDateTime.new(1143214323).eql?(TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3))))
481
+ assert_equal(true, TimeOrDateTime.new(1143214323).eql?(TimeOrDateTime.new(1143214323)))
482
+ assert_equal(true, TimeOrDateTime.new(1143214323).eql?(TimeOrDateTime.new('1143214323')))
483
+ assert_equal(false, TimeOrDateTime.new(1143214323).eql?(TimeOrDateTime.new(1143214324)))
484
+ assert_equal(false, TimeOrDateTime.new(1143214323).eql?(Object.new))
485
+ end
486
+
487
+ def test_hash
488
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3).hash, TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).hash)
489
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3).hash, TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).hash)
490
+ assert_equal(1143214323.hash, TimeOrDateTime.new(1143214323).hash)
491
+ assert_equal(1143214323.hash, TimeOrDateTime.new('1143214323').hash)
492
+ end
493
+
494
+ def test_add
495
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) + 0).to_orig)
496
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721000), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)) + 0).to_orig)
497
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) + 0).to_orig)
498
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000)), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))) + 0).to_orig)
499
+ assert_equal(1143214323, (TimeOrDateTime.new(1143214323) + 0).to_orig)
500
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 4), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) + 1).to_orig)
501
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 4, 721000), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)) + 1).to_orig)
502
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 4), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) + 1).to_orig)
503
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 4 + Rational(721, 1000)), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))) + 1).to_orig)
504
+ assert_equal(1143214324, (TimeOrDateTime.new(1143214323) + 1).to_orig)
505
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 2), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) + (-1)).to_orig)
506
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 2, 721000), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)) + (-1)).to_orig)
507
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 2), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) + (-1)).to_orig)
508
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 2 + Rational(721, 1000)), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))) + (-1)).to_orig)
509
+ assert_equal(1143214322, (TimeOrDateTime.new(1143214323) + (-1)).to_orig)
510
+ end
511
+
512
+ def test_subtract
513
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) - 0).to_orig)
514
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721000), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)) - 0).to_orig)
515
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) - 0).to_orig)
516
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000)), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))) - 0).to_orig)
517
+ assert_equal(1143214323, (TimeOrDateTime.new(1143214323) - 0).to_orig)
518
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 2), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) - 1).to_orig)
519
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 2, 721000), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)) - 1).to_orig)
520
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 2), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) - 1).to_orig)
521
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 2 + Rational(721, 1000)), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))) - 1).to_orig)
522
+ assert_equal(1143214322, (TimeOrDateTime.new(1143214323) - 1).to_orig)
523
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 4), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)) - (-1)).to_orig)
524
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 4, 721000), (TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)) - (-1)).to_orig)
525
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 4), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)) - (-1)).to_orig)
526
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 4 + Rational(721, 1000)), (TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))) - (-1)).to_orig)
527
+ assert_equal(1143214324, (TimeOrDateTime.new(1143214323) - (-1)).to_orig)
528
+ end
529
+
530
+ def test_add_with_convert
531
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).add_with_convert(0).to_orig)
532
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3, 721000), TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)).add_with_convert(0).to_orig)
533
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).add_with_convert(0).to_orig)
534
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000)), TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))).add_with_convert(0).to_orig)
535
+ assert_equal(1143214323, TimeOrDateTime.new(1143214323).add_with_convert(0).to_orig)
536
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 4), TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).add_with_convert(1).to_orig)
537
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 4, 721000), TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)).add_with_convert(1).to_orig)
538
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 4), TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).add_with_convert(1).to_orig)
539
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 4 + Rational(721, 1000)), TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))).add_with_convert(1).to_orig)
540
+ assert_equal(1143214324, TimeOrDateTime.new(1143214323).add_with_convert(1).to_orig)
541
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 2), TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3)).add_with_convert(-1).to_orig)
542
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 2, 721000), TimeOrDateTime.new(Time.utc(2006, 3, 24, 15, 32, 3, 721000)).add_with_convert(-1).to_orig)
543
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 2), TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3)).add_with_convert(-1).to_orig)
544
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 2 + Rational(721, 1000)), TimeOrDateTime.new(DateTime.new(2006, 3, 24, 15, 32, 3 + Rational(721, 1000))).add_with_convert(-1).to_orig)
545
+ assert_equal(1143214322, TimeOrDateTime.new(1143214323).add_with_convert(-1).to_orig)
546
+
547
+ if RubyCoreSupport.time_supports_negative
548
+ assert_equal(Time.utc(1969, 12, 31, 23, 59, 59), TimeOrDateTime.new(Time.utc(1970, 1, 1, 0, 0, 0)).add_with_convert(-1).to_orig)
549
+ assert_equal(-1, TimeOrDateTime.new(0).add_with_convert(-1).to_orig)
550
+ assert_equal(Time.utc(1969, 12, 31, 23, 59, 59, 892000), TimeOrDateTime.new(Time.utc(1970, 1, 1, 0, 0, 0, 892000)).add_with_convert(-1).to_orig)
551
+
552
+ if RubyCoreSupport.time_supports_64bit
553
+ assert_equal(Time.utc(1901, 12, 13, 20, 45, 51), TimeOrDateTime.new(Time.utc(1901, 12, 13, 20, 45, 52)).add_with_convert(-1).to_orig)
554
+ assert_equal(-2147483649, TimeOrDateTime.new(-2147483648).add_with_convert(-1).to_orig)
555
+ assert_equal(Time.utc(1901, 12, 13, 20, 45, 51, 892000), TimeOrDateTime.new(Time.utc(1901, 12, 13, 20, 45, 52, 892000)).add_with_convert(-1).to_orig)
556
+ else
557
+ assert_equal(DateTime.new(1901, 12, 13, 20, 45, 51), TimeOrDateTime.new(Time.utc(1901, 12, 13, 20, 45, 52)).add_with_convert(-1).to_orig)
558
+ assert_equal(DateTime.new(1901, 12, 13, 20, 45, 51), TimeOrDateTime.new(-2147483648).add_with_convert(-1).to_orig)
559
+ assert_equal(DateTime.new(1901, 12, 13, 20, 45, 51 + Rational(892,1000)), TimeOrDateTime.new(Time.utc(1901, 12, 13, 20, 45, 52, 892000)).add_with_convert(-1).to_orig)
560
+ end
561
+ else
562
+ assert_equal(DateTime.new(1969, 12, 31, 23, 59, 59), TimeOrDateTime.new(Time.utc(1970, 1, 1, 0, 0, 0)).add_with_convert(-1).to_orig)
563
+ assert_equal(DateTime.new(1969, 12, 31, 23, 59, 59), TimeOrDateTime.new(0).add_with_convert(-1).to_orig)
564
+ assert_equal(RubyCoreSupport.datetime_new(1969, 12, 31, 23, 59, 59 + Rational(892,1000)), TimeOrDateTime.new(Time.utc(1970, 1, 1, 0, 0, 0, 892000)).add_with_convert(-1).to_orig)
565
+ end
566
+
567
+ if RubyCoreSupport.time_supports_64bit
568
+ assert_equal(Time.utc(2038, 1, 19, 3, 14, 8), TimeOrDateTime.new(Time.utc(2038, 1, 19, 3, 14, 7)).add_with_convert(1).to_orig)
569
+ assert_equal(2147483648, TimeOrDateTime.new(2147483647).add_with_convert(1).to_orig)
570
+ assert_equal(Time.utc(2038, 1, 19, 3, 14, 8, 892000), TimeOrDateTime.new(Time.utc(2038, 1, 19, 3, 14, 7, 892000)).add_with_convert(1).to_orig)
571
+ else
572
+ assert_equal(DateTime.new(2038, 1, 19, 3, 14, 8), TimeOrDateTime.new(Time.utc(2038, 1, 19, 3, 14, 7)).add_with_convert(1).to_orig)
573
+ assert_equal(DateTime.new(2038, 1, 19, 3, 14, 8), TimeOrDateTime.new(2147483647).add_with_convert(1).to_orig)
574
+ assert_equal(DateTime.new(2038, 1, 19, 3, 14, 8 + Rational(892,1000)), TimeOrDateTime.new(Time.utc(2038, 1, 19, 3, 14, 7, 892000)).add_with_convert(1).to_orig)
575
+
576
+ assert_equal(Time.utc(2038, 1, 19, 3, 14, 7, 892000), TimeOrDateTime.new(Time.utc(2038, 1, 19, 3, 14, 6, 892000)).add_with_convert(1).to_orig)
577
+ end
578
+ end
579
+
580
+ def test_wrap_time
581
+ t = TimeOrDateTime.wrap(Time.utc(2006, 3, 24, 15, 32, 3))
582
+ assert_instance_of(TimeOrDateTime, t)
583
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), t.to_orig)
584
+ end
585
+
586
+ def test_wrap_datetime
587
+ t = TimeOrDateTime.wrap(DateTime.new(2006, 3, 24, 15, 32, 3))
588
+ assert_instance_of(TimeOrDateTime, t)
589
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), t.to_orig)
590
+ end
591
+
592
+ def test_wrap_timestamp
593
+ t = TimeOrDateTime.wrap(1143214323)
594
+ assert_instance_of(TimeOrDateTime, t)
595
+ assert_equal(1143214323, t.to_orig)
596
+ end
597
+
598
+ def test_wrap_timestamp_str
599
+ t = TimeOrDateTime.wrap('1143214323')
600
+ assert_instance_of(TimeOrDateTime, t)
601
+ assert_equal(1143214323, t.to_orig)
602
+ end
603
+
604
+ def test_wrap_timeordatetime
605
+ t = TimeOrDateTime.new(1143214323)
606
+ t2 = TimeOrDateTime.wrap(t)
607
+ assert_same(t, t2)
608
+ end
609
+
610
+ def test_wrap_block_time
611
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 4), TimeOrDateTime.wrap(Time.utc(2006, 3, 24, 15, 32, 3)) {|t|
612
+ assert_instance_of(TimeOrDateTime, t)
613
+ assert_equal(Time.utc(2006, 3, 24, 15, 32, 3), t.to_orig)
614
+ t + 1
615
+ })
616
+ end
617
+
618
+ def test_wrap_block_datetime
619
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 4), TimeOrDateTime.wrap(DateTime.new(2006, 3, 24, 15, 32, 3)) {|t|
620
+ assert_instance_of(TimeOrDateTime, t)
621
+ assert_equal(DateTime.new(2006, 3, 24, 15, 32, 3), t.to_orig)
622
+ t + 1
623
+ })
624
+ end
625
+
626
+ def test_wrap_block_timestamp
627
+ assert_equal(1143214324, TimeOrDateTime.wrap(1143214323) {|t|
628
+ assert_instance_of(TimeOrDateTime, t)
629
+ assert_equal(1143214323, t.to_orig)
630
+ t + 1
631
+ })
632
+ end
633
+
634
+ def test_wrap_block_timestamp_str
635
+ assert_equal(1143214324, TimeOrDateTime.wrap('1143214323') {|t|
636
+ assert_instance_of(TimeOrDateTime, t)
637
+ assert_equal(1143214323, t.to_orig)
638
+ t + 1
639
+ })
640
+ end
641
+
642
+ def test_wrap_block_timeordatetime
643
+ t1 = TimeOrDateTime.new(1143214323)
644
+
645
+ t2 = TimeOrDateTime.wrap(t1) {|t|
646
+ assert_same(t1, t)
647
+ t + 1
648
+ }
649
+
650
+ assert t2
651
+ assert_instance_of(TimeOrDateTime, t2)
652
+ assert_equal(1143214324, t2.to_orig)
653
+ end
654
+ end