-
Notifications
You must be signed in to change notification settings - Fork 7
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
Finding start of day in specific time zone #18
Comments
Here is a failing test case: #[test]
fn handles_replace_time_in_timezone() {
assert_eq!(
datetime!(2023-03-26 6:00 UTC)
.to_timezone(timezones::db::europe::STOCKHOLM)
.replace_time(time::Time::MIDNIGHT)
.to_timezone(timezones::db::UTC),
datetime!(2023-03-25 23:00 UTC)
);
} |
Managed to find a workaround, but I'm still not sure I understand if this should be needed or not 🤔 fn start_of_swedish_day(instant: OffsetDateTime) -> OffsetDateTime {
let zoned_date_time = instant.to_timezone(timezones::db::europe::STOCKHOLM);
let zoned_midnight = zoned_date_time.replace_time(time::Time::MIDNIGHT);
// Re-interpret zoned_midnight as being in the Stockholm time zone
let primitive_midnight = PrimitiveDateTime::new(zoned_midnight.date(), zoned_midnight.time());
let zoned_midnight = primitive_midnight.assume_timezone(timezones::db::europe::STOCKHOLM).unwrap_first();
zoned_midnight.to_timezone(timezones::db::UTC)
} |
The reason why it does not work is because When you call replace_time, you replace the time part without leaving any chance to the crate to re-calculate the timezone. In other words, once you call to_timezone you shouldn't modify the OffsetDateTime or you need to recalculate the timezone which is currently only doable through the example code you've shown using PrimitiveDateTime and I know this API may look a bit unsound but I couldn't find a better solution. That said, I can try to add a check in to_timezone to handle the case where the OffsetDateTime is not UTC and run through the proper get_offset_local function, however that would be a breaking change as currently EDIT: You can also do this if all you want is midnight represented in the timezone of your choice: |
Thank you for looking at my issue! Coming back to this today, I realise that this might actually be expected behaviour since it's called Here is how Do you think that I have understood this correctly? So maybe this is a feature request for a |
I like the idea of a |
I've just made a first version of #[test]
fn handles_replace_time_in_timezone() {
assert_eq!(
datetime!(2023-03-26 6:00 UTC)
.to_zoned_date_time(timezones::db::europe::STOCKHOLM)
.replace_time(time::Time::MIDNIGHT)
.to_offset_date_time().unwrap_first(),
datetime!(2023-03-25 23:00 UTC)
);
} Can you try the latest changes on master? Use a |
I started testing this out and realised that I needed some more functions from First up was Good explanation of whyThese rules make arithmetic with
Unfortunately, that doesn't work with the E.g. one would think that and but Similarly This could be fixed by having another Then there is the question about what to do with invalid dates, this applies both to e.g. what should and what should Finally, why did you make |
One could also re-assemble the Duration type through calculations.
What do you mean by invalid date? You don't call
Again you're not supposed to call new manually.
I don't have any such macro, at first glance I'd say if such a macro needs to be implemented, it would simply create a PrimitiveDateTime assuming the date/time you pass is already in the target timezone.
Because Perhaps you considered that |
Update: I've done a version 2 of BTW: If you need to add more methods to the new |
Hello,
I'm currently using this library to bin events into days in a specific time zone.
I have a list of events which occurred at a specific point in time. These event have no concept of time zone. We could represent them with ISO 8601 timestamps in coordinated universal time.
Now I want to know which Swedish day this event belongs to. In order to to this I first convert this timestamp to a zoned date time in the Stockholm time zone.
Now that I have a local date, I set the time to
00:00
, representing the Swedish midnight.Finally, I want to turn this back into a UTC timestamp.
This works great for the most time, but breaks when the date is transition over a daylight savings change.
Example:
Let's start with the timestamp
2023-03-26T06:00:00.000Z
, in Swedish time this is 8:00 AM.Note that even though the wall clock says 8:00 AM, we are actually only 7 hours into this day. This is because a minute after 1:59 AM is 3:00 AM on the Sunday of March 26 2023.
After running it thru the
start_of_swedish_day
we get back the timestamp2023-03-25T22:00:00.000Z
. This is incorrect as at this timestamp the wall clock in Sweden would say 23:00 PM on the 25th.I'm not sure if I have misunderstood how to use the APIs, or if this is a bug the implementation of
replace_time
, or something else. Any help would be appreciated!The text was updated successfully, but these errors were encountered: