[commit: packages/time] master: getCurrentTimezone, with test (cb6d14e)
git at git.haskell.org
git at git.haskell.org
Sun Dec 20 07:46:03 UTC 2015
Repository : ssh://git@git.haskell.org/time
On branch : master
Link : http://git.haskell.org/packages/time.git/commitdiff/cb6d14eea0baae5259775481c8b7cc0b584b1219
>---------------------------------------------------------------
commit cb6d14eea0baae5259775481c8b7cc0b584b1219
Author: Ashley Yakeley <ashley at semantic.org>
Date: Sun Mar 20 22:37:22 2005 -0800
getCurrentTimezone, with test
darcs-hash:20050321063722-ac6dd-9792ff0e686b52fa1c9770058f77e6614445f6fb
>---------------------------------------------------------------
cb6d14eea0baae5259775481c8b7cc0b584b1219
CurrentTime.hs | 6 +++---
Makefile | 5 ++++-
System/Time/Calendar.hs | 16 +++++++++++++++-
timestuff.c | 11 +++++++++++
timestuff.h | 1 +
5 files changed, 34 insertions(+), 5 deletions(-)
diff --git a/CurrentTime.hs b/CurrentTime.hs
index 19e46c1..770699d 100644
--- a/CurrentTime.hs
+++ b/CurrentTime.hs
@@ -4,11 +4,11 @@ import System.Time.Clock
import System.Time.TAI
import System.Time.Calendar
-myzone :: TimeZone
-myzone = hoursToTimezone (- 8)
-
main :: IO ()
main = do
now <- getCurrentTime
putStrLn (show (utctDay now) ++ "," ++ show (utctDayTime now))
+ putStrLn (show (utcToCalendar utc now))
+ myzone <- getCurrentTimezone
+ putStrLn ("timezone minutes: " ++ show (timezoneToMinutes myzone))
putStrLn (show (utcToCalendar myzone now))
diff --git a/Makefile b/Makefile
index 82f4d8a..e29aaa5 100644
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,10 @@ TestTime: TestTime.o libTimeLib.a
CurrentTime: CurrentTime.o libTimeLib.a
ghc $^ -o $@
-libTimeLib.a: $(patsubst %.hs,%.o,$(SRCS))
+timestuff.o: timestuff.c timestuff.h
+ gcc -o $@ -c $<
+
+libTimeLib.a: $(patsubst %.hs,%.o,$(SRCS)) timestuff.o
rm -f $@
ar cru $@ $^
ranlib $@
diff --git a/System/Time/Calendar.hs b/System/Time/Calendar.hs
index 5cc646a..683e017 100644
--- a/System/Time/Calendar.hs
+++ b/System/Time/Calendar.hs
@@ -1,4 +1,4 @@
-{-# OPTIONS -Wall -Werror #-}
+{-# OPTIONS -ffi -Wall -Werror #-}
module System.Time.Calendar
(
@@ -6,6 +6,7 @@ module System.Time.Calendar
TimeZone,timezoneToMinutes,minutesToTimezone,hoursToTimezone,utc,
-- getting the locale time zone
+ getCurrentTimezone,
-- Gregorian "calendrical" format
TimeOfDay(..),CalendarDay(..),CalendarTime(..),
@@ -29,6 +30,9 @@ import System.Time.Clock
import Data.Fixed
import Data.Char
+import Foreign
+import Foreign.C
+
-- | count of minutes
newtype TimeZone = MkTimeZone {
timezoneToMinutes :: Int
@@ -44,6 +48,16 @@ hoursToTimezone i = minutesToTimezone (60 * i)
utc :: TimeZone
utc = minutesToTimezone 0
+foreign import ccall unsafe "timestuff.h get_current_timezone_seconds" get_current_timezone_seconds :: IO CLong
+
+-- | Get the current time-zone
+getCurrentTimezone :: IO TimeZone
+getCurrentTimezone = do
+ secs <- get_current_timezone_seconds
+ case secs of
+ 0x80000000 -> fail "localtime_r failed"
+ _ -> return (minutesToTimezone (div (fromIntegral secs) 60))
+
-- | time of day as represented in hour, minute and second (with picoseconds), typically used to express local time of day
data TimeOfDay = TimeOfDay {
todHour :: Int,
diff --git a/timestuff.c b/timestuff.c
new file mode 100644
index 0000000..79139bd
--- /dev/null
+++ b/timestuff.c
@@ -0,0 +1,11 @@
+#include <sys/time.h>
+
+long int get_current_timezone_seconds ()
+{
+ time_t t = 0;
+ struct tm tmd;
+ struct tm* ptm = localtime_r(&t,&tmd);
+ if (ptm)
+ return ptm -> tm_gmtoff;
+ else return 0x80000000;
+}
diff --git a/timestuff.h b/timestuff.h
new file mode 100644
index 0000000..f58c0f1
--- /dev/null
+++ b/timestuff.h
@@ -0,0 +1 @@
+long int get_current_timezone_seconds ();
More information about the ghc-commits
mailing list