-- | POSIX time, if you need to deal with timestamps and the like.-- Most people won't need this module.
module Data.Time.Clock.POSIX
(
posixDayLength,POSIXTime,posixSecondsToUTCTime,utcTimeToPOSIXSeconds,getPOSIXTime
) where
import Data.Time.Clock.UTC
import Data.Time.Calendar.Days
import Data.Fixed
import Control.Monad
#ifdef mingw32_HOST_OS
import Data.Word ( Word64)
import System.Win32.Time
#else
import Data.Time.Clock.CTimeval
#endif
-- | 86400 nominal seconds in every dayposixDayLength :: NominalDiffTimeposixDayLength = 86400-- | POSIX time is the nominal time since 1970-01-01 00:00 UTC-- -- To convert from a 'Foreign.C.CTime' or 'System.Posix.EpochTime', use 'realToFrac'.--type POSIXTime = NominalDiffTimeunixEpochDay :: DayunixEpochDay = ModifiedJulianDay40587posixSecondsToUTCTime :: POSIXTime -> UTCTimeposixSecondsToUTCTimei = let
(d,t) = divMod'iposixDayLength
in UTCTime (addDaysdunixEpochDay) (realToFract)
utcTimeToPOSIXSeconds :: UTCTime -> POSIXTimeutcTimeToPOSIXSeconds (UTCTimedt) =
(fromInteger (diffDaysdunixEpochDay) *posixDayLength) +minposixDayLength (realToFract)
-- | Get the current POSIX time from the system clock.getPOSIXTime :: IOPOSIXTime
#ifdef mingw32_HOST_OS
-- On Windows, the equlvalent of POSIX time is "file time", defined as-- the number of 100-nanosecond intervals that have elapsed since-- 12:00 A.M. January 1, 1601 (UTC). We can convert this into a POSIX-- time by adjusting the offset to be relative to the POSIX epoch.getPOSIXTime = do
FILETIMEft <- System.Win32.Time.getSystemTimeAsFileTimereturn (fromIntegral (ft-win32_epoch_adjust) /10000000)
win32_epoch_adjust :: Word64win32_epoch_adjust = 116444736000000000
#else
-- Use POSIX timectimevalToPosixSeconds :: CTimeval -> POSIXTimectimevalToPosixSeconds (MkCTimevalsmus) = (fromIntegrals) + (fromIntegralmus) /1000000getPOSIXTime = liftMctimevalToPosixSecondsgetCTimeval
#endif