Prime -> 2010
[haskell-report.git] / report / lib-code / Time.hs
1 module Time (
2 ClockTime,
3 Month(January,February,March,April,May,June,
4 July,August,September,October,November,December),
5 Day(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday),
6 CalendarTime(CalendarTime, ctYear, ctMonth, ctDay, ctHour, ctMin,
7 ctSec, ctPicosec, ctWDay, ctYDay,
8 ctTZName, ctTZ, ctIsDST),
9 TimeDiff(TimeDiff, tdYear, tdMonth, tdDay,
10 tdHour, tdMin, tdSec, tdPicosec),
11 getClockTime, addToClockTime, diffClockTimes,
12 toCalendarTime, toUTCTime, toClockTime,
13 calendarTimeToString, formatCalendarTime ) where
14
15 import Ix(Ix)
16 import Locale(TimeLocale(..),defaultTimeLocale)
17 import Char ( intToDigit )
18
19 data ClockTime = ... -- Implementation-dependent
20 instance Ord ClockTime where ...
21 instance Eq ClockTime where ...
22
23 data Month = January | February | March | April
24 | May | June | July | August
25 | September | October | November | December
26 deriving (Eq, Ord, Enum, Bounded, Ix, Read, Show)
27
28 data Day = Sunday | Monday | Tuesday | Wednesday | Thursday
29 | Friday | Saturday
30 deriving (Eq, Ord, Enum, Bounded, Ix, Read, Show)
31
32 data CalendarTime = CalendarTime {
33 ctYear :: Int,
34 ctMonth :: Month,
35 ctDay, ctHour, ctMin, ctSec :: Int,
36 ctPicosec :: Integer,
37 ctWDay :: Day,
38 ctYDay :: Int,
39 ctTZName :: String,
40 ctTZ :: Int,
41 ctIsDST :: Bool
42 } deriving (Eq, Ord, Read, Show)
43
44 data TimeDiff = TimeDiff {
45 tdYear, tdMonth, tdDay, tdHour, tdMin, tdSec :: Int,
46 tdPicosec :: Integer
47 } deriving (Eq, Ord, Read, Show)
48
49
50 getClockTime :: IO ClockTime
51 getClockTime = ... -- Implementation-dependent
52
53 addToClockTime :: TimeDiff -> ClockTime -> ClockTime
54 addToClockTime td ct = ... -- Implementation-dependent
55
56 diffClockTimes :: ClockTime -> ClockTime -> TimeDiff
57 diffClockTimes ct1 ct2 = ... -- Implementation-dependent
58
59 toCalendarTime :: ClockTime -> IO CalendarTime
60 toCalendarTime ct = ... -- Implementation-dependent
61
62 toUTCTime :: ClockTime -> CalendarTime
63 toUTCTime ct = ... -- Implementation-dependent
64
65 toClockTime :: CalendarTime -> ClockTime
66 toClockTime cal = ... -- Implementation-dependent
67
68 calendarTimeToString :: CalendarTime -> String
69 calendarTimeToString = formatCalendarTime defaultTimeLocale "%c"
70
71 formatCalendarTime :: TimeLocale -> String -> CalendarTime -> String
72 formatCalendarTime l fmt ct@(CalendarTime year mon day hour min sec sdec
73 wday yday tzname _ _) =
74 doFmt fmt
75 where doFmt ('%':c:cs) = decode c ++ doFmt cs
76 doFmt (c:cs) = c : doFmt cs
77 doFmt "" = ""
78
79 to12 :: Int -> Int
80 to12 h = let h' = h `mod` 12 in if h' == 0 then 12 else h'
81
82 decode 'A' = fst (wDays l !! fromEnum wday)
83 decode 'a' = snd (wDays l !! fromEnum wday)
84 decode 'B' = fst (months l !! fromEnum mon)
85 decode 'b' = snd (months l !! fromEnum mon)
86 decode 'h' = snd (months l !! fromEnum mon)
87 decode 'C' = show2 (year `quot` 100)
88 decode 'c' = doFmt (dateTimeFmt l)
89 decode 'D' = doFmt "%m/%d/%y"
90 decode 'd' = show2 day
91 decode 'e' = show2' day
92 decode 'H' = show2 hour
93 decode 'I' = show2 (to12 hour)
94 decode 'j' = show3 yday
95 decode 'k' = show2' hour
96 decode 'l' = show2' (to12 hour)
97 decode 'M' = show2 min
98 decode 'm' = show2 (fromEnum mon+1)
99 decode 'n' = "\n"
100 decode 'p' = (if hour < 12 then fst else snd) (amPm l)
101 decode 'R' = doFmt "%H:%M"
102 decode 'r' = doFmt (time12Fmt l)
103 decode 'T' = doFmt "%H:%M:%S"
104 decode 't' = "\t"
105 decode 'S' = show2 sec
106 decode 's' = ... -- Implementation-dependent
107 decode 'U' = show2 ((yday + 7 - fromEnum wday) `div` 7)
108 decode 'u' = show (let n = fromEnum wday in
109 if n == 0 then 7 else n)
110 decode 'V' =
111 let (week, days) =
112 (yday + 7 - if fromEnum wday > 0 then
113 fromEnum wday - 1 else 6) `divMod` 7
114 in show2 (if days >= 4 then
115 week+1
116 else if week == 0 then 53 else week)
117
118 decode 'W' =
119 show2 ((yday + 7 - if fromEnum wday > 0 then
120 fromEnum wday - 1 else 6) `div` 7)
121 decode 'w' = show (fromEnum wday)
122 decode 'X' = doFmt (timeFmt l)
123 decode 'x' = doFmt (dateFmt l)
124 decode 'Y' = show year
125 decode 'y' = show2 (year `rem` 100)
126 decode 'Z' = tzname
127 decode '%' = "%"
128 decode c = [c]
129
130 show2, show2', show3 :: Int -> String
131 show2 x = [intToDigit (x `quot` 10), intToDigit (x `rem` 10)]
132
133 show2' x = if x < 10 then [ ' ', intToDigit x] else show2 x
134
135 show3 x = intToDigit (x `quot` 100) : show2 (x `rem` 100)
136