根据英国夏令时(BST)/夏令时(DST)(https://www.gov.uk/when-do-the-clocks-change)的规则,时钟:
在2019年,这个本地时间发生在3月31日和10月27日,但每年的日子略有变化。
类似的DST规则适用于中欧时间CET(冬季)> CEST(夏季),检查3月/ 10月的最后一个星期日(https://www.timeanddate.com/time/change/denmark/copenhagen)。这些BST / GMT和CET / CEST规则的组合会影响例如北海周围的所有国家。无论BST / GMT还是CET / CEST,下面的UTC时间戳都应该相同。
我基于time.UTC
编写了以下代码,提供了BST / GMT的日期,但是我想知道是否存在一种更简单/更通用的方法来使用适用于CET / CEST的任意time.Location
(理想情况下)任何DST规则。
time.FixedZone
应该处理不变的时区,但是那里的offset
是什么?如何使以下函数在时区不变?for
在每月的星期日发生循环?map
,以找出下一个时钟更改?代码:
func beginOfMonth(year, month int, loc *time.Location) time.Time {
return time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc)
}
// https://www.gov.uk/when-do-the-clocks-change
func lastUTCSunday(year, month int) time.Time {
beginOfMonth := beginOfMonth(year, month, time.UTC)
// we can find max 5 sundays in a month
sundays := make([]time.Time, 5)
for d := 1; d <= 31; d++ {
currDay := beginOfMonth.Add(time.Duration(24*d) * time.Hour)
if currDay.Weekday().String() == "Sunday" {
sundays = append(sundays, currDay)
}
if currDay.Month() != beginOfMonth.Month() {
break
}
}
// check if last date is same month
if sundays[len(sundays)-1].Month() == beginOfMonth.Month() {
// the 5th sunday
return sundays[len(sundays)-1]
}
// if not like before, then we only have 4 Sundays
return sundays[len(sundays)-2]
}
// https://www.gov.uk/when-do-the-clocks-change
func MarchClockSwitchTime(year int) time.Time {
lastSunday := lastUTCSunday(year, int(time.March)) // month: 3
return time.Date(
year, lastSunday.Month(), lastSunday.Day(),
1, 0, 0, 0, // 1:00 AM
lastSunday.Location(),
)
}
// https://www.gov.uk/when-do-the-clocks-change
func OctoberClockSwitchTime(year int) time.Time {
lastSunday := lastUTCSunday(year, int(time.October)) // month: 10
return time.Date(
year, lastSunday.Month(), lastSunday.Day(),
2, 0, 0, 0, // 2:00 AM
lastSunday.Location(),
)
}
[我还使用GoConvey编写了一些测试,这些测试应验证基于星期日的这些奇怪的夏时制(DST)规则,但它们仅适用于2019年,2020年。找到一种使此代码更通用的方法会很好。] >
func TestLastSunday(t *testing.T) {
Convey("Should find the last UTC Sunday of each month\n\n", t, func() {
for year := 2019; year <= 2020; year++ {
for month := 1; month <= 12; month++ {
lastUtcSunday := lastUTCSunday(year, month)
So(lastUtcSunday.Month(), ShouldEqual, time.Month(month))
So(lastUtcSunday.Weekday().String(), ShouldEqual, "Sunday")
So(lastUtcSunday.Year(), ShouldEqual, year)
So(lastUtcSunday.Day(), ShouldBeGreaterThanOrEqualTo, 28-7)
}
}
})
}
// https://www.gov.uk/when-do-the-clocks-change
func TestClockChange(t *testing.T) {
Convey("Should find the last UTC Sunday for the March switch\n\n", t, func() {
switch2019 := MarchClockSwitchTime(2019)
So(switch2019.Month(), ShouldEqual, time.March)
So(switch2019.Weekday().String(), ShouldEqual, "Sunday")
So(switch2019.Day(), ShouldEqual, 31)
So(switch2019.Location().String(), ShouldEqual, "UTC")
So(switch2019.Location().String(), ShouldEqual, time.UTC.String())
switch2020 := MarchClockSwitchTime(2020)
So(switch2020.Month(), ShouldEqual, time.March)
So(switch2020.Weekday().String(), ShouldEqual, "Sunday")
So(switch2020.Day(), ShouldEqual, 29)
So(switch2020.Location().String(), ShouldEqual, "UTC")
So(switch2020.Location().String(), ShouldEqual, time.UTC.String())
})
Convey("Should find the last UTC Sunday for the October switch\n\n", t, func() {
switch2019 := OctoberClockSwitchTime(2019)
So(switch2019.Month(), ShouldEqual, time.October)
So(switch2019.Weekday().String(), ShouldEqual, "Sunday")
So(switch2019.Day(), ShouldEqual, 27)
So(switch2019.Location().String(), ShouldEqual, "UTC")
So(switch2019.Location().String(), ShouldEqual, time.UTC.String())
switch2020 := OctoberClockSwitchTime(2020)
So(switch2020.Month(), ShouldEqual, time.October)
So(switch2020.Weekday().String(), ShouldEqual, "Sunday")
So(switch2020.Day(), ShouldEqual, 25)
So(switch2020.Location().String(), ShouldEqual, "UTC")
So(switch2020.Location().String(), ShouldEqual, time.UTC.String())
})
}
[根据英国夏令时(BST)/夏令时(DST)(https://www.gov.uk/when-do-the-clocks-change)的规则,时钟:凌晨1点前进1个小时在三月的最后一个星期日(...
Go已经通过IANA TZDB拥有完整的time.LoadLocation
支持。将time.LoadLocation
用于英国,将Europe/London
用于丹麦的CET / CEST,等等。请参见Europe/Copenhagen
。
您不应该自己重新实现时区逻辑。当汤姆·斯科特(Tom Scott)在the list here中恰当地打趣时-“那就是疯狂。”
根据您的情况,改为呼叫The Problem with Time & Timezones并解析响应可能很实际。