Coverage for tests/test_time_utils.py : 17%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22"""Unit tests for methods in butler.time module.
23"""
25import unittest
27from astropy.time import Time, TimeDelta
28from lsst.daf.butler import time_utils
29from lsst.daf.butler.core.time_utils import astropy_to_nsec
32class TimeTestCase(unittest.TestCase):
33 """A test case for time module
34 """
36 def test_time_before_epoch(self):
37 """Tests for before-the-epoch time.
38 """
39 time = Time("1950-01-01T00:00:00", format="isot", scale="tai")
40 value = time_utils.astropy_to_nsec(time)
41 self.assertEqual(value, 0)
43 value = time_utils.nsec_to_astropy(value)
44 self.assertEqual(value, time_utils.EPOCH)
46 def test_max_time(self):
47 """Tests for after-the-end-of-astronomy time.
48 """
49 # there are rounding issues, need more complex comparison
50 time = Time("2101-01-01T00:00:00", format="isot", scale="tai")
51 value = time_utils.astropy_to_nsec(time)
53 value_max = time_utils.astropy_to_nsec(time_utils.MAX_TIME)
54 self.assertEqual(value, value_max)
56 def test_round_trip(self):
57 """Test precision of round-trip conversion.
58 """
59 # do tests at random points between epoch and max. time
60 times = [
61 "1970-01-01T12:00:00.123",
62 "1999-12-31T23:59:57.123456",
63 "2000-01-01T12:00:00.123456",
64 "2030-01-01T12:00:00.123456789",
65 "2075-08-17T00:03:45",
66 "2099-12-31T23:00:50",
67 ]
68 for time in times:
69 atime = Time(time, format="isot", scale="tai")
70 for sec in range(7):
71 # loop over few seconds to add to each time
72 for i in range(100):
73 # loop over additional fractions of seconds
74 delta = sec + 0.3e-9 * i
75 in_time = atime + TimeDelta(delta, format="sec")
76 # do round-trip conversion to nsec and back
77 value = time_utils.astropy_to_nsec(in_time)
78 value = time_utils.nsec_to_astropy(value)
79 delta2 = value - in_time
80 delta2_sec = delta2.to_value("sec")
81 # absolute precision should be better than half
82 # nanosecond, but there are rounding errors too
83 self.assertLess(abs(delta2_sec), 0.51e-9)
85 def test_times_equal(self):
86 """Test for times_equal method
87 """
88 # time == time should always work
89 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
90 self.assertTrue(time_utils.times_equal(time1, time1))
92 # one nsec difference
93 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
94 time2 = Time("2000-01-01T00:00:00.123456788", format="isot", scale="tai")
95 self.assertTrue(time_utils.times_equal(time1, time2, 2.))
96 self.assertTrue(time_utils.times_equal(time2, time1, 2.))
97 self.assertFalse(time_utils.times_equal(time1, time2, .5))
98 self.assertFalse(time_utils.times_equal(time2, time1, .5))
100 # one nsec difference, times in UTC
101 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="utc")
102 time2 = Time("2000-01-01T00:00:00.123456788", format="isot", scale="utc")
103 self.assertTrue(time_utils.times_equal(time1, time2, 2.))
104 self.assertTrue(time_utils.times_equal(time2, time1, 2.))
105 self.assertFalse(time_utils.times_equal(time1, time2, .5))
106 self.assertFalse(time_utils.times_equal(time2, time1, .5))
108 # 1/2 nsec difference
109 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
110 time2 = time1 + TimeDelta(0.5e-9, format="sec")
111 self.assertTrue(time_utils.times_equal(time1, time2))
112 self.assertTrue(time_utils.times_equal(time2, time1))
113 self.assertFalse(time_utils.times_equal(time1, time2, .25))
114 self.assertFalse(time_utils.times_equal(time2, time1, .25))
116 # 1/2 microsec difference
117 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai")
118 time2 = time1 + TimeDelta(0.5e-6, format="sec")
119 self.assertTrue(time_utils.times_equal(time1, time2, 501))
120 self.assertTrue(time_utils.times_equal(time2, time1, 501))
121 self.assertFalse(time_utils.times_equal(time1, time2, 499))
122 self.assertFalse(time_utils.times_equal(time2, time1, 499))
124 # UTC vs TAI
125 time1 = Time('2013-06-17 13:34:45.775000', scale='tai', format='iso')
126 time2 = Time('2013-06-17T13:34:10.775', scale='utc', format='isot')
127 self.assertTrue(time_utils.times_equal(time1, time2))
128 self.assertEqual(astropy_to_nsec(time1), astropy_to_nsec(time2))
131if __name__ == "__main__": 131 ↛ 132line 131 didn't jump to line 132, because the condition on line 131 was never true
132 unittest.main()