{-# LANGUAGE Safe #-}
{-# OPTIONS -fno-warn-orphans #-}
#if __GLASGOW_HASKELL__ < 802
{-# OPTIONS_GHC -Wno-incomplete-patterns -Wno-incomplete-uni-patterns #-}
#endif

module Data.Time.Format.Format.Instances
    (
    ) where

import Control.Applicative ((<|>))
import Data.Char
import Data.Fixed
import Data.Time.Calendar.CalendarDiffDays
import Data.Time.Calendar.Days
import Data.Time.Calendar.Gregorian
import Data.Time.Calendar.OrdinalDate
import Data.Time.Calendar.Month
import Data.Time.Calendar.Private
import Data.Time.Calendar.Week
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.Format.Class
import Data.Time.Format.Locale
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

mapFormatCharacter :: (b -> a) -> Maybe (FormatOptions -> a -> String) -> Maybe (FormatOptions -> b -> String)
mapFormatCharacter :: forall b a.
(b -> a)
-> Maybe (FormatOptions -> a -> String)
-> Maybe (FormatOptions -> b -> String)
mapFormatCharacter b -> a
ba = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a -> b) -> a -> b
$ \a -> String
as -> a -> String
as forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
ba

instance FormatTime LocalTime where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> LocalTime -> String)
formatCharacter Bool
_ Char
'c' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ \FormatOptions
fo -> forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime (FormatOptions -> TimeLocale
foLocale FormatOptions
fo) forall a b. (a -> b) -> a -> b
$ TimeLocale -> String
dateTimeFmt forall a b. (a -> b) -> a -> b
$ FormatOptions -> TimeLocale
foLocale FormatOptions
fo
    formatCharacter Bool
alt Char
c =
        forall b a.
(b -> a)
-> Maybe (FormatOptions -> a -> String)
-> Maybe (FormatOptions -> b -> String)
mapFormatCharacter LocalTime -> Day
localDay (forall t.
FormatTime t =>
Bool -> Char -> Maybe (FormatOptions -> t -> String)
formatCharacter Bool
alt Char
c) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
        forall b a.
(b -> a)
-> Maybe (FormatOptions -> a -> String)
-> Maybe (FormatOptions -> b -> String)
mapFormatCharacter LocalTime -> TimeOfDay
localTimeOfDay (forall t.
FormatTime t =>
Bool -> Char -> Maybe (FormatOptions -> t -> String)
formatCharacter Bool
alt Char
c)

todAMPM :: TimeLocale -> TimeOfDay -> String
todAMPM :: TimeLocale -> TimeOfDay -> String
todAMPM TimeLocale
locale TimeOfDay
day = let
    (String
am, String
pm) = TimeLocale -> (String, String)
amPm TimeLocale
locale
    in if (TimeOfDay -> Int
todHour TimeOfDay
day) forall a. Ord a => a -> a -> Bool
< Int
12
           then String
am
           else String
pm

tod12Hour :: TimeOfDay -> Int
tod12Hour :: TimeOfDay -> Int
tod12Hour TimeOfDay
day = (forall a. Integral a => a -> a -> a
mod (TimeOfDay -> Int
todHour TimeOfDay
day forall a. Num a => a -> a -> a
- Int
1) Int
12) forall a. Num a => a -> a -> a
+ Int
1

instance FormatTime TimeOfDay where
    -- Aggregate
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> TimeOfDay -> String)
formatCharacter Bool
_ Char
'R' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale String
"%H:%M"
    formatCharacter Bool
_ Char
'T' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale String
"%H:%M:%S"
    formatCharacter Bool
_ Char
'X' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale (TimeLocale -> String
timeFmt TimeLocale
locale)
    formatCharacter Bool
_ Char
'r' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale (TimeLocale -> String
time12Fmt TimeLocale
locale)
    -- AM/PM
    formatCharacter Bool
_ Char
'P' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeLocale -> TimeOfDay -> String
todAMPM TimeLocale
locale
    formatCharacter Bool
_ Char
'p' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> TimeLocale -> TimeOfDay -> String
todAMPM TimeLocale
locale
    -- Hour
    formatCharacter Bool
_ Char
'H' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' TimeOfDay -> Int
todHour
    formatCharacter Bool
_ Char
'I' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' TimeOfDay -> Int
tod12Hour
    formatCharacter Bool
_ Char
'k' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
' ' TimeOfDay -> Int
todHour
    formatCharacter Bool
_ Char
'l' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
' ' TimeOfDay -> Int
tod12Hour
    -- Minute
    formatCharacter Bool
_ Char
'M' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' TimeOfDay -> Int
todMin
    -- Second
    formatCharacter Bool
_ Char
'S' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ (forall a b. (RealFrac a, Integral b) => a -> b
floor forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeOfDay -> Pico
todSec :: TimeOfDay -> Int)
    formatCharacter Bool
_ Char
'q' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
True Bool
True Int
12 Char
'0' forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ PadOption
pado -> forall a. HasResolution a => PadOption -> Fixed a -> String
showPaddedFixedFraction PadOption
pado forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeOfDay -> Pico
todSec
    formatCharacter Bool
_ Char
'Q' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
True Bool
False Int
12 Char
'0' forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ PadOption
pado -> String -> String
dotNonEmpty forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HasResolution a => PadOption -> Fixed a -> String
showPaddedFixedFraction PadOption
pado forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeOfDay -> Pico
todSec
      where
        dotNonEmpty :: String -> String
dotNonEmpty String
"" = String
""
        dotNonEmpty String
s = Char
'.' forall a. a -> [a] -> [a]
: String
s
    -- Default
    formatCharacter Bool
_ Char
_ = forall a. Maybe a
Nothing

instance FormatTime ZonedTime where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> ZonedTime -> String)
formatCharacter Bool
_ Char
'c' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale (TimeLocale -> String
dateTimeFmt TimeLocale
locale)
    formatCharacter Bool
_ Char
's' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
1 Char
'0' forall a b. (a -> b) -> a -> b
$ (forall a b. (RealFrac a, Integral b) => a -> b
floor forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> POSIXTime
utcTimeToPOSIXSeconds forall b c a. (b -> c) -> (a -> b) -> a -> c
. ZonedTime -> UTCTime
zonedTimeToUTC :: ZonedTime -> Integer)
    formatCharacter Bool
alt Char
c =
        forall b a.
(b -> a)
-> Maybe (FormatOptions -> a -> String)
-> Maybe (FormatOptions -> b -> String)
mapFormatCharacter ZonedTime -> LocalTime
zonedTimeToLocalTime (forall t.
FormatTime t =>
Bool -> Char -> Maybe (FormatOptions -> t -> String)
formatCharacter Bool
alt Char
c) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
        forall b a.
(b -> a)
-> Maybe (FormatOptions -> a -> String)
-> Maybe (FormatOptions -> b -> String)
mapFormatCharacter ZonedTime -> TimeZone
zonedTimeZone (forall t.
FormatTime t =>
Bool -> Char -> Maybe (FormatOptions -> t -> String)
formatCharacter Bool
alt Char
c)

instance FormatTime TimeZone where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> TimeZone -> String)
formatCharacter Bool
False Char
'z' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
False Bool
True Int
4 Char
'0' forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ -> Bool -> PadOption -> TimeZone -> String
timeZoneOffsetString'' Bool
False
    formatCharacter Bool
True Char
'z' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
False Bool
True Int
5 Char
'0' forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ -> Bool -> PadOption -> TimeZone -> String
timeZoneOffsetString'' Bool
True
    formatCharacter Bool
alt Char
'Z' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ \FormatOptions
fo TimeZone
z -> let
            n :: String
n = TimeZone -> String
timeZoneName TimeZone
z
            idef :: Int
idef =
                if Bool
alt
                    then Int
5
                    else Int
4
            in if forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
n
                   then forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
False Bool
True Int
idef Char
'0' (\TimeLocale
_ -> Bool -> PadOption -> TimeZone -> String
timeZoneOffsetString'' Bool
alt) FormatOptions
fo TimeZone
z
                   else forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString (\TimeLocale
_ -> TimeZone -> String
timeZoneName) FormatOptions
fo TimeZone
z
    formatCharacter Bool
_ Char
_ = forall a. Maybe a
Nothing

instance FormatTime DayOfWeek where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> DayOfWeek -> String)
formatCharacter Bool
_ Char
'u' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
1 Char
'0' forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum
    formatCharacter Bool
_ Char
'w' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
1 Char
'0' forall a b. (a -> b) -> a -> b
$ \DayOfWeek
wd -> (forall a. Integral a => a -> a -> a
mod (forall a. Enum a => a -> Int
fromEnum DayOfWeek
wd) Int
7)
    formatCharacter Bool
_ Char
'a' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale DayOfWeek
wd -> forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ (TimeLocale -> [(String, String)]
wDays TimeLocale
locale) forall a. [a] -> Int -> a
!! (forall a. Integral a => a -> a -> a
mod (forall a. Enum a => a -> Int
fromEnum DayOfWeek
wd) Int
7)
    formatCharacter Bool
_ Char
'A' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale DayOfWeek
wd -> forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ (TimeLocale -> [(String, String)]
wDays TimeLocale
locale) forall a. [a] -> Int -> a
!! (forall a. Integral a => a -> a -> a
mod (forall a. Enum a => a -> Int
fromEnum DayOfWeek
wd) Int
7)
    formatCharacter Bool
_ Char
_ = forall a. Maybe a
Nothing

instance FormatTime Month where
    -- Year Count
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> Month -> String)
formatCharacter Bool
_ Char
'Y' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
False Int
4 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearMonth Integer
y Int
_) -> Integer
y
    formatCharacter Bool
_ Char
'y' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearMonth Integer
y Int
_) -> forall i. Integral i => i -> i
mod100 Integer
y
    formatCharacter Bool
_ Char
'C' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
False Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearMonth Integer
y Int
_) -> forall i. Integral i => i -> i
div100 Integer
y
    -- Month of Year
    formatCharacter Bool
_ Char
'B' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale (YearMonth Integer
_ Int
my) -> forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ (TimeLocale -> [(String, String)]
months TimeLocale
locale) forall a. [a] -> Int -> a
!! (Int
my forall a. Num a => a -> a -> a
- Int
1)
    formatCharacter Bool
_ Char
'b' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale (YearMonth Integer
_ Int
my) -> forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ (TimeLocale -> [(String, String)]
months TimeLocale
locale) forall a. [a] -> Int -> a
!! (Int
my forall a. Num a => a -> a -> a
- Int
1)
    formatCharacter Bool
_ Char
'h' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale (YearMonth Integer
_ Int
my) -> forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ (TimeLocale -> [(String, String)]
months TimeLocale
locale) forall a. [a] -> Int -> a
!! (Int
my forall a. Num a => a -> a -> a
- Int
1)
    formatCharacter Bool
_ Char
'm' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearMonth Integer
_ Int
m) -> Int
m
    -- Default
    formatCharacter Bool
_ Char
_ = forall a. Maybe a
Nothing

instance FormatTime Day where
    -- Aggregate
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> Day -> String)
formatCharacter Bool
_ Char
'D' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale String
"%m/%d/%y"
    formatCharacter Bool
_ Char
'F' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale String
"%Y-%m-%d"
    formatCharacter Bool
_ Char
'x' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale (TimeLocale -> String
dateFmt TimeLocale
locale)
    -- Day of Month
    formatCharacter Bool
_ Char
'd' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearMonthDay Integer
_ Int
_ Int
dm) -> Int
dm
    formatCharacter Bool
_ Char
'e' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
' ' forall a b. (a -> b) -> a -> b
$ \(YearMonthDay Integer
_ Int
_ Int
dm) -> Int
dm
    -- Day of Year
    formatCharacter Bool
_ Char
'j' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
3 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearDay Integer
_ Int
dy) -> Int
dy
    -- ISO 8601 Week Date
    formatCharacter Bool
_ Char
'G' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
False Int
4 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearWeekDay Integer
y Int
_ DayOfWeek
_) -> Integer
y
    formatCharacter Bool
_ Char
'g' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearWeekDay Integer
y Int
_ DayOfWeek
_) -> forall i. Integral i => i -> i
mod100 Integer
y
    formatCharacter Bool
_ Char
'f' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
False Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearWeekDay Integer
y Int
_ DayOfWeek
_) -> forall i. Integral i => i -> i
div100 Integer
y
    formatCharacter Bool
_ Char
'V' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearWeekDay Integer
_ Int
wy DayOfWeek
_) -> Int
wy
    formatCharacter Bool
_ Char
'u' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
1 Char
'0' forall a b. (a -> b) -> a -> b
$ \(YearWeekDay Integer
_ Int
_ DayOfWeek
dw) -> forall a. Enum a => a -> Int
fromEnum DayOfWeek
dw
    -- Day of week
    formatCharacter Bool
_ Char
'a' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TimeLocale -> [(String, String)]
wDays TimeLocale
locale) forall a. [a] -> Int -> a
!!) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> (Int, Int)
sundayStartWeek
    formatCharacter Bool
_ Char
'A' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale -> forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TimeLocale -> [(String, String)]
wDays TimeLocale
locale) forall a. [a] -> Int -> a
!!) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> (Int, Int)
sundayStartWeek
    formatCharacter Bool
_ Char
'U' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> (Int, Int)
sundayStartWeek
    formatCharacter Bool
_ Char
'w' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
1 Char
'0' forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> (Int, Int)
sundayStartWeek
    formatCharacter Bool
_ Char
'W' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
True Int
2 Char
'0' forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> (Int, Int)
mondayStartWeek
    -- Default
    formatCharacter Bool
alt Char
c = forall b a.
(b -> a)
-> Maybe (FormatOptions -> a -> String)
-> Maybe (FormatOptions -> b -> String)
mapFormatCharacter (\(MonthDay Month
m Int
_) -> Month
m) forall a b. (a -> b) -> a -> b
$ forall t.
FormatTime t =>
Bool -> Char -> Maybe (FormatOptions -> t -> String)
formatCharacter Bool
alt Char
c

instance FormatTime UTCTime where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> UTCTime -> String)
formatCharacter Bool
alt Char
c = forall b a.
(b -> a)
-> Maybe (FormatOptions -> a -> String)
-> Maybe (FormatOptions -> b -> String)
mapFormatCharacter (TimeZone -> UTCTime -> ZonedTime
utcToZonedTime TimeZone
utc) forall a b. (a -> b) -> a -> b
$ forall t.
FormatTime t =>
Bool -> Char -> Maybe (FormatOptions -> t -> String)
formatCharacter Bool
alt Char
c

instance FormatTime UniversalTime where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> UniversalTime -> String)
formatCharacter Bool
alt Char
c = forall b a.
(b -> a)
-> Maybe (FormatOptions -> a -> String)
-> Maybe (FormatOptions -> b -> String)
mapFormatCharacter (Rational -> UniversalTime -> LocalTime
ut1ToLocalTime Rational
0) forall a b. (a -> b) -> a -> b
$ forall t.
FormatTime t =>
Bool -> Char -> Maybe (FormatOptions -> t -> String)
formatCharacter Bool
alt Char
c

instance FormatTime NominalDiffTime where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> POSIXTime -> String)
formatCharacter Bool
_ Char
'w' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy forall a b. (a -> b) -> a -> b
$ POSIXTime
7 forall a. Num a => a -> a -> a
* POSIXTime
86400
    formatCharacter Bool
_ Char
'd' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy POSIXTime
86400
    formatCharacter Bool
_ Char
'D' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
7 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Integral b) => a -> a -> b
quotBy POSIXTime
86400
    formatCharacter Bool
_ Char
'h' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy POSIXTime
3600
    formatCharacter Bool
_ Char
'H' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
2 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
24 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Integral b) => a -> a -> b
quotBy POSIXTime
3600
    formatCharacter Bool
_ Char
'm' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy POSIXTime
60
    formatCharacter Bool
_ Char
'M' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
2 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
60 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Integral b) => a -> a -> b
quotBy POSIXTime
60
    formatCharacter Bool
False Char
's' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy POSIXTime
1
    formatCharacter Bool
True Char
's' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
True Bool
False Int
12 Char
'0' forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ PadOption
padf POSIXTime
t -> forall a.
HasResolution a =>
PadOption -> PadOption -> Fixed a -> String
showPaddedFixed PadOption
NoPad PadOption
padf (forall a b. (Real a, Fractional b) => a -> b
realToFrac POSIXTime
t :: Pico)
    formatCharacter Bool
False Char
'S' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
2 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
60 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Integral b) => a -> a -> b
quotBy POSIXTime
1
    formatCharacter Bool
True Char
'S' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
        forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
True Bool
False Int
12 Char
'0' forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ PadOption
padf POSIXTime
t -> let
            padn :: PadOption
padn =
                case PadOption
padf of
                    PadOption
NoPad -> PadOption
NoPad
                    Pad Int
_ Char
c -> Int -> Char -> PadOption
Pad Int
2 Char
c
            in forall a.
HasResolution a =>
PadOption -> PadOption -> Fixed a -> String
showPaddedFixed PadOption
padn PadOption
padf (forall a b. (Real a, Fractional b) => a -> b
realToFrac forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy POSIXTime
60 POSIXTime
t :: Pico)
    formatCharacter Bool
_ Char
_ = forall a. Maybe a
Nothing

instance FormatTime DiffTime where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> DiffTime -> String)
formatCharacter Bool
_ Char
'w' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy forall a b. (a -> b) -> a -> b
$ DiffTime
7 forall a. Num a => a -> a -> a
* DiffTime
86400
    formatCharacter Bool
_ Char
'd' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy DiffTime
86400
    formatCharacter Bool
_ Char
'D' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
7 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Integral b) => a -> a -> b
quotBy DiffTime
86400
    formatCharacter Bool
_ Char
'h' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy DiffTime
3600
    formatCharacter Bool
_ Char
'H' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
2 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
24 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Integral b) => a -> a -> b
quotBy DiffTime
3600
    formatCharacter Bool
_ Char
'm' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy DiffTime
60
    formatCharacter Bool
_ Char
'M' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
2 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
60 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Integral b) => a -> a -> b
quotBy DiffTime
60
    formatCharacter Bool
False Char
's' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy DiffTime
1
    formatCharacter Bool
True Char
's' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
True Bool
False Int
12 Char
'0' forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ PadOption
padf DiffTime
t -> forall a.
HasResolution a =>
PadOption -> PadOption -> Fixed a -> String
showPaddedFixed PadOption
NoPad PadOption
padf (forall a b. (Real a, Fractional b) => a -> b
realToFrac DiffTime
t :: Pico)
    formatCharacter Bool
False Char
'S' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
2 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
60 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Integral b) => a -> a -> b
quotBy DiffTime
1
    formatCharacter Bool
True Char
'S' =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
        forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
True Bool
False Int
12 Char
'0' forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ PadOption
padf DiffTime
t -> let
            padn :: PadOption
padn =
                case PadOption
padf of
                    PadOption
NoPad -> PadOption
NoPad
                    Pad Int
_ Char
c -> Int -> Char -> PadOption
Pad Int
2 Char
c
            in forall a.
HasResolution a =>
PadOption -> PadOption -> Fixed a -> String
showPaddedFixed PadOption
padn PadOption
padf (forall a b. (Real a, Fractional b) => a -> b
realToFrac forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy DiffTime
60 DiffTime
t :: Pico)
    formatCharacter Bool
_ Char
_ = forall a. Maybe a
Nothing

instance FormatTime CalendarDiffDays where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> CalendarDiffDays -> String)
formatCharacter Bool
_ Char
'y' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy Integer
12 forall b c a. (b -> c) -> (a -> b) -> a -> c
. CalendarDiffDays -> Integer
cdMonths
    formatCharacter Bool
_ Char
'b' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ CalendarDiffDays -> Integer
cdMonths
    formatCharacter Bool
_ Char
'B' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
2 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
12 forall b c a. (b -> c) -> (a -> b) -> a -> c
. CalendarDiffDays -> Integer
cdMonths
    formatCharacter Bool
_ Char
'w' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy Integer
7 forall b c a. (b -> c) -> (a -> b) -> a -> c
. CalendarDiffDays -> Integer
cdDays
    formatCharacter Bool
_ Char
'd' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ CalendarDiffDays -> Integer
cdDays
    formatCharacter Bool
_ Char
'D' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
7 forall b c a. (b -> c) -> (a -> b) -> a -> c
. CalendarDiffDays -> Integer
cdDays
    formatCharacter Bool
_ Char
_ = forall a. Maybe a
Nothing

instance FormatTime CalendarDiffTime where
    formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> CalendarDiffTime -> String)
formatCharacter Bool
_ Char
'y' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Integral b) => a -> a -> b
quotBy Integer
12 forall b c a. (b -> c) -> (a -> b) -> a -> c
. CalendarDiffTime -> Integer
ctMonths
    formatCharacter Bool
_ Char
'b' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
1 forall a b. (a -> b) -> a -> b
$ CalendarDiffTime -> Integer
ctMonths
    formatCharacter Bool
_ Char
'B' = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
2 forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> a -> a
remBy Integer
12 forall b c a. (b -> c) -> (a -> b) -> a -> c
. CalendarDiffTime -> Integer
ctMonths
    formatCharacter Bool
alt Char
c = forall b a.
(b -> a)
-> Maybe (FormatOptions -> a -> String)
-> Maybe (FormatOptions -> b -> String)
mapFormatCharacter CalendarDiffTime -> POSIXTime
ctTime forall a b. (a -> b) -> a -> b
$ forall t.
FormatTime t =>
Bool -> Char -> Maybe (FormatOptions -> t -> String)
formatCharacter Bool
alt Char
c