代码之家  ›  专栏  ›  技术社区  ›  PHPirate

怎么知道是夏天还是冬天?

  •  3
  • PHPirate  · 技术社区  · 8 年前

    我想知道 Day (包装 time

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

    JulianDate (包装 astro

    1 回复  |  直到 8 年前
        1
  •  3
  •   PHPirate    8 年前

    因为每个地方时区更改的确切日期不同,我想您必须编写自己的函数来检查这个。

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

    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
    
    推荐文章