{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE OverloadedStrings #-}
module Data.UnixTime.Conv (
formatUnixTime,
formatUnixTimeGMT,
parseUnixTime,
parseUnixTimeGMT,
parseUnixTimeMaybe,
parseUnixTimeGMTMaybe,
webDateFormat,
mailDateFormat,
fromEpochTime,
toEpochTime
) where
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Internal (createAndTrim)
import Data.Char (isDigit)
import Data.UnixTime.Types
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal.Alloc (alloca)
import Foreign.Ptr (Ptr, castPtr)
import Foreign.Storable (peek)
import System.IO.Unsafe (unsafePerformIO)
import System.Posix.Types (EpochTime)
foreign import ccall unsafe "c_init_locale"
c_init_locale :: IO ()
foreign import ccall unsafe "c_parse_unix_time"
c_parse_unix_time :: CString -> CString -> Ptr CInt -> IO CTime
foreign import ccall unsafe "c_parse_unix_time_gmt"
c_parse_unix_time_gmt :: CString -> CString -> Ptr CInt -> IO CTime
foreign import ccall unsafe "c_format_unix_time"
c_format_unix_time :: CString -> CTime -> CString -> CInt -> IO CSize
foreign import ccall unsafe "c_format_unix_time_gmt"
c_format_unix_time_gmt :: CString -> CTime -> CString -> CInt -> IO CSize
localeReady :: ()
localeReady :: ()
localeReady = IO () -> ()
forall a. IO a -> a
unsafePerformIO IO ()
c_init_locale
{-# NOINLINE localeReady #-}
type CParser = CString -> CString -> Ptr CInt -> IO CTime
parseHelper :: CParser -> Format -> ByteString -> (UnixTime, Bool)
parseHelper :: CParser -> Format -> Format -> (UnixTime, Bool)
parseHelper CParser
parser Format
fmt Format
str = ()
localeReady () -> (UnixTime, Bool) -> (UnixTime, Bool)
forall a b. a -> b -> b
`seq` IO (UnixTime, Bool) -> (UnixTime, Bool)
forall a. IO a -> a
unsafePerformIO (
Format -> (CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool)
forall a. Format -> (CString -> IO a) -> IO a
BS.useAsCString Format
fmt ((CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool))
-> (CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool)
forall a b. (a -> b) -> a -> b
$ \CString
cfmt ->
Format -> (CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool)
forall a. Format -> (CString -> IO a) -> IO a
BS.useAsCString Format
str ((CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool))
-> (CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool)
forall a b. (a -> b) -> a -> b
$ \CString
cstr ->
(Ptr CInt -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CInt -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool))
-> (Ptr CInt -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool)
forall a b. (a -> b) -> a -> b
$ \Ptr CInt
pok -> do
sec <- CParser
parser CString
cfmt CString
cstr Ptr CInt
pok
ok <- peek pok
let complete = Char -> Format -> Bool
BS.notElem Char
'\0' Format
fmt Bool -> Bool -> Bool
&& Char -> Format -> Bool
BS.notElem Char
'\0' Format
str
return (UnixTime sec 0, ok /= 0 && complete))
toMaybe :: (UnixTime, Bool) -> Maybe UnixTime
toMaybe :: (UnixTime, Bool) -> Maybe UnixTime
toMaybe (UnixTime
ut, Bool
True) = UnixTime -> Maybe UnixTime
forall a. a -> Maybe a
Just UnixTime
ut
toMaybe (UnixTime, Bool)
_ = Maybe UnixTime
forall a. Maybe a
Nothing
parseUnixTime :: Format -> ByteString -> UnixTime
parseUnixTime :: Format -> Format -> UnixTime
parseUnixTime Format
fmt Format
str = (UnixTime, Bool) -> UnixTime
forall a b. (a, b) -> a
fst ((UnixTime, Bool) -> UnixTime) -> (UnixTime, Bool) -> UnixTime
forall a b. (a -> b) -> a -> b
$ CParser -> Format -> Format -> (UnixTime, Bool)
parseHelper CParser
c_parse_unix_time Format
fmt Format
str
parseUnixTimeGMT :: Format -> ByteString -> UnixTime
parseUnixTimeGMT :: Format -> Format -> UnixTime
parseUnixTimeGMT Format
fmt Format
str = (UnixTime, Bool) -> UnixTime
forall a b. (a, b) -> a
fst ((UnixTime, Bool) -> UnixTime) -> (UnixTime, Bool) -> UnixTime
forall a b. (a -> b) -> a -> b
$ CParser -> Format -> Format -> (UnixTime, Bool)
parseHelper CParser
c_parse_unix_time_gmt Format
fmt Format
str
parseUnixTimeMaybe :: Format -> ByteString -> Maybe UnixTime
parseUnixTimeMaybe :: Format -> Format -> Maybe UnixTime
parseUnixTimeMaybe Format
fmt Format
str = (UnixTime, Bool) -> Maybe UnixTime
toMaybe ((UnixTime, Bool) -> Maybe UnixTime)
-> (UnixTime, Bool) -> Maybe UnixTime
forall a b. (a -> b) -> a -> b
$ CParser -> Format -> Format -> (UnixTime, Bool)
parseHelper CParser
c_parse_unix_time Format
fmt Format
str
parseUnixTimeGMTMaybe :: Format -> ByteString -> Maybe UnixTime
parseUnixTimeGMTMaybe :: Format -> Format -> Maybe UnixTime
parseUnixTimeGMTMaybe Format
fmt Format
str = (UnixTime, Bool) -> Maybe UnixTime
toMaybe ((UnixTime, Bool) -> Maybe UnixTime)
-> (UnixTime, Bool) -> Maybe UnixTime
forall a b. (a -> b) -> a -> b
$ CParser -> Format -> Format -> (UnixTime, Bool)
parseHelper CParser
c_parse_unix_time_gmt Format
fmt Format
str
formatUnixTime :: Format -> UnixTime -> IO ByteString
formatUnixTime :: Format -> UnixTime -> IO Format
formatUnixTime Format
fmt UnixTime
t =
(CString -> CTime -> CString -> CInt -> IO CSize)
-> Format -> UnixTime -> IO Format
formatUnixTimeHelper CString -> CTime -> CString -> CInt -> IO CSize
c_format_unix_time Format
fmt UnixTime
t
{-# INLINE formatUnixTime #-}
formatUnixTimeGMT :: Format -> UnixTime -> ByteString
formatUnixTimeGMT :: Format -> UnixTime -> Format
formatUnixTimeGMT Format
fmt UnixTime
t =
IO Format -> Format
forall a. IO a -> a
unsafePerformIO (IO Format -> Format) -> IO Format -> Format
forall a b. (a -> b) -> a -> b
$ (CString -> CTime -> CString -> CInt -> IO CSize)
-> Format -> UnixTime -> IO Format
formatUnixTimeHelper CString -> CTime -> CString -> CInt -> IO CSize
c_format_unix_time_gmt Format
fmt UnixTime
t
{-# INLINE formatUnixTimeGMT #-}
formatUnixTimeHelper
:: (CString -> CTime -> CString -> CInt -> IO CSize)
-> Format
-> UnixTime
-> IO ByteString
formatUnixTimeHelper :: (CString -> CTime -> CString -> CInt -> IO CSize)
-> Format -> UnixTime -> IO Format
formatUnixTimeHelper CString -> CTime -> CString -> CInt -> IO CSize
formatFun Format
fmt (UnixTime CTime
sec Int32
_) =
()
localeReady () -> IO Format -> IO Format
forall a b. a -> b -> b
`seq` Format -> (CString -> IO Format) -> IO Format
forall a. Format -> (CString -> IO a) -> IO a
BS.useAsCString Format
fmt (\CString
cfmt -> CString -> Int -> IO Format
loop CString
cfmt Int
80)
where
limit :: Int
limit :: Int
limit = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
min (Integer
1024 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
1024) (Integer -> Integer) -> Integer -> Integer
forall a b. (a -> b) -> a -> b
$
Integer
80 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
64 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Format -> Int
BS.length Format
fmt) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Format -> Integer) -> [Format] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map (String -> Integer
forall a. Read a => String -> a
read (String -> Integer) -> (Format -> String) -> Format -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Format -> String
BS.unpack) [Format]
widths)
widths :: [Format]
widths = (Format -> Bool) -> [Format] -> [Format]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Format -> Bool) -> Format -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Format -> Bool
BS.null) ([Format] -> [Format]) -> [Format] -> [Format]
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Format -> [Format]
BS.splitWith (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isDigit) Format
fmt
loop :: CString -> Int -> IO Format
loop CString
cfmt Int
siz = do
bs <- Int -> (Ptr Word8 -> IO Int) -> IO Format
createAndTrim Int
siz ((Ptr Word8 -> IO Int) -> IO Format)
-> (Ptr Word8 -> IO Int) -> IO Format
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr ->
CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> IO CSize -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CString -> CTime -> CString -> CInt -> IO CSize
formatFun CString
cfmt CTime
sec (Ptr Word8 -> CString
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
ptr) (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
siz)
if BS.null bs && siz < limit
then loop cfmt (siz * 2)
else return bs
webDateFormat :: Format
webDateFormat :: Format
webDateFormat = Format
"%a, %d %b %Y %H:%M:%S GMT"
mailDateFormat :: Format
mailDateFormat :: Format
mailDateFormat = Format
"%a, %d %b %Y %H:%M:%S %z"
fromEpochTime :: EpochTime -> UnixTime
fromEpochTime :: CTime -> UnixTime
fromEpochTime CTime
sec = CTime -> Int32 -> UnixTime
UnixTime CTime
sec Int32
0
toEpochTime :: UnixTime -> EpochTime
toEpochTime :: UnixTime -> CTime
toEpochTime (UnixTime CTime
sec Int32
_) = CTime
sec