bugfix: adding weeks ignores timezone differences

This commit is contained in:
SJost 2019-02-14 12:11:58 +01:00
parent d0ed667cf3
commit 5639ea0380

View File

@ -6,7 +6,7 @@ module Handler.Utils.DateTime
, validDateTimeFormats, dateTimeFormatOptions , validDateTimeFormats, dateTimeFormatOptions
, formatTimeMail , formatTimeMail
, addOneWeek, addWeeks , addOneWeek, addWeeks
, weekDiff, weeksToAdd , weeksToAdd
, setYear , setYear
) where ) where
@ -134,36 +134,29 @@ dateTimeFormatOptions sel = do
optionsPairs <=< mapM toOption . Set.toList $ validDateTimeFormats tl sel optionsPairs <=< mapM toOption . Set.toList $ validDateTimeFormats tl sel
setYear :: Integer -> Day -> Day
setYear year date = fromGregorian year month day
where
(_,month,day) = toGregorian date
addOneWeek :: UTCTime -> UTCTime addOneWeek :: UTCTime -> UTCTime
addOneWeek = addWeeks 1 addOneWeek = addWeeks 1
addWeeks :: Integer -> UTCTime -> UTCTime addWeeks :: Integer -> UTCTime -> UTCTime
addWeeks n utct = utct { utctDay = newDay } addWeeks n utct = localTimeToUTCTZ appTZ newLocal
where where
oldDay = utctDay utct oldLocal = utcToLocalTime utct
-- newDay = addGregorianDurationRollOver $ stimes n calendarWeek -- only available in newer version 1.9 of Data.Time.Calendar oldDay = localDay oldLocal
newDay = addDays (7*n) oldDay newDay = addDays (7*n) oldDay
newLocal = oldLocal { localDay = newDay }
weekDiff :: UTCTime -> UTCTime -> Integer
-- ^ Difference between times, rounded down to weeks
weekDiff old new = dayDiff `div` 7
where
dayOld = utctDay old
dayNew = utctDay new
dayDiff = diffDays dayNew dayOld
weeksToAdd :: UTCTime -> UTCTime -> Integer weeksToAdd :: UTCTime -> UTCTime -> Integer
-- ^ Number of weeks needed to add so that first -- ^ Number of weeks needed to add so that first
-- time occurs later than second time -- time occurs later than second time
-- (loop avoids off-by-one error with weekDiff corner cases) -- (loop avoids off-by-one error with corner cases)
weeksToAdd old new = loop 0 old weeksToAdd old new = loop 0 old
where where
loop n t loop n t
| t > new = n | t > new = n
| otherwise = loop (succ n) (addOneWeek t) | otherwise = loop (succ n) (addOneWeek t)
setYear :: Integer -> Day -> Day
setYear year date = fromGregorian year month day
where
(_,month,day) = toGregorian date