{-# LANGUAGE Safe #-}
{-# OPTIONS -fno-warn-orphans #-}
module Data.Time.Format.Parse.Instances (
) where
import Control.Applicative ((<|>))
import Data.Char
import Data.Fixed
import Data.List (elemIndex, find)
import Data.Ratio
import Data.Time.Calendar.CalendarDiffDays
import Data.Time.Calendar.Days
import Data.Time.Calendar.Gregorian
import Data.Time.Calendar.Month
import Data.Time.Calendar.OrdinalDate
import Data.Time.Calendar.Private (clipValid)
import Data.Time.Calendar.WeekDate
import Data.Time.Clock.Internal.DiffTime
import Data.Time.Clock.Internal.NominalDiffTime
import Data.Time.Clock.Internal.UTCTime
import Data.Time.Clock.Internal.UniversalTime
import Data.Time.Clock.POSIX
import Data.Time.Format.Locale
import Data.Time.Format.Parse.Class
import Data.Time.LocalTime.Internal.CalendarDiffTime
import Data.Time.LocalTime.Internal.LocalTime
import Data.Time.LocalTime.Internal.TimeOfDay
import Data.Time.LocalTime.Internal.TimeZone
import Data.Time.LocalTime.Internal.ZonedTime
import Data.Traversable
import Text.Read (readMaybe)
data DayComponent
= DCCentury Integer
| DCCenturyYear Integer
| DCYearMonth MonthOfYear
| DCMonthDay DayOfMonth
| DCYearDay DayOfYear
| DCWeekDay Int
| DCYearWeek
WeekType
WeekOfYear
data WeekType
= ISOWeek
| SundayWeek
| MondayWeek
makeDayComponent :: TimeLocale -> Char -> String -> Maybe [DayComponent]
makeDayComponent :: TimeLocale -> Char -> String -> Maybe [DayComponent]
makeDayComponent TimeLocale
l Char
c String
x =
let ra :: (Read a) => Maybe a
ra :: Maybe a
ra = String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe String
x
zeroBasedListIndex :: [String] -> Maybe Int
zeroBasedListIndex :: [String] -> Maybe Int
zeroBasedListIndex [String]
ss = String -> [String] -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
elemIndex ((Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toUpper String
x) ([String] -> Maybe Int) -> [String] -> Maybe Int
forall a b. (a -> b) -> a -> b
$ (String -> String) -> [String] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toUpper) [String]
ss
oneBasedListIndex :: [String] -> Maybe Int
oneBasedListIndex :: [String] -> Maybe Int
oneBasedListIndex [String]
ss = do
Int
index <- [String] -> Maybe Int
zeroBasedListIndex [String]
ss
Int -> Maybe Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
index
in case Char
c of
Char
'C' -> do
Integer
a <- Maybe Integer
forall a. Read a => Maybe a
ra
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Integer -> DayComponent
DCCentury Integer
a]
Char
'f' -> do
Integer
a <- Maybe Integer
forall a. Read a => Maybe a
ra
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Integer -> DayComponent
DCCentury Integer
a]
Char
'Y' -> do
Integer
a <- Maybe Integer
forall a. Read a => Maybe a
ra
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Integer -> DayComponent
DCCentury (Integer
a Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
100), Integer -> DayComponent
DCCenturyYear (Integer
a Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
100)]
Char
'G' -> do
Integer
a <- Maybe Integer
forall a. Read a => Maybe a
ra
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Integer -> DayComponent
DCCentury (Integer
a Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
100), Integer -> DayComponent
DCCenturyYear (Integer
a Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
100)]
Char
'y' -> do
Integer
a <- Maybe Integer
forall a. Read a => Maybe a
ra
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Integer -> DayComponent
DCCenturyYear Integer
a]
Char
'g' -> do
Integer
a <- Maybe Integer
forall a. Read a => Maybe a
ra
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Integer -> DayComponent
DCCenturyYear Integer
a]
Char
'B' -> do
Int
a <- [String] -> Maybe Int
oneBasedListIndex ([String] -> Maybe Int) -> [String] -> Maybe Int
forall a b. (a -> b) -> a -> b
$ ((String, String) -> String) -> [(String, String)] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String, String) -> String
forall a b. (a, b) -> a
fst ([(String, String)] -> [String]) -> [(String, String)] -> [String]
forall a b. (a -> b) -> a -> b
$ TimeLocale -> [(String, String)]
months TimeLocale
l
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCYearMonth Int
a]
Char
'b' -> do
Int
a <- [String] -> Maybe Int
oneBasedListIndex ([String] -> Maybe Int) -> [String] -> Maybe Int
forall a b. (a -> b) -> a -> b
$ ((String, String) -> String) -> [(String, String)] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String, String) -> String
forall a b. (a, b) -> b
snd ([(String, String)] -> [String]) -> [(String, String)] -> [String]
forall a b. (a -> b) -> a -> b
$ TimeLocale -> [(String, String)]
months TimeLocale
l
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCYearMonth Int
a]
Char
'm' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
1 Int
12 Int
raw
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCYearMonth Int
a]
Char
'd' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
1 Int
31 Int
raw
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCMonthDay Int
a]
Char
'e' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
1 Int
31 Int
raw
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCMonthDay Int
a]
Char
'V' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
1 Int
53 Int
raw
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [WeekType -> Int -> DayComponent
DCYearWeek WeekType
ISOWeek Int
a]
Char
'U' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
0 Int
53 Int
raw
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [WeekType -> Int -> DayComponent
DCYearWeek WeekType
SundayWeek Int
a]
Char
'W' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
0 Int
53 Int
raw
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [WeekType -> Int -> DayComponent
DCYearWeek WeekType
MondayWeek Int
a]
Char
'u' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
1 Int
7 Int
raw
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCWeekDay Int
a]
Char
'a' -> do
Int
a' <- [String] -> Maybe Int
zeroBasedListIndex ([String] -> Maybe Int) -> [String] -> Maybe Int
forall a b. (a -> b) -> a -> b
$ ((String, String) -> String) -> [(String, String)] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String, String) -> String
forall a b. (a, b) -> b
snd ([(String, String)] -> [String]) -> [(String, String)] -> [String]
forall a b. (a -> b) -> a -> b
$ TimeLocale -> [(String, String)]
wDays TimeLocale
l
let a :: Int
a =
if Int
a' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Int
7
else Int
a'
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCWeekDay Int
a]
Char
'A' -> do
Int
a' <- [String] -> Maybe Int
zeroBasedListIndex ([String] -> Maybe Int) -> [String] -> Maybe Int
forall a b. (a -> b) -> a -> b
$ ((String, String) -> String) -> [(String, String)] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String, String) -> String
forall a b. (a, b) -> a
fst ([(String, String)] -> [String]) -> [(String, String)] -> [String]
forall a b. (a -> b) -> a -> b
$ TimeLocale -> [(String, String)]
wDays TimeLocale
l
let a :: Int
a =
if Int
a' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Int
7
else Int
a'
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCWeekDay Int
a]
Char
'w' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a' <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
0 Int
6 Int
raw
let a :: Int
a =
if Int
a' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Int
7
else Int
a'
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCWeekDay Int
a]
Char
'j' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
1 Int
366 Int
raw
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int -> DayComponent
DCYearDay Int
a]
Char
_ -> [DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return []
makeDayComponents :: TimeLocale -> [(Char, String)] -> Maybe [DayComponent]
makeDayComponents :: TimeLocale -> [(Char, String)] -> Maybe [DayComponent]
makeDayComponents TimeLocale
l [(Char, String)]
pairs = do
[[DayComponent]]
components <- [(Char, String)]
-> ((Char, String) -> Maybe [DayComponent])
-> Maybe [[DayComponent]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [(Char, String)]
pairs (((Char, String) -> Maybe [DayComponent])
-> Maybe [[DayComponent]])
-> ((Char, String) -> Maybe [DayComponent])
-> Maybe [[DayComponent]]
forall a b. (a -> b) -> a -> b
$ \(Char
c, String
x) -> TimeLocale -> Char -> String -> Maybe [DayComponent]
makeDayComponent TimeLocale
l Char
c String
x
[DayComponent] -> Maybe [DayComponent]
forall (m :: * -> *) a. Monad m => a -> m a
return ([DayComponent] -> Maybe [DayComponent])
-> [DayComponent] -> Maybe [DayComponent]
forall a b. (a -> b) -> a -> b
$ [[DayComponent]] -> [DayComponent]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[DayComponent]]
components
safeLast :: a -> [a] -> a
safeLast :: a -> [a] -> a
safeLast a
x [a]
xs = [a] -> a
forall a. [a] -> a
last (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs)
instance ParseTime Day where
substituteTimeSpecifier :: Proxy Day -> TimeLocale -> Char -> Maybe String
substituteTimeSpecifier Proxy Day
_ = TimeLocale -> Char -> Maybe String
timeSubstituteTimeSpecifier
parseTimeSpecifier :: Proxy Day
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy Day
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
timeParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe Day
buildTime TimeLocale
l [(Char, String)]
pairs = do
[DayComponent]
cs <- TimeLocale -> [(Char, String)] -> Maybe [DayComponent]
makeDayComponents TimeLocale
l [(Char, String)]
pairs
let y :: Integer
y =
let d :: Integer
d = Integer -> [Integer] -> Integer
forall a. a -> [a] -> a
safeLast Integer
70 [Integer
x | DCCenturyYear Integer
x <- [DayComponent]
cs]
c :: Integer
c =
Integer -> [Integer] -> Integer
forall a. a -> [a] -> a
safeLast
( if Integer
d Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
69
then Integer
19
else Integer
20
)
[Integer
x | DCCentury Integer
x <- [DayComponent]
cs]
in Integer
100 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
c Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
d
rest :: [DayComponent] -> Maybe Day
rest (DCYearMonth Int
m : [DayComponent]
_) =
let d :: Int
d = Int -> [Int] -> Int
forall a. a -> [a] -> a
safeLast Int
1 [Int
x | DCMonthDay Int
x <- [DayComponent]
cs]
in Integer -> Int -> Int -> Maybe Day
fromGregorianValid Integer
y Int
m Int
d
rest (DCYearDay Int
d : [DayComponent]
_) = Integer -> Int -> Maybe Day
fromOrdinalDateValid Integer
y Int
d
rest (DCYearWeek WeekType
wt Int
w : [DayComponent]
_) =
let d :: Int
d = Int -> [Int] -> Int
forall a. a -> [a] -> a
safeLast Int
4 [Int
x | DCWeekDay Int
x <- [DayComponent]
cs]
in case WeekType
wt of
WeekType
ISOWeek -> Integer -> Int -> Int -> Maybe Day
fromWeekDateValid Integer
y Int
w Int
d
WeekType
SundayWeek -> Integer -> Int -> Int -> Maybe Day
fromSundayStartWeekValid Integer
y Int
w (Int
d Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
7)
WeekType
MondayWeek -> Integer -> Int -> Int -> Maybe Day
fromMondayStartWeekValid Integer
y Int
w Int
d
rest (DayComponent
_ : [DayComponent]
xs) = [DayComponent] -> Maybe Day
rest [DayComponent]
xs
rest [] = [DayComponent] -> Maybe Day
rest [Int -> DayComponent
DCYearMonth Int
1]
[DayComponent] -> Maybe Day
rest [DayComponent]
cs
instance ParseTime Month where
substituteTimeSpecifier :: Proxy Month -> TimeLocale -> Char -> Maybe String
substituteTimeSpecifier Proxy Month
_ = TimeLocale -> Char -> Maybe String
timeSubstituteTimeSpecifier
parseTimeSpecifier :: Proxy Month
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy Month
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
timeParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe Month
buildTime TimeLocale
l [(Char, String)]
pairs = do
[DayComponent]
cs <- TimeLocale -> [(Char, String)] -> Maybe [DayComponent]
makeDayComponents TimeLocale
l [(Char, String)]
pairs
let y :: Integer
y =
let d :: Integer
d = Integer -> [Integer] -> Integer
forall a. a -> [a] -> a
safeLast Integer
70 [Integer
x | DCCenturyYear Integer
x <- [DayComponent]
cs]
c :: Integer
c =
Integer -> [Integer] -> Integer
forall a. a -> [a] -> a
safeLast
( if Integer
d Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
69
then Integer
19
else Integer
20
)
[Integer
x | DCCentury Integer
x <- [DayComponent]
cs]
in Integer
100 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
c Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
d
rest :: [DayComponent] -> Maybe Month
rest (DCYearMonth Int
m : [DayComponent]
_) = Integer -> Int -> Maybe Month
fromYearMonthValid Integer
y Int
m
rest (DayComponent
_ : [DayComponent]
xs) = [DayComponent] -> Maybe Month
rest [DayComponent]
xs
rest [] = Integer -> Int -> Maybe Month
fromYearMonthValid Integer
y Int
1
[DayComponent] -> Maybe Month
rest [DayComponent]
cs
mfoldl :: (Monad m) => (a -> b -> m a) -> m a -> [b] -> m a
mfoldl :: (a -> b -> m a) -> m a -> [b] -> m a
mfoldl a -> b -> m a
f =
let mf :: m a -> b -> m a
mf m a
ma b
b = do
a
a <- m a
ma
a -> b -> m a
f a
a b
b
in (m a -> b -> m a) -> m a -> [b] -> m a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl m a -> b -> m a
mf
instance ParseTime TimeOfDay where
substituteTimeSpecifier :: Proxy TimeOfDay -> TimeLocale -> Char -> Maybe String
substituteTimeSpecifier Proxy TimeOfDay
_ = TimeLocale -> Char -> Maybe String
timeSubstituteTimeSpecifier
parseTimeSpecifier :: Proxy TimeOfDay
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy TimeOfDay
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
timeParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe TimeOfDay
buildTime TimeLocale
l =
let f :: TimeOfDay -> (Char, String) -> Maybe TimeOfDay
f t :: TimeOfDay
t@(TimeOfDay Int
h Int
m Pico
s) (Char
c, String
x) =
let ra :: (Read a) => Maybe a
ra :: Maybe a
ra = String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe String
x
getAmPm :: Maybe TimeOfDay
getAmPm =
let upx :: String
upx = (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toUpper String
x
(String
amStr, String
pmStr) = TimeLocale -> (String, String)
amPm TimeLocale
l
in if String
upx String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
amStr
then TimeOfDay -> Maybe TimeOfDay
forall a. a -> Maybe a
Just (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay (Int
h Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
12) Int
m Pico
s
else
if String
upx String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
pmStr
then
TimeOfDay -> Maybe TimeOfDay
forall a. a -> Maybe a
Just (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$
Int -> Int -> Pico -> TimeOfDay
TimeOfDay
( if Int
h Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
12
then Int
h Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
12
else Int
h
)
Int
m
Pico
s
else Maybe TimeOfDay
forall a. Maybe a
Nothing
in case Char
c of
Char
'P' -> Maybe TimeOfDay
getAmPm
Char
'p' -> Maybe TimeOfDay
getAmPm
Char
'H' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
0 Int
23 Int
raw
TimeOfDay -> Maybe TimeOfDay
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
a Int
m Pico
s
Char
'I' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
1 Int
12 Int
raw
TimeOfDay -> Maybe TimeOfDay
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
a Int
m Pico
s
Char
'k' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
0 Int
23 Int
raw
TimeOfDay -> Maybe TimeOfDay
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
a Int
m Pico
s
Char
'l' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
1 Int
12 Int
raw
TimeOfDay -> Maybe TimeOfDay
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
a Int
m Pico
s
Char
'M' -> do
Int
raw <- Maybe Int
forall a. Read a => Maybe a
ra
Int
a <- Int -> Int -> Int -> Maybe Int
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
0 Int
59 Int
raw
TimeOfDay -> Maybe TimeOfDay
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
h Int
a Pico
s
Char
'S' -> do
Integer
raw <- Maybe Integer
forall a. Read a => Maybe a
ra
Integer
a <- Integer -> Integer -> Integer -> Maybe Integer
forall t. Ord t => t -> t -> t -> Maybe t
clipValid Integer
0 Integer
60 Integer
raw
TimeOfDay -> Maybe TimeOfDay
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
h Int
m (Integer -> Pico
forall a. Num a => Integer -> a
fromInteger Integer
a)
Char
'q' -> do
Integer
ps <- (String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Integer) -> String -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
12 (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Int -> Char -> String -> String
forall a. Int -> a -> [a] -> [a]
rpad Int
12 Char
'0' String
x) Maybe Integer -> Maybe Integer -> Maybe Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Integer -> Maybe Integer
forall (m :: * -> *) a. Monad m => a -> m a
return Integer
0
TimeOfDay -> Maybe TimeOfDay
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
h Int
m (Integer -> Integer -> Pico
mkPico (Pico -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
floor Pico
s) Integer
ps)
Char
'Q' ->
if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
x
then TimeOfDay -> Maybe TimeOfDay
forall a. a -> Maybe a
Just TimeOfDay
t
else do
Integer
ps <- (String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Integer) -> String -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
12 (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Int -> Char -> String -> String
forall a. Int -> a -> [a] -> [a]
rpad Int
12 Char
'0' String
x) Maybe Integer -> Maybe Integer -> Maybe Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Integer -> Maybe Integer
forall (m :: * -> *) a. Monad m => a -> m a
return Integer
0
TimeOfDay -> Maybe TimeOfDay
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
h Int
m (Integer -> Integer -> Pico
mkPico (Pico -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
floor Pico
s) Integer
ps)
Char
_ -> TimeOfDay -> Maybe TimeOfDay
forall a. a -> Maybe a
Just TimeOfDay
t
in (TimeOfDay -> (Char, String) -> Maybe TimeOfDay)
-> Maybe TimeOfDay -> [(Char, String)] -> Maybe TimeOfDay
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> m a -> [b] -> m a
mfoldl TimeOfDay -> (Char, String) -> Maybe TimeOfDay
f (TimeOfDay -> Maybe TimeOfDay
forall a. a -> Maybe a
Just TimeOfDay
midnight)
rpad :: Int -> a -> [a] -> [a]
rpad :: Int -> a -> [a] -> [a]
rpad Int
n a
c [a]
xs = [a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Int -> a -> [a]
forall a. Int -> a -> [a]
replicate (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) a
c
mkPico :: Integer -> Integer -> Pico
mkPico :: Integer -> Integer -> Pico
mkPico Integer
i Integer
f = Integer -> Pico
forall a. Num a => Integer -> a
fromInteger Integer
i Pico -> Pico -> Pico
forall a. Num a => a -> a -> a
+ Rational -> Pico
forall a. Fractional a => Rational -> a
fromRational (Integer
f Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
1000000000000)
instance ParseTime LocalTime where
substituteTimeSpecifier :: Proxy LocalTime -> TimeLocale -> Char -> Maybe String
substituteTimeSpecifier Proxy LocalTime
_ = TimeLocale -> Char -> Maybe String
timeSubstituteTimeSpecifier
parseTimeSpecifier :: Proxy LocalTime
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy LocalTime
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
timeParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe LocalTime
buildTime TimeLocale
l [(Char, String)]
xs = Day -> TimeOfDay -> LocalTime
LocalTime (Day -> TimeOfDay -> LocalTime)
-> Maybe Day -> Maybe (TimeOfDay -> LocalTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TimeLocale -> [(Char, String)] -> Maybe Day
forall t. ParseTime t => TimeLocale -> [(Char, String)] -> Maybe t
buildTime TimeLocale
l [(Char, String)]
xs) Maybe (TimeOfDay -> LocalTime)
-> Maybe TimeOfDay -> Maybe LocalTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (TimeLocale -> [(Char, String)] -> Maybe TimeOfDay
forall t. ParseTime t => TimeLocale -> [(Char, String)] -> Maybe t
buildTime TimeLocale
l [(Char, String)]
xs)
enumDiff :: (Enum a) => a -> a -> Int
enumDiff :: a -> a -> Int
enumDiff a
a a
b = (a -> Int
forall a. Enum a => a -> Int
fromEnum a
a) Int -> Int -> Int
forall a. Num a => a -> a -> a
- (a -> Int
forall a. Enum a => a -> Int
fromEnum a
b)
getMilZoneHours :: Char -> Maybe Int
getMilZoneHours :: Char -> Maybe Int
getMilZoneHours Char
c
| Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
< Char
'A' = Maybe Int
forall a. Maybe a
Nothing
getMilZoneHours Char
c
| Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'I' = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Char -> Int
forall a. Enum a => a -> a -> Int
enumDiff Char
c Char
'A'
getMilZoneHours Char
'J' = Maybe Int
forall a. Maybe a
Nothing
getMilZoneHours Char
c
| Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'M' = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Int
10 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Char -> Int
forall a. Enum a => a -> a -> Int
enumDiff Char
c Char
'K'
getMilZoneHours Char
c
| Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'Y' = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ (Char -> Char -> Int
forall a. Enum a => a -> a -> Int
enumDiff Char
'N' Char
c) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
getMilZoneHours Char
'Z' = Int -> Maybe Int
forall a. a -> Maybe a
Just Int
0
getMilZoneHours Char
_ = Maybe Int
forall a. Maybe a
Nothing
getMilZone :: Char -> Maybe TimeZone
getMilZone :: Char -> Maybe TimeZone
getMilZone Char
c =
let yc :: Char
yc = Char -> Char
toUpper Char
c
in do
Int
hours <- Char -> Maybe Int
getMilZoneHours Char
yc
TimeZone -> Maybe TimeZone
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeZone -> Maybe TimeZone) -> TimeZone -> Maybe TimeZone
forall a b. (a -> b) -> a -> b
$ Int -> Bool -> String -> TimeZone
TimeZone (Int
hours Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
60) Bool
False [Char
yc]
getKnownTimeZone :: TimeLocale -> String -> Maybe TimeZone
getKnownTimeZone :: TimeLocale -> String -> Maybe TimeZone
getKnownTimeZone TimeLocale
locale String
x = (TimeZone -> Bool) -> [TimeZone] -> Maybe TimeZone
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\TimeZone
tz -> (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toUpper String
x String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== TimeZone -> String
timeZoneName TimeZone
tz) (TimeLocale -> [TimeZone]
knownTimeZones TimeLocale
locale)
instance ParseTime TimeZone where
substituteTimeSpecifier :: Proxy TimeZone -> TimeLocale -> Char -> Maybe String
substituteTimeSpecifier Proxy TimeZone
_ = TimeLocale -> Char -> Maybe String
timeSubstituteTimeSpecifier
parseTimeSpecifier :: Proxy TimeZone
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy TimeZone
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
timeParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe TimeZone
buildTime TimeLocale
l =
let f :: Char -> String -> TimeZone -> Maybe TimeZone
f :: Char -> String -> TimeZone -> Maybe TimeZone
f Char
'z' String
str (TimeZone Int
_ Bool
dst String
name)
| Just Int
offset <- String -> Maybe Int
readTzOffset String
str = TimeZone -> Maybe TimeZone
forall a. a -> Maybe a
Just (TimeZone -> Maybe TimeZone) -> TimeZone -> Maybe TimeZone
forall a b. (a -> b) -> a -> b
$ Int -> Bool -> String -> TimeZone
TimeZone Int
offset Bool
dst String
name
f Char
'z' String
_ TimeZone
_ = Maybe TimeZone
forall a. Maybe a
Nothing
f Char
'Z' String
str TimeZone
_
| Just Int
offset <- String -> Maybe Int
readTzOffset String
str = TimeZone -> Maybe TimeZone
forall a. a -> Maybe a
Just (TimeZone -> Maybe TimeZone) -> TimeZone -> Maybe TimeZone
forall a b. (a -> b) -> a -> b
$ Int -> Bool -> String -> TimeZone
TimeZone Int
offset Bool
False String
""
f Char
'Z' String
str TimeZone
_
| Just TimeZone
zone <- TimeLocale -> String -> Maybe TimeZone
getKnownTimeZone TimeLocale
l String
str = TimeZone -> Maybe TimeZone
forall a. a -> Maybe a
Just TimeZone
zone
f Char
'Z' String
"UTC" TimeZone
_ = TimeZone -> Maybe TimeZone
forall a. a -> Maybe a
Just TimeZone
utc
f Char
'Z' [Char
c] TimeZone
_
| Just TimeZone
zone <- Char -> Maybe TimeZone
getMilZone Char
c = TimeZone -> Maybe TimeZone
forall a. a -> Maybe a
Just TimeZone
zone
f Char
'Z' String
_ TimeZone
_ = Maybe TimeZone
forall a. Maybe a
Nothing
f Char
_ String
_ TimeZone
tz = TimeZone -> Maybe TimeZone
forall a. a -> Maybe a
Just TimeZone
tz
in (Maybe TimeZone -> (Char, String) -> Maybe TimeZone)
-> Maybe TimeZone -> [(Char, String)] -> Maybe TimeZone
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Maybe TimeZone
mt (Char
c, String
s) -> Maybe TimeZone
mt Maybe TimeZone -> (TimeZone -> Maybe TimeZone) -> Maybe TimeZone
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Char -> String -> TimeZone -> Maybe TimeZone
f Char
c String
s) (TimeZone -> Maybe TimeZone
forall a. a -> Maybe a
Just (TimeZone -> Maybe TimeZone) -> TimeZone -> Maybe TimeZone
forall a b. (a -> b) -> a -> b
$ Int -> TimeZone
minutesToTimeZone Int
0)
readTzOffset :: String -> Maybe Int
readTzOffset :: String -> Maybe Int
readTzOffset String
str =
let getSign :: Char -> Maybe a
getSign Char
'+' = a -> Maybe a
forall a. a -> Maybe a
Just a
1
getSign Char
'-' = a -> Maybe a
forall a. a -> Maybe a
Just (-a
1)
getSign Char
_ = Maybe a
forall a. Maybe a
Nothing
calc :: Char -> Char -> Char -> Char -> Char -> Maybe b
calc Char
s Char
h1 Char
h2 Char
m1 Char
m2 = do
b
sign <- Char -> Maybe b
forall a. Num a => Char -> Maybe a
getSign Char
s
b
h <- String -> Maybe b
forall a. Read a => String -> Maybe a
readMaybe [Char
h1, Char
h2]
b
m <- String -> Maybe b
forall a. Read a => String -> Maybe a
readMaybe [Char
m1, Char
m2]
b -> Maybe b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Maybe b) -> b -> Maybe b
forall a b. (a -> b) -> a -> b
$ b
sign b -> b -> b
forall a. Num a => a -> a -> a
* (b
60 b -> b -> b
forall a. Num a => a -> a -> a
* b
h b -> b -> b
forall a. Num a => a -> a -> a
+ b
m)
in case String
str of
(Char
s : Char
h1 : Char
h2 : Char
':' : Char
m1 : Char
m2 : []) -> Char -> Char -> Char -> Char -> Char -> Maybe Int
forall b.
(Num b, Read b) =>
Char -> Char -> Char -> Char -> Char -> Maybe b
calc Char
s Char
h1 Char
h2 Char
m1 Char
m2
(Char
s : Char
h1 : Char
h2 : Char
m1 : Char
m2 : []) -> Char -> Char -> Char -> Char -> Char -> Maybe Int
forall b.
(Num b, Read b) =>
Char -> Char -> Char -> Char -> Char -> Maybe b
calc Char
s Char
h1 Char
h2 Char
m1 Char
m2
String
_ -> Maybe Int
forall a. Maybe a
Nothing
instance ParseTime ZonedTime where
substituteTimeSpecifier :: Proxy ZonedTime -> TimeLocale -> Char -> Maybe String
substituteTimeSpecifier Proxy ZonedTime
_ = TimeLocale -> Char -> Maybe String
timeSubstituteTimeSpecifier
parseTimeSpecifier :: Proxy ZonedTime
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy ZonedTime
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
timeParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe ZonedTime
buildTime TimeLocale
l [(Char, String)]
xs =
let f :: ZonedTime -> (Char, String) -> Maybe ZonedTime
f (ZonedTime (LocalTime Day
_ TimeOfDay
tod) TimeZone
z) (Char
's', String
x) = do
Integer
a <- String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe String
x
let s :: POSIXTime
s = Integer -> POSIXTime
forall a. Num a => Integer -> a
fromInteger Integer
a
(Integer
_, Pico
ps) = Pico -> (Integer, Pico)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction (TimeOfDay -> Pico
todSec TimeOfDay
tod) :: (Integer, Pico)
s' :: POSIXTime
s' = POSIXTime
s POSIXTime -> POSIXTime -> POSIXTime
forall a. Num a => a -> a -> a
+ Rational -> POSIXTime
forall a. Fractional a => Rational -> a
fromRational (Pico -> Rational
forall a. Real a => a -> Rational
toRational Pico
ps)
ZonedTime -> Maybe ZonedTime
forall (m :: * -> *) a. Monad m => a -> m a
return (ZonedTime -> Maybe ZonedTime) -> ZonedTime -> Maybe ZonedTime
forall a b. (a -> b) -> a -> b
$ TimeZone -> UTCTime -> ZonedTime
utcToZonedTime TimeZone
z (POSIXTime -> UTCTime
posixSecondsToUTCTime POSIXTime
s')
f ZonedTime
t (Char, String)
_ = ZonedTime -> Maybe ZonedTime
forall a. a -> Maybe a
Just ZonedTime
t
in (ZonedTime -> (Char, String) -> Maybe ZonedTime)
-> Maybe ZonedTime -> [(Char, String)] -> Maybe ZonedTime
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> m a -> [b] -> m a
mfoldl ZonedTime -> (Char, String) -> Maybe ZonedTime
f (LocalTime -> TimeZone -> ZonedTime
ZonedTime (LocalTime -> TimeZone -> ZonedTime)
-> Maybe LocalTime -> Maybe (TimeZone -> ZonedTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TimeLocale -> [(Char, String)] -> Maybe LocalTime
forall t. ParseTime t => TimeLocale -> [(Char, String)] -> Maybe t
buildTime TimeLocale
l [(Char, String)]
xs) Maybe (TimeZone -> ZonedTime) -> Maybe TimeZone -> Maybe ZonedTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (TimeLocale -> [(Char, String)] -> Maybe TimeZone
forall t. ParseTime t => TimeLocale -> [(Char, String)] -> Maybe t
buildTime TimeLocale
l [(Char, String)]
xs)) [(Char, String)]
xs
instance ParseTime UTCTime where
substituteTimeSpecifier :: Proxy UTCTime -> TimeLocale -> Char -> Maybe String
substituteTimeSpecifier Proxy UTCTime
_ = TimeLocale -> Char -> Maybe String
timeSubstituteTimeSpecifier
parseTimeSpecifier :: Proxy UTCTime
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy UTCTime
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
timeParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe UTCTime
buildTime TimeLocale
l [(Char, String)]
xs = ZonedTime -> UTCTime
zonedTimeToUTC (ZonedTime -> UTCTime) -> Maybe ZonedTime -> Maybe UTCTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TimeLocale -> [(Char, String)] -> Maybe ZonedTime
forall t. ParseTime t => TimeLocale -> [(Char, String)] -> Maybe t
buildTime TimeLocale
l [(Char, String)]
xs
instance ParseTime UniversalTime where
substituteTimeSpecifier :: Proxy UniversalTime -> TimeLocale -> Char -> Maybe String
substituteTimeSpecifier Proxy UniversalTime
_ = TimeLocale -> Char -> Maybe String
timeSubstituteTimeSpecifier
parseTimeSpecifier :: Proxy UniversalTime
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy UniversalTime
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
timeParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe UniversalTime
buildTime TimeLocale
l [(Char, String)]
xs = Rational -> LocalTime -> UniversalTime
localTimeToUT1 Rational
0 (LocalTime -> UniversalTime)
-> Maybe LocalTime -> Maybe UniversalTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TimeLocale -> [(Char, String)] -> Maybe LocalTime
forall t. ParseTime t => TimeLocale -> [(Char, String)] -> Maybe t
buildTime TimeLocale
l [(Char, String)]
xs
buildTimeMonths :: [(Char, String)] -> Maybe Integer
buildTimeMonths :: [(Char, String)] -> Maybe Integer
buildTimeMonths [(Char, String)]
xs = do
[Integer]
tt <-
[(Char, String)]
-> ((Char, String) -> Maybe Integer) -> Maybe [Integer]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [(Char, String)]
xs (((Char, String) -> Maybe Integer) -> Maybe [Integer])
-> ((Char, String) -> Maybe Integer) -> Maybe [Integer]
forall a b. (a -> b) -> a -> b
$ \(Char
c, String
s) ->
case Char
c of
Char
'y' -> (Integer -> Integer) -> Maybe Integer -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(*) Integer
12) (Maybe Integer -> Maybe Integer) -> Maybe Integer -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe String
s
Char
'b' -> String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe String
s
Char
'B' -> String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe String
s
Char
_ -> Integer -> Maybe Integer
forall (m :: * -> *) a. Monad m => a -> m a
return Integer
0
Integer -> Maybe Integer
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> Maybe Integer) -> Integer -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ [Integer] -> Integer
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Integer]
tt
buildTimeDays :: [(Char, String)] -> Maybe Integer
buildTimeDays :: [(Char, String)] -> Maybe Integer
buildTimeDays [(Char, String)]
xs = do
[Integer]
tt <-
[(Char, String)]
-> ((Char, String) -> Maybe Integer) -> Maybe [Integer]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [(Char, String)]
xs (((Char, String) -> Maybe Integer) -> Maybe [Integer])
-> ((Char, String) -> Maybe Integer) -> Maybe [Integer]
forall a b. (a -> b) -> a -> b
$ \(Char
c, String
s) ->
case Char
c of
Char
'w' -> (Integer -> Integer) -> Maybe Integer -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(*) Integer
7) (Maybe Integer -> Maybe Integer) -> Maybe Integer -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe String
s
Char
'd' -> String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe String
s
Char
'D' -> String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe String
s
Char
_ -> Integer -> Maybe Integer
forall (m :: * -> *) a. Monad m => a -> m a
return Integer
0
Integer -> Maybe Integer
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> Maybe Integer) -> Integer -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ [Integer] -> Integer
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Integer]
tt
buildTimeSeconds :: [(Char, String)] -> Maybe Pico
buildTimeSeconds :: [(Char, String)] -> Maybe Pico
buildTimeSeconds [(Char, String)]
xs = do
[Pico]
tt <-
[(Char, String)] -> ((Char, String) -> Maybe Pico) -> Maybe [Pico]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [(Char, String)]
xs (((Char, String) -> Maybe Pico) -> Maybe [Pico])
-> ((Char, String) -> Maybe Pico) -> Maybe [Pico]
forall a b. (a -> b) -> a -> b
$ \(Char
c, String
s) ->
let readInt :: Integer -> Maybe Pico
readInt :: Integer -> Maybe Pico
readInt Integer
t = do
Integer
i <- String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe String
s
Pico -> Maybe Pico
forall (m :: * -> *) a. Monad m => a -> m a
return (Pico -> Maybe Pico) -> Pico -> Maybe Pico
forall a b. (a -> b) -> a -> b
$ Integer -> Pico
forall a. Num a => Integer -> a
fromInteger (Integer -> Pico) -> Integer -> Pico
forall a b. (a -> b) -> a -> b
$ Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
t
in case Char
c of
Char
'h' -> Integer -> Maybe Pico
readInt Integer
3600
Char
'H' -> Integer -> Maybe Pico
readInt Integer
3600
Char
'm' -> Integer -> Maybe Pico
readInt Integer
60
Char
'M' -> Integer -> Maybe Pico
readInt Integer
60
Char
's' -> String -> Maybe Pico
forall a. Read a => String -> Maybe a
readMaybe String
s
Char
'S' -> String -> Maybe Pico
forall a. Read a => String -> Maybe a
readMaybe String
s
Char
_ -> Pico -> Maybe Pico
forall (m :: * -> *) a. Monad m => a -> m a
return Pico
0
Pico -> Maybe Pico
forall (m :: * -> *) a. Monad m => a -> m a
return (Pico -> Maybe Pico) -> Pico -> Maybe Pico
forall a b. (a -> b) -> a -> b
$ [Pico] -> Pico
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Pico]
tt
instance ParseTime NominalDiffTime where
parseTimeSpecifier :: Proxy POSIXTime
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy POSIXTime
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
durationParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe POSIXTime
buildTime TimeLocale
_ [(Char, String)]
xs = do
Integer
dd <- [(Char, String)] -> Maybe Integer
buildTimeDays [(Char, String)]
xs
Pico
tt <- [(Char, String)] -> Maybe Pico
buildTimeSeconds [(Char, String)]
xs
POSIXTime -> Maybe POSIXTime
forall (m :: * -> *) a. Monad m => a -> m a
return (POSIXTime -> Maybe POSIXTime) -> POSIXTime -> Maybe POSIXTime
forall a b. (a -> b) -> a -> b
$ (Integer -> POSIXTime
forall a. Num a => Integer -> a
fromInteger Integer
dd POSIXTime -> POSIXTime -> POSIXTime
forall a. Num a => a -> a -> a
* POSIXTime
86400) POSIXTime -> POSIXTime -> POSIXTime
forall a. Num a => a -> a -> a
+ Pico -> POSIXTime
forall a b. (Real a, Fractional b) => a -> b
realToFrac Pico
tt
instance ParseTime DiffTime where
parseTimeSpecifier :: Proxy DiffTime
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy DiffTime
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
durationParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe DiffTime
buildTime TimeLocale
_ [(Char, String)]
xs = do
Integer
dd <- [(Char, String)] -> Maybe Integer
buildTimeDays [(Char, String)]
xs
Pico
tt <- [(Char, String)] -> Maybe Pico
buildTimeSeconds [(Char, String)]
xs
DiffTime -> Maybe DiffTime
forall (m :: * -> *) a. Monad m => a -> m a
return (DiffTime -> Maybe DiffTime) -> DiffTime -> Maybe DiffTime
forall a b. (a -> b) -> a -> b
$ (Integer -> DiffTime
forall a. Num a => Integer -> a
fromInteger Integer
dd DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
* DiffTime
86400) DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
+ Pico -> DiffTime
forall a b. (Real a, Fractional b) => a -> b
realToFrac Pico
tt
instance ParseTime CalendarDiffDays where
parseTimeSpecifier :: Proxy CalendarDiffDays
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy CalendarDiffDays
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
durationParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe CalendarDiffDays
buildTime TimeLocale
_ [(Char, String)]
xs = do
Integer
mm <- [(Char, String)] -> Maybe Integer
buildTimeMonths [(Char, String)]
xs
Integer
dd <- [(Char, String)] -> Maybe Integer
buildTimeDays [(Char, String)]
xs
CalendarDiffDays -> Maybe CalendarDiffDays
forall (m :: * -> *) a. Monad m => a -> m a
return (CalendarDiffDays -> Maybe CalendarDiffDays)
-> CalendarDiffDays -> Maybe CalendarDiffDays
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> CalendarDiffDays
CalendarDiffDays Integer
mm Integer
dd
instance ParseTime CalendarDiffTime where
parseTimeSpecifier :: Proxy CalendarDiffTime
-> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
parseTimeSpecifier Proxy CalendarDiffTime
_ = TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String
durationParseTimeSpecifier
buildTime :: TimeLocale -> [(Char, String)] -> Maybe CalendarDiffTime
buildTime TimeLocale
locale [(Char, String)]
xs = do
Integer
mm <- [(Char, String)] -> Maybe Integer
buildTimeMonths [(Char, String)]
xs
POSIXTime
tt <- TimeLocale -> [(Char, String)] -> Maybe POSIXTime
forall t. ParseTime t => TimeLocale -> [(Char, String)] -> Maybe t
buildTime TimeLocale
locale [(Char, String)]
xs
CalendarDiffTime -> Maybe CalendarDiffTime
forall (m :: * -> *) a. Monad m => a -> m a
return (CalendarDiffTime -> Maybe CalendarDiffTime)
-> CalendarDiffTime -> Maybe CalendarDiffTime
forall a b. (a -> b) -> a -> b
$ Integer -> POSIXTime -> CalendarDiffTime
CalendarDiffTime Integer
mm POSIXTime
tt