40.8 Parsing and Formatting Times
These functions convert time values to text in a string, and vice versa. Time values include nil
, numbers, and Lisp timestamps (see Time of Day).
function
date-to-time stringβ
This function parses the time-string string
and returns the corresponding Lisp timestamp. The argument string
should represent a date-time, and should be in one of the forms recognized by parse-time-string
(see below). This function assumes Universal Time if string
lacks explicit time zone information. The operating system limits the range of time and zone values.
function
parse-time-string stringβ
This function parses the time-string string
into a list of the following form:
(sec min hour day mon year dow dst tz)
The format of this list is the same as what decode-time
accepts (see Time Conversion), and is described in more detail there. Any element that cannot be determined from the input will be set to nil
. The argument string
should resemble an RFC 822 (or later) or ISO 8601 string, like βFri, 25 Mar 2016 16:24:56 +0100" or β1998-09-12T12:21:54-0200", but this function will attempt to parse less well-formed time strings as well.
function
iso8601-parse stringβ
For a more strict function (that will error out upon invalid input), this function can be used instead. It can parse all variants of the ISO 8601 standard, so in addition to the formats mentioned above, it also parses things like β1998W45-3" (week number) and β1998-245" (ordinal day number). To parse durations, thereβs iso8601-parse-duration
, and to parse intervals, thereβs iso8601-parse-interval
. All these functions return decoded time structures, except the final one, which returns three of them (the start, the end, and the duration).
function
format-time-string format-string \&optional time zoneβ
This function converts time
(or the current time, if time
is omitted or nil
) to a string according to format-string
. The conversion uses the time zone rule zone
, which defaults to the current time zone rule. See Time Zone Rules. The argument format-string
may contain β%
β-sequences which say to substitute parts of the time. Here is a table of what the β%
β-sequences mean:
β%a
ββ
This stands for the abbreviated name of the day of week.
β%A
ββ
This stands for the full name of the day of week.
β%b
ββ
This stands for the abbreviated name of the month.
β%B
ββ
This stands for the full name of the month.
β%c
ββ
This is a synonym for β%x %X
β.
β%C
ββ
This stands for the century, that is, the year divided by 100, truncated toward zero. The default field width is 2.
β%d
ββ
This stands for the day of month, zero-padded.
β%D
ββ
This is a synonym for β%m/%d/%y
β.
β%e
ββ
This stands for the day of month, blank-padded.
β%F
ββ
This stands for the ISO 8601 date format, which is like β%+4Y-%m-%d
β except that any flags or field width override the β+
β and (after subtracting 6) the β4
β.
β%g
ββ
This stands for the year corresponding to the ISO week within the century.
β%G
ββ
This stands for the year corresponding to the ISO week.
β%h
ββ
This is a synonym for β%b
β.
β%H
ββ
This stands for the hour (00β23).
β%I
ββ
This stands for the hour (01β12).
β%j
ββ
This stands for the day of the year (001β366).
β%k
ββ
This stands for the hour (0β23), blank padded.
β%l
ββ
This stands for the hour (1β12), blank padded.
β%m
ββ
This stands for the month (01β12).
β%M
ββ
This stands for the minute (00β59).
β%n
ββ
This stands for a newline.
β%N
ββ
This stands for the nanoseconds (000000000β999999999). To ask for fewer digits, use β%3N
β for milliseconds, β%6N
β for microseconds, etc. Any excess digits are discarded, without rounding.
β%p
ββ
This stands for βAM
β or βPM
β, as appropriate.
β%q
ββ
This stands for the calendar quarter (1β4).
β%r
ββ
This is a synonym for β%I:%M:%S %p
β.
β%R
ββ
This is a synonym for β%H:%M
β.
β%s
ββ
This stands for the integer number of seconds since the epoch.
β%S
ββ
This stands for the second (00β59, or 00β60 on platforms that support leap seconds).
β%t
ββ
This stands for a tab character.
β%T
ββ
This is a synonym for β%H:%M:%S
β.
β%u
ββ
This stands for the numeric day of week (1β7). Monday is day 1.
β%U
ββ
This stands for the week of the year (01β52), assuming that weeks start on Sunday.
β%V
ββ
This stands for the week of the year according to ISO 8601.
β%w
ββ
This stands for the numeric day of week (0β6). Sunday is day 0.
β%W
ββ
This stands for the week of the year (01β52), assuming that weeks start on Monday.
β%x
ββ
This has a locale-specific meaning. In the default locale (named βC
β), it is equivalent to β%D
β.
β%X
ββ
This has a locale-specific meaning. In the default locale (named βC
β), it is equivalent to β%T
β.
β%y
ββ
This stands for the year without century (00β99).
β%Y
ββ
This stands for the year with century.
β%Z
ββ
This stands for the time zone abbreviation (e.g., βEST
β).
β%z
ββ
This stands for the time zone numerical offset. The βz
β can be preceded by one, two, or three colons; if plain β%z
β stands for β-0500
β, then β%:z
β stands for β-05:00
β, β%::z
β stands for β-05:00:00
β, and β%:::z
β is like β%::z
β except it suppresses trailing instances of β:00
β so it stands for β-05
β in the same example.
β%%
β
This stands for a single β%
β.
One or more flag characters can appear immediately after the β%
β. β0
β pads with zeros, β+
β pads with zeros and also puts β+
β before nonnegative year numbers with more than four digits, β_
β pads with blanks, β-
β suppresses padding, β^
β upper-cases letters, and β#
β reverses the case of letters.
You can also specify the field width and type of padding for any of these β%
β-sequences. This works as in printf
: you write the field width as digits in a β%
β-sequence, after any flags. For example, β%S
β specifies the number of seconds since the minute; β%03S
β means to pad this with zeros to 3 positions, β%_3S
β to pad with spaces to 3 positions. Plain β%3S
β pads with zeros, because that is how β%S
β normally pads to two positions.
The characters βE
β and βO
β act as modifiers when used after any flags and field widths in a β%
β-sequence. βE
β specifies using the current localeβs alternative version of the date and time. In a Japanese locale, for example, %Ex
might yield a date format based on the Japanese Emperorsβ reigns. βE
β is allowed in β%Ec
β, β%EC
β, β%Ex
β, β%EX
β, β%Ey
β, and β%EY
β.
βO
β means to use the current localeβs alternative representation of numbers, instead of the ordinary decimal digits. This is allowed with most letters, all the ones that output numbers.
To help debug programs, unrecognized β%
β-sequences stand for themselves and are output as-is. Programs should not rely on this behavior, as future versions of Emacs may recognize new β%
β-sequences as extensions.
This function uses the C library function strftime
(see Formatting Calendar Time in The GNU C Library Reference Manual) to do most of the work. In order to communicate with that function, it first converts time
and zone
to internal form; the operating system limits the range of time and zone values. This function also encodes format-string
using the coding system specified by locale-coding-system
(see Locales); after strftime
returns the resulting string, this function decodes the string using that same coding system.
function
format-seconds format-string secondsβ
This function converts its argument seconds
into a string of years, days, hours, etc., according to format-string
. The argument format-string
may contain β%
β-sequences which control the conversion. Here is a table of what the β%
β-sequences mean:
β%y
ββ
β%Y
ββ
The integer number of 365-day years.
β%d
ββ
β%D
ββ
The integer number of days.
β%h
ββ
β%H
ββ
The integer number of hours.
β%m
ββ
β%M
ββ
The integer number of minutes.
β%s
ββ
β%S
ββ
The integer number of seconds.
β%z
ββ
Non-printing control flag. When it is used, other specifiers must be given in the order of decreasing size, i.e., years before days, hours before minutes, etc. Nothing will be produced in the result string to the left of β%z
β until the first non-zero conversion is encountered. For example, the default format used by emacs-uptime
(see emacs-uptime) "%Y,Β %D,Β %H,Β %M,Β %z%S"
means that the number of seconds will always be produced, but years, days, hours, and minutes will only be shown if they are non-zero.
β%%
ββ
Produces a literal β%
β.
Upper-case format sequences produce the units in addition to the numbers, lower-case formats produce only the numbers.
You can also specify the field width by following the β%
β with a number; shorter numbers will be padded with blanks. An optional period before the width requests zero-padding instead. For example, "%.3Y"
might produce "004 years"
.