1
-- | POSIX time, if you need to deal with timestamps and the like.
2
-- Most people won't need this module.
3
module Data.Time.Clock.POSIX
5
posixDayLength,POSIXTime,posixSecondsToUTCTime,utcTimeToPOSIXSeconds,getPOSIXTime
8
import Data.Time.Clock.UTC
9
import Data.Time.Calendar.Days
13
#ifdef mingw32_HOST_OS
14
import Data.Word ( Word64)
15
import System.Win32.Time
17
import Data.Time.Clock.CTimeval
20
-- | 86400 nominal seconds in every day
21
posixDayLength :: NominalDiffTime
22
posixDayLength = 86400
24
-- | POSIX time is the nominal time since 1970-01-01 00:00 UTC
26
-- To convert from a 'Foreign.C.CTime' or 'System.Posix.EpochTime', use 'realToFrac'.
28
type POSIXTime = NominalDiffTime
31
unixEpochDay = ModifiedJulianDay 40587
33
posixSecondsToUTCTime :: POSIXTime -> UTCTime
34
posixSecondsToUTCTime i = let
35
(d,t) = divMod' i posixDayLength
36
in UTCTime (addDays d unixEpochDay) (realToFrac t)
38
utcTimeToPOSIXSeconds :: UTCTime -> POSIXTime
39
utcTimeToPOSIXSeconds (UTCTime d t) =
40
(fromInteger (diffDays d unixEpochDay) * posixDayLength) + min posixDayLength (realToFrac t)
42
-- | Get the current POSIX time from the system clock.
43
getPOSIXTime :: IO POSIXTime
45
#ifdef mingw32_HOST_OS
46
-- On Windows, the equlvalent of POSIX time is "file time", defined as
47
-- the number of 100-nanosecond intervals that have elapsed since
48
-- 12:00 A.M. January 1, 1601 (UTC). We can convert this into a POSIX
49
-- time by adjusting the offset to be relative to the POSIX epoch.
52
FILETIME ft <- System.Win32.Time.getSystemTimeAsFileTime
53
return (fromIntegral (ft - win32_epoch_adjust) / 10000000)
55
win32_epoch_adjust :: Word64
56
win32_epoch_adjust = 116444736000000000
61
ctimevalToPosixSeconds :: CTimeval -> POSIXTime
62
ctimevalToPosixSeconds (MkCTimeval s mus) = (fromIntegral s) + (fromIntegral mus) / 1000000
64
getPOSIXTime = liftM ctimevalToPosixSeconds getCTimeval