hodatime
Copyright(C) 2017 Jason Johnson
LicenseBSD-style (see the file LICENSE)
MaintainerJason Johnson <jason.johnson.081@gmail.com>
Stabilityexperimental
PortabilityPOSIX, Windows
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.HodaTime.CalendarDate

Description

This is the module for CalendarDate. A CalendarDate represents a date within the calendar system that is part of its type. It has no reference to a particular time zone or time of day.

Construction

To construct one of these types, see the Calendar module you wish to construct the date in (typically Data.HodaTime.Calendar.Gregorian)

Cookbook

Building and inspecting a date

Dates are built with a calendar's smart constructor, which returns Nothing for a date that does not exist. Read-only components (such as the day of the week) are plain functions.

>>> calendarDate 14 February 2024
Just (fromJust (Gregorian.calendarDate 14 February 2024))
>>> calendarDate 30 February 2024
Nothing
>>> dayOfWeek <$> calendarDate 14 February 2024
Just Wednesday

Re-expressing a date in another calendar

withCalendar re-expresses a date in another calendar, preserving the same day on the absolute timeline; the target calendar is chosen by the result type. Here the Gregorian Christmas becomes the Julian ("Old Calendar") date still used liturgically, which the Julian calendar labels 12 December 2024 (it runs thirteen days behind).

>>> withCalendar <$> calendarDate 25 December 2024 :: Maybe (CalendarDate Julian.Julian)
Just (fromJust (Julian.calendarDate 12 December 2024))

USA holidays for a given year

import Data.Maybe (catMaybes)
import Data.HodaTime.CalendarDate (DayNth(..))
import Data.HodaTime.Calendar.Gregorian (calendarDate, fromNthDay, Month(..), DayOfWeek(..))

usaHolidays y = catMaybes $ (\$ y) \<$\>
  [
     calendarDate 1 January               -- New Year
    ,calendarDate 4 July                  -- Independence Day
    ,calendarDate 25 December             -- Christmas
    ,fromNthDay First Monday September    -- Labor day
    ,fromNthDay Third Monday January      -- MLK day
    ,fromNthDay Second Tuesday February   -- Presidents day
    ,fromNthDay Fourth Thursday November  -- Thanksgiving
    ,calendarDate 29 February             -- Leap day (not a real holiday, but shows a date that may not exist)
  ]
Synopsis

Documentation

data DayNth Source #

Used by several smart constructors to chose a day relative to the start or end of the month.

type Year = Int Source #

type CalendarDate cal = Date cal Source #

A calendar date in the calendar system cal. This is a public synonym for the per-calendar representation Date: each calendar defines its own data instance Date cal (see IsCalendar), so unrelated calendars (e.g. Gregorian and Hebrew) need share nothing in how a date is stored.

type family DoW d Source #

Instances

Instances details
type DoW (CalendarDateTime cal) Source # 
Instance details

Defined in Data.HodaTime.CalendarDateTime.Internal

type DoW (CalendarDateTime cal) = DayOfWeek cal

type family MoY d Source #

Instances

Instances details
type MoY (CalendarDateTime cal) Source # 
Instance details

Defined in Data.HodaTime.CalendarDateTime.Internal

type MoY (CalendarDateTime cal) = Month cal

day :: HasDate d => d -> DayOfMonth Source #

Day-of-month component.

month :: HasDate d => d -> MoY d Source #

Accessor for the Month component of a HasDate.

year :: HasDate d => d -> Year Source #

Year component.

dayOfWeek :: HasDate d => d -> DoW d Source #

Accessor for the Day of the week enum of a HasDate, for example:

>>> dayOfWeek . fromJust $ Gregorian.calendarDate 31 January 2000
Monday

next :: HasDate d => Int -> DoW d -> d -> d Source #

Returns a HasDate shifted to the nth next Day of Week from the current HasDate, for example:

>>> next 1 Monday . fromJust $ Gregorian.calendarDate 31 January 2000
fromJust (Gregorian.calendarDate 7 February 2000)

previous :: HasDate d => Int -> DoW d -> d -> d Source #

Returns a HasDate shifted to the nth previous Day of Week from the current HasDate, for example:

>>> previous 1 Monday . fromJust $ Gregorian.calendarDate 31 January 2000
fromJust (Gregorian.calendarDate 24 January 2000)

yearMonthDay :: HasDate d => d -> (Year, MoY d, DayOfMonth) Source #

Access the year, month and day-of-month components together in a single call, returned as a (year, month, day) tuple.

This is purely an access optimization for code that needs more than one date component at once. Reading the components individually with year, month and day is perfectly correct, but for a packed representation (such as the Gregorian Date, which stores a cycle/century/day-in-century triple) each of those accessors independently decodes the stored value, so asking for all three separately decodes it three times. yearMonthDay decodes once and hands back every component, which is noticeably cheaper on hot paths (for example date formatting). For representations that already store the components separately (such as the Julian Date) there is nothing to decode and this is simply the three field reads, so it is never slower than the individual accessors and callers can use it unconditionally.

withCalendar :: (IsCalendarDateTime a, IsCalendarDateTime b) => CalendarDate a -> CalendarDate b Source #

Re-express a CalendarDate in a different calendar, preserving the same day on the absolute timeline. For example, convert a Gregorian date to the Julian ("Old Calendar") date still used liturgically by the Eastern Orthodox church. The target calendar is chosen by the result type (via TypeApplications or a type annotation).