HORIZON HASKELLDocslts/ghc-10.0.x8c2f8e82026-10-19Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8c2f8e8 · 2026-10-19

Modulehledger-lib-1.52.4GHC2021

Hledger.Data.DayPartition

A partition of time into contiguous spans, for defining reporting periods.

  • 1 type
  • 13 values
  • Packagehledger-lib-1.52.4
  • Exports14
  • LanguageGHC2021
  • LicenceGPL-3.0-or-later
  • SourceDayPartition.hs
newtypenewtype DayPartition
#

A partition of time into one or more contiguous periods, plus a historical period that precedes them. Note DayPartition does not store per-period data - only the periods' start/end dates.

Instances3Eq, Ord, Show

constructors

2 declarations

Construct a DayPartition from a non-empty list of period boundary dates (start dates plus a final exclusive end date).

Example1 expression
boundariesToDayPartition (fromGregorian 2025 01 01 :| [fromGregorian 2025 02 01])DayPartition {dayPartitionToPeriodData = PeriodData{ pdpre = 2024-12-31, pdperiods = fromList [(2025-01-01,2025-01-31)]}}

conversions

5 declarations

operations

5 declarations

Find the start and end dates of the period within a DayPartition which contains a given day. If the day is after the end of the last period, it is assumed to be within the last period. If the day is before the start of the first period (ie, in the historical period), only the historical period's end date is returned.

Split a DateSpan into a DayPartition consisting of consecutive exact spans of the specified Interval, or Nothing if the span is invalid. If no interval is specified, the original span is returned. If the original span is the null date span, ie unbounded, Nothing is returned. If the original span is empty, eg if the end date is <= the start date, Nothing is returned.

Date adjustment

Some intervals respect the "adjust" flag (years, quarters, months, weeks, every Nth weekday of month seem to be the ones that need it). This will move the start date earlier, if needed, to the previous natural interval boundary (first of year, first of quarter, first of month, monday, previous Nth weekday of month). Related: #1982 #2218

The end date is always moved later if needed to the next natural interval boundary, so that the last period is the same length as the others.

Examples
Example14 expressions
let t i y1 m1 d1 y2 m2 d2 = fmap dayPartitionToNonEmpty . splitSpan True i $ DateSpan (Just $ Flex $ fromGregorian y1 m1 d1) (Just $ Flex $ fromGregorian y2 m2 d2)t NoInterval 2008 01 01 2009 01 01Just ((2008-01-01,2008-12-31) :| [])t (Quarters 1) 2008 01 01 2009 01 01Just ((2008-01-01,2008-03-31) :| [(2008-04-01,2008-06-30),(2008-07-01,2008-09-30),(2008-10-01,2008-12-31)])splitSpan True (Quarters 1) nulldatespanNothingt (Days 1) 2008 01 01 2008 01 01  -- an empty datespanNothingt (Quarters 1) 2008 01 01 2008 01 01Nothingt (Months 1) 2008 01 01 2008 04 01Just ((2008-01-01,2008-01-31) :| [(2008-02-01,2008-02-29),(2008-03-01,2008-03-31)])t (Months 2) 2008 01 01 2008 04 01Just ((2008-01-01,2008-02-29) :| [(2008-03-01,2008-04-30)])t (Weeks 1) 2008 01 01 2008 01 15Just ((2007-12-31,2008-01-06) :| [(2008-01-07,2008-01-13),(2008-01-14,2008-01-20)])t (Weeks 2) 2008 01 01 2008 01 15Just ((2007-12-31,2008-01-13) :| [(2008-01-14,2008-01-27)])t (MonthDay 2) 2008 01 01 2008 04 01Just ((2008-01-02,2008-02-01) :| [(2008-02-02,2008-03-01),(2008-03-02,2008-04-01)])t (NthWeekdayOfMonth 2 4) 2011 01 01 2011 02 15Just ((2010-12-09,2011-01-12) :| [(2011-01-13,2011-02-09),(2011-02-10,2011-03-09)])t (DaysOfWeek [2]) 2011 01 01 2011 01 15Just ((2010-12-28,2011-01-03) :| [(2011-01-04,2011-01-10),(2011-01-11,2011-01-17)])t (MonthAndDay 11 29) 2012 10 01 2013 10 15Just ((2012-11-29,2013-11-28) :| [])

Get the natural start for the given interval that falls on or before the given day, when applicable. Works for Weeks, Months, Quarters, Years, eg.

tests

1 declaration