massivefermion / birl

datetime handling for gleam
https://hex.pm/packages/birl
Apache License 2.0
66 stars 9 forks source link

Comparisons between different timezones #26

Open ghivert opened 4 months ago

ghivert commented 4 months ago

Hi !

Following a discussion we had on gleam-lang/hexpm on timestamps, it appears that when comparing two datetime on different timezones, the behaviour is to just compare the millis. But even if on the same timestamp, they don't have the same time because there's an offset.

Is it the desired behaviour?

massivefermion commented 4 months ago

Hi Yes, this is what I had in mind because this way of comparison is what actually means for a time value to be before or after another. Because the same time in two different timezones, is still the same time just represented in two different social contexts. But of course I can see the utility of the kind of comparison you have in mind. I'll try to add it when I can. I'm just not sure what to call it! For now, you can get what you want by using the get_time_of_day function on time values with different offsets and comparing the fields. In cases where the difference in offsets may be large enough where the two time values represent different days, you'd want to also use the get_day function and compare the month and date fields on those too.

ghivert commented 4 months ago

Thanks for your answer. 🙂 Isn't there a way to just compare the unix timestamp? Birl could have something like elm Posix, that could be only the Posix timestamp, and it would solve everything? It could be the boxed timestamp, provide simple functions, and some convert functions from/to Posix to DateTime?

massivefermion commented 4 months ago

The compare function does that. The only difference is, the timestamp in a Time value is in microseconds instead of seconds! But that wouldn't be the comparison you described! There is just the caveat that if both time values have monotonic time, that would be the one used in the comparison. The monotonic time, though, only exists when the Time value is created using one of the now functions.