{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Haspara.Internal.Date where
import qualified Data.Aeson as Aeson
import Data.Bifunctor (first)
import Data.Hashable (Hashable(..))
import qualified Data.Text as T
import qualified Data.Time as DT
newtype Date = MkDate DT.Day deriving (Date -> Date -> Bool
(Date -> Date -> Bool) -> (Date -> Date -> Bool) -> Eq Date
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Date -> Date -> Bool
$c/= :: Date -> Date -> Bool
== :: Date -> Date -> Bool
$c== :: Date -> Date -> Bool
Eq, Int -> Date
Date -> Int
Date -> [Date]
Date -> Date
Date -> Date -> [Date]
Date -> Date -> Date -> [Date]
(Date -> Date)
-> (Date -> Date)
-> (Int -> Date)
-> (Date -> Int)
-> (Date -> [Date])
-> (Date -> Date -> [Date])
-> (Date -> Date -> [Date])
-> (Date -> Date -> Date -> [Date])
-> Enum Date
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Date -> Date -> Date -> [Date]
$cenumFromThenTo :: Date -> Date -> Date -> [Date]
enumFromTo :: Date -> Date -> [Date]
$cenumFromTo :: Date -> Date -> [Date]
enumFromThen :: Date -> Date -> [Date]
$cenumFromThen :: Date -> Date -> [Date]
enumFrom :: Date -> [Date]
$cenumFrom :: Date -> [Date]
fromEnum :: Date -> Int
$cfromEnum :: Date -> Int
toEnum :: Int -> Date
$ctoEnum :: Int -> Date
pred :: Date -> Date
$cpred :: Date -> Date
succ :: Date -> Date
$csucc :: Date -> Date
Enum, Eq Date
Eq Date
-> (Date -> Date -> Ordering)
-> (Date -> Date -> Bool)
-> (Date -> Date -> Bool)
-> (Date -> Date -> Bool)
-> (Date -> Date -> Bool)
-> (Date -> Date -> Date)
-> (Date -> Date -> Date)
-> Ord Date
Date -> Date -> Bool
Date -> Date -> Ordering
Date -> Date -> Date
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Date -> Date -> Date
$cmin :: Date -> Date -> Date
max :: Date -> Date -> Date
$cmax :: Date -> Date -> Date
>= :: Date -> Date -> Bool
$c>= :: Date -> Date -> Bool
> :: Date -> Date -> Bool
$c> :: Date -> Date -> Bool
<= :: Date -> Date -> Bool
$c<= :: Date -> Date -> Bool
< :: Date -> Date -> Bool
$c< :: Date -> Date -> Bool
compare :: Date -> Date -> Ordering
$ccompare :: Date -> Date -> Ordering
$cp1Ord :: Eq Date
Ord)
instance Hashable Date where
hashWithSalt :: Int -> Date -> Int
hashWithSalt Int
salt (MkDate (DT.ModifiedJulianDay Integer
i)) = Int -> Integer -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt Integer
i
instance Read Date where
readsPrec :: Int -> ReadS Date
readsPrec Int
_ = Bool -> ReadS Date -> ReadS Date
forall a. Bool -> ReadS a -> ReadS a
readParen Bool
False (ReadS Date -> ReadS Date) -> ReadS Date -> ReadS Date
forall a b. (a -> b) -> a -> b
$ ((Day, String) -> (Date, String))
-> [(Day, String)] -> [(Date, String)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Day -> Date) -> (Day, String) -> (Date, String)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Day -> Date
MkDate) ([(Day, String)] -> [(Date, String)])
-> (String -> [(Day, String)]) -> ReadS Date
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> TimeLocale -> String -> String -> [(Day, String)]
forall t. ParseTime t => Bool -> TimeLocale -> String -> ReadS t
DT.readSTime Bool
True TimeLocale
DT.defaultTimeLocale String
"%Y-%m-%d"
instance Show Date where
show :: Date -> String
show = Date -> String
toString
instance Aeson.FromJSON Date where
parseJSON :: Value -> Parser Date
parseJSON = String -> (Text -> Parser Date) -> Value -> Parser Date
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"Date" Text -> Parser Date
forall (m :: * -> *). MonadFail m => Text -> m Date
fromText
instance Aeson.ToJSON Date where
toJSON :: Date -> Value
toJSON = Text -> Value
Aeson.String (Text -> Value) -> (Date -> Text) -> Date -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> (Date -> String) -> Date -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Date -> String
forall a. Show a => a -> String
show
fromDay :: DT.Day -> Date
fromDay :: Day -> Date
fromDay = Day -> Date
MkDate
fromYMD :: Integer -> Int -> Int -> Date
fromYMD :: Integer -> Int -> Int -> Date
fromYMD Integer
y Int
m Int
d = Day -> Date
fromDay (Integer -> Int -> Int -> Day
DT.fromGregorian Integer
y Int
m Int
d)
fromString :: MonadFail m => String -> m Date
fromString :: String -> m Date
fromString = String -> String -> m Date
forall (m :: * -> *). MonadFail m => String -> String -> m Date
fromFormattedString String
"%Y-%m-%d"
fromFormattedString :: MonadFail m => String -> String -> m Date
fromFormattedString :: String -> String -> m Date
fromFormattedString String
fmt = (Day -> Date) -> m Day -> m Date
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Day -> Date
fromDay (m Day -> m Date) -> (String -> m Day) -> String -> m Date
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> TimeLocale -> String -> String -> m Day
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
DT.parseTimeM Bool
False TimeLocale
DT.defaultTimeLocale String
fmt
fromText :: MonadFail m => T.Text -> m Date
fromText :: Text -> m Date
fromText = String -> m Date
forall (m :: * -> *). MonadFail m => String -> m Date
fromString (String -> m Date) -> (Text -> String) -> Text -> m Date
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
fromFormattedText :: MonadFail m => String -> T.Text -> m Date
fromFormattedText :: String -> Text -> m Date
fromFormattedText String
fmt = String -> String -> m Date
forall (m :: * -> *). MonadFail m => String -> String -> m Date
fromFormattedString String
fmt (String -> m Date) -> (Text -> String) -> Text -> m Date
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
toDay :: Date -> DT.Day
toDay :: Date -> Day
toDay (MkDate Day
d) = Day
d
toYMD :: Date -> (Integer, Int, Int)
toYMD :: Date -> (Integer, Int, Int)
toYMD = Day -> (Integer, Int, Int)
DT.toGregorian (Day -> (Integer, Int, Int))
-> (Date -> Day) -> Date -> (Integer, Int, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Date -> Day
toDay
toString :: Date -> String
toString :: Date -> String
toString = String -> Date -> String
toFormattedString String
"%Y-%m-%d"
toFormattedString :: String -> Date -> String
toFormattedString :: String -> Date -> String
toFormattedString String
fmt = TimeLocale -> String -> Day -> String
forall t. FormatTime t => TimeLocale -> String -> t -> String
DT.formatTime TimeLocale
DT.defaultTimeLocale String
fmt (Day -> String) -> (Date -> Day) -> Date -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Date -> Day
toDay
toText :: Date -> T.Text
toText :: Date -> Text
toText = String -> Text
T.pack (String -> Text) -> (Date -> String) -> Date -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Date -> String
toString
toFormattedText :: String -> Date -> T.Text
toFormattedText :: String -> Date -> Text
toFormattedText String
fmt = String -> Text
T.pack (String -> Text) -> (Date -> String) -> Date -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Date -> String
toFormattedString String
fmt
addDays :: Integer -> Date -> Date
addDays :: Integer -> Date -> Date
addDays Integer
x (MkDate Day
d) = Day -> Date
MkDate (Integer -> Day -> Day
DT.addDays Integer
x Day
d)