1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

Add more date builtins

This commit is contained in:
Nicolas Williams
2015-03-08 18:56:51 -05:00
parent 3e8183fcd5
commit ccfba00178
4 changed files with 234 additions and 48 deletions

View File

@ -1594,24 +1594,69 @@ sections:
- title: "Dates"
body: |
The `strptime(fmt)` function parses input strings matching the
`fmt` argument. The output is an array of eight numbers: the
year, the month, the day of the month, the hour of the day,
the minute of the hour, the second of the minute, the day of
the week, and the day of the year, all zero-based except for
the year and the month, which are one-based.
jq provides some basic date handling functionality, with some
high-level and low-level builtins. In all cases these
builtins deal exclusively with time in UTC.
The `mktime` function consumes outputs from `strptime/1` and
produces the time in seconds since the Unix epoch.
The `fromdateiso8601` builtin parses datetimes in the ISO 8601
format to a number of seconds since the Unix epoch
(1970-01-01T00:00:00Z). The `todateiso8601` builtin does the
inverse.
The `fromdate` builtin parses datetime strings. Currently
`fromdate` only supports ISO 8601 datetime strings, but in the
future it will attempt to parse datetime strings in more
formats.
The `todate` builtin is an alias for `todateiso8601`.
The `now` builtin outputs the current time, in seconds since
the Unix epoch.
Low-level jq interfaces to the C-library time functions are
also provided: `strptime`, `strftime`, `mktime`, and `gmtime`.
Refer to your host operating system's documentation for the
format strings used by `strptime` and `strftime`. Note: these
are not necessarily stable interfaces in jq, particularly as
to their localization functionality.
The `gmtime` builtin consumes a number of seconds since the
Unix epoch and outputs a "broken down time" representation of
time as an array of numbers representing (in this order): the
year, the month (zero-based), the day of the month, the hour
of the day, the minute of the hour, the second of the minute,
the day of the week, and the day of the year -- all one-based
unless otherwise stated.
The `mktime` builtin consumes "broken down time"
representations of time output by `gmtime` and `strptime`.
The `strptime(fmt)` builtin parses input strings matching the
`fmt` argument. The output is in the "broken down time"
representation consumed by `gmtime` and output by `mktime`.
The `strftime(fmt)` builtin formats a time with the given
format.
The format strings for `strptime` and `strftime` are described
in typical C library documentation. The format string for ISO
8601 datetime is `"%Y-%m-%dT%H:%M:%SZ"`.
jq may not support some or all of this date functionality on
some systems.
examples:
- program: 'strptime'
input: '"2015-03-05 23:51:47Z"'
- program: 'fromdate'
input: '"2015-03-05T23:51:47Z"'
output: ['1425599507']
- program: 'strptime("%Y-%m-%dT%H:%M:%SZ")'
input: '"2015-03-05T23:51:47Z"'
output: ['[2015,2,5,23,51,47,4,63]']
- program: 'strptime|mktime'
input: '"2015-03-05 23:51:47Z"'
output: ['1425621107']
- program: 'strptime("%Y-%m-%dT%H:%M:%SZ")|mktime'
input: '"2015-03-05T23:51:47Z"'
output: ['1425599507']
- title: Conditionals and Comparisons
entries: