lib: nthdayof(month|year)containing now validate input a bit (#852)

This commit is contained in:
Simon Michael 2018-07-30 11:05:38 +01:00
parent 0f3a030a40
commit daa37a9993

View File

@ -526,8 +526,10 @@ prevyear = startofyear . addGregorianYearsClip (-1)
nextyear = startofyear . addGregorianYearsClip 1 nextyear = startofyear . addGregorianYearsClip 1
startofyear day = fromGregorian y 1 1 where (y,_,_) = toGregorian day startofyear day = fromGregorian y 1 1 where (y,_,_) = toGregorian day
-- | For given date d find year-long interval that starts on given MM/DD of year -- | For given date d find year-long interval that starts on given
-- and covers it. -- MM/DD of year and covers it.
-- The given MM and DD should be basically valid (1-12 & 1-31),
-- or an error is raised.
-- --
-- Examples: lets take 2017-11-22. Year-long intervals covering it that -- Examples: lets take 2017-11-22. Year-long intervals covering it that
-- starts before Nov 22 will start in 2017. However -- starts before Nov 22 will start in 2017. However
@ -546,14 +548,18 @@ startofyear day = fromGregorian y 1 1 where (y,_,_) = toGregorian day
-- >>> nthdayofyearcontaining 1 1 wed22nd -- >>> nthdayofyearcontaining 1 1 wed22nd
-- 2017-01-01 -- 2017-01-01
nthdayofyearcontaining :: Month -> MonthDay -> Day -> Day nthdayofyearcontaining :: Month -> MonthDay -> Day -> Day
nthdayofyearcontaining m n d | mmddOfSameYear <= d = mmddOfSameYear nthdayofyearcontaining m md date
| not (validMonth $ show m) = error' $ "nthdayofyearcontaining: invalid month "++show m
| not (validDay $ show md) = error' $ "nthdayofyearcontaining: invalid day " ++show md
| mmddOfSameYear <= date = mmddOfSameYear
| otherwise = mmddOfPrevYear | otherwise = mmddOfPrevYear
where mmddOfSameYear = addDays (fromIntegral n-1) $ applyN (m-1) nextmonth s where mmddOfSameYear = addDays (fromIntegral md-1) $ applyN (m-1) nextmonth s
mmddOfPrevYear = addDays (fromIntegral n-1) $ applyN (m-1) nextmonth $ prevyear s mmddOfPrevYear = addDays (fromIntegral md-1) $ applyN (m-1) nextmonth $ prevyear s
s = startofyear d s = startofyear date
-- | For given date d find month-long interval that starts on nth day of month -- | For given date d find month-long interval that starts on nth day of month
-- and covers it. -- and covers it.
-- The given day of month should be basically valid (1-31), or an error is raised.
-- --
-- Examples: lets take 2017-11-22. Month-long intervals covering it that -- Examples: lets take 2017-11-22. Month-long intervals covering it that
-- start on 1st-22nd of month will start in Nov. However -- start on 1st-22nd of month will start in Nov. However
@ -570,11 +576,13 @@ nthdayofyearcontaining m n d | mmddOfSameYear <= d = mmddOfSameYear
-- >>> nthdayofmonthcontaining 30 wed22nd -- >>> nthdayofmonthcontaining 30 wed22nd
-- 2017-10-30 -- 2017-10-30
nthdayofmonthcontaining :: MonthDay -> Day -> Day nthdayofmonthcontaining :: MonthDay -> Day -> Day
nthdayofmonthcontaining n d | nthOfSameMonth <= d = nthOfSameMonth nthdayofmonthcontaining md date
| not (validDay $ show md) = error' $ "nthdayofmonthcontaining: invalid day " ++show md
| nthOfSameMonth <= date = nthOfSameMonth
| otherwise = nthOfPrevMonth | otherwise = nthOfPrevMonth
where nthOfSameMonth = nthdayofmonth n s where nthOfSameMonth = nthdayofmonth md s
nthOfPrevMonth = nthdayofmonth n $ prevmonth s nthOfPrevMonth = nthdayofmonth md $ prevmonth s
s = startofmonth d s = startofmonth date
-- | For given date d find week-long interval that starts on nth day of week -- | For given date d find week-long interval that starts on nth day of week
-- and covers it. -- and covers it.