Search code examples
haskell

How to find out if it's summer or winter time?


I want to find out if on a certain Day (package time) it's summer time or winter time.

I am especially interested in central Europe, where in summer the timezone is CEST = GMT+2 and in winter CEST = GMT+1.

I need it because I need to convert JulianDate (package astro) to local time and back.


Solution

  • Because the exact dates on which the timezone changes differ per place, I think you have to write your own function which check this.

    I happen to know in Europe summer time starts on the last Sunday of March, and ends at the last Sunday of October. I used this in the function below, it's easy to change for other locations.

    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