~ubuntu-branches/ubuntu/precise/ghc/precise

« back to all changes in this revision

Viewing changes to libraries/time/Data/Time/Clock/POSIX.hs

  • Committer: Bazaar Package Importer
  • Author(s): Joachim Breitner
  • Date: 2011-01-17 12:49:24 UTC
  • Revision ID: james.westby@ubuntu.com-20110117124924-do1pym1jlf5o636m
Tags: upstream-7.0.1
ImportĀ upstreamĀ versionĀ 7.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
(
 
5
        posixDayLength,POSIXTime,posixSecondsToUTCTime,utcTimeToPOSIXSeconds,getPOSIXTime
 
6
) where
 
7
 
 
8
import Data.Time.Clock.UTC
 
9
import Data.Time.Calendar.Days
 
10
import Data.Fixed
 
11
import Control.Monad
 
12
 
 
13
#ifdef mingw32_HOST_OS
 
14
import Data.Word        ( Word64)
 
15
import System.Win32.Time
 
16
#else
 
17
import Data.Time.Clock.CTimeval
 
18
#endif
 
19
 
 
20
-- | 86400 nominal seconds in every day
 
21
posixDayLength :: NominalDiffTime
 
22
posixDayLength = 86400
 
23
 
 
24
-- | POSIX time is the nominal time since 1970-01-01 00:00 UTC
 
25
-- 
 
26
-- To convert from a 'Foreign.C.CTime' or 'System.Posix.EpochTime', use 'realToFrac'.
 
27
--
 
28
type POSIXTime = NominalDiffTime
 
29
 
 
30
unixEpochDay :: Day
 
31
unixEpochDay = ModifiedJulianDay 40587
 
32
 
 
33
posixSecondsToUTCTime :: POSIXTime -> UTCTime
 
34
posixSecondsToUTCTime i = let
 
35
        (d,t) = divMod' i posixDayLength
 
36
 in UTCTime (addDays d unixEpochDay) (realToFrac t)
 
37
 
 
38
utcTimeToPOSIXSeconds :: UTCTime -> POSIXTime
 
39
utcTimeToPOSIXSeconds (UTCTime d t) =
 
40
 (fromInteger (diffDays d unixEpochDay) * posixDayLength) + min posixDayLength (realToFrac t)
 
41
 
 
42
-- | Get the current POSIX time from the system clock.
 
43
getPOSIXTime :: IO POSIXTime
 
44
 
 
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.
 
50
 
 
51
getPOSIXTime = do
 
52
  FILETIME ft <- System.Win32.Time.getSystemTimeAsFileTime
 
53
  return (fromIntegral (ft - win32_epoch_adjust) / 10000000)
 
54
 
 
55
win32_epoch_adjust :: Word64
 
56
win32_epoch_adjust = 116444736000000000
 
57
 
 
58
#else
 
59
 
 
60
-- Use POSIX time
 
61
ctimevalToPosixSeconds :: CTimeval -> POSIXTime
 
62
ctimevalToPosixSeconds (MkCTimeval s mus) = (fromIntegral s) + (fromIntegral mus) / 1000000
 
63
 
 
64
getPOSIXTime = liftM ctimevalToPosixSeconds getCTimeval
 
65
 
 
66
#endif