From 71bee5c41b3587d706145d3583598e8b500668d5 Mon Sep 17 00:00:00 2001 From: Juan Romero Cortes Date: Fri, 1 Sep 2023 14:36:04 +1000 Subject: [PATCH 1/2] Add optional tz argument to make_aware function --- tests/test_localtime.py | 17 +++++++++++++++++ xocto/localtime.py | 12 ++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/test_localtime.py b/tests/test_localtime.py index 877e27b..9f4bb1c 100644 --- a/tests/test_localtime.py +++ b/tests/test_localtime.py @@ -41,6 +41,23 @@ def test_seconds_in_past(self): ) +class TestMakeAwareAssumingLocal: + def test_assume_default_timezone_when_none_provided(self): + dt = datetime.datetime(2022, 1, 1, 0) + + timezone_aware_dt = localtime.make_aware_assuming_local(dt) + + assert timezone_aware_dt.tzinfo == timezone.get_current_timezone() + + def test_make_aware_with_provided_timezone(self): + dt = datetime.datetime(2022, 1, 1, 0) + tz = zoneinfo.ZoneInfo("Etc/GMT-10") + + timezone_aware_dt = localtime.make_aware_assuming_local(dt, tz=tz) + + assert timezone_aware_dt.tzinfo == tz + + class TestDate: def test_date_calculation_near_midnight_during_bst(self): near_midnight_in_utc = datetime.datetime(2016, 6, 1, 23, 50, 0, tzinfo=localtime.UTC) diff --git a/xocto/localtime.py b/xocto/localtime.py index f69d58e..0917b1b 100644 --- a/xocto/localtime.py +++ b/xocto/localtime.py @@ -349,13 +349,17 @@ def as_range( return midnight(_date, tz), latest(_date, tz) -def make_aware_assuming_local(dt: datetime_.datetime) -> datetime_.datetime: +def make_aware_assuming_local( + dt: datetime_.datetime, tz: Optional[datetime_.tzinfo] = None +) -> datetime_.datetime: """ Just a wrapper for Django's method, which will takes a naive datetime, and makes it timezone - aware, assuming the current timezone if none is passed (which it isn't from this wrapper - function). It will also raise an exception if the passed datetime is already timezone-aware. + aware, assuming the current timezone if none is passed. + It will also raise an exception if the passed datetime is already timezone-aware. """ - return timezone.make_aware(dt) + if tz is None: + tz = timezone.get_current_timezone() + return timezone.make_aware(dt, timezone=tz) def make_aware_assuming_utc(dt: datetime_.datetime) -> datetime_.datetime: From 9ef63c3a2cb686647ecd891b5310e31f90b9c704 Mon Sep 17 00:00:00 2001 From: Juan Romero Cortes Date: Fri, 1 Sep 2023 14:41:10 +1000 Subject: [PATCH 2/2] Add optional tz argument to localtime.datetime --- tests/test_localtime.py | 7 +++++++ xocto/localtime.py | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/test_localtime.py b/tests/test_localtime.py index 9f4bb1c..70153b3 100644 --- a/tests/test_localtime.py +++ b/tests/test_localtime.py @@ -316,6 +316,13 @@ def test_dst_ambiguity(self): utc_dt = dt.astimezone(localtime.UTC) assert utc_dt.hour == 0 + def test_datetime_with_specific_timezone(self): + aus_time = zoneinfo.ZoneInfo("Etc/GMT-10") + dt = localtime.datetime(2016, 8, 5, tz=aus_time) + assert dt.hour == 0 + utc_dt = dt.astimezone(localtime.UTC) + assert utc_dt.hour == 14 + class TestAsLocaltime: def test_conversion_of_gmt_dt(self): diff --git a/xocto/localtime.py b/xocto/localtime.py index 0917b1b..c133cb6 100644 --- a/xocto/localtime.py +++ b/xocto/localtime.py @@ -60,12 +60,15 @@ def datetime( minute: int = 0, second: int = 0, microsecond: int = 0, + tz: timezone.zoneinfo.ZoneInfo | None = None, ) -> datetime_.datetime: """ - Return a datetime in the local timezone. + Return a datetime in the local timezone, or in the desired timezone if tz is provided. """ + if tz is None: + tz = timezone.get_current_timezone() dt = datetime_.datetime(year, month, day, hour, minute, second, microsecond) - return timezone.make_aware(dt) + return timezone.make_aware(dt, timezone=tz) # Returning dates