Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Copyright (c) 2015 Adrian "Boom" Nwk
- -- Extensively revised by Bart Massey
- -- Day-of-week computations
- import Text.Printf
- data Months = Jan | Feb | Mar | Apr | May | Jun
- | Jul | Aug | Sep | Oct | Nov | Dec
- deriving (Show, Eq, Enum, Bounded)
- data Days = Mon | Tue | Wed | Thu | Fri | Sat | Sun
- deriving (Show, Eq, Enum, Bounded)
- data Date = Date { dayOfWeek :: Days
- , dayNumber :: Int
- , month :: Months
- , year :: Int }
- deriving Eq
- instance Show Date where
- show d = printf "%s %d/%s/%d" (show (dayOfWeek d))
- (dayNumber d) (show (month d)) (year d)
- circSucc :: (Enum a, Bounded a, Eq a) => a -> a
- circSucc e
- | e == (maxBound `asTypeOf` e) = minBound `asTypeOf` e
- | otherwise = succ e
- dateSucc :: Date -> Date
- dateSucc (Date d dn m y) = Date (circSucc d) (succ dn) m y
- daysFromDate :: Date -> [Date]
- daysFromDate now =
- normalNow : daysFromDate (dateSucc normalNow)
- where
- normalNow = dateNormalize now
- dateToDate :: Date -> Date -> [Date]
- dateToDate now later =
- takeWhile (\d -> d /= later) $ daysFromDate now
- divides :: Integral a => a -> a -> Bool
- divides d n | n < 0 = divides d (-n)
- divides d n = n `mod` d == 0
- -- If the date is not normalized, it means that it is the end of the month
- -- If the date is normalized, it means that the date is still within the month
- dateNormalized :: Date -> Bool
- dateNormalized (Date _ dn m0 y0) =
- (monthsWith30 m0 && dn <= 30) ||
- (monthsWith31 m0 && dn <= 31) ||
- (m0 == Feb && dn <= febDays y0)
- where
- monthsWith31 m = m `elem` [Jan, Mar, May, Jul, Aug, Oct, Dec]
- monthsWith30 m = m `elem` [Apr, Jun, Sep, Nov]
- febDays y
- | 400 `divides` y = 29
- | 100 `divides` y = 28
- | 4 `divides` y = 29
- | otherwise = 28
- dateNormalize :: Date -> Date
- dateNormalize now@(Date d _ m y)
- | dateNormalized now =
- now -- Normal day
- | m == Dec =
- Date d 1 Jan (succ y) -- End of Year
- | otherwise =
- Date d 1 (succ m) y -- End of Month
- ------------------------------------- New Edits
- main :: IO ()
- main = putStr $ unlines $ map show $
- [x | x <- days, dayOfWeek x `elem` [Mon, Wed, Fri]]
- where
- days = dateToDate (Date Mon 2 Aug 2010) (Date Sat 22 Aug 2015)
Advertisement
Add Comment
Please, Sign In to add comment