-
Notifications
You must be signed in to change notification settings - Fork 110
Split ZoneOffset into UtcOffset and FixedOffsetTimeZone #125
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
Conversation
encoder.encodeString(value.id) | ||
} | ||
|
||
} | ||
|
||
public object UtcOffsetSerializer: KSerializer<UtcOffset> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also provide an alternative serializer that stores offset as an integer primitive or a single component object.
dd35124
to
6e0f75c
Compare
@@ -28,12 +27,12 @@ public actual open class TimeZone internal constructor(internal val zoneId: Zone | |||
|
|||
public actual companion object { | |||
public actual fun currentSystemDefault(): TimeZone = ZoneId.systemDefault().let(::TimeZone) | |||
public actual val UTC: TimeZone = jtZoneOffset.UTC.let(::TimeZone) | |||
public actual val UTC: FixedOffsetTimeZone = UtcOffset(jtZoneOffset.UTC).asTimeZone() | |||
|
|||
public actual fun of(zoneId: String): TimeZone = try { | |||
val zone = ZoneId.of(zoneId) | |||
if (zone is jtZoneOffset) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't we want to do You said that this would be in the following PR. Keeping the message for the following note:zone.rules.isFixedOffset
here instead and normalize the fixed-offset time zones?
By the way, somewhat bad news: 310bp implements isFixedOffset
as follows:
public boolean isFixedOffset() {
return savingsInstantTransitions.length == 0;
}
So, I think this means that there is no way to discern the inherently fixed-offset time zones from those that are simply yet to have their first transition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any examples of such time zones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I know of.
|
||
override fun hashCode(): Int = zoneOffset.hashCode().toInt() | ||
override fun equals(other: Any?): Boolean = other is UtcOffset && this.zoneOffset == other.zoneOffset | ||
override fun toString(): String = zoneOffset.toString() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the Native implementation, these methods are simple and seemingly don't gain anything by being delegated to ZoneOffset
, so I don't see a reason to store that as opposed to just a plain number of seconds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep ZoneOffset instance for interop with js-joda library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
js-joda caches ZoneOffset
instances: https://github.com/js-joda/js-joda/blob/master/packages/core/src/ZoneOffset.js#L287, so I don't think constructing ZoneOffset
on the fly in the converter functions could become a bottleneck.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same goes for the Java implementation.
|
||
public actual fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime = try { | ||
java.time.LocalDateTime.ofInstant(this.value, timeZone.zoneId).let(::LocalDateTime) | ||
} catch (e: DateTimeException) { | ||
throw DateTimeArithmeticException(e) | ||
} | ||
|
||
internal actual fun Instant.toLocalDateTime(offset: UtcOffset): LocalDateTime = try { | ||
java.time.LocalDateTime.ofInstant(this.value, offset.zoneOffset).let(::LocalDateTime) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do decide not to store a ZoneOffset
inside a UtcOffset
, then LocalDateTime.ofEpochSecond
would probably fit better here.
val zone = TimeZone.of(decoder.decodeString()) | ||
if (zone is ZoneOffset) { | ||
if (zone is FixedOffsetTimeZone) { | ||
return zone | ||
} else { | ||
throw SerializationException("Timezone identifier '$zone' does not correspond to a fixed-offset timezone") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be a problem if a time zone is recognized as fixed-offset in one platform, but not as fixed-offset in other.
c01b720
to
88f8ca3
Compare
Extract some former native-only ZoneOffset tests into common UtcOffset tests.
On the contrary, two FixedOffsetTimeZones with the same offset can differ by id (this aspect is native-only for now).
Roundtripping zone names when converting FixedOffsetTimeZone to NSTimeZone and back is not guaranteed. Instead ensure that the returned time zone is a FixedOffsetTimeZone and has the same offset.
Override some methods in FixedOffsetTimeZone to provide a simpler implementation
public fun parse(offsetString: String): UtcOffset | ||
} | ||
} | ||
public expect fun UtcOffset(hours: Int? = null, minutes: Int? = null, seconds: Int? = null): UtcOffset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be documented. The other new functions as well, but the rest of them are much more self-explanatory.
This PR separates the concept of a time zone with a fixed UTC offset and from the concept of UTC offset itself.
Remaining task items:
make UtcOffset comparabledecide on the default orderingWork postponed for a further PR:
UTC+04
orEtc/UTC
, to be represented byFixedOffsetTimeZone
preserving itsid
: PR Normalize to FixedOffsetTimeZone in JVM and JS #130