Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeapSecondInfo: use dataclass instead of NamedTuple #13

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions leapseconddata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@
import re
import urllib.request
from dataclasses import dataclass, field
from typing import BinaryIO, List, NamedTuple, Optional, Union
from typing import BinaryIO, List, Optional, Union

tai = datetime.timezone(datetime.timedelta(0), "TAI")

NTP_EPOCH = datetime.datetime(1900, 1, 1, tzinfo=datetime.timezone.utc)

LeapSecondInfo = NamedTuple(
"LeapSecondInfo", (("start", datetime.datetime), ("tai", datetime.timedelta))
)

LeapSecondInfo.start.__doc__ = """The UTC timestamp just after the insertion of the leap second.
@dataclass(frozen=True)
class LeapSecondInfo:
"""Information about a particular leap second"""

start: datetime.datetime
"""The UTC timestamp just after the insertion of the leap second.

The leap second is actually the 61th second of the previous minute (xx:xx:60)"""

The leap second is actually the 60th second of the previous minute"""
LeapSecondInfo.tai.__doc__ = (
tai_offset: datetime.timedelta
"""The new TAI-UTC offset. Positive numbers indicate that TAI is ahead of UTC"""
)


class ValidityError(ValueError):
Expand Down Expand Up @@ -130,13 +132,14 @@ def tai_offset(
return datetime.timedelta(0)

old_tai = datetime.timedelta()
for start, tai_offset in self.leap_seconds:
for leap_second in self.leap_seconds:
start = leap_second.start
if is_tai:
start += tai_offset - datetime.timedelta(seconds=1)
start += leap_second.tai_offset - datetime.timedelta(seconds=1)
if when < start:
return old_tai
old_tai = tai_offset
return self.leap_seconds[-1].tai
old_tai = leap_second.tai_offset
return self.leap_seconds[-1].tai_offset

def to_tai(
self, when: datetime.datetime, check_validity: bool = True
Expand Down
4 changes: 2 additions & 2 deletions leapseconddata/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def main() -> None:
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()}")
for leap_second in lsd.leap_seconds[-10:]:
print(f"{leap_second.start:%Y-%m-%d}: {leap_second.tai_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()}")

Expand Down