2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-99
4 \section[Time]{Haskell 1.4 Time of Day Library}
6 The {\em Time} library provides standard functionality for
7 clock times, including timezone information (i.e, the functionality of
8 "time.h", adapted to the Haskell environment), It follows RFC 1129 in
9 its use of Coordinated Universal Time (UTC).
12 {-# OPTIONS -#include "cbits/timezone.h" -#include "cbits/stgio.h" #-}
18 , ClockTime(..) -- non-standard, lib. report gives this as abstract
25 , timeDiffToString -- non-standard
26 , formatTimeDiff -- non-standard
32 , calendarTimeToString
47 import PrelPack ( unpackCString, new_ps_array,
48 freeze_ps_array, unpackCStringBA
53 import Char ( intToDigit )
58 One way to partition and give name to chunks of a year and a week:
62 = January | February | March | April
63 | May | June | July | August
64 | September | October | November | December
65 deriving (Eq, Ord, Enum, Bounded, Ix, Read, Show)
68 = Sunday | Monday | Tuesday | Wednesday
69 | Thursday | Friday | Saturday
70 deriving (Eq, Ord, Enum, Bounded, Ix, Read, Show)
74 @ClockTime@ is an abstract type, used for the internal clock time.
75 Clock times may be compared, converted to strings, or converted to an
76 external calendar time @CalendarTime@.
80 -- I believe Int64 is more than big enough.
81 -- In fact, I think one of Int32 or Word32 would do. - ADR
82 data ClockTime = TOD Int64 Int64 deriving (Eq, Ord)
84 data ClockTime = TOD Integer Integer deriving (Eq, Ord)
88 When a @ClockTime@ is shown, it is converted to a string of the form
89 @"Mon Nov 28 21:45:41 GMT 1994"@.
91 For now, we are restricted to roughly:
92 Fri Dec 13 20:45:52 1901 through Tue Jan 19 03:14:07 2038, because
93 we use the C library routines based on 32 bit integers.
97 #warning Show ClockTime is bogus
98 instance Show ClockTime
100 instance Show ClockTime where
101 showsPrec p (TOD (S# i) _nsec) =
102 case int2Integer# i of (# s, d #) -> showsPrec p (TOD (J# s d) _nsec)
103 showsPrec _ (TOD (J# s# d#) _nsec) =
104 showString $ unsafePerformIO $ do
105 let buflen@(I# buflen#) = 50 -- big enough for error message
106 buf <- allocChars buflen
107 if s# <# (negateInt# 1#) || s# ># 1# then
108 return "ClockTime.show{Time}: out of range"
110 rc <- showTime (I# s#) d# buflen buf
112 return "ClockTime.show{Time}: internal error"
114 ba <- stToIO (freeze_ps_array buf buflen#)
115 return (unpackCStringBA ba)
117 showList = showList__ (showsPrec 0)
122 @CalendarTime@ is a user-readable and manipulable
123 representation of the internal $ClockTime$ type. The
124 numeric fields have the following ranges.
130 year -maxInt .. maxInt [Pre-Gregorian dates are inaccurate]
131 mon 0 .. 11 [Jan = 0, Dec = 11]
135 sec 0 .. 61 [Allows for two leap seconds]
136 picosec 0 .. (10^12)-1 [This could be over-precise?]
137 wday 0 .. 6 [Sunday = 0, Saturday = 6]
138 yday 0 .. 365 [364 in non-Leap years]
139 tz -43200 .. 43200 [Variation from UTC in seconds]
142 The {\em tzname} field is the name of the time zone. The {\em isdst}
143 field indicates whether Daylight Savings Time would be in effect.
157 ctPicosec :: Integer,
165 deriving (Eq,Ord,Read,Show)
169 The @TimeDiff@ type records the difference between two clock times in
182 tdPicosec :: Int64 -- not standard
184 tdPicosec :: Integer -- not standard
187 deriving (Eq,Ord,Read,Show)
190 @getClockTime@ returns the current time in its internal representation.
193 getClockTime :: IO ClockTime
197 rc <- primGetClockTime i1 i2
200 sec <- cvtUnsigned i1
201 nsec <- cvtUnsigned i2
202 return (TOD sec (nsec * 1000))
204 constructErrorAndFail "getClockTime"
207 malloc1 = primNewByteArray sizeof_int64
208 cvtUnsigned arr = primReadInt64Array arr 0
210 malloc1 :: IO (MutableByteArray RealWorld Int)
211 malloc1 = IO $ \ s# ->
212 case newIntArray# 1# s# of
213 (# s2#, barr# #) -> (# s2#, MutableByteArray bottom barr# #)
216 bottom = error "Time.bottom"
218 -- The C routine fills in an unsigned word. We don't have
219 -- `unsigned2Integer#,' so we freeze the data bits and use them
220 -- for an MP_INT structure. Note that zero is still handled specially,
221 -- although (J# 1# (ptr to 0#)) is probably acceptable to gmp.
223 cvtUnsigned :: MutableByteArray RealWorld Int -> IO Integer
224 cvtUnsigned (MutableByteArray _ arr#) = IO $ \ s# ->
225 case readIntArray# arr# 0# s# of
226 (# s2#, r# #) | r# ==# 0# -> (# s2#, 0 #)
228 case unsafeFreezeByteArray# arr# s2# of
229 (# s3#, frozen# #) -> (# s3#, J# 1# frozen# #)
233 @addToClockTime@ {\em d} {\em t} adds a time difference {\em d} and a
234 clock time {\em t} to yield a new clock time. The difference {\em d}
235 may be either positive or negative. @[diffClockTimes@ {\em t1} {\em
236 t2} returns the difference between two clock times {\em t1} and {\em
241 addToClockTime :: TimeDiff -> ClockTime -> ClockTime
242 addToClockTime (TimeDiff year mon day hour min sec psec)
243 (TOD c_sec c_psec) = unsafePerformIO $ do
245 rc <- toClockSec year mon day hour min sec (0::Int) res
248 diff_sec <- cvtUnsigned res
250 return (TOD (c_sec + diff_sec) (c_psec + diff_psec))
252 error "Time.addToClockTime: can't perform conversion of TimeDiff"
254 diffClockTimes :: ClockTime -> ClockTime -> TimeDiff
255 diffClockTimes tod_a tod_b =
257 CalendarTime year_a mon_a day_a hour_a min_a sec_a psec_a _ _ _ _ _ = toUTCTime tod_a
258 CalendarTime year_b mon_b day_b hour_b min_b sec_b psec_b _ _ _ _ _ = toUTCTime tod_b
260 TimeDiff (year_a - year_b)
269 @toCalendarTime@ {\em t} converts {\em t} to a local time, modified by
270 the current timezone and daylight savings time settings. @toUTCTime@
271 {\em t} converts {\em t} into UTC time. @toClockTime@ {\em l}
272 converts {\em l} into the corresponding internal @ClockTime@. The
273 {\em wday}, {\em yday}, {\em tzname}, and {\em isdst} fields are
278 toCalendarTime :: ClockTime -> IO CalendarTime
279 toCalendarTime (TOD sec psec) = do
280 res <- allocWords sizeof_int64
281 zoneNm <- allocChars 32
282 prim_SETZONE res zoneNm
283 rc <- prim_toLocalTime sec res
285 then constructErrorAndFail "Time.toCalendarTime: out of range"
287 sec <- get_tm_sec res
288 min <- get_tm_min res
289 hour <- get_tm_hour res
290 mday <- get_tm_mday res
291 mon <- get_tm_mon res
292 year <- get_tm_year res
293 wday <- get_tm_wday res
294 yday <- get_tm_yday res
295 isdst <- get_tm_isdst res
296 zone <- prim_ZONE res
297 tz <- prim_GMTOFF res
298 tzname <- primUnpackCString zone
299 return (CalendarTime (1900+year) mon mday hour min sec psec
300 (toEnum wday) yday tzname tz (isdst /= 0))
302 toUTCTime :: ClockTime -> CalendarTime
303 toUTCTime (TOD sec psec) = unsafePerformIO $ do
304 res <- allocWords sizeof_int64
305 zoneNm <- allocChars 32
306 prim_SETZONE res zoneNm
307 rc <- prim_toUTCTime sec res
309 then error "Time.toUTCTime: out of range"
311 sec <- get_tm_sec res
312 min <- get_tm_min res
313 hour <- get_tm_hour res
314 mday <- get_tm_mday res
315 mon <- get_tm_mon res
316 year <- get_tm_year res
317 wday <- get_tm_wday res
318 yday <- get_tm_yday res
319 return (CalendarTime (1900+year) mon mday hour min sec psec
320 (toEnum wday) yday "UTC" 0 False)
322 toClockTime :: CalendarTime -> ClockTime
323 toClockTime (CalendarTime year mon mday hour min sec psec wday yday tzname tz isdst) =
324 if psec < 0 || psec > 999999999999 then
325 error "Time.toClockTime: picoseconds out of range"
326 else if tz < -43200 || tz > 43200 then
327 error "Time.toClockTime: timezone offset out of range"
330 res <- allocWords sizeof_int64
331 rc <- toClockSec year mon mday hour min sec isDst res
334 tm <- primReadInt64Array res 0
336 else error "Time.toClockTime: can't perform conversion"
339 isDst = if isdst then (1::Int) else 0
341 toCalendarTime :: ClockTime -> IO CalendarTime
342 toCalendarTime (TOD (S# i) psec)
343 = case int2Integer# i of (# s, d #) -> toCalendarTime (TOD (J# s d) psec)
344 toCalendarTime (TOD (J# s# d#) psec) = do
345 res <- allocWords sizeof_struct_tm
346 zoneNm <- allocChars 32
347 prim_SETZONE res zoneNm
348 rc <- prim_toLocalTime (I# s#) d# res
350 then constructErrorAndFail "Time.toCalendarTime: out of range"
352 sec <- get_tm_sec res
353 min <- get_tm_min res
354 hour <- get_tm_hour res
355 mday <- get_tm_mday res
356 mon <- get_tm_mon res
357 year <- get_tm_year res
358 wday <- get_tm_wday res
359 yday <- get_tm_yday res
360 isdst <- get_tm_isdst res
363 let tzname = unpackCString zone
364 return (CalendarTime (1900+year) mon mday hour min sec psec
365 (toEnum wday) yday tzname tz (isdst /= (0::Int)))
367 toUTCTime :: ClockTime -> CalendarTime
368 toUTCTime (TOD (S# i) psec)
369 = case int2Integer# i of (# s, d #) -> toUTCTime (TOD (J# s d) psec)
370 toUTCTime (TOD (J# s# d#) psec) = unsafePerformIO $ do
371 res <- allocWords sizeof_struct_tm
372 zoneNm <- allocChars 32
373 prim_SETZONE res zoneNm
374 rc <- prim_toUTCTime (I# s#) d# res
376 then error "Time.toUTCTime: out of range"
378 sec <- get_tm_sec res
379 min <- get_tm_min res
380 hour <- get_tm_hour res
381 mday <- get_tm_mday res
382 mon <- get_tm_mon res
383 year <- get_tm_year res
384 wday <- get_tm_wday res
385 yday <- get_tm_yday res
386 return (CalendarTime (1900+year) mon mday hour min sec psec
387 (toEnum wday) yday "UTC" 0 False)
389 toClockTime :: CalendarTime -> ClockTime
390 toClockTime (CalendarTime year mon mday hour min sec psec _wday _yday _tzname tz isdst) =
391 if psec < 0 || psec > 999999999999 then
392 error "Time.toClockTime: picoseconds out of range"
393 else if tz < -43200 || tz > 43200 then
394 error "Time.toClockTime: timezone offset out of range"
398 rc <- toClockSec year mon mday hour min sec isDst res
403 else error "Time.toClockTime: can't perform conversion"
406 isDst = if isdst then (1::Int) else 0
410 -- (copied from PosixUtil, for now)
411 -- Allocate a mutable array of characters with no indices.
414 allocChars :: Int -> IO (PrimMutableByteArray RealWorld)
415 allocChars size = primNewByteArray size
417 -- Allocate a mutable array of words with no indices
419 allocWords :: Int -> IO (PrimMutableByteArray RealWorld)
420 allocWords size = primNewByteArray size
422 allocChars :: Int -> IO (MutableByteArray RealWorld Int)
423 allocChars (I# size#) = stToIO (new_ps_array size#)
425 -- Allocate a mutable array of words with no indices
427 allocWords :: Int -> IO (MutableByteArray RealWorld Int)
428 allocWords (I# size#) = IO $ \ s# ->
429 case newIntArray# size# s# of
431 (# s2#, MutableByteArray bot barr# #)
433 bot = error "Time.allocWords"
438 calendarTimeToString :: CalendarTime -> String
439 calendarTimeToString = formatCalendarTime defaultTimeLocale "%c"
441 formatCalendarTime :: TimeLocale -> String -> CalendarTime -> String
442 formatCalendarTime l fmt (CalendarTime year mon day hour min sec _
443 wday yday tzname _ _) =
445 where doFmt ('%':c:cs) = decode c ++ doFmt cs
446 doFmt (c:cs) = c : doFmt cs
449 decode 'A' = fst (wDays l !! fromEnum wday) -- day of the week, full name
450 decode 'a' = snd (wDays l !! fromEnum wday) -- day of the week, abbrev.
451 decode 'B' = fst (months l !! fromEnum mon) -- month, full name
452 decode 'b' = snd (months l !! fromEnum mon) -- month, abbrev
453 decode 'h' = snd (months l !! fromEnum mon) -- ditto
454 decode 'C' = show2 (year `quot` 100) -- century
455 decode 'c' = doFmt (dateTimeFmt l) -- locale's data and time format.
456 decode 'D' = doFmt "%m/%d/%y"
457 decode 'd' = show2 day -- day of the month
458 decode 'e' = show2' day -- ditto, padded
459 decode 'H' = show2 hour -- hours, 24-hour clock, padded
460 decode 'I' = show2 (to12 hour) -- hours, 12-hour clock
461 decode 'j' = show3 yday -- day of the year
462 decode 'k' = show2' hour -- hours, 24-hour clock, no padding
463 decode 'l' = show2' (to12 hour) -- hours, 12-hour clock, no padding
464 decode 'M' = show2 min -- minutes
465 decode 'm' = show2 (fromEnum mon+1) -- numeric month
467 decode 'p' = (if hour < 12 then fst else snd) (amPm l) -- am or pm
468 decode 'R' = doFmt "%H:%M"
469 decode 'r' = doFmt (time12Fmt l)
470 decode 'T' = doFmt "%H:%M:%S"
472 decode 'S' = show2 sec -- seconds
473 decode 's' = show2 sec -- number of secs since Epoch. (ToDo.)
474 decode 'U' = show2 ((yday + 7 - fromEnum wday) `div` 7) -- week number, starting on Sunday.
475 decode 'u' = show (let n = fromEnum wday in -- numeric day of the week (1=Monday, 7=Sunday)
476 if n == 0 then 7 else n)
477 decode 'V' = -- week number (as per ISO-8601.)
478 let (week, days) = -- [yep, I've always wanted to be able to display that too.]
479 (yday + 7 - if fromEnum wday > 0 then
480 fromEnum wday - 1 else 6) `divMod` 7
481 in show2 (if days >= 4 then
483 else if week == 0 then 53 else week)
485 decode 'W' = -- week number, weeks starting on monday
486 show2 ((yday + 7 - if fromEnum wday > 0 then
487 fromEnum wday - 1 else 6) `div` 7)
488 decode 'w' = show (fromEnum wday) -- numeric day of the week, weeks starting on Sunday.
489 decode 'X' = doFmt (timeFmt l) -- locale's preferred way of printing time.
490 decode 'x' = doFmt (dateFmt l) -- locale's preferred way of printing dates.
491 decode 'Y' = show year -- year, including century.
492 decode 'y' = show2 (year `rem` 100) -- year, within century.
493 decode 'Z' = tzname -- timezone name
498 show2, show2', show3 :: Int -> String
499 show2 x = [intToDigit (x `quot` 10), intToDigit (x `rem` 10)]
501 show2' x = if x < 10 then [ ' ', intToDigit x] else show2 x
503 show3 x = intToDigit (x `quot` 100) : show2 (x `rem` 100)
506 to12 h = let h' = h `mod` 12 in if h' == 0 then 12 else h'
509 Useful extensions for formatting TimeDiffs.
512 timeDiffToString :: TimeDiff -> String
513 timeDiffToString = formatTimeDiff defaultTimeLocale "%c"
515 formatTimeDiff :: TimeLocale -> String -> TimeDiff -> String
516 formatTimeDiff l fmt (TimeDiff year month day hour min sec _)
520 doFmt ('%':c:cs) = decode c ++ doFmt cs
521 doFmt (c:cs) = c : doFmt cs
525 'B' -> fst (months l !! fromEnum month)
526 'b' -> snd (months l !! fromEnum month)
527 'h' -> snd (months l !! fromEnum month)
528 'C' -> show2 (year `quot` 100)
529 'D' -> doFmt "%m/%d/%y"
533 'I' -> show2 (to12 hour)
535 'l' -> show2' (to12 hour)
537 'm' -> show2 (fromEnum month + 1)
539 'p' -> (if hour < 12 then fst else snd) (amPm l)
541 'r' -> doFmt (time12Fmt l)
542 'T' -> doFmt "%H:%M:%S"
545 's' -> show2 sec -- Implementation-dependent, sez the lib doc..
546 'X' -> doFmt (timeFmt l)
547 'x' -> doFmt (dateFmt l)
549 'y' -> show2 (year `rem` 100)
556 foreign import "libHS_cbits" "get_tm_sec" get_tm_sec :: MBytes -> IO Int
557 foreign import "libHS_cbits" "get_tm_min" get_tm_min :: MBytes -> IO Int
558 foreign import "libHS_cbits" "get_tm_hour" get_tm_hour :: MBytes -> IO Int
559 foreign import "libHS_cbits" "get_tm_mday" get_tm_mday :: MBytes -> IO Int
560 foreign import "libHS_cbits" "get_tm_mon" get_tm_mon :: MBytes -> IO Int
561 foreign import "libHS_cbits" "get_tm_year" get_tm_year :: MBytes -> IO Int
562 foreign import "libHS_cbits" "get_tm_wday" get_tm_wday :: MBytes -> IO Int
563 foreign import "libHS_cbits" "get_tm_yday" get_tm_yday :: MBytes -> IO Int
564 foreign import "libHS_cbits" "get_tm_isdst" get_tm_isdst :: MBytes -> IO Int
566 foreign import "libHS_cbits" "prim_ZONE" prim_ZONE :: Bytes -> IO Addr
567 foreign import "libHS_cbits" "prim_GMTOFF" prim_GMTOFF :: Bytes -> IO Int
569 foreign import "libHS_cbits" "sizeof_struct_tm" sizeof_struct_tm :: Int
572 -- believed to be at least 1 bit (the sign bit!) bigger than sizeof_time_t
577 type MBytes = MutableByteArray RealWorld Int
579 foreign import "libHS_cbits" "sizeof_time_t" sizeof_time_t :: Int
581 foreign import "libHS_cbits" "prim_SETZONE" prim_SETZONE :: MBytes -> MBytes -> IO Int
583 foreign import "libHS_cbits" "prim_toLocalTime" prim_toLocalTime :: Int64 -> MBytes -> IO Int
584 foreign import "libHS_cbits" "prim_toUTCTime" prim_toUTCTime :: Int64 -> MBytes -> IO Int
586 foreign import "libHS_cbits" "toLocalTime" prim_toLocalTime :: Int -> Bytes -> MBytes -> IO Int
587 foreign import "libHS_cbits" "toUTCTime" prim_toUTCTime :: Int -> Bytes -> MBytes -> IO Int
590 foreign import "libHS_cbits" "get_ZONE" get_ZONE :: MBytes -> IO Addr
591 foreign import "libHS_cbits" "GMTOFF" get_GMTOFF :: MBytes -> IO Int
594 foreign import "libHS_cbits" "toClockSec"
595 toClockSec :: Int -> Int -> Int -> Int -> Int
596 -> Int -> Int -> MBytes -> IO Int
598 foreign import "libHS_cbits" "getClockTime"
599 primGetClockTime :: MutableByteArray RealWorld Int
600 -> MutableByteArray RealWorld Int
602 foreign import "libHS_cbits" "showTime"