[project @ 2001-01-14 15:36:04 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / Time.hsc
index f40ed6f..dc7077d 100644 (file)
@@ -1,5 +1,5 @@
 -- -----------------------------------------------------------------------------
--- $Id: Time.hsc,v 1.2 2001/01/12 16:40:07 simonmar Exp $
+-- $Id: Time.hsc,v 1.5 2001/01/14 15:36:04 simonmar Exp $
 --
 -- (c) The University of Glasgow, 1995-2001
 --
@@ -228,11 +228,19 @@ noTimeDiff = TimeDiff 0 0 0 0 0 0 0
 -- -----------------------------------------------------------------------------
 -- getClockTime returns the current time in its internal representation.
 
-#if defined(_WIN32) && !defined(cygwin32_TARGET_OS)
-  -- 
+#if HAVE_GETTIMEOFDAY
+getClockTime = do
+  allocaBytes (#const sizeof(struct timeval)) $ \ p_timeval -> do
+    throwErrnoIfMinus1_ "getClockTime" $ gettimeofday p_timeval nullPtr
+    sec  <- (#peek struct timeval,tv_sec)  p_timeval :: IO CLong
+    usec <- (#peek struct timeval,tv_usec) p_timeval :: IO CLong
+    return (TOD (fromIntegral sec) ((fromIntegral usec) * 1000))
+#elif HAVE_FTIME && !defined(cygwin32_TARGET_OS)
+  --
   -- ftime() as implemented by cygwin (in B20.1) is
   -- not right, so stay away & use time() there instead.
-  -- 
+  --
 getClockTime = do
   allocaBytes (#const sizeof(struct timeb)) $ \ p_timeb -> do
   ftime p_timeb
@@ -240,14 +248,6 @@ getClockTime = do
   msec <- (#peek struct timeb,millitime) p_timeb :: IO CUShort
   return (TOD (fromIntegral sec) (fromIntegral msec * 1000{-ToDo: correct???-}))
 
-#elif defined(HAVE_GETTIMEOFDAY)
-getClockTime = do
-  allocaBytes (#const sizeof(struct timeval)) $ \ p_timeval -> do
-    throwErrnoIfMinus1_ "getClockTime" $ gettimeofday p_timeval nullPtr
-    sec  <- (#peek struct timeval,tv_sec)  p_timeval :: IO CLong
-    usec <- (#peek struct timeval,tv_usec) p_timeval :: IO CLong
-    return (TOD (fromIntegral sec) ((fromIntegral usec) * 1000))
 #else /* use POSIX time() */
 getClockTime = do
     secs <- time nullPtr -- can't fail, according to POSIX
@@ -327,11 +327,11 @@ normalizeTimeDiff td =
         }
 
 -- -----------------------------------------------------------------------------
--- toCalendarTime t converts t to a local time, modified by
--- the current timezone and daylight savings time settings.  toUTCTime
--- t converts t into UTC time.  toClockTime l converts l into the 
--- corresponding internal ClockTime.  The wday, yday, tzname, and isdst fields
--- are ignored.
+-- How do we deal with timezones on this architecture?
+
+-- The POSIX way to do it is through the global variable tzname[].
+-- But that's crap, so we do it The BSD Way if we can: namely use the
+-- tm_zone and tm_gmtoff fields of struct tm, if they're available.
 
 #if HAVE_TM_ZONE
 zone x      = (#peek struct tm,tm_zone) x   :: IO (Ptr CChar)
@@ -375,6 +375,13 @@ gmtoff x = do
 # endif /* ! HAVE_ALTZONE */
 #endif  /* ! HAVE_TM_ZONE */
 
+-- -----------------------------------------------------------------------------
+-- toCalendarTime t converts t to a local time, modified by
+-- the current timezone and daylight savings time settings.  toUTCTime
+-- t converts t into UTC time.  toClockTime l converts l into the 
+-- corresponding internal ClockTime.  The wday, yday, tzname, and isdst fields
+-- are ignored.
+
 
 toCalendarTime :: ClockTime -> IO CalendarTime
 toCalendarTime =  clockToCalendarTime localtime False
@@ -461,7 +468,7 @@ toClockTime (CalendarTime year mon mday hour min sec psec
         -- result.
         -- 
         gmtoff <- gmtoff p_tm
-       let res = fromIntegral t + tz + fromIntegral gmtoff
+       let res = fromIntegral t + tz - fromIntegral gmtoff
        return (TOD (fromIntegral res) 0)
 
 -- -----------------------------------------------------------------------------
@@ -609,12 +616,12 @@ foreign import unsafe strftime  :: Ptr CChar -> CSize -> Addr## -> Ptr CTm -> IO
 foreign import unsafe mktime    :: Ptr CTm   -> IO CTime
 foreign import unsafe time      :: Ptr CTime -> IO CTime
 
-#ifdef HAVE_GETTIMEOFDAY
+#if HAVE_GETTIMEOFDAY
 type CTimeVal = ()
 foreign import unsafe gettimeofday :: Ptr CTimeVal -> Ptr () -> IO CInt
 #endif
 
-#if defined(_WIN32) && !defined(cygwin32_TARGET_OS)
-type CTimeB
+#if HAVE_FTIME
+type CTimeB = ()
 foreign import unsafe ftime :: Ptr CTimeB -> IO CInt
 #endif