2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 \section[LibTime]{Haskell 1.3 Time of Day Library}
6 The {\em LibTime} library provides the functionality of "time.h",
7 adapted to the Haskell environment. It includes timezone information,
8 as in System V, and follows RFC 1129 in its use of Coordinated
30 $ClockTime$ is an abstract type, used for the internal clock time.
31 Clock times may be compared, converted to strings, or converted to an
32 external calendar time $CalendarTime$.
35 data ClockTime = TOD Integer Integer
39 When a $ClockTime$ is shown, it is converted to a string of the form
40 $"Mon Nov 28 21:45:41 GMT 1994"$.
42 For now, we are restricted to roughly:
43 Fri Dec 13 20:45:52 1901 through Tue Jan 19 03:14:07 2038, because
44 we use the C library routines based on 32 bit integers.
47 instance Text ClockTime where
48 showsPrec p (TOD sec@(J# a# s# d#) nsec) =
49 showString (unsafePerformPrimIO (
50 _ccall_ showTime (I# s#) (_ByteArray (error "ClockTime.show") d#)
52 _ccall_ strlen str `thenPrimIO` \ len ->
53 _packCBytesST len str `thenStrictlyST` \ ps ->
54 returnPrimIO (_unpackPS ps)))
58 $CalendarTime$ is a user-readable and manipulable
59 representation of the internal $ClockTime$ type. The
60 numeric fields have the following ranges.
66 year -maxInt .. maxInt [Pre-Gregorian dates are inaccurate]
67 mon 0 .. 11 [Jan = 0, Dec = 11]
71 sec 0 .. 61 [Allows for two leap seconds]
72 picosec 0 .. (10^12)-1 [This could be over-precise?]
73 wday 0 .. 6 [Sunday = 0, Saturday = 6]
74 yday 0 .. 365 [364 in non-Leap years]
75 tz -43200 .. 43200 [Variation from UTC in seconds]
78 The {\em tzname} field is the name of the time zone. The {\em isdst}
79 field indicates whether Daylight Savings Time would be in effect.
82 -- year mon day hour min sec picosec wday yday tzname tz isdst
84 CalendarTime Int Int Int Int Int Int Integer Int Int String Int Bool
87 The $TimeDiff$ type records the difference between two clock times in
91 -- year mon day hour min sec picosec
92 data TimeDiff = TimeDiff Int Int Int Int Int Int Integer
96 $getClockTime$ returns the current time in its internal representation.
99 getClockTime :: IO ClockTime
101 malloc1 `thenStrictlyST` \ i1 ->
102 malloc1 `thenStrictlyST` \ i2 ->
103 _ccall_ getClockTime i1 i2 `thenPrimIO` \ rc ->
105 cvtUnsigned i1 `thenStrictlyST` \ sec ->
106 cvtUnsigned i2 `thenStrictlyST` \ nsec ->
107 return (TOD sec (nsec * 1000))
109 _constructError `thenPrimIO` \ ioError ->
113 case newIntArray# 1# s# of
114 StateAndMutableByteArray# s2# barr# -> (_MutableByteArray bot barr#, S# s2#)
115 bot = error "getClockTime"
117 -- The C routine fills in an unsigned word. We don't have `unsigned2Integer#,'
118 -- so we freeze the data bits and use them for an MP_INT structure. Note that
119 -- zero is still handled specially, although (J# 1# 1# (ptr to 0#)) is probably
120 -- acceptable to gmp.
122 cvtUnsigned (_MutableByteArray _ arr#) (S# s#) =
123 case readIntArray# arr# 0# s# of
124 StateAndInt# s2# r# ->
128 case unsafeFreezeByteArray# arr# s2# of
129 StateAndByteArray# s3# frozen# -> (J# 1# 1# frozen#, S# s3#)
133 $addToClockTime$ {\em d} {\em t} adds a time difference {\em d} and a
134 clock time {\em t} to yield a new clock time. The difference {\em d}
135 may be either positive or negative. $diffClockTimes$ {\em t1} {\em
136 t2} returns the difference between two clock times {\em t1} and {\em
141 addToClockTime :: TimeDiff -> ClockTime -> ClockTime
142 addToClockTime _ _ = error "addToClockTime unimplemented"
144 diffClockTimes :: ClockTime -> ClockTime -> TimeDiff
145 diffClockTimes _ _ = error "diffClockTimes unimplemented"
148 $toCalendarTime$ {\em t} converts {\em t} to a local time, modified by
149 the current timezone and daylight savings time settings. $toUTCTime$
150 {\em t} converts {\em t} into UTC time. $toClockTime$ {\em l}
151 converts {\em l} into the corresponding internal $ClockTime$. The
152 {\em wday}, {\em yday}, {\em tzname}, and {\em isdst} fields are
156 toCalendarTime :: ClockTime -> CalendarTime
157 toCalendarTime (TOD sec@(J# a# s# d#) psec) = unsafePerformPrimIO (
158 _ccall_ toLocalTime (I# s#) (_ByteArray (error "toCalendarTime") d#)
160 if tm == (``NULL''::_Addr) then
161 error "toCalendarTime{LibTime}: out of range"
163 _casm_ ``%r = ((struct tm *)%0)->tm_sec;'' tm
164 `thenPrimIO` \ sec ->
165 _casm_ ``%r = ((struct tm *)%0)->tm_min;'' tm
166 `thenPrimIO` \ min ->
167 _casm_ ``%r = ((struct tm *)%0)->tm_hour;'' tm
168 `thenPrimIO` \ hour ->
169 _casm_ ``%r = ((struct tm *)%0)->tm_mday;'' tm
170 `thenPrimIO` \ mday ->
171 _casm_ ``%r = ((struct tm *)%0)->tm_mon;'' tm
172 `thenPrimIO` \ mon ->
173 _casm_ ``%r = ((struct tm *)%0)->tm_year;'' tm
174 `thenPrimIO` \ year ->
175 _casm_ ``%r = ((struct tm *)%0)->tm_wday;'' tm
176 `thenPrimIO` \ wday ->
177 _casm_ ``%r = ((struct tm *)%0)->tm_yday;'' tm
178 `thenPrimIO` \ yday ->
179 _casm_ ``%r = ((struct tm *)%0)->tm_isdst;'' tm
180 `thenPrimIO` \ isdst ->
181 _ccall_ ZONE tm `thenPrimIO` \ zone ->
182 _ccall_ GMTOFF tm `thenPrimIO` \ tz ->
183 _ccall_ strlen zone `thenPrimIO` \ len ->
184 _packCBytesST len zone `thenStrictlyST` \ tzname ->
185 returnPrimIO (CalendarTime (1900+year) mon mday hour min sec psec
186 wday yday (_unpackPS tzname) tz (isdst /= 0))
189 toUTCTime :: ClockTime -> CalendarTime
190 toUTCTime (TOD sec@(J# a# s# d#) psec) = unsafePerformPrimIO (
191 _ccall_ toUTCTime (I# s#) (_ByteArray (error "toCalendarTime") d#)
193 if tm == (``NULL''::_Addr) then
194 error "toUTCTime{LibTime}: out of range"
196 _casm_ ``%r = ((struct tm *)%0)->tm_sec;'' tm
197 `thenPrimIO` \ sec ->
198 _casm_ ``%r = ((struct tm *)%0)->tm_min;'' tm
199 `thenPrimIO` \ min ->
200 _casm_ ``%r = ((struct tm *)%0)->tm_hour;'' tm
201 `thenPrimIO` \ hour ->
202 _casm_ ``%r = ((struct tm *)%0)->tm_mday;'' tm
203 `thenPrimIO` \ mday ->
204 _casm_ ``%r = ((struct tm *)%0)->tm_mon;'' tm
205 `thenPrimIO` \ mon ->
206 _casm_ ``%r = ((struct tm *)%0)->tm_year;'' tm
207 `thenPrimIO` \ year ->
208 _casm_ ``%r = ((struct tm *)%0)->tm_wday;'' tm
209 `thenPrimIO` \ wday ->
210 _casm_ ``%r = ((struct tm *)%0)->tm_yday;'' tm
211 `thenPrimIO` \ yday ->
212 returnPrimIO (CalendarTime (1900+year) mon mday hour min sec psec
213 wday yday "UTC" 0 False)
216 toClockTime :: CalendarTime -> ClockTime
217 toClockTime (CalendarTime year mon mday hour min sec psec wday yday tzname tz isdst) =
218 if psec < 0 || psec > 999999999999 then
219 error "toClockTime{LibTime}: picoseconds out of range"
220 else if tz < -43200 || tz > 43200 then
221 error "toClockTime{LibTime}: timezone offset out of range"
223 unsafePerformPrimIO (
224 _ccall_ toClockSec year mon mday hour min sec tz
225 `thenPrimIO` \ ptr@(A# ptr#) ->
226 if ptr /= ``NULL'' then
227 returnPrimIO (TOD (int2Integer# (indexIntOffAddr# ptr# 0#)) psec)
229 error "toClockTime{LibTime}: can't perform conversion"