-
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
Changes from all commits
498bf5c
53f0224
8cd4ab7
f7d5e45
c4d4d3a
130a0ec
8f4848d
318d5c3
90c0864
cadd19f
a35ae66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2019-2021 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.datetime | ||
|
||
import kotlinx.datetime.serializers.UtcOffsetSerializer | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable(with = UtcOffsetSerializer::class) | ||
public expect class UtcOffset { | ||
public val totalSeconds: Int | ||
|
||
public companion object { | ||
public val ZERO: UtcOffset | ||
public fun parse(offsetString: String): UtcOffset | ||
} | ||
} | ||
public expect fun UtcOffset(hours: Int? = null, minutes: Int? = null, seconds: Int? = null): UtcOffset | ||
|
||
@Deprecated("Use UtcOffset.ZERO instead", ReplaceWith("UtcOffset.ZERO"), DeprecationLevel.ERROR) | ||
public fun UtcOffset(): UtcOffset = UtcOffset.ZERO | ||
|
||
public fun UtcOffset.asTimeZone(): FixedOffsetTimeZone = FixedOffsetTimeZone(this) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,9 @@ | |
|
||
package kotlinx.datetime.serializers | ||
|
||
import kotlinx.datetime.FixedOffsetTimeZone | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.ZoneOffset | ||
import kotlinx.datetime.UtcOffset | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
|
@@ -23,21 +24,35 @@ public object TimeZoneSerializer: KSerializer<TimeZone> { | |
|
||
} | ||
|
||
public object ZoneOffsetSerializer: KSerializer<ZoneOffset> { | ||
public object FixedOffsetTimeZoneSerializer: KSerializer<FixedOffsetTimeZone> { | ||
|
||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ZoneOffset", PrimitiveKind.STRING) | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("FixedOffsetTimeZone", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): ZoneOffset { | ||
override fun deserialize(decoder: Decoder): FixedOffsetTimeZone { | ||
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 commentThe 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. |
||
} | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: ZoneOffset) { | ||
override fun serialize(encoder: Encoder, value: FixedOffsetTimeZone) { | ||
encoder.encodeString(value.id) | ||
} | ||
|
||
} | ||
|
||
public object UtcOffsetSerializer: KSerializer<UtcOffset> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("UtcOffset", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): UtcOffset { | ||
return UtcOffset.parse(decoder.decodeString()) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: UtcOffset) { | ||
encoder.encodeString(value.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.
This needs to be documented. The other new functions as well, but the rest of them are much more self-explanatory.