Time.Period

lib/Time/Period.gob

An amount of calendar time: whole months and whole days, each counted on the calendar rather than as a fixed length. One month from January 31 is February 28 or 29; one day across a change to daylight saving time is 23 or 25 hours. An amount of elapsed time is a Duration instead.

Years and weeks are not stored. A year is twelve months and a week is seven days in every case the ISO calendar has, so years(1) == months(12) and shifting by either always gives the same date. Keeping them as fields would only add ways to write the same period.

The fields need not share a sign. Shifting applies the months first, then the days, so months(1) + days(-1) from January 31 is February 27 or 28.

There is deliberately no Ord: whether a month is longer than 30 days depends on which month, so periods have no order that holds everywhere.

type Period = Period { months : Int, days : Int }
fun years : fun(Int) -> Period
fun months : fun(Int) -> Period
fun weeks : fun(Int) -> Period
fun days : fun(Int) -> Period
let zero : Period
fun parse : fun(String) -> Option Period

ISO 8601 date-based durations: P1Y2M3W4D, with any subset of the fields in that order, each optionally signed, and an optional sign before the P that negates the whole. Weeks become days. A time part (T…) is not a period.

instance Add Period
instance Sub Period
instance Neg Period
instance Show Period

ISO 8601: P1Y2M10D, P0D for zero. A negative field carries its own sign, P-1Y-2M, since the fields need not agree; a period that is negative throughout still reads that way rather than as -P1Y2M, so that every period has one spelling.