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 the same?
Test transition of DST, i.e. when you are currently in summer time, select a time value from winter. Test boundary cases, such as a timezone that is UTC+12, with DST, making the local time UTC+13 in summer and even places that are UTC+13 in winter.
In 2021, DST ended on Nov. 7 in the U.S., when most Americans set the clock back an hour, and the cycle will began again. Daylight saving time in the U.S. began on March 13, 2022, and it ends on Nov. 6, 2022, according to timeanddate.com.
Daylight saving time (DST) begins each year on the second Sunday in March at 2 a.m. (local time). Clocks must be moved ahead one hour when DST goes into effect so at 2 a.m. it becomes 3 a.m. The changeover back to standard time (ST) occurs on the first Sunday in November at 2 a.m. (local time).
As of 2022, DST is observed in most of Europe, most of North America and parts of Asia around the Northern Hemisphere summer, and in parts of South America and Oceania around the Southern Hemisphere summer.
In August 2021 go 1.17 was released which now adds the time.Time
method IsDST:
IsDST reports whether the time in the configured location is in Daylight Savings Time.
You can infer the result. For example,
package main
import (
"fmt"
"time"
)
// isTimeDST returns true if time t occurs within daylight saving time
// for its time zone.
func isTimeDST(t time.Time) bool {
// If the most recent (within the last year) clock change
// was forward then assume the change was from std to dst.
hh, mm, _ := t.UTC().Clock()
tClock := hh*60 + mm
for m := -1; m > -12; m-- {
// assume dst lasts for least one month
hh, mm, _ := t.AddDate(0, m, 0).UTC().Clock()
clock := hh*60 + mm
if clock != tClock {
if clock > tClock {
// std to dst
return true
}
// dst to std
return false
}
}
// assume no dst
return false
}
func main() {
pstLoc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
fmt.Println(err)
return
}
utc := time.Date(2018, 10, 29, 14, 0, 0, 0, time.UTC)
fmt.Println(utc, utc.Location(), ": DST", isTimeDST(utc))
local := utc.In(time.Local)
fmt.Println(local, local.Location(), ": DST", isTimeDST(local))
pst := utc.In(pstLoc)
fmt.Println(pst, pst.Location(), ": DST", isTimeDST(pst))
utc = utc.AddDate(0, 3, 0)
fmt.Println(utc, utc.Location(), ": DST", isTimeDST(utc))
local = utc.In(time.Local)
fmt.Println(local, local.Location(), ": DST", isTimeDST(local))
pst = utc.In(pstLoc)
fmt.Println(pst, pst.Location(), ": DST", isTimeDST(pst))
}
Output:
2018-10-29 14:00:00 +0000 UTC UTC : DST false
2018-10-29 10:00:00 -0400 EDT Local : DST true
2018-10-29 07:00:00 -0700 PDT America/Los_Angeles : DST true
2019-01-29 14:00:00 +0000 UTC UTC : DST false
2019-01-29 09:00:00 -0500 EST Local : DST false
2019-01-29 06:00:00 -0800 PST America/Los_Angeles : DST false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With