首页 > 解决方案 > 如何确定夏令时是否有效?

问题描述

我正在尝试为 time.Time 编写一个 isDST 函数,但是我不确定如何去做。问题是时间包中的Location类型没有导出区域信息(其中包含isDST bool)

见下文摘录。有没有办法得到这些信息?

// A Location maps time instants to the zone in use at that time.
// Typically, the Location represents the collection of time offsets
// in use in a geographical area. For many Locations the time offset varies
// depending on whether daylight savings time is in use at the time instant.
type Location struct {
    name string
    zone []zone
    tx   []zoneTrans

    // The tzdata information can be followed by a string that describes
    // how to handle DST transitions not recorded in zoneTrans.
    // The format is the TZ environment variable without a colon; see
    // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html.
    // Example string, for America/Los_Angeles: PST8PDT,M3.2.0,M11.1.0
    extend string

    // Most lookups will be for the current time.
    // To avoid the binary search through tx, keep a
    // static one-element cache that gives the correct
    // zone for the time when the Location was created.
    // if cacheStart <= t < cacheEnd,
    // lookup can return cacheZone.
    // The units for cacheStart and cacheEnd are seconds
    // since January 1, 1970 UTC, to match the argument
    // to lookup.
    cacheStart int64
    cacheEnd   int64
    cacheZone  *zone
}

// A zone represents a single time zone such as CET.
type zone struct {
    name   string // abbreviated name, "CET"
    offset int    // seconds east of UTC
    isDST  bool   // is this zone Daylight Savings Time?
}

标签: go

解决方案


推荐阅读