我想确定某个特定的Day
(包time
)是夏季时间还是冬季时间。
我对中欧特别感兴趣,在欧洲中部,夏令时为CEST = GMT + 2,冬季为CEST = GMT + 1。
我需要它,因为我需要将JulianDate
(包astro
)转换为本地时间并返回。
最佳答案
因为时区更改的确切日期在每个地方都不同,所以我认为您必须编写自己的函数进行检查。
我碰巧知道在欧洲,夏季时间从3月的最后一个星期日开始,到10月的最后一个星期日结束。我在下面的函数中使用了它,很容易更改其他位置。
import Data.Time.Calendar
import Data.Time.Calendar.WeekDate
import Data.Tuple.Select
-- | Uses only days, it does not take the exact hour into account at which summer time starts and ends.
isSummerTime :: Day -- ^ Date to check if summer time is active
-> Bool -- ^ Whether summer time is active
isSummerTime date = date > lastSundayMarch && date < lastSundayOctober
where
year = sel1 $ toGregorian date
-- Find last Sunday in March
aprilOne = fromGregorian year 4 1
-- 1 is Monday, ..., 7 is Sunday
aprilOneWeekDay = sel3 $ toWeekDate aprilOne
-- Use the day number to find Sunday of the previous week: the last Sunday in March
lastSundayMarch = addDays (-(toInteger aprilOneWeekDay)) aprilOne
-- Same for end of summer time in October
novemberOne = fromGregorian year 11 1
novemberOneWeekDay = sel3 $ toWeekDate novemberOne
lastSundayOctober = addDays (-(toInteger novemberOneWeekDay)) novemberOne
isSummerTime $ fromGregorian 2018 3 25 -- False
isSummerTime $ fromGregorian 2018 3 26 -- True
isSummerTime $ fromGregorian 2018 10 27 -- True
isSummerTime $ fromGregorian 2018 10 28 -- False