haspara-0.0.0.1: A library providing definitions to work with monetary values.
Safe HaskellNone
LanguageHaskell2010

Haspara.Date

Description

This module provides definitions and functions to encode and work on date values.

Synopsis

Date

Definition

data Date Source #

Type encoding for date values.

This is a convenience wrapper around Day type. It helps us to avoid defining orphan instances.

Instances

Instances details
Enum Date Source # 
Instance details

Defined in Haspara.Internal.Date

Methods

succ :: Date -> Date #

pred :: Date -> Date #

toEnum :: Int -> Date #

fromEnum :: Date -> Int #

enumFrom :: Date -> [Date] #

enumFromThen :: Date -> Date -> [Date] #

enumFromTo :: Date -> Date -> [Date] #

enumFromThenTo :: Date -> Date -> Date -> [Date] #

Eq Date Source # 
Instance details

Defined in Haspara.Internal.Date

Methods

(==) :: Date -> Date -> Bool #

(/=) :: Date -> Date -> Bool #

Ord Date Source # 
Instance details

Defined in Haspara.Internal.Date

Methods

compare :: Date -> Date -> Ordering #

(<) :: Date -> Date -> Bool #

(<=) :: Date -> Date -> Bool #

(>) :: Date -> Date -> Bool #

(>=) :: Date -> Date -> Bool #

max :: Date -> Date -> Date #

min :: Date -> Date -> Date #

Read Date Source #

Read instance for Date.

>>> read "2021-01-01" :: Date
2021-01-01
>>> read "Just 2021-01-01" :: Maybe Date
Just 2021-01-01
Instance details

Defined in Haspara.Internal.Date

Show Date Source #

Show instance for Date.

>>> fromYMD 2020 12 31
2020-12-31
Instance details

Defined in Haspara.Internal.Date

Methods

showsPrec :: Int -> Date -> ShowS #

show :: Date -> String #

showList :: [Date] -> ShowS #

Hashable Date Source #

Hashable instance for Date.

Instance details

Defined in Haspara.Internal.Date

Methods

hashWithSalt :: Int -> Date -> Int #

hash :: Date -> Int #

ToJSON Date Source #

ToJSON instance for Date.

>>> Aeson.encode (MkDate (read "2021-01-01"))
"\"2021-01-01\""
Instance details

Defined in Haspara.Internal.Date

FromJSON Date Source #

FromJSON instance for Date.

>>> Aeson.decode "\"2020-12-31\"" :: Maybe Date
Just 2020-12-31
Instance details

Defined in Haspara.Internal.Date

Constructors

fromDay :: Day -> Date Source #

Builds a Date from a given Day.

>>> fromDay (read "2021-01-01")
2021-01-01

fromYMD :: Integer -> Int -> Int -> Date Source #

Builds a Date from a given year, month and day as in Gregorian calendar.

>>> fromYMD 2021 1 1
2021-01-01

fromString :: MonadFail m => String -> m Date Source #

Attempts to parse and return Date from a given String with ISO format.

>>> fromString "2021-01-01" :: Maybe Date
Just 2021-01-01
>>> fromString "20210101" :: Maybe Date
Nothing

fromFormattedString :: MonadFail m => String -> String -> m Date Source #

Attempts to parse and return Date from a given String with given date format.

>>> fromFormattedString "%Y-%m-%d" "2021-01-01" :: Maybe Date
Just 2021-01-01
>>> fromFormattedString "%Y%m%d" "20210101" :: Maybe Date
Just 2021-01-01
>>> fromFormattedString "%Y%m%d" "202101" :: Maybe Date
Nothing

fromText :: MonadFail m => Text -> m Date Source #

Attempts to parse and return Date from a given Text with ISO format.

>>> fromText "2021-01-01" :: Maybe Date
Just 2021-01-01
>>> fromText "20210101" :: Maybe Date
Nothing

fromFormattedText :: MonadFail m => String -> Text -> m Date Source #

Attempts to parse and return Date from a given Text with ISO format.

>>> fromFormattedText "%Y-%m-%d" "2021-01-01" :: Maybe Date
Just 2021-01-01
>>> fromFormattedText "%Y%m%d" "20210101" :: Maybe Date
Just 2021-01-01
>>> fromFormattedText "%Y%m%d" "202101" :: Maybe Date
Nothing

Conversions

toDay :: Date -> Day Source #

Converts Date value to a Day value.

>>> toDay (read "2021-01-01")
2021-01-01

toYMD :: Date -> (Integer, Int, Int) Source #

Converts Date value to a 3-tuple of year, month and day.

>>> toYMD (read "2020-12-31")
(2020,12,31)

toString :: Date -> String Source #

Converts Date value into a String value with ISO format.

>>> toString (read "2021-01-01")
"2021-01-01"

toFormattedString :: String -> Date -> String Source #

Converts Date value into a String value with the given format.

>>> toFormattedString "%Y-%m-%d" (read "2021-01-01")
"2021-01-01"
>>> toFormattedString "%d/%m/%Y" (read "2021-01-01")
"01/01/2021"

toText :: Date -> Text Source #

Converts Date value into a Text value with ISO format.

>>> toText (read "2021-01-01")
"2021-01-01"

toFormattedText :: String -> Date -> Text Source #

Converts Date value into a Text value with the given format.

>>> toFormattedText "%Y-%m-%d" (read "2021-01-01")
"2021-01-01"
>>> toFormattedText "%d/%m/%Y" (read "2021-01-01")
"01/01/2021"

Operations

addDays :: Integer -> Date -> Date Source #

Adds (or subtracts) some days.

>>> addDays (-1) $ fromYMD 2021 1 1
2020-12-31
>>> addDays 1 $ addDays (-1) $ fromYMD 2021 1 1
2021-01-01