srfi-19

Time Data Types and Procedures

  1. srfi-19
  2. Documentation
    1. Core Procedures
      1. SRFI-19 Document Changes
        1. make-date
        2. read-leap-second-table
        3. leap-year?
      2. SRFI-18 Time
        1. time->srfi-18-time
        2. srfi-18-time->time
      3. Time Conversion
        1. seconds->time/type
        2. seconds->date/type
        3. time->nanoseconds
        4. nanoseconds->time
        5. nanoseconds->seconds
        6. time->milliseconds
        7. milliseconds->time
        8. milliseconds->seconds
        9. time->date
        10. time->julian-day
        11. time->modified-julian-day
      4. Time Arithmetic
        1. make-duration
        2. divide-duration
        3. divide-duration!
        4. multiply-duration
        5. multiply-duration!
        6. time-negative?
        7. time-positve?
        8. time-zero?
        9. time-abs
        10. time-abs!
        11. time-negate
        12. time-negate!
      5. Time Comparison
        1. time-compare
        2. time-max
        3. time-min
      6. Dates
        1. default-date-clock-type
        2. copy-date
        3. date->time
        4. date-zone-name
        5. date-dst?
      7. Date Arithmetic
        1. date-difference
        2. date-add-duration
        3. date-subtract-duration
      8. Date Comparison
        1. date-compare
        2. date=?
        3. date>?
        4. date<?
        5. date>=?
        6. date<=?
      9. Timezone
        1. local-timezone-locale
        2. utc-timezone-locale
        3. timezone-locale-name
        4. timezone-locale-offset
        5. timezone-locale-dst?
    2. Time Period
      1. make-null-time-period
      2. make-time-period
      3. copy-time-period
      4. time-period-begin
      5. time-period-end
      6. time-period-last
      7. time-period-type
      8. time-period?
      9. time-period-null?
      10. time-period-length
      11. time-period-compare
      12. time-period=?
      13. time-period<?
      14. time-period>?
      15. time-period<=?
      16. time-period>=?
      17. time-period-preceding
      18. time-period-succeeding
      19. time-period-contains/period?
      20. time-period-contains/time?
      21. time-period-contains/date?
      22. time-period-contains?
      23. time-period-intersects?
      24. time-period-intersection
      25. time-period-union
      26. time-period-span
      27. time-period-shift
      28. time-period-shift!
    3. Input/Output Procedures
      1. format-date
      2. scan-date
  3. Usage
  4. Examples
  5. Notes
  6. Requirements
  7. Bugs and Limitations
  8. Author
  9. Version history
  10. License

Documentation

This is a Chicken port of SRFI-19. This document only describes the extensions. For the SRFI-19 API see SRFI-19.

Core Procedures

(require-extension srfi-19-core)

SRFI-19 Document Changes

The nanosecond time object element is an integer between 0 and 999,999,999 inclusive. (The SRFI-19 document mis-states the value.)

A tz-offset value follows ISO 8601; positive for east of UTC, and negative for west. This is the opposite of the POSIX TZ environment variable.

Where the SRFI-19 document states a tz-offset argument a timezone-components object is also legal.

The string->date procedure allows the template-name argument to be optional. When missing the locale's date-time-format string is used. The supplied locale bundle's strings are invertible.

make-date
[procedure] (make-date NANOSECOND SECOND MINUTE HOUR DAY MONTH YEAR ZONE-OFFSET [TZ-NAME #f] [DST-FLAG #f])

Same as SRFI-19 except for the optional parameters and allowing a timezone-components object for the ZONE-OFFSET.

read-leap-second-table
[procedure] (read-leap-second-table FILENAME)

Sets the leap second table from the specified FILENAME.

The file format is the same as the "tai-utc.dat" file in the distribution. Provided by the U.S. Naval Observatory.

leap-year?
[procedure] (leap-year? DATE)

Does the specified DATE fall on a leap year?

SRFI-18 Time

Due to conflicts between SRFI-18 and SRFI-19 procedure variables srfi-19:current-time is a synonym for current-time and srfi-19:time? is a synonym for time?.

time->srfi-18-time
[procedure] (time->srfi-18-time TIME)

Converts a SRFI-19 time object to a SRFI-18 time object. The conversion is really only meaningful for time-duration, but any time-type is accepted.

srfi-18-time->time
[procedure] (srfi-18-time->time TIME)

Converts a SRFI-18 time object into a SRFI-19 time-duration object.

Time Conversion

seconds->time/type
[procedure] (seconds->time/type SECONDS [TIME-TYPE time-duration])

Converts a SECONDS value, may be fractional, into a TIME-TYPE time object.

seconds->date/type
[procedure] (seconds->date/type SECONDS [TIMEZONE-INFO #f])

Converts a SECONDS value, which may be fractional, into a date object. The TIMEZONE-INFO is #t for the local timezone, #f for the utc timezone, or a timezone-components object.

SECONDS is relative to 00:00:00 January 1, 1970 UTC.

time->nanoseconds
[procedure] (time->nanoseconds TIME)

Returns the TIME object value as a nanoseconds value.

nanoseconds->time
[procedure] (nanoseconds->time NANOSECONDS [TIME-TYPE time-duration])

Returns the NANOSECONDS value as a time TIME-TYPE object.

nanoseconds->seconds
[procedure] (nanoseconds->seconds NANOSECONDS)

Returns the NANOSECONDS value as an inexact seconds value.

time->milliseconds
[procedure] (time->milliseconds TIME)

Returns the TIME object value as a milliseconds value.

milliseconds->time
[procedure] (milliseconds->time MILLISECONDS [TIME-TYPE time-duration])

Returns the MILLISECONDS value as a time TIME-TYPE object.

milliseconds->seconds
[procedure] (milliseconds->seconds MILLISECONDS)

Returns the MILLISECONDS value as an inexact seconds value.

time->date
[procedure] (time->date TIME)

Returns the TIME object value as a date. A shorthand for the (time-*->date...) procedures.

time->julian-day
[procedure] (time->julian-day TIME)

Returns the julian day for the TIME object.

time->modified-julian-day
[procedure] (time->modified-julian-day TIME)

Returns the modified julian day for the TIME object.

Time Arithmetic

make-duration
[procedure] (make-duration [#:days 0] [#:hours 0] [#:minutes 0] [#:seconds 0] [#:milliseconds 0] [#:microseconds 0] [#:nanoseconds 0])

Returns a time-object of clock-type time-duration where the seconds and nanoseconds values are calculated by summing the keyword arguments.

ONE-SECOND-DURATION and ONE-NANOSECOND-DURATION are pre-defined.

divide-duration
[procedure] (divide-duration DURATION NUMBER)

Returns a duration, from DURATION, divided by NUMBER, without remainder.

divide-duration!
[procedure] (divide-duration! DURATION NUMBER)

Returns DURATION, divided by NUMBER, without remainder.

multiply-duration
[procedure] (multiply-duration DURATION NUMBER)

Returns a duration, from DURATION, multiplied by NUMBER, truncated.

multiply-duration!
[procedure] (multiply-duration! DURATION NUMBER)

Returns DURATION, multiplied by NUMBER, truncated.

time-negative?
[procedure] (time-negative? TIME)

Is TIME negative?

A time object will never have a negative nanoseconds value.

time-positve?
[procedure] (time-positve? TIME)

Is TIME positive?

time-zero?
[procedure] (time-zero? TIME)

Is TIME zero?

time-abs
[procedure] (time-abs TIME)

Returns the absolute time value, from TIME.

time-abs!
[procedure] (time-abs! TIME)

Returns the absolute TIME value.

time-negate
[procedure] (time-negate TIME)

Returns the sign inverted time value, from TIME.

time-negate!
[procedure] (time-negate! TIME)

Returns the TIME sign inverted value.

Time Comparison

time-compare
[procedure] (time-compare TIME1 TIME2)

Returns -1, 0, or 1.

time-max
[procedure] (time-max TIME1 [TIME2...])

Returns the maximum time object from TIME1 TIME2....

time-min
[procedure] (time-min TIME1 [TIME2...])

Returns the minimum time object from TIME1 TIME2....

Dates

default-date-clock-type
[parameter] (default-date-clock-type [CLOCK-TYPE time-utc])

Sets or gets the clock-type used by default for conversion of a date to a time.

copy-date
[procedure] (copy-date DATE)

Returns an exact copy of the specified DATE object.

date->time
[procedure] (date->time DATE [CLOCK-TYPE (default-date-clock-type)])

Returns the specified DATE as a time-object of type CLOCK-TYPE.

date-zone-name
[procedure] (date-zone-name DATE)

Returns the timezone abbreviation of the specified DATE object. The result is either a string or #f.

date-dst?
[procedure] (date-dst? DATE)

Returns the daylight saving time flag of the specified DATE object.

Only valid for "current" dates. Historical dates will not have a correct setting. Future dates cannot have a correct setting.

Date Arithmetic

date-difference
[procedure] (date-difference DATE1 DATE2 [CLOCK-TYPE])

Returns the duration between DATE1 and DATE2.

date-add-duration
[procedure] (date-add-duration DATE DURATION [CLOCK-TYPE])

Returns the DATE plus the DURATION.

date-subtract-duration
[procedure] (date-subtract-duration DATE DURATION [CLOCK-TYPE])

Returns the DATE minus the DURATION.

Date Comparison

date-compare
[procedure] (date-compare DATE1 DATE2)

Returns -1, 0, or 1.

date=?
[procedure] (date=? DATE1 DATE2)

Is DATE1 on DATE2?

date>?
[procedure] (date>? DATE1 DATE2)

Is DATE1 after DATE2?

date<?
[procedure] (date<? DATE1 DATE2)

Is DATE1 before DATE2?

date>=?
[procedure] (date>=? DATE1 DATE2)

Is DATE1 after or on DATE2?

date<=?
[procedure] (date<=? DATE1 DATE2)

Is DATE1 before or on DATE2?

Timezone

Remember that SRFI-19 timezone offset follows ISO 8601.

local-timezone-locale
[parameter] (local-timezone-locale [TZ-COMPONENTS])

Gets or sets the local timezone-locale object.

utc-timezone-locale
[parameter] (utc-timezone-locale [TZ-COMPONENTS])

Gets or sets the utc timezone-locale object.

Probably not a good idea to change the value.

timezone-locale-name
[procedure] (timezone-locale-name [TZ-COMPONENTS])

Returns the timezone-locale name of the supplied TZ-COMPONENTS, or the (local-timezone-locale) if missing.

timezone-locale-offset
[procedure] (timezone-locale-offset [TZ-COMPONENTS])

Returns the timezone-locale offset of the supplied TZ-COMPONENTS, or the (local-timezone-locale) if missing.

timezone-locale-dst?
[procedure] (timezone-locale-dst? [TZ-COMPONENTS])

Returns the timezone-locale daylight saving time flag of the supplied TZ-COMPONENTS, or the (local-timezone-locale) if missing.

Time Period

(require-extension srfi-19-period)

A time-period is an interval, [begin end), where begin and end are time objects of the same clock type. When end <= begin the interval is null.

make-null-time-period
[procedure] (make-null-time-period [CLOCK-TYPE (default-date-clock-type)])

Returns a null interval for the specified CLOCK-TYPE.

make-time-period
[procedure] (make-time-period BEGIN END [CLOCK-TYPE (default-date-clock-type)])

Returns a new time-period object. The clock types must be compatible.

BEGIN maybe a seconds value, a date, or a time (except time-duration). A seconds value or date are converted to CLOCK-TYPE.

END maybe a seconds value, a date, or a time. A seconds value or date are converted to the same clock type as BEGIN. A time-duration is treated as an offset from BEGIN.

copy-time-period
[procedure] (copy-time-period TIME-PERIOD)

Returns a copy of TIME-PERIOD.

time-period-begin
[procedure] (time-period-begin TIME-PERIOD)

Returns the start time for the TIME-PERIOD.

time-period-end
[procedure] (time-period-end TIME-PERIOD)

Returns the end time for the TIME-PERIOD.

time-period-last
[procedure] (time-period-last TIME-PERIOD)

Returns the last time for the TIME-PERIOD; (time-period-end - 1ns).

time-period-type
[procedure] (time-period-type TIME-PERIOD)

Returns the clock-type of the TIME-PERIOD.

time-period?
[procedure] (time-period? OBJECT)

Is OBJECT a time-period?

time-period-null?
[procedure] (time-period-null? TIME-PERIOD)

Is the TIME-PERIOD null?

time-period-length
[procedure] (time-period-length TIME-PERIOD)

Returns the time-duration of the TIME-PERIOD.

time-period-compare
[procedure] (time-period-compare TIME-PERIOD-1 TIME-PERIOD-2) => INTEGER

Returns -1 when TIME-PERIOD-1 < TIME-PERIOD-2, 0 when TIME-PERIOD-1 = TIME-PERIOD-2 and 1 TIME-PERIOD-1 > TIME-PERIOD-2.

time-period=?
[procedure] (time-period=? TIME-PERIOD-1 TIME-PERIOD-2)

Does TIME-PERIOD-1 begin & end with TIME-PERIOD-2?

time-period<?
[procedure] (time-period<? TIME-PERIOD-1 TIME-PERIOD-2)

Does TIME-PERIOD-1 end before TIME-PERIOD-2 begins?

time-period>?
[procedure] (time-period>? TIME-PERIOD-1 TIME-PERIOD-2)

Does TIME-PERIOD-1 begin after TIME-PERIOD-2 ends?

time-period<=?
[procedure] (time-period<=? TIME-PERIOD-1 TIME-PERIOD-2)

Does TIME-PERIOD-1 end on or before TIME-PERIOD-2 begins?

time-period>=?
[procedure] (time-period>=? TIME-PERIOD-1 TIME-PERIOD-2)

Does TIME-PERIOD-1 begin on or after TIME-PERIOD-2 ends?

time-period-preceding
[procedure] (time-period-preceding TIME-PERIOD-1 TIME-PERIOD-2)

Return the portion of TIME-PERIOD-1 before TIME-PERIOD-2 or #f when it doesn't precede.

time-period-succeeding
[procedure] (time-period-succeeding TIME-PERIOD-1 TIME-PERIOD-2)

Return the portion of TIME-PERIOD-1 after TIME-PERIOD-2 or #f when it doesn't succeed.

time-period-contains/period?
[procedure] (time-period-contains/period? TIME-PERIOD-1 TIME-PERIOD-2)

Is TIME-PERIOD-2 within TIME-PERIOD-1?

time-period-contains/time?
[procedure] (time-period-contains/time? TIME-PERIOD TIME)

Is TIME within TIME-PERIOD?

TIME is converted to a compatible clock-type if possible.

time-period-contains/date?
[procedure] (time-period-contains/date? TIME-PERIOD DATE)

Is DATE within TIME-PERIOD?

DATE is converted to a compatible time if possible.

time-period-contains?
[procedure] (time-period-contains? TIME-PERIOD OBJECT)

Is OBJECT within TIME-PERIOD?

OBJECT maybe a time, date, or time-period.

time-period-intersects?
[procedure] (time-period-intersects? TIME-PERIOD-1 TIME-PERIOD-2)

Does TIME-PERIOD-2 overlap TIME-PERIOD-1?

time-period-intersection
[procedure] (time-period-intersection TIME-PERIOD-1 TIME-PERIOD-2)

The overlapping time-period of TIME-PERIOD-2 and TIME-PERIOD-1, or #f when no overlap.

time-period-union
[procedure] (time-period-union TIME-PERIOD-1 TIME-PERIOD-2)

Returns the time-period spanned by TIME-PERIOD-1 and TIME-PERIOD-2, or #f when they do not intersect.

time-period-span
[procedure] (time-period-span TIME-PERIOD-1 TIME-PERIOD-2)

Returns the time-period spanned by TIME-PERIOD-1 and TIME-PERIOD-2, including any gaps.

time-period-shift
[procedure] (time-period-shift TIME-PERIOD DURATION)

Returns a copy of TIME-PERIOD shifted by DURATION.

time-period-shift!
[procedure] (time-period-shift! TIME-PERIOD DURATION)

Returns TIME-PERIOD shifted by DURATION.

Input/Output Procedures

(require-extension srfi-19-io)
format-date
[procedure] (format-date DESTINATION DATE-FORMAT-STRING [DATE])

Displays a text form of the DATE on the DESTINATION using the DATE-FORMAT-STRING.

When the destination is #t the (current-output-port) is used, and the date object must be specified.

When the destination is a string the DATE-FORMAT-STRING value must be a date object, the DESTINATION value is used as the DATE-FORMAT-STRING, and the result is returned as a string.

When the destination is a port it must be an output-port, and the date object must be specified. When the destination is a number the (current-error-port) is the destination, and the DATE object must be specified.

When the destination is #f the result is returned as a string, and the DATE object must be specified.

scan-date
[procedure] (scan-date SOURCE TEMPLATE-STRING)

Reads a text form of a date from the SOURCE, following the TEMPLATE-STRING, and returns a date object.

When the source is #t the (current-input-port) is used.

When the source is a port it must be an input-port.

When the source is string it should be a date text form.

Usage

This loads all modules.

(require-extension srfi-19

Examples

Notes

Requirements

locale srfi-29 miscmacros numbers

Bugs and Limitations

Author

kon lovett

Version history

2.9.1
Shared error routines. Added 'time-period-compare'.
2.9.0
Inlining of "leap-second-delta" algorithm. Needs release 0.5.0 of "locale".
2.8.1
Updated 'tai-utc.dat'.
2.8.0
Timezone locale creation removed. Removed 'make-local-timezone-locale'. Deprecated 'make-timezone-locale' & 'timezone-locale?'.
2.7.1
Fix for 'add/subtract-duration[!].
2.7.0
Replaced date comparison w/ a field by field algorithm. Removed local-timezone-info, local-timezone-name, local-timezone-offset, and local-timezone-dst?.
2.6.11
Printing of "xxx.0" => xxx. More use of fixnum ops in srfi-19-io.
2.6.10
Dropped :optional.
2.6.9
Needs Chicken 2.610 for MacOS X & Windows.
2.6.8
Bug fix for make-date supplied dst flag. Added Bugs section.
2.6.7
Uses fixnum arithmetic where possible. Added time->julian-day, time->modified-julian-day, date comparisons. Bug fix for multiply-duration, divide-duration, & make-duration. Changed read-leap-second-table to required filename parameter.
2.6.6
Bug fix for time<=? & time>=?. Added time-period-preceding & time-period-succeeding. Split periods into srfi-19-period.
2.6.5
Bug fix for platforms not MacOS X, timezone offset was sign reversed.
2.6.4
Bug fix to time ctors/setters - allowed negative nanoseconds. Added make-local-timezone-locale w/ special case for platforms not Windows or Macintosh. Made local-timezone-locale a parameter.
2.6.3
Made time-max & time-min n-ary. Added make-null-time-period, time-negative?, time-positive?, time-zero?. Bug fix for local-timezone-offset [reported by Hans Bulfone]
2.6.2
Bug fix for local-timezone-locale, seconds->date/type, current-nanoseconds, & current-date [reported by Hans Bulfone]
2.6.1
Deprecated local-timezone-info, local-timezone-name, local-timezone-offset, and local-timezone-dst?. Bug fix for milliseconds->time. Bug fix for current-date when no tz-locale. [reported by Graham Fawcett]
2.6
Added time-period, date arithmetic, duration routines, fix for possible seconds limit in time object
2.5
Added Dutch locale, ISO-8601 conversion bug fix, 'date-year-day' bug fix [thanks to Arno Peters]
2.4
Removed annoying warnings, made srfi-19 an umbrella - uses -core & -io
2.3
Date dst? field, timezone-locale structure accessors
2.2
Bug fix for no local timezone setting situation [reported by Mario Domenech Goulart]
2.1
Bug fix for ->fixnum [reported by Mario Domenech Goulart]
2.0
Removed I/O routines to own extension
1.9
Fix, in conjunction w/ srfi-29, for locale details
1.8
Version removed
1.7
Brazilian Portuguese [thanks to Mario Domenech Goulart]
1.6
Bug fix for inexact seconds in time->date [thanks to Peter Bex]
1.5
Bug fix for compiled use
1.4
Exports
1.3
Bug fix
1.2
Slightly smaller and faster
1.1
Some SRFI-18 conflict reduction
1.0
Initial release

License

Copyright (C) 2009 Kon Lovett. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Copyright (C) I/NET, Inc. (2000, 2002, 2003). All Rights Reserved. Copyright (C) Neodesic Corporation (2000). All Rights Reserved.

This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way, such as by removing the copyright notice or references to the Scheme Request For Implementation process or editors, except as needed for the purpose of developing SRFIs in which case the procedures for copyrights defined in the SRFI process must be followed, or as required to translate it into languages other than English.

The limited permissions granted above are perpetual and will not be revoked by the authors or their successors or assigns.

This document and the information contained herein is provided on an AS IS basis and THE AUTHOR AND THE SRFI EDITORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.