-- |
-- Module : System.Time
-- Copyright : (c) The University of Glasgow 2001
--- License : BSD-style (see the file libraries/core/LICENSE)
+-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : libraries@haskell.org
-- Stability : provisional
) where
-#ifndef __HUGS__
+#ifdef __GLASGOW_HASKELL__
#include "HsBase.h"
#endif
+#ifdef __NHC__
+#include <time.h>
+# ifdef __sun
+# define HAVE_TZNAME 1
+# else
+# define HAVE_TM_ZONE 1
+# endif
+import Ix
+#endif
+
import Prelude
import Data.Ix
#elif HAVE_GETTIMEOFDAY
getClockTime = do
+ let realToInteger = round . realToFrac :: Real a => a -> Integer
allocaBytes (#const sizeof(struct timeval)) $ \ p_timeval -> do
throwErrnoIfMinus1_ "getClockTime" $ gettimeofday p_timeval nullPtr
sec <- (#peek struct timeval,tv_sec) p_timeval :: IO CTime
usec <- (#peek struct timeval,tv_usec) p_timeval :: IO CTime
- return (TOD (fromIntegral sec) ((fromIntegral usec) * 1000000))
+ return (TOD (realToInteger sec) ((realToInteger usec) * 1000000))
#elif HAVE_FTIME
getClockTime = do
+ let realToInteger = round . realToFrac :: Real a => a -> Integer
allocaBytes (#const sizeof(struct timeb)) $ \ p_timeb -> do
ftime p_timeb
sec <- (#peek struct timeb,time) p_timeb :: IO CTime
msec <- (#peek struct timeb,millitm) p_timeb :: IO CUShort
- return (TOD (fromIntegral sec) (fromIntegral msec * 1000000000))
+ return (TOD (realToInteger sec) (fromIntegral msec * 1000000000))
#else /* use POSIX time() */
getClockTime = do
secs <- time nullPtr -- can't fail, according to POSIX
- return (TOD (fromIntegral secs) 0)
+ let realToInteger = round . realToFrac :: Real a => a -> Integer
+ return (TOD (realToInteger secs) 0)
#endif
cal = toUTCTime (TOD (c_sec + sec_diff) (c_psec + psec))
-- FIXME! ^^^^
new_mon = fromEnum (ctMonth cal) + r_mon
- (month', yr_diff)
+ month' = fst tmp
+ yr_diff = snd tmp
+ tmp
| new_mon < 0 = (toEnum (12 + new_mon), (-1))
| new_mon > 11 = (toEnum (new_mon `mod` 12), 1)
| otherwise = (toEnum new_mon, 0)
# define tzname _tzname
# endif
# ifndef mingw32_TARGET_OS
-foreign import ccall unsafe "&tzname" tzname :: Ptr (Ptr CChar)
+foreign import ccall unsafe "time.h &tzname" tzname :: Ptr (Ptr CChar)
# else
foreign import ccall unsafe "__hscore_timezone" timezone :: Ptr CLong
foreign import ccall unsafe "__hscore_tzname" tzname :: Ptr (Ptr CChar)
# else /* ! HAVE_DECL_ALTZONE */
#if !defined(mingw32_TARGET_OS)
-foreign import ccall unsafe "timezone" timezone :: Ptr CLong
+foreign import ccall "time.h &timezone" timezone :: Ptr CLong
#endif
-- Assume that DST offset is 1 hour ...
-- result.
--
gmtoff <- gmtoff p_tm
- let res = fromIntegral t - tz + fromIntegral gmtoff
- return (TOD (fromIntegral res) psec)
+ let realToInteger = round . realToFrac :: Real a => a -> Integer
+ res = realToInteger t - fromIntegral tz + fromIntegral gmtoff
+ return (TOD res psec)
#endif /* ! __HUGS__ */
-- -----------------------------------------------------------------------------
type CTm = () -- struct tm
#if HAVE_LOCALTIME_R
-foreign import ccall unsafe localtime_r :: Ptr CTime -> Ptr CTm -> IO (Ptr CTm)
+foreign import ccall unsafe "time.h localtime_r"
+ localtime_r :: Ptr CTime -> Ptr CTm -> IO (Ptr CTm)
#else
-foreign import ccall unsafe localtime :: Ptr CTime -> IO (Ptr CTm)
+foreign import ccall unsafe "time.h localtime"
+ localtime :: Ptr CTime -> IO (Ptr CTm)
#endif
#if HAVE_GMTIME_R
-foreign import ccall unsafe gmtime_r :: Ptr CTime -> Ptr CTm -> IO (Ptr CTm)
+foreign import ccall unsafe "time.h gmtime_r"
+ gmtime_r :: Ptr CTime -> Ptr CTm -> IO (Ptr CTm)
#else
-foreign import ccall unsafe gmtime :: Ptr CTime -> IO (Ptr CTm)
+foreign import ccall unsafe "time.h gmtime"
+ gmtime :: Ptr CTime -> IO (Ptr CTm)
#endif
-foreign import ccall unsafe mktime :: Ptr CTm -> IO CTime
-foreign import ccall unsafe time :: Ptr CTime -> IO CTime
+foreign import ccall unsafe "time.h mktime"
+ mktime :: Ptr CTm -> IO CTime
+foreign import ccall unsafe "time.h time"
+ time :: Ptr CTime -> IO CTime
#if HAVE_GETTIMEOFDAY
type CTimeVal = ()
-foreign import ccall unsafe gettimeofday :: Ptr CTimeVal -> Ptr () -> IO CInt
+foreign import ccall unsafe "time.h gettimeofday"
+ gettimeofday :: Ptr CTimeVal -> Ptr () -> IO CInt
#endif
#if HAVE_FTIME
type CTimeB = ()
#ifndef mingw32_TARGET_OS
-foreign import ccall unsafe ftime :: Ptr CTimeB -> IO CInt
+foreign import ccall unsafe "time.h ftime" ftime :: Ptr CTimeB -> IO CInt
#else
-foreign import ccall unsafe ftime :: Ptr CTimeB -> IO ()
+foreign import ccall unsafe "time.h ftime" ftime :: Ptr CTimeB -> IO ()
#endif
#endif
#endif /* ! __HUGS__ */