I've seen this question asked before but I don't t...
# kotlinx-datetime
r
I've seen this question asked before but I don't think I've seen an official answer. Is it intended that
TimeZone.UTC
is not equivalent to
TimeZone.of("UTC")
? That's incredibly unintuitive.
k
r
That issue is confusing to me as written. I don't think the root issue is the serialization. I think the issue is that the object returned by
TimeZone.of("UTC")
is not equal to
TimeZone.UTC
.
The root cause is presumably the same, but I don't think that ticket communicates well why it's so confusing
k
Drop a comment that adds to this, then? Someone else mentions later down in the thread that
.of("UTC")
and
.UTC
are not equal.
But, for what it's worth, I agree. It's incredibly confusing and surprising behavior.
And I still don't see the use case for
FixedOffsetTimeZone
that UtcOffset doesn't cover 🙃
r
I logged https://github.com/Kotlin/kotlinx-datetime/issues/474 and left a comment that they're related
💯 2
d
Looking at the source, it appears that
.of("Z")
will be the same as
.UTC
, but GMT/UTC/UT are handled differently:
Copy code
if (zoneId == "UTC" || zoneId == "GMT" || zoneId == "UT") {
                    return FixedOffsetTimeZone(UtcOffset.ZERO, zoneId)
                }
Also looking at the source, it looks like the equals operator doesn't take into account the
Z
vs
UTC
being equivalent:
Copy code
actual override fun equals(other: Any?): Boolean =
        this === other || other is TimeZone && this.id == other.id

    override fun hashCode(): Int = id.hashCode()
Further investigation, it looks like the UtcOffset class on the JVM has this:
Copy code
public actual val ZERO: UtcOffset = UtcOffset(ZoneOffset.UTC)
and
Copy code
actual override fun toString(): String = zoneOffset.toString()
ZoneOffset is a Java class, and its
toString
will return
Z
for the zero offset, or will return the
[-|+]hh:mm:ss
(optional seconds) based on the offset.
k
I commented on one of the issues I’ve filed and figured it might be good to drop here. There’s currently three Github issues which touch on this confusion. https://github.com/Kotlin/kotlinx-datetime/issues/222#issuecomment-2619294021