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

How to identify if a generic DateTime<Tz> is in a DST transition? #1242

Closed
davidhewitt opened this issue Sep 1, 2023 · 2 comments
Closed

Comments

@davidhewitt
Copy link

Looking into PyO3/pyo3#3266, I hit a stumbling point regarding DST transitions.

For NativeDateTime::and_local_timezone a LocalResult is returned to handle the case where the naive local datetime might be ambiguous or not present in the corresponding timezone.

What about going the other way? If I have DateTime<Tz> for some generic Tz: TimeZone, what's the recommended way to identify if this tz-aware datetime represents an ambiguous local timezone? I think I can do something like:

let is_dst_folded = match dt.naive_local().and_local_offset(dt.offset) {
    LocalResult::None => unreachable!(),
    LocalResult::Single(_) => false,
    LocalResult::Ambiguous(_, folded) => dt == folded
};

... but that seems quite a roundabout way of doing it, so I was wondering if you thought there was anything better?

@pitdicker
Copy link
Collaborator

pitdicker commented Sep 1, 2023

That is mostly it. However, LocalResult::Ambiguous is only returned if a transition creates a fold (clock is moved backward), and LocalResult::None when the transition crates a gap (clock is moved forward).

The shortest way to write this is using LocalResult::single():

let is_dst_folded = dt.naive_local().and_local_offset(dt.offset).single().is_none();

Note that we sometimes rely on platform functions to convert to DateTime<Local>. On Windows and wasm the platform function always returns a single value, so we can't return anything but LocalResult::Single there yet.

And also unfortunately an error in the platform function makes us return LocalResult::None, just like during a transition gap :-(. But if you use something like chrono_tz which is fully under our control, this method works.

@davidhewitt
Copy link
Author

Thanks for the confirmation. 👍

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