Copyright © 2012 Daniel Lowe <dlowe dlowe.net>
Copyright © 2012 Attila Lendvai <attila.lendvai gmail.com>
This manual describes the
local-timeCommon Lisp library which is based on Erik Naggum's The Long, Painful History of Time [NaggumPaper] paper.
local-time library is a Common Lisp library for the manipulation of
dates, times and intervals. It was originally based almost entirely
upon Erik Naggum's paper The Long Painful History of Time
[NaggumPaper]. Many of the core concepts originated from this paper,
such as the seperation of days and seconds, the choice of 2000-03-01
as the standard epoch, and the timestring format.
Caveats: This implementation assumes that time zone information is stored in the tzfile format. The default timezone is loaded from /etc/localtime. On non-POSIX systems, this will certainly give different results than the system time handling.
It's a good idea to treat all values as immutable objects.
will not modify any object it was given unless explicitly asked to by
:into keyword argument.
timestampvalues can represent either a date, a daytime or a time value. It has the following slots:(defclass timestamp () ((day :type integer) (sec :type integer) (nsec :type (integer 0 999999999))))
The following constraints apply to the specific types:
- date: must have a +utc-zone+ timezone and the sec slot must be the first second of a day; In other words, the time elements of the
timestampvalue must have their least possible values.
- time: the day slot must be zero.
timezoneobjects represent timezones - local and political modifications to the time representation. Timezones are responsible for storing offsets from GMT, abbreviations for different sub-timezones, and the times each sub-timezone is to be in effect.
The variable *default-timezone* contains the timezone that will be used by default if none is specified. It is loaded from /etc/localtime when the library is loaded. If /etc/localtime is not present, it will default to UTC.
Define zone-name (a symbol or a string) as a new timezone, lazy-loaded from zone-file (a pathname designator relative to the zoneinfo directory on this system. If load is true, load immediately.
timestampinstance from the provided universal time universal. Universal time is defined in the Common Lisp Specification as the number of seconds since 1900-01-01T00:00:00Z.
timestampinstance from the provided unix time unix. Unix time is defined by POSIX as the number of seconds since 1970-01-01T00:00:00Z.
timestampinstance with the current time. Under sbcl, the new timestamp will be precise to the microsecond. Otherwise, the precision is limited to the second.
timestampinstance that corresponds to today's date, which is the midnight of the current day in the UTC zone.
Returns a new
timestampinstance corresponding to the specified time elements. The offset is the number of seconds offset from UTC of the locale. If offset is not specified, the offset will be guessed from the timezone. If a
timestampis passed as the into argument, its value will be set and that
timestampwill be returned. Otherwise, a new
Expands to an expression that creates an instance of a
timestampexactly as specified.
Expands to an expression that creates another copy of timestamp that is
Returns the day component of timestamp. Although Naggum's paper specifies that the day should be a signed fixnum, it is left unbounded for flexibility reasons.
Returns the 'seconds' component of the time. Valid values for the seconds range from 0 to 86399.
Returns the 'microseconds' component of the time. Valid values for the nanoseconds range from 0 to 999999999.
This returns the date/time specified in timestamp encoded as the number of seconds since January 1st, 1900 12:00am UTC.
This returns the date/time specified in timestamp encoded as the number of seconds since January 1st, 1970 12:00am UTC. It corresponds with the time received from the POSIX call
Returns as multiple values the time zone applicable at the given time as the number of seconds east of UTC, a boolean daylight-saving-p, and the customary abbreviation of the timezone.
This macro binds variables to the decoded elements of timestamp. The timezone argument is used for decoding the timestamp, and is not bound by the macro. The value of day-of-week starts from 0 which means Sunday.
Returns the decoded time as
(values ns ss mm hh day month year day-of-week daylight-saving-time-p timezone-offset timezone-abbreviation).
These comparison functions act like their string and char counterparts.
Returns the earliest timestamp passed to it.
This returns the index of the day of the week, starting at 0 which means Sunday.Note: ”Day of the week” is ambigous and locale dependent.
Returns the UNIVERSAL-TIME corresponding to timestamp.Note: Subsecond precision is not preserved.
Returns the ordinal millennium, century or decade upon which the timestamp falls. Ordinal time values start at 1, so the (timestamp-century (now)) will return 21.
Returns the decoded part of the timestamp.
Add or subtract the amount to the time using the specified unit. unit may be one of (
:year). The value of the parts of the timestamp of higher resolution than the UNIT will never be touched. If you want a precise number of seconds from a time, you should specify the offset in seconds.
Returns a timestamp with its parts maximized up to part. part can be any of (:nsec :sec :min :hour :day :month). If into is specified, it will be modified and returned, otherwise a new timestamp will be created.
Returns a timestamp with its parts minimized up to part. part can be any of (:nsec :sec :min :hour :day :month). If into is specified, it will be modified and returned, otherwise a new timestamp will be created.
Alters various parts of timestamp, given a list of changes. The changes are in the format
(offset part value)and
(set part value).;; Return a new
timestampvalue that points to the previous Monday (adjust-timestamp (today) (offset :day-of-week :monday)) ;; Return a new
timestampvalue that points three days ahead from now (adjust-timestamp (today) (offset :day 3))
Keep in mind that
adjust-timestampis not a mere setter for fields but instead it handles overflows and timezone conversions as expected. Also note that it's possible to specify multiple commands.
The list of possible places to manipulate are:
adjust-timestamp, but instead of returning a freshly constructed value, it alters the provided timestamp value (and returns it).
Returns the number of whole years elapsed between time-a and time-b.Note: This is useful for calculating anniversaries and birthdays.
Returns the number of days in a given month of the specified year.
The constant +iso-8601-format+ is bound to a description of the ISO 8601 format. An output with this format will look like this: ‘2008-03-01T19:42:34.608506+01:00’. This is the default format for the
The constant +asctime-format+ is bound to a format mirroring the output of the POSIX asctime() function. An output with this format will look like this: ‘Sat Mar 1 19:42:34 2008’.
The constant +rfc-1123-format+ is bound to a description of the format defined in RFC 1123 for Internet timestamps. An output with this format will look like this: ‘Sat, 01 Mar 2008 19:42:34 EDT’.
The constant +iso-week-date-format+ is bound to a description of the ISO 8601 Week Date format. An output with this format will look like this: ‘2009-W53-5’.
Parses a timestring and returns the corresponding
timestamp. Parsing begins at start and stops at the end position. If there are invalid characters within
timestringand fail-on-error is
T, then an
invalid-timestringerror is signaled, otherwise
If there is no timezone specified in
timestringthen offset is used as the default timezone offset (in seconds).
Constructs a string representation of TIMESTAMP according to FORMAT and returns it. If destination is T, the string is written to *standard-output*. If destination is a stream, the string is written to the stream.
FORMAT is a list containing one or more of strings, characters, and keywords. Strings and characters are output literally, while keywords are replaced by the values here:
- *numeric month
- *day of month
- *numeric day of week, starting from 0 which means Sunday
- *year for ISO week date (can be different from regular calendar year)
- *ISO week number (i.e. 1 through 53)
- *ISO compatible weekday number (i.e. monday=1, sunday=7)
- day of month as an ordinal (e.g. 1st, 23rd)
- long form of weekday (e.g. Sunday, Monday)
- short form of weekday (e.g. Sun, Mon)
- long form of month (e.g. January, February)
- short form of month (e.g. Jan, Feb)
- hour on a 12-hour clock
- am/pm marker in lowercase
- the gmt-offset of the time, in +00:00 form
- like :gmt-offset, but is Z when UTC
- timezone abbrevation for the time
Elements marked by * can be placed in a list in the form:(:keyword padding &optional (padchar #\0))
The string representation of the value will be padded with the padchar.
You can see examples by examining the values in +iso-8601-format+, +asctime-format+, and +rfc-1123-format+.
Produces on stream the timestring corresponding to the timestamp with the given options. If stream is
nil, only returns a string containing what would have been the output. If stream is
t, prints the string to *standard-output*.
Example output:LOCAL-TIME> (format-timestring nil (now)) "2008-03-01T19:42:34.608506+01:00"
Formats the time like format-timestring, but in RFC 3339 format. The options control valid options in the RFC.
Returns the julian date of the date portion of timestamp.
Returns the modified julian date of the date portion of timestamp.