Skip to content

Conversation

@maganaluis
Copy link

Description

This PR adds support to sort FixedTimeZones by adding a comparison method isless.

@maganaluis maganaluis marked this pull request as ready for review June 17, 2025 17:23
Comment on lines +102 to +106
Defines a true chronological ordering for FixedTimeZone objects, answering
the question: "which time zone represents an earlier moment in absolute time?"
The core principle is that a time zone is "chronologically earlier" (less than)
if its numerical UTC offset is "mathematically greater".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordering of a FixedTimeZone vs. a FixedTimeZone could definitely work either way and I don't think it should be relied upon for performing chronological sorting of time instants.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this definition the comparison would define "UTC-5" as less than "UTC-4" since it is numerically smaller.

julia> FixedTimeZone("UTC-5").offset.std
-18000 seconds

julia> FixedTimeZone("UTC-4").offset.std
-14400 seconds

Copy link
Member

@omus omus Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree there should be an ordering but I'm not sure on what the ordering should be. I would expect most users would expect the most negative offset to be first in the list. However, that is chronologically backwards:

julia> using TimeZones

julia> tzs = [tz"UTC-4", tz"UTC+1", tz"UTC-5"]
3-element Vector{FixedTimeZone}:
 UTC-04:00
 UTC+01:00
 UTC-05:00

julia> sort!(ZonedDateTime.(2025,6,17,tzs))
3-element Vector{ZonedDateTime}:
 2025-06-17T00:00:00+01:00
 2025-06-17T00:00:00-04:00
 2025-06-17T00:00:00-05:00

julia> Base.isless(a::FixedTimeZone, b::FixedTimeZone) = isless(a.offset, b.offset)

julia> sort!(tzs)
3-element Vector{FixedTimeZone}:
 UTC-05:00
 UTC-04:00
 UTC+01:00

julia> Base.isless(a::FixedTimeZone, b::FixedTimeZone) = isless(b.offset, a.offset)

julia> sort!(tzs)
3-element Vector{FixedTimeZone}:
 UTC+01:00
 UTC-04:00
 UTC-05:00

Maybe the compromise is to define a is_chrono_less(::FixedTimeZone, ::FixedTimeZone)? That way downstream dependents can have a guaranteed ordering

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree this can break downstream, I don't think this PR is needed anymore, I'll do the comparison on the offset isless(y.offset, x.offset)

add suggestions

Co-authored-by: Curtis Vogt <[email protected]>
@maganaluis
Copy link
Author

Closing as this PR is not longer needed.

@maganaluis maganaluis closed this Jun 17, 2025
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

Successfully merging this pull request may close these issues.

2 participants