FixedOffsetTimeZone

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
}

Constructors

Link copied to clipboard
actual constructor(offset: UtcOffset)
expect constructor(offset: UtcOffset)

Constructs a time zone with the fixed offset from UTC.

actual constructor(offset: UtcOffset)
actual constructor(offset: UtcOffset)

Properties

Link copied to clipboard
open override val id: String
Link copied to clipboard
actual val offset: UtcOffset
expect val offset: UtcOffset

The constant offset from UTC that this time zone has.

actual val offset: UtcOffset
actual val offset: UtcOffset

Functions