Skip to content

Commit

Permalink
Add localtime function to get local tzinfo
Browse files Browse the repository at this point in the history
I was surprised to find that we don't already have this function.
  • Loading branch information
Peter554 committed Jan 30, 2025
1 parent 8084980 commit 0d88cbd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Add `localtime.get_local_tzinfo` [#195](https://github.com/octoenergy/xocto/pull/195).
- [Breaking] Update `FiniteDatetimeRange.days` to require passing a timezone
[#195](https://github.com/octoenergy/xocto/pull/195).

Expand Down
10 changes: 8 additions & 2 deletions xocto/localtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
MIDNIGHT_TIME = datetime_.time(0, 0)


def get_local_tzinfo() -> datetime_.tzinfo:
"""
Return the local tzinfo.
"""
return timezone.get_current_timezone()


def as_localtime(
dt: datetime_.datetime, tz: datetime_.tzinfo | None = None
) -> datetime_.datetime:
Expand Down Expand Up @@ -383,8 +390,7 @@ def is_local_time(dt: datetime_.datetime) -> bool:
"""
Test whether a given (timezone-aware) datetime is in local time or not.
"""
current_timezone = timezone.get_current_timezone()
return dt.tzinfo == current_timezone
return dt.tzinfo == get_local_tzinfo()


def within_date_range(
Expand Down

0 comments on commit 0d88cbd

Please sign in to comment.