chickadee » srfi-19

srfi-19

Time Data Types and Procedures

This is a Chicken port of SRFI-19. This document describes the original SRFI API (SRFI-19) and the extensions (*).

TOC »

Abstract

Points in time are represented as the number of seconds (with nanosecond precision) since "the epoch," a zero point in time. Several standard variants are defined, including UTC (universal coordinated time), TAI (international atomic time), and monotonic time. A point in time can also be represented as a Julian Day or Modified Julian Day number. Time durations, including time spent in a process or thread, are defined. Conversion routines are provided. The procedure CURRENT-TIME queries the current time in a specified variant, with a system-dependent resolution. Procedures for time arithmetic and time comparisons are also provided.

A date is a representation of a point in time in the Gregorian calendar, a 24 hour clock (with nanosecond precision) and a time zone offset from UTC. Procedures for converting between time and dates are provided, as well as for reading and writing string representations of dates.

Specification

A Time object, which is distinct from all existing types, defines a point in time or a time duration in some standard time system. The standard time systems are:

Time duration.

A time object consists of three components:

A Date object, which is distinct from all existing types, represents a point in time as represented by the Gregorian calendar as well as by a time zone. Dates are immutable. A date consists of the following components:

A Julian Day represents a point in time as a real number of days since -4713-11-24T12:00:00Z (midday UT on 24 November 4714 BC in the proleptic Gregorian calendar (1 January 4713 BC in the proleptic Julian calendar)).

A Modified Julian Day represents a point in time as a real number of days since 1858-11-17T00:00:00Z (midnight UT on 17 November AD 1858).

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; see Timezone

Documentation

Usage

(import srfi-19)
(import srfi-19-core)

Time

(import srfi-19-time)

Constants

time-duration
time-durationconstant

Symbol representing Time duration.

time-monotonic
time-monotonicconstant

Symbol representing monotonic time.

time-process
time-processconstant

Symbol representing time spent in current process.

time-tai
time-taiconstant

Symbol representing TAI time.

time-thread
time-threadconstant

Symbol representing time spent in current thread.

time-utc
time-utcconstant

Current time and clock resolution

current-time
current-time #!optional TIME-TYPEprocedure

Current time, of type time-type system, which defaults to TIME-UTC.

monotonic-time (*)
utc-time (*)
tai-time (*)
gc-time (*)
process-time (*)
thread-time (*)
monotonic-timeprocedure
utc-timeprocedure
tai-timeprocedure
gc-timeprocedure
process-timeprocedure
thread-timeprocedure

Return current-time for the named time type.

From Chibi (srfi 19).

time-resolution
time-resolution #!optional TIME-TYPEprocedure

Clock resolution, in nanoseconds, of the system clock of type type time-type system, which defaults to TIME-UTC.

Time object and accessors

make-time
make-time TYPE NANOSECOND SECONDprocedure

Creates a time object.

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

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

time?
time? OBJECTprocedure

#t if object is a time object, otherwise, #f.

time-type
time-type TIMEprocedure

Time type.

time-nanosecond
time-nanosecond TIMEprocedure

Time nanosecond.

time-second
time-second TIMEprocedure

Time second.

set-time-type!
set-time-type! TIME TIME-TYPEprocedure

Changes time type. Note: This changes the semantics of the time object. To convert a time to another system of representation, use one of the conversion procedures.

set-time-nanosecond!
set-time-nanosecond! TIME INTEGERprocedure

Changes time nanosecond.

set-time-second!
set-time-second! TIME INTEGERprocedure

Changes time second.

time-hash (*)
time-hash TIME #!optional BOUNDprocedure

Hash value for TIME within BOUND.

copy-time
time-copy (*)
copy-time TIMEprocedure
time-copy TIMEprocedure

Creates a new time, with the same time type, nanosecond, and second as TIME.

Time comparison procedures

All of the time comparison procedures require the time objects to be of the same type. It is an error to use these procedures on time objects of different types. For the point-in-time measurements (e.g., TIME-TAI and TIME-UTC), the semantics are described in plain text. For durations, (e.g., TIME-DURATION, TIME-CPU, the semantics are described in parentheses.

time-compare (*)
time-compare TIME1 TIME2procedure

Returns negative, zero, or positive.

time-min (*)
time-max (*)
time-min TIME1 #!optional TIME2...procedure
time-max TIME1 #!optional TIME2...procedure

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

time<=?
time<=? TIME1 TIME2procedure

#t if time1 is before or at (less than or equal to) time2, #f otherwise.

time<?
time<? TIME1 TIME2procedure

#t if time1 is before (less than) time2, #f otherwise.

time=?
time=? TIME1 TIME2procedure

#t if time1 at (equal) time2, #f otherwise.

time>=?
time>=? TIME1 TIME2procedure

#t if time1 is at or after (greater than or equal to) time2, #f otherwise.

time>?
time>? TIME1 TIME2procedure

#t if time1 is after (greater than) time2, #f otherwise.

Time arithmetic procedures

one-second-duration (*)
one-second-durationprocedure
one-nanosecond-duration (*)
one-nanosecond-durationprocedure
zero-time (*)
(zero-time [TIME-TYPE time-duration]) -> timeprocedure
time-difference
time-difference TIME1 TIME2procedure

The TIME-DURATION between time1 and time2. It is an error if time1 and time2 are of different time types. A new time object is created.

time-difference!
time-difference! TIME1 TIME2procedure

The TIME-DURATION between time1 and time2. It is an error if time1 and time2 are of different time types. Time1 may be used to create the resulting TIME-DURATION object.

add-duration
add-duration TIME1 TIME-DURATIONprocedure

The time resulting from adding time-duration to time1, which is a time object of the same time type as time1. A new time object is created.

add-duration!
add-duration! TIME1 TIME-DURATIONprocedure

The time resulting from adding time-duration to time1, which is a time object of the same time type as time1. Time1 may used to create the resulting time object.

subtract-duration
subtract-duration TIME1 TIME-DURATIONprocedure

The time resulting from subtracting time-duration to time1, which is a time object of the same time type as time1. A new time object is created.

subtract-duration!
subtract-duration! TIME1 TIME-DURATIONprocedure

The time resulting from subtracting time-duration to time1, which is a time object of the same time type as time1. Time1 may used to create the resulting time object.

divide-duration (*)
divide-duration! (*)
divide-duration DURATION REALprocedure
divide-duration! DURATION REALprocedure

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

multiply-duration (*)
multiply-duration! (*)
multiply-duration DURATION REALprocedure
multiply-duration! DURATION REALprocedure

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

time-negative? (*)
time-positve? (*)
time-zero? (*)
time-negative? TIMEprocedure
time-positve? TIMEprocedure
time-zero? TIMEprocedure

Is TIME negative/positive/zero?

nonoseconds seconds where +/- - is negative, +/- + is positive, & 0 0 is zero.

time-abs (*)
time-abs! (*)
time-abs TIMEprocedure
time-abs! TIMEprocedure

Returns the absolute time value, from TIME.

time-negate (*)
time-negate! (*)
time-negate TIMEprocedure
time-negate! TIMEprocedure

Returns the sign inverted time value, from TIME.

Only time-second participates.

Time Conversion (*)

seconds->time
(seconds->time SECONDS [TIME-TYPE time-duration]) -> timeprocedure

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

seconds->date
(seconds->date SECONDS [TIMEZONE-INFO #t]) -> dateprocedure

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
time->nanoseconds TIMEprocedure

Returns the TIME object value as a nanoseconds value.

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

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

nanoseconds->seconds
nanoseconds->seconds NANOSECONDSprocedure

Returns the NANOSECONDS value as a seconds value.

time->milliseconds
time->milliseconds TIMEprocedure

Returns the TIME object value as a milliseconds value.

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

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

milliseconds->seconds
milliseconds->seconds MILLISECONDSprocedure

Returns the MILLISECONDS value as a seconds value.

time->date
time->date TIMEprocedure

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

time->julian-day
time->julian-day TIMEprocedure

Returns the julian day for the TIME object.

time->modified-julian-day
time->modified-julian-day TIMEprocedure

Returns the modified julian day for the TIME object.

SRFI-18 Time (*)

Note that the SRFI-18 identifiers time?, current-time, seconds->time, time->seconds, milliseconds->time, and time->milliseconds are in conflict with those of SRFI-19.

time->srfi-18-time
time->srfi-18-time TIMEprocedure

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
srfi-18-time->time TIMEprocedure

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

Date

(import srfi-19-date)

Current date

current-date
current-date #!optional TZ-OFFSETprocedure

Date corresponding to the current UTC time.

current-julian-day
current-julian-dayprocedure

Current Julian Day.

current-modified-julian-day
current-modified-julian-dayprocedure

Current Modified Julian Day.

Date object and accessors

Date objects are immutable once created.

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

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

make-date (extended)
make-date NANOSECOND SECOND MINUTE HOUR DAY MONTH YEAR #!optional ZONE-OFFSET TZ-NAME DST-FLAGprocedure

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

The ZONE-OFFSET is an integer or timezone-components. Default is the (timezone-locale-offset), the current locale timezone offset.

The TZ-NAME is a string or #f, and is the timezone name. Default is #f.

The DST-FLAG is a boolean, and indicates whether Day Saving TIme (or Summer Time) is active. Default is #f.

When the ZONE-OFFSET is a timezone-components object the TZ-NAME and DST-FLAG are pulled from the timezone-components, unless explicitly supplied.

copy-date (*)
date-copy (*)
copy-date DATEprocedure
date-copy DATEprocedure

Returns an exact copy of the specified DATE object.

date?
date? DATEprocedure

#t if object is a time object, otherwise, #f.

date-nanosecond
date-nanosecond DATEprocedure

Date nanosecond.

date-second
date-second DATEprocedure

Date second.

date-minute
date-minute DATEprocedure

Date minute.

date-hour
date-hour DATEprocedure

Date hour.

date-day
date-day DATEprocedure

Date day.

date-month
date-month DATEprocedure

Date month.

date-year
date-year DATEprocedure

Date year.

date-zone-offset
date-zone-offset DATEprocedure

Date time zone offset.

date-zone-name (*)
date-zone-name DATEprocedure
date-zone-name DATEprocedure

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

date-dst? (*)
date-dst? DATEprocedure

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-year-day
date-year-day DATEprocedure

The ordinal day of the year of this date. January 1 is 1, etc.

date-week-day
date-week-day DATEprocedure

The day of the week of this date, where Sunday=0, Monday=1, etc.

date-week-number
date-week-number DATE DAY-OF-WEEK-STARTING-WEEKprocedure

The ordinal week of the year which holds this date, ignoring a first partial week. 'Day-of-week-starting-week' is the integer corresponding to the day of the week which is to be considered the first day of the week (Sunday=0, Monday=1, etc.).

leap-year? (*)
leap-year? DATEprocedure

Does the specified DATE fall on a leap year?

The DATE may be a numeric year or a date object.

Time/Date/Julian Day/Modified Julian Day Converters

date->seconds (*)
date->seconds DATE #!optional CLOCK-TYPE default-date-clock-typeprocedure

Returns the specified DATE as as a seconds value, based on the CLOCK-TYPE.

The seconds value is relative to the TAI-EPOCH - 1 January 1970 CE at 00:00:00 UTC.

date->time (*)
date->time DATE #!optional CLOCK-TYPE default-date-clock-typeprocedure

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

date->julian-day
date->julian-day DATEprocedure

Convert date to Julian Day.

date->modified-julian-day
date->modified-julian-day DATEprocedure

Convert date to Modified Julian Day.

date->time-monotonic
date->time-monotonic DATEprocedure

Convert date to monotonic time.

date->time-tai
date->time-tai DATEprocedure

Convert date to TAI time.

date->time-utc
date->time-utc DATEprocedure

Convert date to UTC time.

julian-day->date
julian-day->date JD #!optional TZ-OFFSETprocedure

Convert Julian Day to date, , using time zone offset, which defaults to the local time zone.

julian-day->time-monotonic
julian-day->time-monotonic JDprocedure

Convert Julian Day to monotonic time.

julian-day->time-tai
julian-day->time-tai JDprocedure

Convert Julian Day to TAI time.

julian-day->time-utc
julian-day->time-utc JDprocedure

Convert Julian Day to UTC time.

modified-julian-day->date
modified-julian-day->date MJD #!optional TZ-OFFSETprocedure

Convert Modified Julian Day to date, using time zone offset, which defaults to the local time zone.

modified-julian-day->time-monotonic
modified-julian-day->time-monotonic MJDprocedure

Convert Modified Julian Day to monotonic time.

modified-julian-day->time-tai
modified-julian-day->time-tai MJDprocedure

Convert Modified Julian Day to TAI time.

modified-julian-day->time-utc
modified-julian-day->time-utc MJDprocedure

Convert Modified Julian Day to UTC time.

time-monotonic->date
time-monotonic->date TIME-MONOTONIC #!optional TZ-OFFSETprocedure

Convert monotonic time to date, using time zone offset, which defaults to the local time zone.

time-monotonic->julian-day
time-monotonic->julian-day TIME-MONOTONICprocedure

Convert monotonic time to Julian Day.

time-monotonic->modified-julian-day
time-monotonic->modified-julian-day TIME-MONOTONICprocedure

Convert monotonic time to Modified Julian Day.

time-monotonic->time-tai
time-monotonic->time-tai TIME-MONOTONICprocedure

Convert monotonic time to TAI time.

time-monotonic->time-tai!
time-monotonic->time-tai! TIME-MONOTONICprocedure

Convert monotonic time to TAI time. The time structure may be reused.

time-monotonic->time-utc
time-monotonic->time-utc TIME-MONOTONICprocedure

Convert monotonic time to UTC time.

time-monotonic->time-utc!
time-monotonic->time-utc! TIME-MONOTONICprocedure

Convert monotonic time to UTC time. The time structure may be reused.

time-tai->date
time-tai->date TIME-TAI #!optional TZ-OFFSETprocedure

Convert TAI time to date, using time zone offset, which defaults to the local time zone.

time-tai->julian-day
time-tai->julian-day TIME-TAIprocedure

Convert TAI time to Julian Day.

time-tai->modified-julian-day
time-tai->modified-julian-day TIME-TAIprocedure

Convert TAI time to Modified Julian Day.

time-tai->time-monotonic
time-tai->time-monotonic TIME-TAIprocedure

Convert TAI time to monotonic time.

time-tai->time-monotonic!
time-tai->time-monotonic! TIME-TAIprocedure

Convert TAI time to monotonic time. The time structure may be reused.

time-tai->time-utc
time-tai->time-utc TIME-TAIprocedure

Convert TAI time to monotonic time.

time-tai->time-utc!
time-tai->time-utc! TIME-TAIprocedure

Convert TAI time to monotonic time. The time structure may be reused.

time-utc->date
time-utc->date TIME-UTC #!optional TZ-OFFSETprocedure

Convert UTC time to date, using time zone offset, which defaults to the local time zone.

time-utc->julian-day
time-utc->julian-day TIME-UTCprocedure

Convert UTC time to Julian Day

time-utc->modified-julian-day
time-utc->modified-julian-day TIME-UTCprocedure

Convert UTC time to Modified Julian Day.

time-utc->time-monotonic
time-utc->time-monotonic TIME-UTCprocedure

Convert UTC time to monotonic time.

time-utc->time-monotonic!
time-utc->time-monotonic! TIME-UTCprocedure

Convert UTC time to monotonic time. The time structure may be reused.

time-utc->time-tai
time-utc->time-tai TIME-UTCprocedure

Convert UTC time to TAI time.

time-utc->time-tai!
time-utc->time-tai! TIME-UTCprocedure

Convert UTC time to TAI time. The time structure may be reused.

Date Arithmetic (*)

date-difference
date-difference DATE1 DATE2 #!optional CLOCK-TYPEprocedure

Returns the time-duration between DATE1 and DATE2.

date-add-duration
date-add-duration DATE DURATION #!optional CLOCK-TYPEprocedure

Returns a new date, the DATE plus the DURATION.

date-subtract-duration
date-subtract-duration DATE DURATION #!optional CLOCK-TYPEprocedure

Returns a new date, the DATE minus the DURATION.

date-adjust
date-adjust DATE AMOUNT KEY #!optional CLOCK-TYPEprocedure

Returns a new date, the DATE adjusted by the AMOUNT of KEY.

AMOUNT
integer ;
KEY
symbol ; '(years quarters months weeks days hours minutes seconds milliseconds microseconds nanoseconds).

If the day of the month of DATE is greater than the number of days in the final month, the day of the month will change to the last day in the final month.

Adjusting a time KEY (ex: 'hours) follows the semantics of date-add-duration.

date-adjust*
date-adjust* DATE KEY AMOUNT ... #!optional CLOCK-TYPEprocedure

Returns a new date, the DATE adjusted by each of the KEY AMOUNT pairs, in-order.

AMOUNT
integer ;
KEY
keyword ; '(#:years #:quarters #:months #:weeks #:days #:hours #:minutes #:seconds #:milliseconds #:microseconds #:nanoseconds).

A convince wrapper of date-adjust.

Date Comparison (*)

date-compare
date-compare DATE1 DATE2procedure

Returns negative, zero, or positive.

date-hash
date-hash DATE #!optional BOUNDprocedure

Hash value for DATE within BOUND.

date=?
date=? DATE1 DATE2procedure

Is DATE1 on DATE2?

date>?
date>? DATE1 DATE2procedure

Is DATE1 after DATE2?

date<?
date<? DATE1 DATE2procedure

Is DATE1 before DATE2?

date>=?
date>=? DATE1 DATE2procedure

Is DATE1 after or on DATE2?

date<=?
date<=? DATE1 DATE2procedure

Is DATE1 before or on DATE2?

read-leap-second-table
read-leap-second-table FILENAMEprocedure

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.

Timezone (*)

The daylight saving time (summer time) flag is always taken from the system (locale egg) unless supplied. Any summer time rule component of a timezone-components object is not processed.

TZ-COMPONENTS
timezone-components ;a timezone object

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

(import srfi-19-timezone)

local-timezone-locale

local-timezone-locale #!optional TZ-COMPONENTSparameter

Gets or sets the local timezone-locale object.

utc-timezone-locale

utc-timezone-locale #!optional TZ-COMPONENTSparameter

Gets or sets the utc timezone-locale object.

Not a good idea to change the value.

timezone-locale-name

timezone-locale-name #!optional TZ-COMPONENTSprocedure

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

timezone-locale-offset

timezone-locale-offset #!optional TZ-COMPONENTSprocedure

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

timezone-locale-dst?

timezone-locale-dst? #!optional TZ-COMPONENTSprocedure

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

Date to String/String to Date Converters (Input/Output Procedures)

(import srfi-19-io)

format-date (*)

format-date DESTINATION DATE-FORMAT DATEprocedure
format-date DATE-FORMAT DATEprocedure

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

DESTINATION
; the result is returned as a string.
DESTINATION
#f ; the result is returned as a string.
DESTINATION
#t ; the (current-output-port) is used.
DESTINATION
output-port ; text output port.
DESTINATION
number ; the (current-error-port) is the DESTINATION.

scan-date (*)

scan-date SOURCE TEMPLATEprocedure

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

SOURCE
#t ; (current-input-port).
SOURCE
input-port ; text input port.
SOURCE
string ; a date text form.

A partial date resulting from a time-only format, missing the d-m-y, is completed with from the (current-date), as is any timezone information, and missing h:m:s:ns are 0.

date->string

date->string DATE #!optional DATE-FORMATprocedure
DATE
date
DATE-FORMAT
string ; default "~c".

string->date

string->date SOURCE #!optional TEMPLATEprocedure
SOURCE
see scan-date.
TEMPLATE
string ; default (localized-template/default 'srfi-19 'date-time).

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.

DATE->STRING conversion specifiers

ChConversion
~~a literal ~
~alocale's abbreviated weekday name (Sun...Sat)
~Alocale's full weekday name (Sunday...Saturday)
~blocale's abbreviate month name (Jan...Dec)
~Blocale's full month day (January...December)
~clocale's date and time (e.g., "Fri Jul 14 20:28:42-0400 2000")
~dday of month, zero padded (01...31)
~Ddate (mm/dd/yy)
~eday of month, blank padded ( 1...31)
~fseconds+fractional seconds, using locale's decimal separator (e.g. 5.2).
~gday of month
~hsame as ~b
~Hhour, zero padded, 24-hour clock (00...23)
~Ihour, zero padded, 12-hour clock (01...12)
~jday of year, zero padded
~khour, blank padded, 24-hour clock ( 0...23)
~lhour, blank padded, 12-hour clock ( 1...12)
~mmonth, zero padded (01...12)
~Mminute, zero padded (00...59)
~nnew line
~Nnanosecond, zero padded
~plocale's AM or PM
~rtime, 12 hour clock, same as "~I:~M:~S ~p"
~snumber of full seconds since "the epoch" (in UTC)
~Ssecond, zero padded (00...60)
~thorizontal tab
~Ttime, 24 hour clock, same as "~H:~M:~S"
~uday ordinal suffix
~Uweek number of year with Sunday as first day of week (00...53)
~VISO 8601 week number of the year with Monday as first day of week (01..53)
~wday of week (0...6)
~Wweek number of year with Monday as first day of week (01...52)
~xlocale's date representation
~Xlocale's time representation
~ylast two digits of year (00...99)
~Yyear
~ztime zone in RFC-822 style
~Zsymbol time zone (not-implemented)
~1ISO-8601 year-month-day format
~2ISO-8601 hour-minute-second-timezone format
~3ISO-8601 hour-minute-second format
~4ISO-8601 year-month-day-hour-minute-second-timezone format
~5ISO-8601 year-month-day-hour-minute-second format

The normally zero-padded conversions f, H, I, j, m, M, N, S, y have a padding character override feature. If the tilde is followed by a - then padding is suppressed. If followed by a _ the space character is used for padding. Otherwise, the default, zero-padding is perfomed.

g & u from Chibi (srfi 19).

STRING->DATE conversion specifiers

ChSkip toReadSet
~~anyread literal~
~achar-alphabetic?abbreviated weekday in localenothing
~Achar-alphabetic?full weekday in localenothing
~bchar-alphabetic?abbreviated month name in localenothing
~Bchar-alphabetic?full month name in localenothing
~dchar-numeric?day of monthdate-day
~eanyday of month, blank paddeddate-day
~hchar-alphabetic?same as ~bnothing
~Hchar-numeric?hourdate-hour
~kanyhour, blank paddeddate-hour
~mchar-numeric?monthdate-month
~Mchar-numeric?minutedate-minute
~Schar-numeric?seconddate-second
~yany2-digit yeardate-year within 50 years
~Ychar-numeric?yeardate-year
~zanytime zonedate-zone-offset

Date/Time Literals (*)

Literal Date/Time forms.

Usage

(import srfi-19-literals)
date-literal-form
date-literal-form #!optional FORMparameter

The date literal format.

#f
#<srfi-19#date ...>
SRFI-10
#,(srfi-19-date ...)
#t
#@... ; the default
|#@|
#@...
time-literal-form
time-literal-form #!optional FORMparameter

The time literal format.

#f
#<srfi-19#time ...>
SRFI-10
#,(srfi-19-time ...) ; the default
#t ##...
|##|
##...

Use the ## form at your peril.

read-date-literal
read-date-literal #!optional PORTprocedure

Read a date-literal from the PORT, in one of the supported forms, and return the object creation source form; '(make-date ....).

PORT
input-port ; default (current-input-port)

Supported forms:

"~Y-~m-~dT~H:~M:~S~z"
date time timezone
"~Y-~m-~dT~H:~M:~S"
date time
"~Y-~m-~d"
date
"~H:~M:~S~z"
time timezone; date components from (current-date)
"~H:~M:~S"
time; date components from (current-date)
write-date-literal
write-date-literal DATE #!optional PORTprocedure

Write the DATE to the PORT using a string ISO form; "~Y-~m-~dT~H:~M:~S~z".

PORT
output-port ; default (current-output-port)

Time Period (*)

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

Usage

(import srfi-19-period)
make-time-period
make-time-period BEGIN END #!optional CLOCK-TYPE default-date-clock-typeprocedure

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.

time-period-copy
time-period-copy TIME-PERIODprocedure

Returns a copy of TIME-PERIOD.

time-period-begin
time-period-begin TIME-PERIODprocedure

Returns the start time for the TIME-PERIOD.

time-period-end
time-period-end TIME-PERIODprocedure

Returns the end time for the TIME-PERIOD.

time-period-last
time-period-last TIME-PERIODprocedure

Returns the last time for the TIME-PERIOD; (time-period-end - TIME-FINE-GRAIN).

time-period-type
time-period-type TIME-PERIODprocedure

Returns the clock-type of the TIME-PERIOD.

time-period?
time-period? OBJECTprocedure

Is OBJECT a time-period?

time-period-length
time-period-length TIME-PERIODprocedure

Returns the time-duration of the TIME-PERIOD.

time-period-hash
time-period-hash TIME-PERIOD #!optional BOUNDprocedure

Hash value for TIME-PERIOD within BOUND.

time-period-compare
time-period-compare TIME-PERIOD-1 TIME-PERIOD-2procedure

Returns negative, zero, or positive.

time-period=?
time-period=? TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period<?
time-period<? TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period>?
time-period>? TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period<=?
time-period<=? TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period>=?
time-period>=? TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period-preceding
time-period-preceding TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period-succeeding
time-period-succeeding TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period-contains/period?
time-period-contains/period? TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period-contains/time?
time-period-contains/time? TIME-PERIOD TIMEprocedure

Is TIME within TIME-PERIOD?

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

time-period-contains/date?
time-period-contains/date? TIME-PERIOD DATEprocedure

Is DATE within TIME-PERIOD?

DATE is converted to a compatible time if possible.

time-period-contains?
time-period-contains? TIME-PERIOD OBJECTprocedure

Is OBJECT within TIME-PERIOD?

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

time-period-intersects?
time-period-intersects? TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period-intersection
time-period-intersection TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period-union
time-period-union TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period-span
time-period-span TIME-PERIOD-1 TIME-PERIOD-2procedure

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

time-period-shift
time-period-shift TIME-PERIOD DURATIONprocedure

Returns a copy of TIME-PERIOD shifted by DURATION.

time-period-shift!
time-period-shift! TIME-PERIOD DURATIONprocedure

Returns TIME-PERIOD shifted by DURATION.

SRFI 29 bundles

en
English (American) - only shipping u support at the moment
de
German
it
Italian
es
Spanish
nl
Dutch
pt_br
Brazilian Portuguese

Default bundle is en.

Examples

;must be done before 1st invocation of a srfi-19 export
(import locale)
(set-locale-category! 'timezone
  (posix-timezone-string->timezone-components
    ;some acceptable posix tz form
    "XSX+2:00XDX+1:00:00"
    ;source of tz info
    '("POSIX" "TZ")))

(current-timezone-components)
;=> ((tag . timezone) (name . "XSX+2:00XDX+1:00:00") (source "POSIX" "TZ")
;=> (std-name . "XSX") (std-offset . 7200) (dst-name . "XDX") (dst-offset . 3600)
;=> (dst? . #t))

;now we can use srfi-19 in our runtime tz
(import (chicken format))
(import srfi-19)
(format #t "Present Day: ~A~%Present Time: ~A~%: Hah, hah, hah.~%"
  (current-date) (current-time))
(import (only (srfi 128) make-comparator))

(define (make-date-comparator)
  (import (only srfi-19-date date? date=? date<? date-hash))
  (make-comparator date? date=? date<? date-hash) )

(define (make-time-comparator)
  (import (only srfi-19-time time? time=? time<? time-hash))
  (make-comparator time? time=? time<? time-hash) )

(define (make-time-period-comparator)
  (import (only srfi-19-period time-period? time-period=? time-period<? time-period-hash))
  (make-comparator srfi-19-period time-period? time-period=? time-period<? time-period-hash) )

Notes

Bugs and Limitations

If you are adding hours, minutes, seconds, or milliseconds, the assumption is that you want precision to the hour, and will result in a different hour.

var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US
m.hours(); // 5
m.add(24, 'hours').hours(); // 6

but this implementation says 1 day = 24 hours, so same hour.

Requirements

utf8 srfi-1 srfi-18 srfi-29 miscmacros locale record-variants check-errors

format

Author

Kon Lovett

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/srfi-19

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Acknowledgments

Version history

4.9.5
Remove code/data duplicates.
4.9.4
Restore copy-time & add copy-date.
4.9.3
Fix *-hash range test.
4.9.2
Fix time-hash result bounds.
4.9.1
Fix date-adjust synonym lookup.
4.9.0
SRFI-69 conforming date-hash, time-hash, & period-hash. Drop srfi-69 dependency.
4.8.2
Fix ~V.
4.8.1
Better date/time-hash. Fix time-compare negative seconds. nanoseconds->time, milliseconds->time, seconds->time, & date->seconds have optional. time-abs & time-negate affect time-second only.
4.8.0
Add Chibi time routines. Fix seconds->time & time->seconds negative seconds.
4.7.5
Add default locale bundle.
4.7.4
Add C locale bundle.
4.7.3
make-date accepts year 0.
4.7.2
date-adjust* accepts symbols.
4.7.1
Fix date<?/date>? swapped bug; reported on #irc by gahr, Jan 26 '23
4.7.0
Add Chibi ~g & ~u output conversion specifiers.
4.6.0
Add date-adjust*.
4.5.2
Better time-literal-form default.
4.5.1
Fix SRFI-69 compatible time-hash, date-hash, time-period-hash.
4.5.0
Add SRFI-69/128 compatible time-hash, date-hash, time-period-hash.
4.4.10
Indicate pure procedures. Fix time conversion procedure signatures.
4.4.9
Add strong typing.
4.4.8
Fix time-copy, add time-copy/date-copy, deprecate copy-date && copy-time, add time-period interval relations.
4.4.7
.
4.4.6
Fix time-utc->date default TZ.
4.4.0
Add Date/Time source literals via the srfi-19-literals module.
4.3.2
Allow partial (time only) date read.
4.3.1
.
4.3.0
Deprecate 'srfi-10 record print form. Fix record printing.
4.3.0
nanoseconds->seconds, milliseconds->seconds, & date->seconds do not necessarily return an inexact.
4.2.1
.
4.2.0
Fix make-date w/o tz.
4.1.2
.
4.1.1
Fix seconds->date flonum argument handling.
4.1.0
Variants.
4.0.2
Add dependencies.
4.0.1
Add dependencies.
4.0.0
C5 port.

License

Copyright (C) 2009-2023 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.

Contents »