Skip to content

Commit

Permalink
Merge #13: add fold to constructor
Browse files Browse the repository at this point in the history
Approved-by: p-nilsson
Auto-deploy: false
  • Loading branch information
OpsBotPrime committed Feb 5, 2024
2 parents 023a7ff + 9f387c0 commit 7a451dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
1.3.0
-----

Released 2024-01-08.
Released 2024-02-05.

**Breaking changes**:

Expand All @@ -10,6 +10,7 @@ Released 2024-01-08.
Release highlights:

- Adds support for python 3.12.
- Adds `fold` to the constructor of `datetime_tz`

1.2.0
-----
Expand Down
7 changes: 6 additions & 1 deletion src/heliclockter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

tz_local = cast(ZoneInfo, _datetime.datetime.now().astimezone().tzinfo)

__version__ = '1.2.0'
__version__ = '1.3.0'


DateTimeTzT = TypeVar('DateTimeTzT', bound='datetime_tz')
Expand Down Expand Up @@ -77,6 +77,7 @@ def __init__(
microsecond: int = 0,
*,
tzinfo: _datetime.tzinfo,
fold: int = 0,
) -> None:
pass

Expand All @@ -92,6 +93,7 @@ def __init__( # pylint: disable=unused-argument
second: int = 0,
microsecond: int = 0,
tzinfo: _datetime.tzinfo = None,
fold: int = 0,
) -> None:
msg = f'{self.__class__} must have a timezone'
assert tzinfo is not None and self.tzinfo is not None, msg
Expand Down Expand Up @@ -167,6 +169,7 @@ def from_datetime(cls: Type[DateTimeTzT], dt: _datetime.datetime) -> DateTimeTzT
second=dt.second,
microsecond=dt.microsecond,
tzinfo=dt.tzinfo,
fold=dt.fold,
).astimezone(tz=assumed_tz)

else:
Expand All @@ -182,6 +185,7 @@ def from_datetime(cls: Type[DateTimeTzT], dt: _datetime.datetime) -> DateTimeTzT
second=dt.second,
microsecond=dt.microsecond,
tzinfo=dt.tzinfo, # type: ignore[arg-type]
fold=dt.fold,
)

@classmethod
Expand Down Expand Up @@ -271,6 +275,7 @@ def __deepcopy__(self: DateTimeTzT, memodict: object) -> DateTimeTzT:
second=self.second,
microsecond=self.microsecond,
tzinfo=self.tzinfo, # type: ignore[arg-type]
fold=self.fold,
)


Expand Down
13 changes: 13 additions & 0 deletions tests/instantiation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,16 @@ def test_future_and_past_no_tz() -> None:

with pytest.raises(DatetimeTzError, match=error_msg):
datetime_tz.past(days=2)


@parameterized.expand([(0,), (1,)])
def test_fold_tz(fold: int) -> None:
dt = datetime_tz(2023, 10, 29, 2, 30, fold=fold, tzinfo=ZoneInfo("Europe/Berlin"))
iso = "2023-10-29T02:30:00+01:00" if fold == 1 else '2023-10-29T02:30:00+02:00'
assert dt.isoformat() == iso


@parameterized.expand([(0,), (1,)])
def test_fold_utc(fold: int) -> None:
dt = datetime_utc(2023, 10, 29, 2, 30, fold=fold, tzinfo=ZoneInfo("UTC"))
assert dt.isoformat() == "2023-10-29T02:30:00+00:00"

0 comments on commit 7a451dd

Please sign in to comment.