Time Zone
Inheritors
A time zone, provides the conversion between Instant and LocalDateTime values using a collection of rules specifying which LocalDateTime value corresponds to each Instant.
A time zone can be used in Instant.toLocalDateTime and LocalDateTime.toInstant, and also in those arithmetic operations on Instant that require knowing the calendar.
A TimeZone can be constructed using the TimeZone.of function, which accepts the string identifier, like "Europe/Berlin"
, "America/Los_Angeles"
, etc. For a list of such identifiers, see TimeZone.availableZoneIds. Also, the constant TimeZone.UTC is provided for the UTC time zone.
For interaction with kotlinx-serialization
, TimeZoneSerializer is provided that serializes the time zone as its identifier.
On the JVM, there are TimeZone.toJavaZoneId()
and java.time.ZoneId.toKotlinTimeZone()
extension functions to convert between kotlinx.datetime
and java.time
objects used for the same purpose. Similarly, on the Darwin platforms, there are TimeZone.toNSTimeZone()
and NSTimeZone.toKotlinTimeZone()
extension functions.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Using a time zone to convert a local date-time to an instant and back
val zone = TimeZone.of("Europe/Berlin")
val localDateTime = LocalDate(2021, 3, 28).atTime(2, 16, 20)
val instant = localDateTime.toInstant(zone)
check(instant == Instant.parse("2021-03-28T01:16:20Z"))
val newLocalDateTime = instant.toLocalDateTime(zone)
check(newLocalDateTime == LocalDate(2021, 3, 28).atTime(3, 16, 20))
//sampleEnd
}
Inheritors
Inheritors
Inheritors
Types
Properties
Functions
Compares this time zone to the other one. Time zones are equal if their identifier is the same.
Finds the offset from UTC this time zone has at the specified instant of physical time.
Returns an instant that corresponds to this civil datetime value in the time zone provided as an implicit receiver.
Converts this kotlinx.datetime.TimeZone value to a java.time.ZoneId value.
Return the civil datetime value that this instant has in the time zone provided as an implicit receiver.
Converts the TimeZone to NSTimeZone.