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

TZ and zoneinfo support #26

Open
AndreyNikiforov opened this issue Aug 24, 2024 · 1 comment
Open

TZ and zoneinfo support #26

AndreyNikiforov opened this issue Aug 24, 2024 · 1 comment

Comments

@AndreyNikiforov
Copy link

What is the correct way to convert datetime (utc) to [arbitrary] timezone? Looks like this lib supports neither zoneinfo module nor astimezone() method...

Example I would like to run in CircuitPython:

from datetime import timezone
LOS_ANGELES = ZoneInfo("America/Los_Angeles")
dt_utc = datetime(2020, 11, 1, 8, tzinfo=timezone.utc)

# Before the PDT -> PST transition
print(dt_utc.astimezone(LOS_ANGELES))

# After the PDT -> PST transition
print((dt_utc + timedelta(hours=1)).astimezone(LOS_ANGELES))
@FoamyGuy
Copy link
Contributor

To the best of my knowledge the timezone support is limited. There is code in a test here that shows how to convert from the default UTC time into an arbitrary timezone:

dt_utc = dt.replace(tzinfo=timezone(timedelta(hours=-4), "EDT"))

After doing so the tzinfo will be set on your object and if you call isoformat() it will output the timestamp with a timezone offset e.g. 2024-11-14T15:49:04-04:00

You could theoretically use timedelta with utcoffset to get the time in the specified timezone, but I do not believe we currently have any support for something more convenient like astimezone(). We would welcome a PR from anyone who wants to add more timezone support that matches cpython.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants