module Data.Time.Util where

import           Control.Applicative

import           Data.Function
import           Data.Monoid         (mempty)
import           Data.Monoid.Textual hiding (foldr, map)
import           Data.Time
import           Data.Time.Format    (ParseTime, defaultTimeLocale)


toString' :: (TextualMonoid t) => t -> String
toString' :: t -> String
toString' = (t -> String) -> t -> String
forall t. TextualMonoid t => (t -> String) -> t -> String
toString (String -> (Char -> String) -> Maybe Char -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"?" (Char -> String -> String
forall a. a -> [a] -> [a]
:[]) (Maybe Char -> String) -> (t -> Maybe Char) -> t -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
characterPrefix)

parseTimeUsing :: (TextualMonoid t, TextualMonoid t', ParseTime time) => [t] -> t' -> Maybe time
parseTimeUsing :: [t] -> t' -> Maybe time
parseTimeUsing [t]
formats t'
t = (Maybe time -> Maybe time -> Maybe time)
-> Maybe time -> [Maybe time] -> Maybe time
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Maybe time -> Maybe time -> Maybe time
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) Maybe time
forall a. Maybe a
Nothing ([Maybe time] -> Maybe time) -> [Maybe time] -> Maybe time
forall a b. (a -> b) -> a -> b
$ (t -> Maybe time) -> [t] -> [Maybe time]
forall a b. (a -> b) -> [a] -> [b]
map t -> Maybe time
forall (m :: * -> *) t t.
(MonadFail m, ParseTime t, TextualMonoid t) =>
t -> m t
parse [t]
formats
    where parse :: t -> m t
parse t
format = Bool -> TimeLocale -> String -> String -> m t
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
True TimeLocale
defaultTimeLocale (t -> String
forall t. TextualMonoid t => t -> String
toString' t
format) (t' -> String
forall t. TextualMonoid t => t -> String
toString' t'
t)