{-# 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 -> ByteString -> ByteString -> (UnixTime, Bool)
parseHelper CParser
parser ByteString
fmt ByteString
str = ()
localeReady () -> (UnixTime, Bool) -> (UnixTime, Bool)
forall a b. a -> b -> b
`seq` IO (UnixTime, Bool) -> (UnixTime, Bool)
forall a. IO a -> a
unsafePerformIO (
ByteString
-> (CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool)
forall a. ByteString -> (CString -> IO a) -> IO a
BS.useAsCString ByteString
fmt ((CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool))
-> (CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool)
forall a b. (a -> b) -> a -> b
$ \CString
cfmt ->
ByteString
-> (CString -> IO (UnixTime, Bool)) -> IO (UnixTime, Bool)
forall a. ByteString -> (CString -> IO a) -> IO a
BS.useAsCString ByteString
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 -> ByteString -> Bool
BS.notElem Char
'\0' ByteString
fmt Bool -> Bool -> Bool
&& Char -> ByteString -> Bool
BS.notElem Char
'\0' ByteString
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 :: ByteString -> ByteString -> UnixTime
parseUnixTime ByteString
fmt ByteString
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 -> ByteString -> ByteString -> (UnixTime, Bool)
parseHelper CParser
c_parse_unix_time ByteString
fmt ByteString
str
parseUnixTimeGMT :: Format -> ByteString -> UnixTime
parseUnixTimeGMT :: ByteString -> ByteString -> UnixTime
parseUnixTimeGMT ByteString
fmt ByteString
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 -> ByteString -> ByteString -> (UnixTime, Bool)
parseHelper CParser
c_parse_unix_time_gmt ByteString
fmt ByteString
str
parseUnixTimeMaybe :: Format -> ByteString -> Maybe UnixTime
parseUnixTimeMaybe :: ByteString -> ByteString -> Maybe UnixTime
parseUnixTimeMaybe ByteString
fmt ByteString
str = (UnixTime, Bool) -> Maybe UnixTime
toMaybe ((UnixTime, Bool) -> Maybe UnixTime)
-> (UnixTime, Bool) -> Maybe UnixTime
forall a b. (a -> b) -> a -> b
$ CParser -> ByteString -> ByteString -> (UnixTime, Bool)
parseHelper CParser
c_parse_unix_time ByteString
fmt ByteString
str
parseUnixTimeGMTMaybe :: Format -> ByteString -> Maybe UnixTime
parseUnixTimeGMTMaybe :: ByteString -> ByteString -> Maybe UnixTime
parseUnixTimeGMTMaybe ByteString
fmt ByteString
str = (UnixTime, Bool) -> Maybe UnixTime
toMaybe ((UnixTime, Bool) -> Maybe UnixTime)
-> (UnixTime, Bool) -> Maybe UnixTime
forall a b. (a -> b) -> a -> b
$ CParser -> ByteString -> ByteString -> (UnixTime, Bool)
parseHelper CParser
c_parse_unix_time_gmt ByteString
fmt ByteString
str
formatUnixTime :: Format -> UnixTime -> IO ByteString
formatUnixTime :: ByteString -> UnixTime -> IO ByteString
formatUnixTime ByteString
fmt UnixTime
t =
(CString -> CTime -> CString -> CInt -> IO CSize)
-> ByteString -> UnixTime -> IO ByteString
formatUnixTimeHelper CString -> CTime -> CString -> CInt -> IO CSize
c_format_unix_time ByteString
fmt UnixTime
t
{-# INLINE formatUnixTime #-}
formatUnixTimeGMT :: Format -> UnixTime -> ByteString
formatUnixTimeGMT :: ByteString -> UnixTime -> ByteString
formatUnixTimeGMT ByteString
fmt UnixTime
t =
IO ByteString -> ByteString
forall a. IO a -> a
unsafePerformIO (IO ByteString -> ByteString) -> IO ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ (CString -> CTime -> CString -> CInt -> IO CSize)
-> ByteString -> UnixTime -> IO ByteString
formatUnixTimeHelper CString -> CTime -> CString -> CInt -> IO CSize
c_format_unix_time_gmt ByteString
fmt UnixTime
t
{-# INLINE formatUnixTimeGMT #-}
formatUnixTimeHelper
:: (CString -> CTime -> CString -> CInt -> IO CSize)
-> Format
-> UnixTime
-> IO ByteString
formatUnixTimeHelper :: (CString -> CTime -> CString -> CInt -> IO CSize)
-> ByteString -> UnixTime -> IO ByteString
formatUnixTimeHelper CString -> CTime -> CString -> CInt -> IO CSize
formatFun ByteString
fmt (UnixTime CTime
sec Int32
_) =
()
localeReady () -> IO ByteString -> IO ByteString
forall a b. a -> b -> b
`seq` ByteString -> (CString -> IO ByteString) -> IO ByteString
forall a. ByteString -> (CString -> IO a) -> IO a
BS.useAsCString ByteString
fmt (\CString
cfmt -> CString -> Int -> IO ByteString
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 (ByteString -> Int
BS.length ByteString
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 ((ByteString -> Integer) -> [ByteString] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map (String -> Integer
forall a. Read a => String -> a
read (String -> Integer)
-> (ByteString -> String) -> ByteString -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
BS.unpack) [ByteString]
widths)
widths :: [ByteString]
widths = (ByteString -> Bool) -> [ByteString] -> [ByteString]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (ByteString -> Bool) -> ByteString -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Bool
BS.null) ([ByteString] -> [ByteString]) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> ByteString -> [ByteString]
BS.splitWith (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isDigit) ByteString
fmt
loop :: CString -> Int -> IO ByteString
loop CString
cfmt Int
siz = do
bs <- Int -> (Ptr Word8 -> IO Int) -> IO ByteString
createAndTrim Int
siz ((Ptr Word8 -> IO Int) -> IO ByteString)
-> (Ptr Word8 -> IO Int) -> IO ByteString
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 :: ByteString
webDateFormat = ByteString
"%a, %d %b %Y %H:%M:%S GMT"
mailDateFormat :: Format
mailDateFormat :: ByteString
mailDateFormat = ByteString
"%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