hledger-lib-0.26: Core data types, parsers and utilities for the hledger accounting tool.

Safe Haskell | None
Language | Haskell2010

Hledger.Data.Dates

Contents

- Misc date handling utilities

Description

Date parsing and utilities for hledger.

For date and time values, we use the standard Day and UTCTime types.

A `SmartDate` is a date which may be partially-specified or relative. Eg 2008/12/31, but also 2008/12, 12/31, tomorrow, last week, next year. We represent these as a triple of strings like ("2008","12",""), ("","","tomorrow"), ("","last","week").

A `DateSpan` is the span of time between two specific calendar dates, or an open-ended span where one or both dates are unspecified. (A date span with both ends unspecified matches all dates.)

An `Interval` is ledger's "reporting interval" - weekly, monthly, quarterly, etc.

Synopsis

- getCurrentDay :: IO Day
- getCurrentMonth :: IO Int
- getCurrentYear :: IO Integer
- nulldate :: Day
- spanContainsDate :: DateSpan -> Day -> Bool
- parsedateM :: String -> Maybe Day
- parsedate :: String -> Day
- showDate :: Day -> String
- showDateSpan :: DateSpan -> String
- elapsedSeconds :: Fractional a => UTCTime -> UTCTime -> a
- prevday :: Day -> Day
- parsePeriodExpr :: Day -> String -> Either ParseError (Interval, DateSpan)
- nulldatespan :: DateSpan
- tests_Hledger_Data_Dates :: Test
- failIfInvalidYear :: Monad m => String -> m ()
- failIfInvalidMonth :: Monad m => String -> m ()
- failIfInvalidDay :: Monad m => String -> m ()
- datesepchar :: Stream [Char] m Char => ParsecT [Char] st m Char
- datesepchars :: [Char]
- spanStart :: DateSpan -> Maybe Day
- spanEnd :: DateSpan -> Maybe Day
- spansSpan :: [DateSpan] -> DateSpan
- spanIntersect :: DateSpan -> DateSpan -> DateSpan
- spansIntersect :: [DateSpan] -> DateSpan
- spanDefaultsFrom :: DateSpan -> DateSpan -> DateSpan
- spanUnion :: DateSpan -> DateSpan -> DateSpan
- spansUnion :: [DateSpan] -> DateSpan
- smartdate :: Stream [Char] m Char => ParsecT [Char] st m SmartDate
- splitSpan :: Interval -> DateSpan -> [DateSpan]
- fixSmartDate :: Day -> SmartDate -> Day
- fixSmartDateStr :: Day -> String -> String
- fixSmartDateStrEither :: Day -> String -> Either ParseError String
- fixSmartDateStrEither' :: Day -> String -> Either ParseError Day
- daysInSpan :: DateSpan -> Maybe Integer
- maybePeriod :: Day -> String -> Maybe (Interval, DateSpan)
- mkdatespan :: String -> String -> DateSpan

# Misc date handling utilities

getCurrentDay :: IO Day Source

Get the current local date.

getCurrentMonth :: IO Int Source

Get the current local month number.

getCurrentYear :: IO Integer Source

Get the current local year.

nulldate :: Day Source

spanContainsDate :: DateSpan -> Day -> Bool Source

Does the span include the given date ?

parsedateM :: String -> Maybe Day Source

Parse a couple of date string formats to a time type.

parsedate :: String -> Day Source

Parse a date string to a time type, or raise an error.

showDate :: Day -> String Source

showDateSpan :: DateSpan -> String Source

Render a datespan as a display string, abbreviating into a compact form if possible.

elapsedSeconds :: Fractional a => UTCTime -> UTCTime -> a Source

prevday :: Day -> Day Source

parsePeriodExpr :: Day -> String -> Either ParseError (Interval, DateSpan) Source

Parse a period expression to an Interval and overall DateSpan using the provided reference date, or return a parse error.

nulldatespan :: DateSpan Source

tests_Hledger_Data_Dates :: Test Source

failIfInvalidYear :: Monad m => String -> m () Source

failIfInvalidMonth :: Monad m => String -> m () Source

failIfInvalidDay :: Monad m => String -> m () Source

datesepchar :: Stream [Char] m Char => ParsecT [Char] st m Char Source

datesepchars :: [Char] Source

spanStart :: DateSpan -> Maybe Day Source

spanEnd :: DateSpan -> Maybe Day Source

spansSpan :: [DateSpan] -> DateSpan Source

Get overall span enclosing multiple sequentially ordered spans.

spanIntersect :: DateSpan -> DateSpan -> DateSpan Source

Calculate the intersection of two datespans.

spansIntersect :: [DateSpan] -> DateSpan Source

Calculate the intersection of a number of datespans.

spanDefaultsFrom :: DateSpan -> DateSpan -> DateSpan Source

Fill any unspecified dates in the first span with the dates from the second one. Sort of a one-way spanIntersect.

spanUnion :: DateSpan -> DateSpan -> DateSpan Source

Calculate the union of two datespans.

spansUnion :: [DateSpan] -> DateSpan Source

Calculate the union of a number of datespans.

smartdate :: Stream [Char] m Char => ParsecT [Char] st m SmartDate Source

Parse a date in any of the formats allowed in ledger's period expressions, and maybe some others:

    2004
    2004/10
    2004/10/1
    10/1
    21
    october, oct
    yesterday, today, tomorrow
    this/next/last week/day/month/quarter/year

Returns a SmartDate, to be converted to a full date later (see fixSmartDate). Assumes any text in the parse stream has been lowercased.

splitSpan :: Interval -> DateSpan -> [DateSpan] Source

Split a DateSpan into one or more consecutive whole spans of the specified length which enclose it. If no interval is specified, the original span is returned.

fixSmartDate :: Day -> SmartDate -> Day Source

Convert a SmartDate to an absolute date using the provided reference date.

fixSmartDateStr :: Day -> String -> String Source

Convert a smart date string to an explicit yyyy/mm/dd string using the provided reference date, or raise an error.

fixSmartDateStrEither :: Day -> String -> Either ParseError String Source

A safe version of fixSmartDateStr.

fixSmartDateStrEither' :: Day -> String -> Either ParseError Day Source

daysInSpan :: DateSpan -> Maybe Integer Source

Count the days in a DateSpan, or if it is open-ended return Nothing.

maybePeriod :: Day -> String -> Maybe (Interval, DateSpan) Source

mkdatespan :: String -> String -> DateSpan Source

Make a datespan from two valid date strings parseable by parsedate (or raise an error). Eg: mkdatespan "201111" "20111231".