1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
<title>The Haskell 98 Library Report: Dates and Times</title>
<body bgcolor="#ffffff"> <i>The Haskell 98 Library Report</i><br> <a href="index.html">top</a> | <a href="system.html">back</a> | <a href="locale.html">next</a> | <a href="libindex.html">contents</a> <br><hr>
<a name="time"></a><a name="sect14"></a>
<h2>14<tt> </tt>Dates and Times</h2>
<p>
<table border=2 cellpadding=3>
<tr><td>
<tt><br>
module Time (<br>
ClockTime, <br>
Month(January,February,March,April,May,June,<br>
July,August,September,October,November,December),<br>
Day(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday),<br>
CalendarTime(CalendarTime, ctYear, ctMonth, ctDay, ctHour, ctMin,<br>
ctPicosec, ctWDay, ctYDay, ctTZName, ctTZ, ctIsDST),<br>
TimeDiff(TimeDiff, tdYear, tdMonth, tdDay, tdHour,<br>
tdMin, tdSec, tdPicosec),<br>
getClockTime, addToClockTime, diffClockTimes,<br>
toCalendarTime, toUTCTime, toClockTime,<br>
calendarTimeToString, formatCalendarTime ) where<br>
<br>
import Ix(Ix)<br>
<br>
data ClockTime = ... -- Implementation-dependent<br>
instance Ord ClockTime where ...<br>
instance Eq ClockTime where ...<br>
<br>
data Month = January | February | March | April<br>
| May | June | July | August<br>
| September | October | November | December<br>
deriving (Eq, Ord, Enum, Bounded, Ix, Read, Show)<br>
<br>
data Day = Sunday | Monday | Tuesday | Wednesday | Thursday <br>
| Friday | Saturday<br>
deriving (Eq, Ord, Enum, Bounded, Ix, Read, Show)<br>
<br>
data CalendarTime = CalendarTime {<br>
ctYear :: Int,<br>
ctMonth :: Month,<br>
ctDay, ctHour, ctMin, ctSec :: Int,<br>
ctPicosec :: Integer,<br>
ctWDay :: Day,<br>
ctYDay :: Int,<br>
ctTZName :: String,<br>
ctTZ :: Int,<br>
ctIsDST :: Bool<br>
} deriving (Eq, Ord, Read, Show)<br>
<br>
data TimeDiff = TimeDiff {<br>
tdYear, tdMonth, tdDay, tdHour, tdMin, tdSec :: Int,<br>
tdPicosec :: Integer<br>
} deriving (Eq, Ord, Read, Show)<br>
</tt></td></tr></table>
<table border=2 cellpadding=3>
<tr><td>
<tt><br>
-- Functions on times<br>
getClockTime :: IO ClockTime<br>
<br>
addToClockTime :: TimeDiff -> ClockTime -> ClockTime<br>
diffClockTimes :: ClockTime -> ClockTime -> TimeDiff<br>
<br>
toCalendarTime :: ClockTime -> IO CalendarTime<br>
toUTCTime :: ClockTime -> CalendarTime<br>
toClockTime :: CalendarTime -> ClockTime<br>
calendarTimeToString :: CalendarTime -> String<br>
formatCalendarTime :: TimeLocale -> String -> CalendarTime -> String<br>
</tt></td></tr></table>
<p>
The <tt>Time</tt> library provides standard functionality for clock times,
including timezone information. It follows RFC 1129 in its use of
Coordinated Universal Time (UTC).<p>
<tt>ClockTime</tt> is an abstract type, used for the system's internal clock
time. Clock times may be compared directly or converted to a calendar
time <tt>CalendarTime</tt> for I/O or other manipulations.
<tt>CalendarTime</tt> is a user-readable and manipulable representation of
the internal <tt>ClockTime</tt> type. The numeric fields have the following
ranges.
<table border=2 cellpadding=3>
<tr><td>
<table >
<tr><td>
</td><td><I>Value</I></td><td></td><td><I>Range</I></td><td></td><td><I>Comments</I> </td></tr><tr></tr><tr><td></td><td>ctYear</td><td></td><td>-maxInt...</td><td>maxInt</td><td>Pre-Gregorian dates are inaccurate </td></tr><tr><td></td><td>ctDay</td><td></td><td>1...</td><td>31 </td></tr><tr><td></td><td>ctHour</td><td></td><td>0...</td><td>23</td></tr><tr><td></td><td>ctMin</td><td></td><td>0...</td><td>59</td></tr><tr><td></td><td>ctSec</td><td></td><td>0...</td><td>61</td><td>Allows for two Leap Seconds</td></tr><tr><td></td><td>ctPicosec</td><td></td><td>0...</td><td>(10<sup>12</sup>)-1 </td></tr><tr><td></td><td>ctYDay</td><td></td><td>0...</td><td>365</td><td>364 in non-Leap years </td></tr><tr><td></td><td>ctTZ</td><td></td><td>-89999...</td><td>89999</td><td>Variation from UTC in seconds
</td></tr></table>
</td></tr></table>
The <I>ctTZName</I> field is the name of the time zone. The <I>ctIsDST</I> field
is <tt>True</tt> if Daylight Savings Time would be in effect, and <tt>False
</tt>otherwise.
The <tt>TimeDiff</tt> type records the difference between two clock times in
a user-readable way.<p>
Function <tt>getClockTime</tt> returns the current time in its internal
representation. The expression
<tt>addToClockTime</tt> <I>d</I> <I>t</I> adds a time difference <I>d</I> and a
clock time <I>t</I> to yield a new clock time. The difference <I>d</I> may be either
positive or negative. The expression <tt>diffClockTimes</tt> <I>t1</I> <I>t2
</I>returns the difference between two clock times <I>t1</I> and <I>t2</I> as a
<tt>TimeDiff</tt>. <p>
Function <tt>toCalendarTime</tt> <I>t</I> converts <I>t</I> to a local time, modified
by the timezone and daylight savings time settings in force at the
time of conversion. Because of this dependence on the local environment,
<tt>toCalendarTime</tt> is in the <tt>IO</tt> monad.<p>
Function <tt>toUTCTime</tt> <I>t</I> converts <I>t</I> into a <tt>CalendarTime
</tt>in standard UTC format.
<tt>toClockTime</tt> <I>l</I> converts <I>l</I> into the corresponding internal
<tt>ClockTime</tt> ignoring the contents of the <I>ctWDay</I>, <I>ctYDay</I>,
<I>ctTZName</I>, and <I>ctIsDST</I> fields.<p>
Function <tt>calendarTimeToString</tt> formats
calendar times using local conventions and a formatting string.<p>
<a name="sect14.1"></a>
<h3>14.1<tt> </tt>Library <tt>Time</tt></h3>
<tt><br>
module Time (<br>
ClockTime, <br>
Month(January,February,March,April,May,June,<br>
July,August,September,October,November,December),<br>
Day(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday),<br>
CalendarTime(CalendarTime, ctYear, ctMonth, ctDay, ctHour, ctMin,<br>
ctPicosec, ctWDay, ctYDay, ctTZName, ctTZ, ctIsDST),<br>
TimeDiff(TimeDiff, tdYear, tdMonth, tdDay, <br>
tdHour, tdMin, tdSec, tdPicosec),<br>
getClockTime, addToClockTime, diffClockTimes,<br>
toCalendarTime, toUTCTime, toClockTime,<br>
calendarTimeToString, formatCalendarTime ) where<br>
<br>
import Ix(Ix)<br>
import Locale(TimeLocale(..),defaultTimeLocale)<br>
import Char ( intToDigit )<br>
<br>
data ClockTime = ... -- Implementation-dependent<br>
instance Ord ClockTime where ...<br>
instance Eq ClockTime where ...<br>
<br>
data Month = January | February | March | April<br>
| May | June | July | August<br>
| September | October | November | December<br>
deriving (Eq, Ord, Enum, Bounded, Ix, Read, Show)<br>
<br>
data Day = Sunday | Monday | Tuesday | Wednesday | Thursday <br>
| Friday | Saturday<br>
deriving (Eq, Ord, Enum, Bounded, Ix, Read, Show)<br>
<br>
data CalendarTime = CalendarTime {<br>
ctYear :: Int,<br>
ctMonth :: Month,<br>
ctDay, ctHour, ctMin, ctSec :: Int,<br>
ctPicosec :: Integer,<br>
ctWDay :: Day,<br>
ctYDay :: Int,<br>
ctTZName :: String,<br>
ctTZ :: Int,<br>
ctIsDST :: Bool<br>
} deriving (Eq, Ord, Read, Show)<br>
<br>
data TimeDiff = TimeDiff {<br>
tdYear, tdMonth, tdDay, tdHour, tdMin, tdSec :: Int,<br>
tdPicosec :: Integer<br>
} deriving (Eq, Ord, Read, Show)<br>
<br>
<br>
getClockTime :: IO ClockTime<br>
getClockTime = ... -- Implementation-dependent<br>
<br>
addToClockTime :: TimeDiff -> ClockTime -> ClockTime<br>
addToClockTime td ct = ... -- Implementation-dependent<br>
<br>
diffClockTimes :: ClockTime -> ClockTime -> TimeDiff<br>
diffClockTimes ct1 ct2 = ... -- Implementation-dependent<br>
<br>
toCalendarTime :: ClockTime -> IO CalendarTime<br>
toCalendarTime ct = ... -- Implementation-dependent<br>
<br>
toUTCTime :: ClockTime -> CalendarTime<br>
toUTCTime ct = ... -- Implementation-dependent<br>
<br>
toClockTime :: CalendarTime -> ClockTime<br>
toClockTime cal = ... -- Implementation-dependent<br>
<br>
calendarTimeToString :: CalendarTime -> String<br>
calendarTimeToString = formatCalendarTime defaultTimeLocale "%c"<br>
<br>
formatCalendarTime :: TimeLocale -> String -> CalendarTime -> String<br>
formatCalendarTime l fmt ct@(CalendarTime year mon day hour min sec sdec <br>
wday yday tzname _ _) =<br>
doFmt fmt<br>
where doFmt ('%':c:cs) = decode c ++ doFmt cs<br>
doFmt (c:cs) = c : doFmt cs<br>
doFmt "" = ""<br>
<br>
to12 :: Int -> Int<br>
to12 h = let h' = h `mod` 12 in if h' == 0 then 12 else h'<br>
<br>
decode 'A' = fst (wDays l !! fromEnum wday)<br>
decode 'a' = snd (wDays l !! fromEnum wday)<br>
decode 'B' = fst (months l !! fromEnum mon)<br>
decode 'b' = snd (months l !! fromEnum mon)<br>
decode 'h' = snd (months l !! fromEnum mon)<br>
decode 'C' = show2 (year `quot` 100)<br>
decode 'c' = doFmt (dateTimeFmt l)<br>
decode 'D' = doFmt "%m/%d/%y"<br>
decode 'd' = show2 day<br>
decode 'e' = show2' day<br>
decode 'H' = show2 hour<br>
decode 'I' = show2 (to12 hour)<br>
decode 'j' = show3 yday<br>
decode 'k' = show2' hour<br>
decode 'l' = show2' (to12 hour)<br>
decode 'M' = show2 min<br>
decode 'm' = show2 (fromEnum mon+1)<br>
decode 'n' = "\n"<br>
decode 'p' = (if hour < 12 then fst else snd) (amPm l)<br>
decode 'R' = doFmt "%H:%M"<br>
decode 'r' = doFmt (time12Fmt l)<br>
decode 'T' = doFmt "%H:%M:%S"<br>
decode 't' = "\t"<br>
decode 'S' = show2 sec<br>
decode 's' = ... -- Implementation-dependent<br>
decode 'U' = show2 ((yday + 7 - fromEnum wday) `div` 7)<br>
decode 'u' = show (let n = fromEnum wday in <br>
if n == 0 then 7 else n)<br>
decode 'V' = <br>
let (week, days) = <br>
(yday + 7 - if fromEnum wday > 0 then <br>
fromEnum wday - 1 else 6) `divMod` 7<br>
in show2 (if days >= 4 then<br>
week+1 <br>
else if week == 0 then 53 else week)<br>
<br>
decode 'W' = <br>
show2 ((yday + 7 - if fromEnum wday > 0 then <br>
fromEnum wday - 1 else 6) `div` 7)<br>
decode 'w' = show (fromEnum wday)<br>
decode 'X' = doFmt (timeFmt l)<br>
decode 'x' = doFmt (dateFmt l)<br>
decode 'Y' = show year<br>
decode 'y' = show2 (year `rem` 100)<br>
decode 'Z' = tzname<br>
decode '%' = "%"<br>
decode c = [c]<br>
<br>
show2, show2', show3 :: Int -> String<br>
show2 x = [intToDigit (x `quot` 10), intToDigit (x `rem` 10)]<br>
<br>
show2' x = if x < 10 then [ ' ', intToDigit x] else show2 x<br>
<br>
show3 x = intToDigit (x `quot` 100) : show2 (x `rem` 100)<br>
<br>
<p>
<body bgcolor="#ffffff"> <i>The Haskell 98 Library Report</i><br> <a href="index.html">top</a> | <a href="system.html">back</a> | <a href="locale.html">next</a> | <a href="libindex.html">contents</a> <br><hr>
</tt>
|