Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-3a66518
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
17import datetime
18import sys
19from decimal import Decimal
20from unittest import mock
21
22import pytest
23
24from freezegun import freeze_time
25
26from 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', [
35 (datetime.datetime(2000, 1, 1, 0, 0, 0), 630720013),
36 (datetime.date(2000, 1, 2), 630806413),
37])
38def test_utc_to_gps(date, gps):
39 out = gpstime.utc_to_gps(date)
40 assert isinstance(out, LIGOTimeGPS)
41 assert out == LIGOTimeGPS(gps)
42
43
44@pytest.mark.parametrize('gps, date', [
45 (630720013, datetime.datetime(2000, 1, 1, 0, 0, 0)),
46 (LIGOTimeGPS(630720013, 12345), datetime.datetime(2000, 1, 1, 0, 0, 0, 12)),
47 (LIGOTimeGPS(630720013, 12999), datetime.datetime(2000, 1, 1, 0, 0, 0, 13)),
48 (Decimal("1234567890.123456789"), datetime.datetime(2019, 2, 18, 23, 31, 12, 123457)),
49])
50def test_gps_to_utc(gps, date):
51 assert gpstime.gps_to_utc(gps) == date
52
53
54@mock.patch("lal.gpstime._gps_time_now", return_value=LIGOTimeGPS(100))
56 assert gpstime.utc_time_now() == datetime.datetime(
57 1980, 1, 6, 0, 1, 40,
58 )
59
60
61@freeze_time("2015-09-14 09:50:45.391")
62@mock.patch("lal.gpstime._gps_time_now", return_value=LIGOTimeGPS(1126259462))
63@pytest.mark.parametrize('text, gps', [
64 (None, 1126259462),
65 ("now", 1126259462),
66 ("today", 1126224017),
67 ("tomorrow", 1126310417),
68 ("yesterday", 1126137617),
69 ("Sep 14 2015 09:50:45.391", LIGOTimeGPS(1126259462, 391000000)),
70])
71def test_str_to_gps(_, text, gps):
72 out = gpstime.str_to_gps(text)
73 print(type(out))
74 assert isinstance(out, LIGOTimeGPS)
75 assert out == LIGOTimeGPS(gps)
76
77
78@pytest.mark.parametrize('gps, text', [
79 (LIGOTimeGPS(1126259462, 391000000),
80 "September 14 2015, 09:50:45.391000 UTC"),
81 (1126259462, "September 14 2015, 09:50:45 UTC"),
82])
83def test_gps_to_str(gps, text):
84 assert gpstime.gps_to_str(gps) == text
85
86
88 assert gpstime.gps_to_str(12345, form="%Y%m%d") == "19800106"
89
90
91@mock.patch("lal.gpstime._gps_time_now", return_value=LIGOTimeGPS(1126259462))
92@pytest.mark.parametrize('in_, result', [
93 ("now", LIGOTimeGPS(1126259462)),
94 (LIGOTimeGPS(1126259462, 391000000),
95 "September 14 2015, 09:50:45.391000 UTC"),
96])
97def test_tconvert(_, in_, result):
98 out = gpstime.tconvert(in_)
99 assert type(out) == type(result)
100 assert out == result
101
102
103if __name__ == '__main__':
104 args = sys.argv[1:] or ["-v", "-rs", "--junit-xml=junit-gpstime.xml"]
105 sys.exit(pytest.main(args=[__file__] + args))
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