Local Time of a Time in Different Time Zones

This example demonstrates how to convert a CST time into a UTC time and an EDT time.

Notes:

In this example, the TimeZone.load function is used to load the time zone information. Different dependencies exist in loading time zone information on different platforms. Setting needs to be performed as required.

import std.time.*

main() {
    let datetime = DateTime.of(year: 2024, month: May, dayOfMonth: 22, hour: 12, timeZone: TimeZone.load("Asia/Shanghai"))

    println("CST: ${datetime}")
    println("UTC: ${datetime.inUTC()}")
    println("EDT: ${datetime.inTimeZone(TimeZone.load("America/New_York"))}")
}

Running result:

CST: 2024-05-22T12:00:00+08:00
UTC: 2024-05-22T04:00:00Z
EDT: 2024-05-22T00:00:00-04:00