-- Copyright 2007 Ashish Hanwadikar
module DateStream where
import Data.Time.Calendar (gregorianMonthLength)
-- Basic idea taken from Ketil Malde (http://www.haskell.org/pipermail/libraries/2005-January/002929.html)
-- converted into Monad
data Year = Y Int
data MonthEnum = January | February | March | April | May | June | July | August
| September | October | November | December deriving (Show, Eq, Enum, Bounded, Ord)
data DayEnum = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday deriving (Show, Eq, Enum, Bounded)
data Month = M MonthEnum Year
data Day = Dm Int Month
instance Show Month where
show (M m t) = show m ++ " " ++ show t
instance Show Year where
show (Y y) = " " ++ show y
instance Show Day where
show (Dm d t) = " " ++ show d ++ " " ++ show t
dayofweek :: Day -> DayEnum
dayofweek (Dm d (M m (Y y))) =
let t = [Sunday, Wednesday, Tuesday, Friday, Sunday, Wednesday, Friday, Monday, Thursday, Saturday, Tuesday, Thursday]
y1 = (if m < March then (y - 1) else y)
dw = mod (y1 + (div y1 4) - (div y1 100) + (div y1 400) + (fromEnum(t !! (fromEnum ( m))) + d)) 7 in
toEnum dw
isDayOfWeek dw dt =(dayofweek dt) == dw
months (Y y) = [M m (Y y) | m <- [January .. December]]
monthLength y m = gregorianMonthLength (toInteger y) (fromEnum m+1)
days (M m yy@(Y y)) = [Dm d (M m yy) | d <- [1..monthLength y m]]
years = [Y y | y <- [2007..]]