How to check whether current local time is DST?

前端 未结 3 1482
孤独总比滥情好
孤独总比滥情好 2021-01-04 12:29

In Ruby, for example, there\'s the Time#dst? function, which returns true in the case it is daylight saving time. Is there a Go standard library API call to do

3条回答
  •  盖世英雄少女心
    2021-01-04 12:55

    In a pinch, this should do the trick:

    func inDST(t time.Time) bool {
    
        jan1st := time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location()) // January 1st is always outside DST window
    
        _, off1 := t.Zone()
        _, off2 := jan1st.Zone()
    
        return off1 != off2
    }
    

提交回复
热议问题