0 | module Data.Time.Date
3 | import Data.Refined.Integer
4 | import Derive.Prelude
5 | import Derive.Refined
8 | %language ElabReflection
18 | {auto 0 valid : FromTo 0 9999 year}
20 | %runElab derive "Year" [Show, Eq, Ord, RefinedInteger]
23 | Interpolation Year where
24 | interpolate (Y y) = padLeft 4 '0' $
show y
31 | data Month : Type where
45 | %runElab derive "Month" [Show, Eq, Ord]
48 | Interpolation Month where
49 | interpolate m = padLeft 2 '0' $
show (conIndexMonth m + 1)
52 | DaysInMonth : Month -> Integer
53 | DaysInMonth JAN = 31
54 | DaysInMonth FEB = 29
55 | DaysInMonth MAR = 31
56 | DaysInMonth APR = 30
57 | DaysInMonth MAY = 31
58 | DaysInMonth JUN = 30
59 | DaysInMonth JUL = 31
60 | DaysInMonth AUG = 31
61 | DaysInMonth SEP = 30
62 | DaysInMonth OCT = 31
63 | DaysInMonth NOV = 30
64 | DaysInMonth DEC = 31
67 | intToMonth : Integer -> Maybe Month
68 | intToMonth 1 = Just JAN
69 | intToMonth 2 = Just FEB
70 | intToMonth 3 = Just MAR
71 | intToMonth 4 = Just APR
72 | intToMonth 5 = Just MAY
73 | intToMonth 6 = Just JUN
74 | intToMonth 7 = Just JUL
75 | intToMonth 8 = Just AUG
76 | intToMonth 9 = Just SEP
77 | intToMonth 10 = Just OCT
78 | intToMonth 11 = Just NOV
79 | intToMonth 12 = Just DEC
80 | intToMonth _ = Nothing
87 | record Day (m : Month) where
90 | {auto 0 valid : FromTo 1 (DaysInMonth m) day}
92 | %runElab deriveIndexed "Day" [Show, Eq, Ord]
95 | refineDay : {m : _} -> Integer -> Maybe (Day m)
96 | refineDay n = case hdec0 {p = FromTo 1 (DaysInMonth m)} n of
98 | Just0 v => Just $
D n
105 | -> {auto 0 p : IsJust (refineDay {m} $
cast n)}
107 | fromInteger n = fromJust $
refineDay (cast n)
110 | Interpolation (Day m) where
111 | interpolate (D d) = padLeft 2 '0' $
show d
126 | MkDate y1 m1 d1 == MkDate y2 m2 d2 =
127 | y1 == y2 && m1 == m2 && d1.day == d2.day
131 | compare (MkDate y1 m1 d1) (MkDate y2 m2 d2) =
132 | if y1 /= y2 then compare y1 y2
133 | else if m1 /= m2 then compare m1 m2
134 | else compare d1.day d2.day
136 | %runElab derive "Date" [Show]
139 | Interpolation Date where
140 | interpolate (MkDate y m d) = "\{y}-\{m}-\{d}"