diff --git a/leapseconddata.py b/leapseconddata/__init__.py similarity index 90% rename from leapseconddata.py rename to leapseconddata/__init__.py index 556c927..3565831 100755 --- a/leapseconddata.py +++ b/leapseconddata/__init__.py @@ -359,38 +359,3 @@ class LeapSecondData(_LeapSecondData): ) return LeapSecondData(leap_seconds, valid_until, last_updated) - - -def main() -> None: - """When run as a main program, print some information about leap seconds""" - logging.getLogger().setLevel(logging.INFO) - lsd = LeapSecondData.from_standard_source() - print(f"Last updated: {lsd.last_updated:%Y-%m-%d}") - print(f"Valid until: {lsd.valid_until:%Y-%m-%d}") - for when, offset in lsd.leap_seconds[-10:]: - print(f"{when:%Y-%m-%d}: {offset.total_seconds()}") - when = datetime.datetime(2011, 1, 1, tzinfo=datetime.timezone.utc) - print(f"TAI-UTC on {when:%Y-%m-%d} was {lsd.tai_offset(when).total_seconds()}") - - when_tai = lsd.to_tai(when) - when_rt = lsd.tai_to_utc(when_tai) - print(f"{when:%Y-%m-%d %H:%M:%S} UTC -> {when_tai:%Y-%m-%d %H:%M:%S} TAI") - print(f"{when_tai:%Y-%m-%d %H:%M:%S} TAI -> {when_rt:%Y-%m-%d %H:%M:%S} UTC") - print(f"is leap second? {lsd.is_leap_second(when)}") - - u = datetime.datetime( - 1999, 1, 1, tzinfo=datetime.timezone.utc - ) - datetime.timedelta(seconds=2) - t = lsd.to_tai(u) - - print("replaying leapsecond at end of 1998") - for _ in range(5): - print( - f"{u:%Y-%m-%d %H:%M:%S} UTC {'LS' if lsd.is_leap_second(u) else ' '} = {t:%Y-%m-%d %H:%M:%S} TAI {'LS' if lsd.is_leap_second(t) else ' '}" - ) - t += datetime.timedelta(seconds=1) - u = lsd.tai_to_utc(t) - - -if __name__ == "__main__": # pragma no cover - main() diff --git a/leapseconddata/__main__.py b/leapseconddata/__main__.py new file mode 100644 index 0000000..69b347a --- /dev/null +++ b/leapseconddata/__main__.py @@ -0,0 +1,48 @@ +# SPDX-FileCopyrightText: 2022 Jeff Epler +# +# SPDX-License-Identifier: GPL-3.0-only + +""" +Smoke test program for leapseconddata + +If this was a useful program, it would be exposed as an entry point in setup.cfg. +""" + +import datetime +import logging +from . import LeapSecondData + + +def main() -> None: + """When run as a main program, print some information about leap seconds""" + logging.getLogger().setLevel(logging.INFO) + lsd = LeapSecondData.from_standard_source() + print(f"Last updated: {lsd.last_updated:%Y-%m-%d}") + print(f"Valid until: {lsd.valid_until:%Y-%m-%d}") + for when, offset in lsd.leap_seconds[-10:]: + print(f"{when:%Y-%m-%d}: {offset.total_seconds()}") + when = datetime.datetime(2011, 1, 1, tzinfo=datetime.timezone.utc) + print(f"TAI-UTC on {when:%Y-%m-%d} was {lsd.tai_offset(when).total_seconds()}") + + when_tai = lsd.to_tai(when) + when_rt = lsd.tai_to_utc(when_tai) + print(f"{when:%Y-%m-%d %H:%M:%S} UTC -> {when_tai:%Y-%m-%d %H:%M:%S} TAI") + print(f"{when_tai:%Y-%m-%d %H:%M:%S} TAI -> {when_rt:%Y-%m-%d %H:%M:%S} UTC") + print(f"is leap second? {lsd.is_leap_second(when)}") + + u = datetime.datetime( + 1999, 1, 1, tzinfo=datetime.timezone.utc + ) - datetime.timedelta(seconds=2) + t = lsd.to_tai(u) + + print("replaying leapsecond at end of 1998") + for _ in range(5): + print( + f"{u:%Y-%m-%d %H:%M:%S} UTC {'LS' if lsd.is_leap_second(u) else ' '} = {t:%Y-%m-%d %H:%M:%S} TAI {'LS' if lsd.is_leap_second(t) else ' '}" + ) + t += datetime.timedelta(seconds=1) + u = lsd.tai_to_utc(t) + + +if __name__ == "__main__": # pragma no cover + main() diff --git a/leapseconddata/py.typed b/leapseconddata/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/setup.cfg b/setup.cfg index 8e8b4a6..1510fb4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,5 +22,10 @@ classifiers = Operating System :: OS Independent [options] +package_dir = + =. python_requires = >=3.7 -py_modules = leapseconddata +packages = leapseconddata + +[options.package_data] +leapseconddata = py.typed diff --git a/testleapseconddata.py b/testleapseconddata.py index 80f6f0d..3ba1301 100644 --- a/testleapseconddata.py +++ b/testleapseconddata.py @@ -9,6 +9,7 @@ import datetime import unittest import leapseconddata +import leapseconddata.__main__ db = leapseconddata.LeapSecondData.from_standard_source() @@ -17,7 +18,7 @@ GMT1 = datetime.timezone(datetime.timedelta(seconds=3600), "GMT1") class LeapSecondDataTest(unittest.TestCase): def test_main(self) -> None: # pylint: disable=no-self-use - leapseconddata.main() + leapseconddata.__main__.main() def test_corrupt(self) -> None: self.assertRaises(