Time.Calendar

lib/Time/Calendar.gob

Day numbers of the proleptic Gregorian calendar: the conversion between a (year, month, day) and the count of days since 1970-01-01, which is how Date is stored. Everything here is total arithmetic; validating a date is the caller's job.

The conversions are Howard Hinnant's days_from_civil and civil_from_days. They shift the year to start on March 1, so that the leap day is the last day of its year and the month lengths from March on follow the line (153 * m + 2) / 5, then count in 400-year eras of exactly 146,097 days. Each era is handled with non-negative numbers only, which is why every division of a value that can be negative goes through floorDiv: / rounds toward zero, and would put day -1 in the wrong era.

type DayNumber : Ord

A day of the calendar a wall clock is read against, by its number. Unlike a Date it has no range: a time zone rule is worked out for the year either side of the dates there are.

fun fromEpochDays : fun(Int) -> DayNumber

The day n days after 1970-01-01, and back: for Date's public fromEpochDays and epochDays, and for Frame, which counts seconds from the same midnight. Everything else moves between days with the functions below.

fun epochDays : fun(DayNumber) -> Int
fun addDays : fun(DayNumber, Int) -> DayNumber
fun daysBetween : fun(DayNumber, DayNumber) -> Int

The number of days from from to to: negative if to is earlier.

fun weekday : fun(DayNumber) -> Weekday
fun onOrAfter : fun(DayNumber, Weekday) -> DayNumber

The first w on or after d.

let minYear : Int

The years a Date may have: four digits either side of year 0, the range ISO 8601 writes without an agreed expansion.

let maxYear : Int
fun isLeapYear : fun(Int) -> Bool
fun daysInMonth : fun(Int, Month) -> Int
fun floorDiv : fun(Int, Int) -> Int

Rounds toward negative infinity, unlike /. Written so that no step overflows, since instants use it on the whole range of Int.

fun daysFromCivil : fun(Int, Month, Int) -> DayNumber

The day number of a valid date.

fun civilFromDays : fun(DayNumber) -> (Int, Month, Int)

The date of a day number, as (year, month, day).