This is a WG2 proposal for date and time arithmetic, loosely based on Java's Joda Time functions. It's possible to implement parts of SRFI 19 on top of it, but it provides much more flexibility. See also TimePeriodsCowan and TimeFormattingCowan.
For the purposes of this proposal, an instant is an exact integer representing a particular second of the TAI scale, such that 0 represents midnight on January 1, 1970 TAI (equivalent to ten seconds before midnight Universal Time) and the value 1 represents one TAI second later. The current instant can be obtained more or less accurately by evaluating (round (current-second)).
A chronology is an immutable member of a disjoint type that describes a particular calendar, such as the ISO, Gregorian, Julian, TAI, Jewish, Islamic, Persian, French Revolutionary, Maya, Chinese, Buddhist, Thai, Coptic, Ethiopic, or Martian calendar. The chronologies primitively supported by an application are called the base chronologies. Implementations MUST support the ISO chronology, and MAY support any of the other calendars mentioned here, or indeed any calendar not mentioned here. (The ISO chronology is similar to the Gregorian chronology: see the "ISO date fields" section for detailed differences.)
Chronologies also incorporate the concept of time zone. An implementation MUST support the UTC (Universal Coordinated Time) time zone and any time zones expressible as a fixed offset in minutes from UTC. It SHOULD support the historical time zones of the tz database.
(current-chronology)
A parameter representing a chronology used by default. Its initial value should normally represent the user's preferred calendar and current time zone. It can be rebound with parameterize.
(base-chronology name)
Returns a base chronology named by the symbol name. The ISO chronology is named iso. If provided, the Gregorian chronology is named gregorian, the Julian chronology is named julian, and the TAI chronology is named tai. All these chronologies are proleptic (extend into the past before the date they were adopted). The names and behaviors of other chronologies are implementation-dependent. The chronologies returned by this procedure are in the UTC time zone, unless they are meant for planets other than Earth, in which case the nearest equivalent is used. If the name is not known to the implementation, an error is signalled.
(base-chronology-names)
Returns a list of the symbols naming the base chronologies provided by the implementation. It is an error to modify this list.
(chronology-name chronology)
Returns the symbol that names chronology, or #f if it is not a base chronology.
(chronology-fields chronology)
Returns a list of the symbols naming the possible fields in date objects backed by this chronology (see below). It is an error to modify this list.
(chronology? obj)
Returns #t if obj is a chronology and #f otherwise.
(derive-chronology [ chronology ] time-zone)
Returns a chronology based on chronology, by default the value of (current-chronology), but using the time zone specified by timezone. In the ISO and Gregorian chronologies, if timezone is an integer, it represents the number of minutes ahead of UTC, but if the implementation supports the tz database, and time-zone is a string containing a time zone name defined by that database, it represents the time zone with that name. Otherwise, the interpretation of timezone is implementation-dependent. Derived chronologies do not have names.
(chronology-time-zone chronology)
Returns a time-zone value which, when applied to the base chronology of chronology using derive-chronology, would return an equivalent chronology, or #f if no such time zone value exists.
(make-compound-chronology chronology1 chronology2 instant)
Returns a chronology that uses chronology2 at all times before instant, and chronology1 for all times including and following instant. This is useful for constructing chronologies that transition from Julian to Gregorian or from one timezone to another at a specified date. Compound chronologies do not have names.
A date object is an immutable member of a disjoint type that specifies information about a date or time with respect to a certain chronology. For example, with respect to the ISO, Gregorian, or Julian chronologies, a date may represent a specific year, a specific week of a specific year, or an instant in time precise to a second. On the other hand, a date object may contain just a particular month. Date objects have multiple numeric-valued fields such as year or minute-of-day, whose meanings and possible values are determined by the chronology.
(make-date [ chronology ] alist)
Returns a date object using chronology, defaulting to the value of (current-chronology). Alist is an association list that maps symbols which are field names meaningful to chronology to associated numeric values. An error is signaled if the field values are invalid or mutually inconsistent.
(date? obj)
Returns #t if obj is a date object, and #f otherwise.
(date->alist date)
Returns a newly allocated alist containing the fields of date. Implementations SHOULD provide computed fields as well as explicitly set ones.
(date-field date fieldname)
Returns the numeric value of the field named fieldname (a symbol) within date, or #f if there is no such field. If the specified field was not provided when date was constructed, and it is possible to compute its value from the values of fields that were provided, the value is computed and returned.
(convert-date [ chronology ] date)
Equivalent to (make-date [ chronology ] (date->alist date)), but potentially much more efficient.
(date-update date fieldname value)
Returns a date object based on date, but with the field named fieldname updated to value. An error is signaled if the field is unknown or the value is out of range.
(date-increment date fieldname increment)
Returns a date object which is later than date by increment measured in the units specified by fieldname, or earlier if increment is negative. Returns #f if an appropriate date object cannot be constructed due to lack of information in date An error is signaled if fieldname is unknown.
For example, (date-increment date 'day-of-month 7) adds seven days to date (which is typically the same as one week, but may be different in some chronologies).
(date-chronology date)
Returns the chronology associated with date.
(date-field-maximum date fieldname)
(date-field-minimum date fieldname)
Returns the maximum or minimum legal value of the field named fieldname in the chronology associated with date. This value is not necessarily the same for all instants; for example, 28 is the maximum value of day-of-month if month has the value 2 (February). Returns #f if the value cannot be determined (there is no maximum or minimum year in the ISO chronology, for example).
(date-round date fieldname)
(date-ceiling date fieldname)
(date-floor date fieldname)
(date-truncate date fieldname)
Returns a date object which is the same as date, but adjusted to the nearest integral value of fieldname using the round, ceiling, floor, or truncate functions. This may cause other fields to change their values as well.
The values of these fields MUST be either exact integers or #f if they were not explicitly set and cannot be calculated from the given information. These fields are specified for the ISO, Gregorian, and Julian chronologies, but they may be relevant to other chronologies as well; it is easiest to use a chronology if it supports the standard fields. For interoperability, every chronology must support the instant field. The field names come from Joda Time, and ultimately from ISO 8601.