Skip to content

TimeZone..of("UTC") isn't equal to TimeZone.UTC #474

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

Open
russhwolf opened this issue Jan 12, 2025 · 4 comments · May be fixed by #521
Open

TimeZone..of("UTC") isn't equal to TimeZone.UTC #474

russhwolf opened this issue Jan 12, 2025 · 4 comments · May be fixed by #521
Labels
breaking change This could break existing code
Milestone

Comments

@russhwolf
Copy link

It's very unintuitive that this assert fails.

assertEquals(TimeZone.UTC, TimeZone.of("UTC"))

I've re-discovered this fact multiple times when writing tests around datetime logic with time zones.

Presumably this is the same root cause as #222

@dkhalanskyjb
Copy link
Collaborator

This is a complex topic. The logic today is simple: if two identifiers are different strings, they are different time zones, even if their rules are the same. If we break this logic, it's unclear where to put the boundary and why.

I agree that what you're showing is unintuitive. Yet where does the unintuitiveness stop? Is it intuitive that TimeZone.UTC is also not equal to TimeZone.of("Etc/UTC")? Is it intuitive that TimeZone.of("UTC") is not equal to TimeZone.of("Etc/UTC")? If the answer to both is no, they should be equal, then should TimeZone.of("GMT"), TimeZone.of("GMT+0"), TimeZone.of("GMT-0"), TimeZone.of("GMT0"), TimeZone.of("Greenwich"), TimeZone.of("UCT"), TimeZone.of("UTC"), and the corresponding Etc/ versions also all be equal?

In the timezone database, there is a notion of timezone aliases (different names for the same thing), but the problem is, some timezones that are aliases today may well stop being equal after a political change, so even this is not correct in general: usually, we need to faithfully preserve the string passed to of. Special-casing some time zones like the plethora of names for UTC to work around the inherent complexity of the timezone databases also doesn't feel right.

@russhwolf
Copy link
Author

I hear you on the complexity of this topic in general, but the specific example seems fixable. Why is the identifier of TimeZone.UTC not "UTC"? That seems like it would be a simple fix for this issue without wading into other problems around zone equality. Or if there are other reasons to keep the identifier as "Z", why not rename the constant to TimeZone.Z?

@dkhalanskyjb
Copy link
Collaborator

dkhalanskyjb commented Feb 19, 2025

Oh, ok, I misunderstood your question, then. So, it's okay that TimeZone.of("UTC") doesn't equal TimeZone.of("Z"), the only problem is that TimeZone.UTC.id is not "UTC", right?

@russhwolf
Copy link
Author

Yes, that's correct.

@dkhalanskyjb dkhalanskyjb added the breaking change This could break existing code label Apr 9, 2025
@dkhalanskyjb dkhalanskyjb added this to the 0.9.0 milestone Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change This could break existing code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants