DateTime Comparison

In this example, China Standard Time (CST, time zone ID: Asia/Shanghai) is compared with American Eastern Daylight Time (EDT, time zone ID: America/New_York).

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 tzSH = TimeZone.load("Asia/Shanghai")
    let tzNY = TimeZone.load("America/New_York")
    // 2024-05-25T00:00:00Z
    let shanghai1 = DateTime.of(year: 2024, month: May, dayOfMonth: 25, hour: 8, timeZone: tzSH)
    let new_york1 = DateTime.of(year: 2024, month: May, dayOfMonth: 24, hour: 20, timeZone: tzNY)

    // 2024-05-25T01:00:00Z
    let shanghai2 = DateTime.of(year: 2024, month: May, dayOfMonth: 25, hour: 9, timeZone: tzSH)
    let new_york2 = DateTime.of(year: 2024, month: May, dayOfMonth: 24, hour: 21, timeZone: tzNY)

    println(shanghai1 == new_york1)
    println(shanghai1 != new_york2)
    println(shanghai1 <= new_york2)
    println(shanghai1 < new_york2)
    println(shanghai2 >= new_york1)
    println(shanghai2 > new_york1)
}

Running result:

true
true
true
true
true
true