{-# LANGUAGE Safe #-}
module Data.Time.Format.Format.Class
(
formatTime
, FormatNumericPadding
, FormatOptions(..)
, FormatTime(..)
, ShowPadded
, PadOption
, formatGeneral
, formatString
, formatNumber
, formatNumberStd
, showPaddedFixed
, showPaddedFixedFraction
, quotBy
, remBy
) where
import Data.Char
import Data.Fixed
import Data.Maybe
import Data.Time.Calendar.Private
import Data.Time.Format.Locale
type FormatNumericPadding = Maybe Char
data FormatOptions = MkFormatOptions
{ FormatOptions -> TimeLocale
foLocale :: TimeLocale
, FormatOptions -> Maybe FormatNumericPadding
foPadding :: Maybe FormatNumericPadding
, FormatOptions -> Maybe Int
foWidth :: Maybe Int
}
class FormatTime t where
formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> t -> String)
getPadOption :: Bool -> Bool -> Int -> Char -> Maybe FormatNumericPadding -> Maybe Int -> PadOption
getPadOption :: Bool
-> Bool
-> Int
-> Char
-> Maybe FormatNumericPadding
-> Maybe Int
-> PadOption
getPadOption Bool
trunc Bool
fdef Int
idef Char
cdef Maybe FormatNumericPadding
mnpad Maybe Int
mi = let
c :: Char
c =
case Maybe FormatNumericPadding
mnpad of
Just (Just Char
c') -> Char
c'
Just FormatNumericPadding
Nothing -> Char
' '
Maybe FormatNumericPadding
_ -> Char
cdef
i :: Int
i =
case Maybe Int
mi of
Just Int
i' ->
case Maybe FormatNumericPadding
mnpad of
Just FormatNumericPadding
Nothing -> Int
i'
Maybe FormatNumericPadding
_ ->
if Bool
trunc
then Int
i'
else forall a. Ord a => a -> a -> a
max Int
i' Int
idef
Maybe Int
Nothing -> Int
idef
f :: Bool
f =
case Maybe Int
mi of
Just Int
_ -> Bool
True
Maybe Int
Nothing ->
case Maybe FormatNumericPadding
mnpad of
Maybe FormatNumericPadding
Nothing -> Bool
fdef
Just FormatNumericPadding
Nothing -> Bool
False
Just (Just Char
_) -> Bool
True
in if Bool
f
then Int -> Char -> PadOption
Pad Int
i Char
c
else PadOption
NoPad
formatGeneral ::
Bool -> Bool -> Int -> Char -> (TimeLocale -> PadOption -> t -> String) -> (FormatOptions -> t -> String)
formatGeneral :: forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
trunc Bool
fdef Int
idef Char
cdef TimeLocale -> PadOption -> t -> String
ff FormatOptions
fo =
TimeLocale -> PadOption -> t -> String
ff (FormatOptions -> TimeLocale
foLocale FormatOptions
fo) forall a b. (a -> b) -> a -> b
$ Bool
-> Bool
-> Int
-> Char
-> Maybe FormatNumericPadding
-> Maybe Int
-> PadOption
getPadOption Bool
trunc Bool
fdef Int
idef Char
cdef (FormatOptions -> Maybe FormatNumericPadding
foPadding FormatOptions
fo) (FormatOptions -> Maybe Int
foWidth FormatOptions
fo)
formatString :: (TimeLocale -> t -> String) -> (FormatOptions -> t -> String)
formatString :: forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString TimeLocale -> t -> String
ff = forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
False Bool
False Int
1 Char
' ' forall a b. (a -> b) -> a -> b
$ \TimeLocale
locale PadOption
pado -> PadOption -> String -> String
showPadded PadOption
pado forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeLocale -> t -> String
ff TimeLocale
locale
formatNumber :: (ShowPadded i) => Bool -> Int -> Char -> (t -> i) -> (FormatOptions -> t -> String)
formatNumber :: forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
fdef Int
idef Char
cdef t -> i
ff = forall t.
Bool
-> Bool
-> Int
-> Char
-> (TimeLocale -> PadOption -> t -> String)
-> FormatOptions
-> t
-> String
formatGeneral Bool
False Bool
fdef Int
idef Char
cdef forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ PadOption
pado -> forall t. ShowPadded t => PadOption -> t -> String
showPaddedNum PadOption
pado forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> i
ff
formatNumberStd :: Int -> (t -> Integer) -> (FormatOptions -> t -> String)
formatNumberStd :: forall t. Int -> (t -> Integer) -> FormatOptions -> t -> String
formatNumberStd Int
n = forall i t.
ShowPadded i =>
Bool -> Int -> Char -> (t -> i) -> FormatOptions -> t -> String
formatNumber Bool
False Int
n Char
'0'
showPaddedFixed :: HasResolution a => PadOption -> PadOption -> Fixed a -> String
showPaddedFixed :: forall a.
HasResolution a =>
PadOption -> PadOption -> Fixed a -> String
showPaddedFixed PadOption
padn PadOption
padf Fixed a
x
| Fixed a
x forall a. Ord a => a -> a -> Bool
< Fixed a
0 = Char
'-' forall a. a -> [a] -> [a]
: forall a.
HasResolution a =>
PadOption -> PadOption -> Fixed a -> String
showPaddedFixed PadOption
padn PadOption
padf (forall a. Num a => a -> a
negate Fixed a
x)
showPaddedFixed PadOption
padn PadOption
padf Fixed a
x = let
ns :: String
ns = forall t. ShowPadded t => PadOption -> t -> String
showPaddedNum PadOption
padn forall a b. (a -> b) -> a -> b
$ (forall a b. (RealFrac a, Integral b) => a -> b
floor Fixed a
x :: Integer)
fs :: String
fs = forall a. HasResolution a => PadOption -> Fixed a -> String
showPaddedFixedFraction PadOption
padf Fixed a
x
ds :: String
ds =
if forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
fs
then String
""
else String
"."
in String
ns forall a. [a] -> [a] -> [a]
++ String
ds forall a. [a] -> [a] -> [a]
++ String
fs
showPaddedFixedFraction :: HasResolution a => PadOption -> Fixed a -> String
showPaddedFixedFraction :: forall a. HasResolution a => PadOption -> Fixed a -> String
showPaddedFixedFraction PadOption
pado Fixed a
x = let
digits :: String
digits = forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall a. Eq a => a -> a -> Bool
== Char
'.') forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall a. Eq a => a -> a -> Bool
/= Char
'.') forall a b. (a -> b) -> a -> b
$ forall {k} (a :: k). HasResolution a => Bool -> Fixed a -> String
showFixed Bool
True Fixed a
x
n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
length String
digits
in case PadOption
pado of
PadOption
NoPad -> String
digits
Pad Int
i Char
c ->
if Int
i forall a. Ord a => a -> a -> Bool
< Int
n
then forall a. Int -> [a] -> [a]
take Int
i String
digits
else String
digits forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate (Int
i forall a. Num a => a -> a -> a
- Int
n) Char
c
formatTime :: (FormatTime t) => TimeLocale -> String -> t -> String
formatTime :: forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
_ [] t
_ = String
""
formatTime TimeLocale
locale (Char
'%':String
cs) t
t =
case forall t. FormatTime t => TimeLocale -> String -> t -> Maybe String
formatTime1 TimeLocale
locale String
cs t
t of
Just String
result -> String
result
Maybe String
Nothing -> Char
'%' forall a. a -> [a] -> [a]
: (forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale String
cs t
t)
formatTime TimeLocale
locale (Char
c:String
cs) t
t = Char
c forall a. a -> [a] -> [a]
: (forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale String
cs t
t)
formatTime1 :: (FormatTime t) => TimeLocale -> String -> t -> Maybe String
formatTime1 :: forall t. FormatTime t => TimeLocale -> String -> t -> Maybe String
formatTime1 TimeLocale
locale (Char
'_':String
cs) t
t = forall t.
FormatTime t =>
TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> String
-> t
-> Maybe String
formatTime2 TimeLocale
locale forall a. a -> a
id (forall a. a -> Maybe a
Just (forall a. a -> Maybe a
Just Char
' ')) String
cs t
t
formatTime1 TimeLocale
locale (Char
'-':String
cs) t
t = forall t.
FormatTime t =>
TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> String
-> t
-> Maybe String
formatTime2 TimeLocale
locale forall a. a -> a
id (forall a. a -> Maybe a
Just forall a. Maybe a
Nothing) String
cs t
t
formatTime1 TimeLocale
locale (Char
'0':String
cs) t
t = forall t.
FormatTime t =>
TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> String
-> t
-> Maybe String
formatTime2 TimeLocale
locale forall a. a -> a
id (forall a. a -> Maybe a
Just (forall a. a -> Maybe a
Just Char
'0')) String
cs t
t
formatTime1 TimeLocale
locale (Char
'^':String
cs) t
t = forall t.
FormatTime t =>
TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> String
-> t
-> Maybe String
formatTime2 TimeLocale
locale (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Char -> Char
toUpper) forall a. Maybe a
Nothing String
cs t
t
formatTime1 TimeLocale
locale (Char
'#':String
cs) t
t = forall t.
FormatTime t =>
TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> String
-> t
-> Maybe String
formatTime2 TimeLocale
locale (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Char -> Char
toLower) forall a. Maybe a
Nothing String
cs t
t
formatTime1 TimeLocale
locale String
cs t
t = forall t.
FormatTime t =>
TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> String
-> t
-> Maybe String
formatTime2 TimeLocale
locale forall a. a -> a
id forall a. Maybe a
Nothing String
cs t
t
getDigit :: Char -> Maybe Int
getDigit :: Char -> Maybe Int
getDigit Char
c
| Char
c forall a. Ord a => a -> a -> Bool
< Char
'0' = forall a. Maybe a
Nothing
getDigit Char
c
| Char
c forall a. Ord a => a -> a -> Bool
> Char
'9' = forall a. Maybe a
Nothing
getDigit Char
c = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ (Char -> Int
ord Char
c) forall a. Num a => a -> a -> a
- (Char -> Int
ord Char
'0')
pullNumber :: Maybe Int -> String -> (Maybe Int, String)
pullNumber :: Maybe Int -> String -> (Maybe Int, String)
pullNumber Maybe Int
mx [] = (Maybe Int
mx, [])
pullNumber Maybe Int
mx s :: String
s@(Char
c:String
cs) =
case Char -> Maybe Int
getDigit Char
c of
Just Int
i -> Maybe Int -> String -> (Maybe Int, String)
pullNumber (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ (forall a. a -> Maybe a -> a
fromMaybe Int
0 Maybe Int
mx) forall a. Num a => a -> a -> a
* Int
10 forall a. Num a => a -> a -> a
+ Int
i) String
cs
Maybe Int
Nothing -> (Maybe Int
mx, String
s)
formatTime2 ::
(FormatTime t) => TimeLocale -> (String -> String) -> Maybe FormatNumericPadding -> String -> t -> Maybe String
formatTime2 :: forall t.
FormatTime t =>
TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> String
-> t
-> Maybe String
formatTime2 TimeLocale
locale String -> String
recase Maybe FormatNumericPadding
mpad String
cs t
t = let
(Maybe Int
mwidth, String
rest) = Maybe Int -> String -> (Maybe Int, String)
pullNumber forall a. Maybe a
Nothing String
cs
in forall t.
FormatTime t =>
TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> Maybe Int
-> String
-> t
-> Maybe String
formatTime3 TimeLocale
locale String -> String
recase Maybe FormatNumericPadding
mpad Maybe Int
mwidth String
rest t
t
formatTime3 ::
(FormatTime t)
=> TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> Maybe Int
-> String
-> t
-> Maybe String
formatTime3 :: forall t.
FormatTime t =>
TimeLocale
-> (String -> String)
-> Maybe FormatNumericPadding
-> Maybe Int
-> String
-> t
-> Maybe String
formatTime3 TimeLocale
locale String -> String
recase Maybe FormatNumericPadding
mpad Maybe Int
mwidth (Char
'E':String
cs) = forall t.
FormatTime t =>
Bool
-> (String -> String)
-> FormatOptions
-> String
-> t
-> Maybe String
formatTime4 Bool
True String -> String
recase (TimeLocale
-> Maybe FormatNumericPadding -> Maybe Int -> FormatOptions
MkFormatOptions TimeLocale
locale Maybe FormatNumericPadding
mpad Maybe Int
mwidth) String
cs
formatTime3 TimeLocale
locale String -> String
recase Maybe FormatNumericPadding
mpad Maybe Int
mwidth String
cs = forall t.
FormatTime t =>
Bool
-> (String -> String)
-> FormatOptions
-> String
-> t
-> Maybe String
formatTime4 Bool
False String -> String
recase (TimeLocale
-> Maybe FormatNumericPadding -> Maybe Int -> FormatOptions
MkFormatOptions TimeLocale
locale Maybe FormatNumericPadding
mpad Maybe Int
mwidth) String
cs
formatTime4 :: (FormatTime t) => Bool -> (String -> String) -> FormatOptions -> String -> t -> Maybe String
formatTime4 :: forall t.
FormatTime t =>
Bool
-> (String -> String)
-> FormatOptions
-> String
-> t
-> Maybe String
formatTime4 Bool
alt String -> String
recase FormatOptions
fo (Char
c:String
cs) t
t = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ (String -> String
recase (forall t.
FormatTime t =>
Bool -> Char -> FormatOptions -> t -> String
formatChar Bool
alt Char
c FormatOptions
fo t
t)) forall a. [a] -> [a] -> [a]
++ (forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime (FormatOptions -> TimeLocale
foLocale FormatOptions
fo) String
cs t
t)
formatTime4 Bool
_alt String -> String
_recase FormatOptions
_fo [] t
_t = forall a. Maybe a
Nothing
formatChar :: (FormatTime t) => Bool -> Char -> FormatOptions -> t -> String
formatChar :: forall t.
FormatTime t =>
Bool -> Char -> FormatOptions -> t -> String
formatChar Bool
_ Char
'%' = forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ t
_ -> String
"%"
formatChar Bool
_ Char
't' = forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ t
_ -> String
"\t"
formatChar Bool
_ Char
'n' = forall t.
(TimeLocale -> t -> String) -> FormatOptions -> t -> String
formatString forall a b. (a -> b) -> a -> b
$ \TimeLocale
_ t
_ -> String
"\n"
formatChar Bool
alt Char
c =
case forall t.
FormatTime t =>
Bool -> Char -> Maybe (FormatOptions -> t -> String)
formatCharacter Bool
alt Char
c of
Just FormatOptions -> t -> String
f -> FormatOptions -> t -> String
f
Maybe (FormatOptions -> t -> String)
_ -> \FormatOptions
_ t
_ -> String
""