haskell - 如何判断现在是夏季还是冬季?

标签 haskell

我想知道是否在某个Day上(package time) 现在是夏令时或冬令时。

我对中欧特别感兴趣,那里夏季时区为 CEST = GMT+2,冬季时区为 CEST = GMT+1。

我需要它,因为我需要转换 JulianDate (包astro)到本地时间并返回。

最佳答案

由于时区更改的确切日期因地点而异,我认为您必须编写自己的函数来检查这一点。

我碰巧知道欧洲夏令时从三月的最后一个星期日开始,到十月的最后一个星期日结束。我在下面的函数中使用了它,很容易更改为其他位置。

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

关于haskell - 如何判断现在是夏季还是冬季?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51791639/

相关文章:

haskell - 没有明确的实现警告

haskell - 如何在 Haskell 中将其更改为 while 循环?

haskell - Haskell 中的数据类型设计

haskell - 为 newtype 创建 MonadBaseControl 实例

c - hsc2hs:用 Haskell 改变 C 结构

haskell - 在haskell中处理IO值

haskell - 无法使用 -interactive-print 在 ghci 中的每行之后打印时间戳

haskell - 如何编写结合其他 nix-shell 环境的 shell.nix 文件?

list - Haskell List With Guards错误

haskell - 了解 foldr 和 foldl 函数