[commit: packages/time] master: add name to Timezone (d028ced)

git at git.haskell.org git at git.haskell.org
Sun Dec 20 07:46:43 UTC 2015


Repository : ssh://git@git.haskell.org/time

On branch  : master
Link       : http://git.haskell.org/packages/time.git/commitdiff/d028cedc35e60de0e111a5c4318f0c41c46db52e

>---------------------------------------------------------------

commit d028cedc35e60de0e111a5c4318f0c41c46db52e
Author: Ashley Yakeley <ashley at semantic.org>
Date:   Wed May 4 04:16:42 2005 -0700

    add name to Timezone
    
    darcs-hash:20050504111642-ac6dd-aeb9239e546e584e7f6d027e7b3a70b87ea793f8


>---------------------------------------------------------------

d028cedc35e60de0e111a5c4318f0c41c46db52e
 System/Time/Calendar/Timezone.hs | 24 ++++++++++++++----------
 TestFormat.hs                    | 13 +++++++++----
 TestFormatStuff.c                |  3 ++-
 TestFormatStuff.h                |  2 +-
 timestuff.c                      |  3 ++-
 timestuff.h                      |  2 +-
 6 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/System/Time/Calendar/Timezone.hs b/System/Time/Calendar/Timezone.hs
index 87defcd..ec003dc 100644
--- a/System/Time/Calendar/Timezone.hs
+++ b/System/Time/Calendar/Timezone.hs
@@ -18,12 +18,13 @@ import Foreign.C
 
 -- | count of minutes
 data Timezone = MkTimezone {
+	timezoneMinutes :: Int,
 	timezoneDST :: Bool,
-	timezoneMinutes :: Int
+	timezoneName :: String
 } deriving (Eq,Ord)
 
 minutesToTimezone :: Int -> Timezone
-minutesToTimezone = MkTimezone False
+minutesToTimezone m = MkTimezone m False ""
 
 hoursToTimezone :: Int -> Timezone
 hoursToTimezone i = minutesToTimezone (60 * i)
@@ -32,32 +33,35 @@ showT :: Int -> String
 showT t = (show2 (div t 60)) ++ (show2 (mod t 60))
 
 instance Show Timezone where
-	show (MkTimezone _ t) | t < 0 = '-':(showT (negate t))
-	show (MkTimezone _ t) = '+':(showT t)
+	show (MkTimezone t _ _) | t < 0 = '-':(showT (negate t))
+	show (MkTimezone t _ _) = '+':(showT t)
 
 instance FormatTime Timezone where
 	formatCharacter _ 'z' zone = Just (show zone)
+	formatCharacter _ 'Z' (MkTimezone _ _ name) = Just name
 	formatCharacter _ _ _ = Nothing
 
 -- | The UTC time zone
 utc :: Timezone
-utc = minutesToTimezone 0
+utc = MkTimezone 0 False "UTC"
 
-foreign import ccall unsafe "timestuff.h get_current_timezone_seconds" get_current_timezone_seconds :: CTime -> Ptr CInt -> IO CLong
+foreign import ccall unsafe "timestuff.h get_current_timezone_seconds" get_current_timezone_seconds :: CTime -> Ptr CInt -> Ptr CString -> IO CLong
 
 posixToCTime :: POSIXTime -> CTime
 posixToCTime  = fromInteger . floor
 
 -- | Get the local time-zone for a given time (varying as per summertime adjustments)
 getTimezone :: UTCTime -> IO Timezone
-getTimezone time = with 0 (\pdst -> do
-	secs <- get_current_timezone_seconds (posixToCTime (utcTimeToPOSIXSeconds time)) pdst
+getTimezone time = with 0 (\pdst -> with nullPtr (\pcname -> do
+	secs <- get_current_timezone_seconds (posixToCTime (utcTimeToPOSIXSeconds time)) pdst pcname
 	case secs of
 		0x80000000 -> fail "localtime_r failed"
 		_ -> do
 			dst <- peek pdst
-			return (MkTimezone (dst == 1) (div (fromIntegral secs) 60))
-	)
+			cname <- peek pcname
+			name <- peekCString cname
+			return (MkTimezone (div (fromIntegral secs) 60) (dst == 1) name)
+	))
 
 -- | Get the current time-zone
 getCurrentTimezone :: IO Timezone
diff --git a/TestFormat.hs b/TestFormat.hs
index e3f2728..21a84f7 100644
--- a/TestFormat.hs
+++ b/TestFormat.hs
@@ -16,7 +16,7 @@ import Foreign.C
 	int isdst,int gmtoff,time_t t);
 -}
 
-foreign import ccall unsafe "TestFormatStuff.h format_time" format_time :: CString -> CSize -> CString -> CInt -> CInt -> CTime -> IO CSize
+foreign import ccall unsafe "TestFormatStuff.h format_time" format_time :: CString -> CSize -> CString -> CInt -> CInt -> CString -> CTime -> IO CSize
 
 withBuffer :: Int -> (CString -> IO CSize) -> IO String
 withBuffer n f = withArray (replicate n 0) (\buffer -> do
@@ -25,9 +25,14 @@ withBuffer n f = withArray (replicate n 0) (\buffer -> do
 		)
 
 unixFormatTime :: String -> Timezone -> UTCTime -> IO String
-unixFormatTime fmt zone time = withCString fmt (\pfmt ->
-		withBuffer 100 (\buffer -> format_time buffer 100 pfmt (if timezoneDST zone then 1 else 0) (fromIntegral (timezoneMinutes zone * 60)) (fromInteger (truncate (utcTimeToPOSIXSeconds time))))
-		)
+unixFormatTime fmt zone time = withCString fmt (\pfmt -> withCString (timezoneName zone) (\pzonename ->
+		withBuffer 100 (\buffer -> format_time buffer 100 pfmt
+				(if timezoneDST zone then 1 else 0)
+				(fromIntegral (timezoneMinutes zone * 60))
+				pzonename
+				(fromInteger (truncate (utcTimeToPOSIXSeconds time)))
+			)
+		))
 
 locale :: TimeLocale
 locale = defaultTimeLocale {dateTimeFmt = "%a %b %e %H:%M:%S %Y"}
diff --git a/TestFormatStuff.c b/TestFormatStuff.c
index 8450fb5..8d314ba 100644
--- a/TestFormatStuff.c
+++ b/TestFormatStuff.c
@@ -3,12 +3,13 @@
 size_t format_time (
 	char* buffer, size_t maxsize,
 	const char* format,
-	int isdst,int gmtoff,time_t t)
+	int isdst,int gmtoff,char* zonename,time_t t)
 {
 	t += gmtoff;
 	struct tm tmd;
 	gmtime_r(&t,&tmd);
 	tmd.tm_isdst = isdst;
 	tmd.tm_gmtoff = gmtoff;
+	tmd.tm_zone = zonename;
 	return strftime(buffer,maxsize,format,&tmd);
 }
diff --git a/TestFormatStuff.h b/TestFormatStuff.h
index 5f9e853..f2f7175 100644
--- a/TestFormatStuff.h
+++ b/TestFormatStuff.h
@@ -3,4 +3,4 @@
 size_t format_time (
 	char *s, size_t maxsize,
 	const char *format,
-	int isdst,int gmtoff,time_t t);
+	int isdst,int gmtoff,char* zonename,time_t t);
diff --git a/timestuff.c b/timestuff.c
index 6968a9d..386616e 100644
--- a/timestuff.c
+++ b/timestuff.c
@@ -1,12 +1,13 @@
 #include "timestuff.h"
 
-long int get_current_timezone_seconds (time_t t,int* dst)
+long int get_current_timezone_seconds (time_t t,int* dst,char** name)
 {
 	struct tm tmd;
 	struct tm* ptm = localtime_r(&t,&tmd);
 	if (ptm)
 	{
 		*dst = ptm -> tm_isdst;
+		*name = ptm -> tm_zone;
 		return ptm -> tm_gmtoff;
 	}
 	else return 0x80000000;
diff --git a/timestuff.h b/timestuff.h
index 6eaf614..936cd84 100644
--- a/timestuff.h
+++ b/timestuff.h
@@ -1,3 +1,3 @@
 #include <time.h>
 
-long int get_current_timezone_seconds (time_t,int* dst);
+long int get_current_timezone_seconds (time_t,int* dst,char** name);



More information about the ghc-commits mailing list