Fixed Offset Time Zone
A time zone that is known to always have the same offset from UTC.
TimeZone.of will return an instance of this class if the time zone rules are fixed.
Time zones that are FixedOffsetTimeZone at some point in time can become non-fixed in the future due to changes in legislation or other reasons.
On the JVM, there are FixedOffsetTimeZone.toJavaZoneOffset()
and java.time.ZoneOffset.toKotlinFixedOffsetTimeZone()
extension functions to convert between kotlinx.datetime
and java.time
objects used for the same purpose. Note also the functions available for TimeZone in general.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Providing special behavior for fixed-offset time zones
val localDateTime = LocalDate(2023, 6, 2).atTime(12, 30)
for ((zoneId, expectedString) in listOf(
"UTC+01:30" to "2023-06-02T12:30+01:30",
"Europe/Berlin" to "2023-06-02T12:30+02:00[Europe/Berlin]",
)) {
val zone = TimeZone.of(zoneId)
// format the local date-time with either just the offset or the offset and the full time zone
val formatted = buildString {
append(localDateTime)
if (zone is FixedOffsetTimeZone) {
append(zone.offset)
} else {
append(localDateTime.toInstant(zone).offsetIn(zone))
append('[')
append(zone.id)
append(']')
}
}
check(formatted == expectedString)
}
//sampleEnd
}
Properties
Functions
Link copied to clipboard
Converts this kotlinx.datetime.FixedOffsetTimeZone value to a java.time.ZoneOffset value.