1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied.  See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18class TimeTest < Test::Unit::TestCase
19  sub_test_case("#==") do
20    test("same unit") do
21      assert do
22        Arrow::Time.new(:second, 10) == Arrow::Time.new(:second, 10)
23      end
24    end
25
26    test("different unit") do
27      assert do
28        Arrow::Time.new(:second, 10) == Arrow::Time.new(:milli, 10 * 1000)
29      end
30    end
31
32    test("false") do
33      assert do
34        not(Arrow::Time.new(:second, 10) == Arrow::Time.new(:second, 11))
35      end
36    end
37  end
38
39  sub_test_case("#cast") do
40    test("same unit") do
41      time = Arrow::Time.new(Arrow::TimeUnit::SECOND, 10)
42      casted_time = time.cast(Arrow::TimeUnit::SECOND)
43      assert_equal([time.unit, time.value],
44                   [casted_time.unit, casted_time.value])
45    end
46
47    test("second -> milli") do
48      time = Arrow::Time.new(Arrow::TimeUnit::SECOND, 10)
49      casted_time = time.cast(Arrow::TimeUnit::MILLI)
50      assert_equal([
51                     Arrow::TimeUnit::MILLI,
52                     time.value * 1000,
53                   ],
54                   [
55                     casted_time.unit,
56                     casted_time.value,
57                   ])
58    end
59
60    test("second -> micro") do
61      time = Arrow::Time.new(Arrow::TimeUnit::SECOND, 10)
62      casted_time = time.cast(Arrow::TimeUnit::MICRO)
63      assert_equal([
64                     Arrow::TimeUnit::MICRO,
65                     time.value * 1000 * 1000,
66                   ],
67                   [
68                     casted_time.unit,
69                     casted_time.value,
70                   ])
71    end
72
73    test("second -> nano") do
74      time = Arrow::Time.new(Arrow::TimeUnit::SECOND, 10)
75      casted_time = time.cast(Arrow::TimeUnit::NANO)
76      assert_equal([
77                     Arrow::TimeUnit::NANO,
78                     time.value * 1000 * 1000 * 1000,
79                   ],
80                   [
81                     casted_time.unit,
82                     casted_time.value,
83                   ])
84    end
85
86    test("milli -> second") do
87      time = Arrow::Time.new(Arrow::TimeUnit::MILLI, 10_200)
88      casted_time = time.cast(Arrow::TimeUnit::SECOND)
89      assert_equal([
90                     Arrow::TimeUnit::SECOND,
91                     10,
92                   ],
93                   [
94                     casted_time.unit,
95                     casted_time.value,
96                   ])
97    end
98
99    test("milli -> micro") do
100      time = Arrow::Time.new(Arrow::TimeUnit::MILLI, 10_200)
101      casted_time = time.cast(Arrow::TimeUnit::MICRO)
102      assert_equal([
103                     Arrow::TimeUnit::MICRO,
104                     time.value * 1000,
105                   ],
106                   [
107                     casted_time.unit,
108                     casted_time.value,
109                   ])
110    end
111
112    test("milli -> nano") do
113      time = Arrow::Time.new(Arrow::TimeUnit::MILLI, 10_200)
114      casted_time = time.cast(Arrow::TimeUnit::NANO)
115      assert_equal([
116                     Arrow::TimeUnit::NANO,
117                     time.value * 1000 * 1000,
118                   ],
119                   [
120                     casted_time.unit,
121                     casted_time.value,
122                   ])
123    end
124
125    test("micro -> second") do
126      time = Arrow::Time.new(Arrow::TimeUnit::MICRO, 10_200_300)
127      casted_time = time.cast(Arrow::TimeUnit::SECOND)
128      assert_equal([
129                     Arrow::TimeUnit::SECOND,
130                     10,
131                   ],
132                   [
133                     casted_time.unit,
134                     casted_time.value,
135                   ])
136    end
137
138    test("micro -> milli") do
139      time = Arrow::Time.new(Arrow::TimeUnit::MICRO, 10_200_300)
140      casted_time = time.cast(Arrow::TimeUnit::MILLI)
141      assert_equal([
142                     Arrow::TimeUnit::MILLI,
143                     10_200,
144                   ],
145                   [
146                     casted_time.unit,
147                     casted_time.value,
148                   ])
149    end
150
151    test("micro -> nano") do
152      time = Arrow::Time.new(Arrow::TimeUnit::MICRO, 10_200_300)
153      casted_time = time.cast(Arrow::TimeUnit::NANO)
154      assert_equal([
155                     Arrow::TimeUnit::NANO,
156                     time.value * 1000,
157                   ],
158                   [
159                     casted_time.unit,
160                     casted_time.value,
161                   ])
162    end
163
164    test("nano -> second") do
165      time = Arrow::Time.new(Arrow::TimeUnit::NANO, 10_200_300_400)
166      casted_time = time.cast(Arrow::TimeUnit::SECOND)
167      assert_equal([
168                     Arrow::TimeUnit::SECOND,
169                     10,
170                   ],
171                   [
172                     casted_time.unit,
173                     casted_time.value,
174                   ])
175    end
176
177    test("nano -> milli") do
178      time = Arrow::Time.new(Arrow::TimeUnit::NANO, 10_200_300_400)
179      casted_time = time.cast(Arrow::TimeUnit::MILLI)
180      assert_equal([
181                     Arrow::TimeUnit::MILLI,
182                     10_200,
183                   ],
184                   [
185                     casted_time.unit,
186                     casted_time.value,
187                   ])
188    end
189
190    test("nano -> micro") do
191      time = Arrow::Time.new(Arrow::TimeUnit::NANO, 10_200_300_400)
192      casted_time = time.cast(Arrow::TimeUnit::MICRO)
193      assert_equal([
194                     Arrow::TimeUnit::MICRO,
195                     10_200_300,
196                   ],
197                   [
198                     casted_time.unit,
199                     casted_time.value,
200                   ])
201    end
202  end
203
204  sub_test_case("#to_f") do
205    test("second") do
206      time = Arrow::Time.new(Arrow::TimeUnit::SECOND, 10)
207      assert_in_delta(10.0, time.to_f)
208    end
209
210    test("milli") do
211      time = Arrow::Time.new(Arrow::TimeUnit::MILLI, 10_200)
212      assert_in_delta(10.2, time.to_f)
213    end
214
215    test("micro") do
216      time = Arrow::Time.new(Arrow::TimeUnit::MICRO, 10_200_300)
217      assert_in_delta(10.2003, time.to_f)
218    end
219
220    test("nano") do
221      time = Arrow::Time.new(Arrow::TimeUnit::NANO, 10_200_300_400)
222      assert_in_delta(10.2003004, time.to_f)
223    end
224  end
225
226  sub_test_case("#positive?") do
227    test("true") do
228      time = Arrow::Time.new(Arrow::TimeUnit::SECOND, 10)
229      assert do
230        time.positive?
231      end
232    end
233
234    test("false") do
235      time = Arrow::Time.new(Arrow::TimeUnit::SECOND, -10)
236      assert do
237        not time.positive?
238      end
239    end
240  end
241
242  sub_test_case("#negative?") do
243    test("true") do
244      time = Arrow::Time.new(Arrow::TimeUnit::SECOND, -10)
245      assert do
246        time.negative?
247      end
248    end
249
250    test("false") do
251      time = Arrow::Time.new(Arrow::TimeUnit::SECOND, 10)
252      assert do
253        not time.negative?
254      end
255    end
256  end
257
258  test("#hour") do
259    time = Arrow::Time.new(Arrow::TimeUnit::SECOND,
260                           (5 * 60 * 60) + (12 * 60) + 10)
261    assert_equal(5, time.hour)
262  end
263
264  test("#minute") do
265    time = Arrow::Time.new(Arrow::TimeUnit::SECOND,
266                           (5 * 60 * 60) + (12 * 60) + 10)
267    assert_equal(12, time.minute)
268  end
269
270  test("#second") do
271    time = Arrow::Time.new(Arrow::TimeUnit::SECOND,
272                           (5 * 60 * 60) + (12 * 60) + 10)
273    assert_equal(10, time.second)
274  end
275
276  test("#nano_second") do
277    time = Arrow::Time.new(Arrow::TimeUnit::NANO, 1234)
278    assert_equal(1234, time.nano_second)
279  end
280
281  test("#to_s") do
282    time = Arrow::Time.new(Arrow::TimeUnit::NANO,
283                           -(((5 * 60 * 60) + (12 * 60) + 10) * 1_000_000_000 +
284                             1234))
285    assert_equal("-05:12:10.000001234",
286                 time.to_s)
287  end
288end
289