LAL  7.5.0.1-b72065a
test_gpstime.py
Go to the documentation of this file.
1 # Copyright (C) 2019 Duncan Macleod
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation; either version 2 of the License, or (at your
6 # option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 # Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17 import datetime
18 import sys
19 from decimal import Decimal
20 from unittest import mock
21 
22 import pytest
23 
24 from freezegun import freeze_time
25 
26 from lal import (LIGOTimeGPS, gpstime)
27 
28 
29 @mock.patch("lal.gpstime._gps_time_now", return_value=LIGOTimeGPS(100))
31  assert gpstime.gps_time_now() == 100.
32 
33 
34 @pytest.mark.parametrize('date, gps', [ (datetime.datetime(2000, 1, 1, 0, 0, 0), 630720013),
35  (datetime.date(2000, 1, 2), 630806413),
36 ])
37 def test_utc_to_gps(date, gps):
38  out = gpstime.utc_to_gps(date)
39  assert isinstance(out, LIGOTimeGPS)
40  assert out == LIGOTimeGPS(gps)
41 
42 
43 @pytest.mark.parametrize('gps, date', [ (630720013, datetime.datetime(2000, 1, 1, 0, 0, 0)),
44  (LIGOTimeGPS(630720013, 12345), datetime.datetime(2000, 1, 1, 0, 0, 0, 12)),
45  (LIGOTimeGPS(630720013, 12999), datetime.datetime(2000, 1, 1, 0, 0, 0, 13)),
46  (Decimal("1234567890.123456789"), datetime.datetime(2019, 2, 18, 23, 31, 12, 123457)),
47 ])
48 def test_gps_to_utc(gps, date):
49  assert gpstime.gps_to_utc(gps) == date
50 
51 
52 @mock.patch("lal.gpstime._gps_time_now", return_value=LIGOTimeGPS(100))
53 def test_utc_time_now(_):
54  assert gpstime.utc_time_now() == datetime.datetime(
55  1980, 1, 6, 0, 1, 40,
56  )
57 
58 
59 @freeze_time("2015-09-14 09:50:45.391")
60 @mock.patch("lal.gpstime._gps_time_now", return_value=LIGOTimeGPS(1126259462))
61 @pytest.mark.parametrize('text, gps', [ (None, 1126259462),
62  ("now", 1126259462),
63  ("today", 1126224017),
64  ("tomorrow", 1126310417),
65  ("yesterday", 1126137617),
66  ("Sep 14 2015 09:50:45.391", LIGOTimeGPS(1126259462, 391000000)),
67 ])
68 def test_str_to_gps(_, text, gps):
69  out = gpstime.str_to_gps(text)
70  print(type(out))
71  assert isinstance(out, LIGOTimeGPS)
72  assert out == LIGOTimeGPS(gps)
73 
74 
75 @pytest.mark.parametrize('gps, text', [ (LIGOTimeGPS(1126259462, 391000000),
76  "September 14 2015, 09:50:45.391000 UTC"),
77  (1126259462, "September 14 2015, 09:50:45 UTC"),
78 ])
79 def test_gps_to_str(gps, text):
80  assert gpstime.gps_to_str(gps) == text
81 
82 
84  assert gpstime.gps_to_str(12345, form="%Y%m%d") == "19800106"
85 
86 
87 @mock.patch("lal.gpstime._gps_time_now", return_value=LIGOTimeGPS(1126259462))
88 @pytest.mark.parametrize('in_, result', [ ("now", LIGOTimeGPS(1126259462)),
89  (LIGOTimeGPS(1126259462, 391000000),
90  "September 14 2015, 09:50:45.391000 UTC"),
91 ])
92 def test_tconvert(_, in_, result):
93  out = gpstime.tconvert(in_)
94  assert type(out) == type(result)
95  assert out == result
96 
97 
98 if __name__ == '__main__':
99  args = sys.argv[1:] or ["-v", "-rs", "--junit-xml=junit-gpstime.xml"]
100  sys.exit(pytest.main(args=[__file__] + args))
101 
def test_utc_time_now(_)
Definition: test_gpstime.py:55
def test_gps_time_now(_)
Definition: test_gpstime.py:30
def test_str_to_gps(_, text, gps)
Definition: test_gpstime.py:71
def test_gps_to_str(gps, text)
Definition: test_gpstime.py:83
def test_gps_to_str_form()
Definition: test_gpstime.py:87
def test_utc_to_gps(date, gps)
Definition: test_gpstime.py:38
def test_gps_to_utc(gps, date)
Definition: test_gpstime.py:50
def test_tconvert(_, in_, result)
Definition: test_gpstime.py:97