{-# LANGUAGE CPP #-}
module Data.Time.ISO8601
( formatISO8601
, formatISO8601Millis
, formatISO8601Micros
, formatISO8601Nanos
, formatISO8601Picos
, formatISO8601Javascript
, parseISO8601
) where
import Data.Time.Clock (UTCTime)
import Data.Time.Format (formatTime)
#if MIN_VERSION_time(1,5,0)
import Data.Time.Format (defaultTimeLocale, parseTimeM)
#else
import Data.Time.Format (parseTime)
import System.Locale (defaultTimeLocale)
#endif
import Control.Applicative ((<|>))
formatISO8601 :: UTCTime -> String
formatISO8601 :: UTCTime -> String
formatISO8601 UTCTime
t = TimeLocale -> String -> UTCTime -> String
forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%FT%T%QZ" UTCTime
t
formatPadded :: UTCTime -> String
formatPadded :: UTCTime -> String
formatPadded UTCTime
t
| String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
str Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
19 = String
str String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
".000000000000"
| Bool
otherwise = String
str String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"000000000000"
where
str :: String
str = TimeLocale -> String -> UTCTime -> String
forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%FT%T%Q" UTCTime
t
formatISO8601Millis :: UTCTime -> String
formatISO8601Millis :: UTCTime -> String
formatISO8601Millis UTCTime
t = Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
23 (UTCTime -> String
formatPadded UTCTime
t) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"Z"
formatISO8601Micros :: UTCTime -> String
formatISO8601Micros :: UTCTime -> String
formatISO8601Micros UTCTime
t = Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
26 (UTCTime -> String
formatPadded UTCTime
t) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"Z"
formatISO8601Nanos :: UTCTime -> String
formatISO8601Nanos :: UTCTime -> String
formatISO8601Nanos UTCTime
t = Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
29 (UTCTime -> String
formatPadded UTCTime
t) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"Z"
formatISO8601Picos :: UTCTime -> String
formatISO8601Picos :: UTCTime -> String
formatISO8601Picos UTCTime
t = Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
32 (UTCTime -> String
formatPadded UTCTime
t) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"Z"
formatISO8601Javascript :: UTCTime -> String
formatISO8601Javascript :: UTCTime -> String
formatISO8601Javascript = UTCTime -> String
formatISO8601Millis
parseISO8601 :: String -> Maybe UTCTime
#if MIN_VERSION_time(1,5,0)
parseISO8601 :: String -> Maybe UTCTime
parseISO8601 String
t = Bool -> TimeLocale -> String -> String -> Maybe UTCTime
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
True TimeLocale
defaultTimeLocale String
"%FT%T%QZ" String
t Maybe UTCTime -> Maybe UTCTime -> Maybe UTCTime
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
Bool -> TimeLocale -> String -> String -> Maybe UTCTime
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
True TimeLocale
defaultTimeLocale String
"%FT%T%Q%z" String
t
#else
parseISO8601 t = parseTime defaultTimeLocale "%FT%T%QZ" t <|>
parseTime defaultTimeLocale "%FT%T%Q%z" t
#endif