| Copyright | (C) 2017 Jason Johnson |
|---|---|
| License | BSD-style (see the file LICENSE) |
| Maintainer | Jason Johnson <jason.johnson.081@gmail.com> |
| Stability | experimental |
| Portability | POSIX, Windows |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
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 2024Just (fromJust (Gregorian.calendarDate 14 February 2024))>>>calendarDate 30 February 2024Nothing>>>dayOfWeek <$> calendarDate 14 February 2024Just 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
- data DayNth
- = FourthToLast
- | ThirdToLast
- | SecondToLast
- | Last
- | First
- | Second
- | Third
- | Fourth
- | Fifth
- type Year = Int
- type WeekNumber = Int
- type DayOfMonth = Int
- type CalendarDate cal = Date cal
- class HasDate d
- type family DoW d
- type family MoY d
- day :: HasDate d => d -> DayOfMonth
- month :: HasDate d => d -> MoY d
- year :: HasDate d => d -> Year
- dayOfWeek :: HasDate d => d -> DoW d
- next :: HasDate d => Int -> DoW d -> d -> d
- previous :: HasDate d => Int -> DoW d -> d -> d
- yearMonthDay :: HasDate d => d -> (Year, MoY d, DayOfMonth)
- withCalendar :: (IsCalendarDateTime a, IsCalendarDateTime b) => CalendarDate a -> CalendarDate b
Documentation
Used by several smart constructors to chose a day relative to the start or end of the month.
Constructors
| FourthToLast | |
| ThirdToLast | |
| SecondToLast | |
| Last | |
| First | |
| Second | |
| Third | |
| Fourth | |
| Fifth |
type WeekNumber = Int Source #
type DayOfMonth = 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.
Minimal complete definition
day, setDay, month, setMonthIndex, year, setYear, dayOfWeek, next, previous
Instances
| IsCalendar cal => HasDate (CalendarDateTime cal) Source # | |
Defined in Data.HodaTime.CalendarDateTime.Internal Methods day :: CalendarDateTime cal -> DayOfMonth Source # setDay :: DayOfMonth -> CalendarDateTime cal -> CalendarDateTime cal month :: CalendarDateTime cal -> MoY (CalendarDateTime cal) Source # setMonthIndex :: Int -> CalendarDateTime cal -> CalendarDateTime cal year :: CalendarDateTime cal -> Year Source # setYear :: Year -> CalendarDateTime cal -> CalendarDateTime cal dayOfWeek :: CalendarDateTime cal -> DoW (CalendarDateTime cal) Source # next :: Int -> DoW (CalendarDateTime cal) -> CalendarDateTime cal -> CalendarDateTime cal Source # previous :: Int -> DoW (CalendarDateTime cal) -> CalendarDateTime cal -> CalendarDateTime cal Source # yearMonthDay :: CalendarDateTime cal -> (Year, MoY (CalendarDateTime cal), DayOfMonth) Source # | |
Instances
| type DoW (CalendarDateTime cal) Source # | |
Defined in Data.HodaTime.CalendarDateTime.Internal | |
Instances
| type MoY (CalendarDateTime cal) Source # | |
Defined in Data.HodaTime.CalendarDateTime.Internal | |
day :: HasDate d => d -> DayOfMonth Source #
Day-of-month 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 2000Monday
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).