Permalink
Browse files

Add IO variant and posixToUTC

It's useful to expose an IO variant for "now" and "yesterday", etc,
though the pure variant will *usually* be safe for these, in practical
cases.  Absolute timestamps are always safe.

Instead of UTCTime-returning variants of approxidate, expose a
posixToUTC converter.

Update documentation to reflect the safety of the IO/non-IO variants.
  • Loading branch information...
1 parent dcb58a1 commit 6ad03f32c40a05e16cbe7fe4cc7703f33ee4ab9c @singpolyma committed May 12, 2012
Showing with 19 additions and 14 deletions.
  1. +19 −14 Data/Time/Git.hs
View
@@ -1,6 +1,6 @@
{-# LANGUAGE ForeignFunctionInterface #-}
-module Data.Time.Git (approxidate, approxidateUTC) where
+module Data.Time.Git (approxidate, io_approxidate, posixToUTC) where
import Foreign
import Foreign.C.Types
@@ -12,25 +12,30 @@ import Data.Time.Clock.POSIX
import qualified Data.ByteString as BS
import qualified Data.ByteString.UTF8 as BS (fromString)
+foreign import ccall unsafe "date.c approxidate_careful"
+ c_approxidate_careful ::
+ CString -> Ptr CInt ->
+ IO CULong
+
-- | Parse a date/time string and return Just a Unix timestamp.
-- Return Nothing if the string could not be interpreted.
-- If no timezone is in the string, the local timezone is used.
+-- This is not in IO and you cannot safely ask it to parse strings like
+-- \"now\" and \"yesterday\".
approxidate :: String -> Maybe Integer
-approxidate = unsafePerformIO . io_approxidate_careful
+approxidate = unsafePerformIO . io_approxidate
--- | The same as approxidate, but returns a UTCTime instead of a raw
--- timestamp.
-approxidateUTC :: String -> Maybe UTCTime
-approxidateUTC input =
- fmap (posixSecondsToUTCTime . realToFrac) (approxidate input)
+-- | Convert a Unix timestamp to a UTCTime
+posixToUTC :: Integer -> UTCTime
+posixToUTC = posixSecondsToUTCTime . realToFrac
-foreign import ccall unsafe "date.c approxidate_careful"
- c_approxidate_careful ::
- CString -> Ptr CInt ->
- IO CULong
-
-io_approxidate_careful :: String -> IO (Maybe Integer)
-io_approxidate_careful input =
+-- | Parse a date/time string and return Just a Unix timestamp.
+-- Return Nothing if the string could not be interpreted.
+-- If no timezone is in the string, the local timezone is used.
+-- This is in IO so that you can safely ask it to parse strings like
+-- \"now\" and \"yesterday\".
+io_approxidate :: String -> IO (Maybe Integer)
+io_approxidate input =
BS.useAsCString (BS.fromString input) (\c_input ->
alloca (\c_error_ret -> do
poke c_error_ret 0

0 comments on commit 6ad03f3

Please sign in to comment.