wrote test case; found bugs in date math; fixed and simplified using Data.Time.Calendar
This commit is contained in:
parent
b815988d16
commit
9fff243ff2
1 changed files with 32 additions and 21 deletions
|
@ -1,6 +1,6 @@
|
||||||
{- scheduled activities
|
{- scheduled activities
|
||||||
-
|
-
|
||||||
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
- Copyright 2013-2014 Joey Hess <joey@kitenet.net>
|
||||||
-
|
-
|
||||||
- Licensed under the GNU GPL version 3 or higher.
|
- Licensed under the GNU GPL version 3 or higher.
|
||||||
-}
|
-}
|
||||||
|
@ -24,6 +24,7 @@ module Utility.Scheduled (
|
||||||
toSchedule,
|
toSchedule,
|
||||||
parseSchedule,
|
parseSchedule,
|
||||||
prop_schedule_roundtrips,
|
prop_schedule_roundtrips,
|
||||||
|
prop_past_sane,
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Utility.Data
|
import Utility.Data
|
||||||
|
@ -177,29 +178,15 @@ calcNextTime schedule@(Schedule recurrance scheduledtime) lasttime currenttime
|
||||||
|
|
||||||
-- Check if the new Day occurs one month or more past the old Day.
|
-- Check if the new Day occurs one month or more past the old Day.
|
||||||
oneMonthPast :: Day -> Day -> Bool
|
oneMonthPast :: Day -> Day -> Bool
|
||||||
new `oneMonthPast` old
|
new `oneMonthPast` old = fromGregorian y (m+1) d <= new
|
||||||
| mday new >= mday old && (new `newerMonth` old || new `newerYear` old) = True
|
where
|
||||||
| new `skippedAMonth` old || new `skippedAYear` old = True
|
(y,m,d) = toGregorian old
|
||||||
| otherwise = False
|
|
||||||
|
|
||||||
-- Check if the new Day occurs one year or more past the old Day.
|
-- Check if the new Day occurs one year or more past the old Day.
|
||||||
oneYearPast :: Day -> Day -> Bool
|
oneYearPast :: Day -> Day -> Bool
|
||||||
new `oneYearPast` old
|
new `oneYearPast` old = fromGregorian (y+1) m d <= new
|
||||||
| yday new >= yday old && new `newerYear` old = True
|
where
|
||||||
| new `skippedAYear` old = True
|
(y,m,d) = toGregorian old
|
||||||
| otherwise = False
|
|
||||||
|
|
||||||
newerMonth :: Day -> Day -> Bool
|
|
||||||
new `newerMonth` old = mnum new > mnum old
|
|
||||||
|
|
||||||
newerYear :: Day -> Day -> Bool
|
|
||||||
new `newerYear` old = ynum new > ynum old
|
|
||||||
|
|
||||||
skippedAMonth :: Day -> Day -> Bool
|
|
||||||
new `skippedAMonth` old = mnum new > mnum old + 1
|
|
||||||
|
|
||||||
skippedAYear :: Day -> Day -> Bool
|
|
||||||
new `skippedAYear` old = ynum new > ynum old + 1
|
|
||||||
|
|
||||||
endOfMonth :: Day -> Day
|
endOfMonth :: Day -> Day
|
||||||
endOfMonth day =
|
endOfMonth day =
|
||||||
|
@ -383,3 +370,27 @@ instance Arbitrary Recurrance where
|
||||||
|
|
||||||
prop_schedule_roundtrips :: Schedule -> Bool
|
prop_schedule_roundtrips :: Schedule -> Bool
|
||||||
prop_schedule_roundtrips s = toSchedule (fromSchedule s) == Just s
|
prop_schedule_roundtrips s = toSchedule (fromSchedule s) == Just s
|
||||||
|
|
||||||
|
prop_past_sane :: Bool
|
||||||
|
prop_past_sane = and
|
||||||
|
[ all (checksout oneMonthPast) (mplus1 ++ yplus1)
|
||||||
|
, all (not . (checksout oneMonthPast)) (map swap (mplus1 ++ yplus1))
|
||||||
|
, all (checksout oneYearPast) yplus1
|
||||||
|
, all (not . (checksout oneYearPast)) (map swap yplus1)
|
||||||
|
]
|
||||||
|
where
|
||||||
|
mplus1 = -- new date old date, 1+ months before it
|
||||||
|
[ (fromGregorian 2014 01 15, fromGregorian 2013 12 15)
|
||||||
|
, (fromGregorian 2014 01 15, fromGregorian 2013 02 15)
|
||||||
|
, (fromGregorian 2014 02 15, fromGregorian 2013 01 15)
|
||||||
|
, (fromGregorian 2014 03 01, fromGregorian 2013 01 15)
|
||||||
|
, (fromGregorian 2014 03 01, fromGregorian 2013 12 15)
|
||||||
|
, (fromGregorian 2015 01 01, fromGregorian 2010 01 01)
|
||||||
|
]
|
||||||
|
yplus1 = -- new date old date, 1+ years before it
|
||||||
|
[ (fromGregorian 2014 01 15, fromGregorian 2012 01 16)
|
||||||
|
, (fromGregorian 2014 01 15, fromGregorian 2013 01 14)
|
||||||
|
, (fromGregorian 2022 12 31, fromGregorian 2000 01 01)
|
||||||
|
]
|
||||||
|
checksout cmp (new, old) = new `cmp` old
|
||||||
|
swap (a,b) = (b,a)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue