Module datetime
Since: 2.10.0
The datetime module provides support for the
[datetime](index-box_datetimeand :ref:interval ) data types. It allows
creating the date and time values either via the object interface or via
parsing string values conforming to the ISO-8601 standard.
Below is a list of datetime functions, properties, and related
objects.
Functions | |
|---|---|
Create an object of the | |
Create an object of the | |
Check whether the specified value is a | |
Convert an input string with the date and time information into a | |
Check whether the specified value is an | |
Create an object of the | Properties |
A Lua table that maps timezone names and abbreviations to its index and vice-versa. | |
Methods | |
Modify an existing | |
Convert the standard | |
Update the field values in the existing | |
Modify an existing | |
Convert the information from a |
Create an object of the datetime type from a table of time units. See the description of units and examples below.
Parameters:
units(table) — Table of time units. If an empty table or no arguments are passed, thedatetimeobject with the default values corresponding to Unix Epoch is created:1970-01-01T00:00:00Z.
Returns
Return type
cdata
Possible input time units for datetime.new()
Name | Description | Type | Default |
|---|---|---|---|
nsec (usec, msec) | Fractional part of the last second. You can specify either nanoseconds ( | number | 0 |
sec | Seconds. Value range: 0 - 60. A leap second is supported at the most basic level, see the section leap second. | number | 0 |
min | Minutes. Value range: 0 - 59. | number | 0 |
hour | Hours. Value range: 0 - 23. | number | 0 |
day | Day number. Value range: 1 - 31. The special value | number | 1 |
month | Month number. Value range: 1 - 12. | number | 1 |
year | Year. | number | 1970 |
timestamp | Timestamp, in seconds. Similar to the Unix timestamp, but can have a fractional part that is converted in nanoseconds in the resulting | number | 0 |
tzoffset | A time zone offset from UTC, in minutes. Value range: -720 - 840 inclusive. If both | number | 0 |
tz | A time zone name according to the Time Zone Database. See the timezone section. | string |
Examples:
tarantool> datetime.new {nsec = 123456789,sec = 20,min = 25,hour = 18,day = 20,month = 8,year = 2021,tzoffset = 180}---- 2021-08-20T18:25:20.123456789+0300...tarantool> datetime.new {nsec = 123456789,sec = 20,min = 25,hour = 18,day = 20,month = 8,year = 2021,tzoffset = 60,tz = 'Europe/Moscow'}---- 2021-08-20T18:25:20.123456789 Europe/Moscow...tarantool> datetime.new {day = -1,month = 2,year = 2021,}---- 2021-02-28T00:00:00Z...tarantool> datetime.new {timestamp = 1656664205.123,tz = 'Europe/Moscow'}---- 2022-07-01T08:30:05.122999906 Europe/Moscow...tarantool> datetime.new {nsec = 123,timestamp = 1656664205,tz = 'Europe/Moscow'}---- 2022-07-01T08:30:05.000000123 Europe/Moscow...
datetime.now()
Create an object of the datetime type with the current date and time.
Returns
Return type
cdata
datetime.is_datetime([value])
Check whether the specified value is a datetime object.
value(any) — the value to check
Returns
true if the specified value is a datetime object; otherwise,
false
Return type
boolean
datetime.parse( 'input_string'[, {format, tzoffset} ] )
Convert an input string with the date and time information into a
datetime object. The input string should be formatted according to one
of the following standards:
By default fields that are not specified are equal to appropriate values in a Unix time.
Leap second is supported at the most basic level, see the section leap second.
-
input_string(string) — string with the date and time information. -
format(string) — indicator of theinput_stringformat. Possible values: 'iso8601', 'rfc3339', orstrptime-like format string. If no value is set, the default formatting is used ("%F %T %Z"). Note that only a part of possible ISO 8601 and RFC 3339 formats are supported. To parse unsupported formats, you can specify a format string manually using conversion specifications and ordinary characters. -
tzoffset(number) — time zone offset from UTC, in minutes.
Returns
a datetime_object
Return type
cdata
Returns
a number of parsed characters
Return type
number
Implementation details:
-
For formats with a decimal fraction of the second ([1], 5.3.1.4, a) the tail beyond 9 fracitonal digits is truncated.
tarantool> datetime.parse('2024-07-31T17:30:00.123456789999', {format = 'iso8601'})---- 2024-07-31T17:30:00.123456789Z- 32... -
For formats with a decimal fraction of the hour ([1], 5.3.1.4, c) or minute ([1], 5.3.1.4, b) fractions are truncated to seconds precision. If second fractions are desired, explicit representation (format a) must be used.
tarantool> datetime.parse('2024-07-31T17,333333333', {format = 'iso8601'})---- 2024-07-31T17:19:59Z- 23...tarantool> datetime.parse('2024-07-31T17:30.333333333', {format = 'iso8601'})
| :30:19Z | nsec (usec, msec) | Fractional part of the last second. You can specify either nanoseconds ( |
|---|---|---|---|
number | 0 | sec | Seconds |
number | 0 | min | Minutes |
number | 0 | hour | Hours |
number | 0 | day | Day number |
number | 0 | week | Week number |
number | 0 | month | Month number |
number | 0 | year | Year |
number | 0 adjust : | Defines how to round days in a month after an arithmetic operation. | string |
'none' Examples: | ` tarantoolses rantool> datet | ion me.interval.new() | 0 seconds . |
rantool> datet mont year } +1 years, 6 mo . | me.interval.new { = 6, = 1 ths | rantool> datet day } -1 days . ` | me.interval.new { -1 |
Properties | TZnce: ``2.11.0 <` | release/2.11.0>` | Lua table that breviations (l imezone](timez |
maps timezone names (like | scow`) and tim a. See the | zone | ` tarantoolses rantool> datet 947 . |
ion me.TZ['Europe/Moscow'] | rantool> datet Europe/Moscow . ` : | me.TZ[947] | Related obje |
ts | ect | atetime_object | |
ct. | : method d( input[, { | djust } ] ) | dify an existi gument. See al rformed taking e set, see the
g datetime object by adding values o: |
f the input ). The additio t | is ds | ram table inpu an [interval **Example #1 | object](#interval_obj) or an equival *) |
nt table (see | ram string adj defines how Possible val Defaults to
st o round days in a month after an ar es: ``none | thmetic operat Example #2**). | on. |
turn datetime_obj | ct | ype cdata | Example #1:** |
` tarantoolses rantool> dt = day mont year tzof } . | ion atetime.new { 26, = 8, = 2021, set = 180 | rantool> iv = . | atetime.interval.new {day = 7} |
rantool> dt, i 2021-08-26T00: +7 days .
0: | rantool> dt:ad 2021-09-02T00: . | (iv) 0:00+0300 | rantool> dt:ad 2021-09-09T00: . ` |
{ day = 7 } 0:00+0300 | Example #2:** | ` tarantoolses rantool> dt = day mont year } . | ion atetime.new { 29, = 2, = 2020 |
rantool> dt:ad 2020-03-29T00: . | {month = 1, adjust = 'none'} 0:00Z | rantool> dt = day mont year } . | atetime.new { 29, = 2, = 2020 |
rantool> dt:ad 2020-03-31T00: . | {month = 1, adjust = 'last'} 0:00Z | rantool> dt = day mont year } . | atetime.new { 31, = 1, = 2020 |
rantool> dt:ad 2020-03-02T00: . ` : | {month = 1, adjust = 'excess'} 0:00Z | : method rmat( ['inpu | _string'] ) |
nvert the stan ring. The conv trftime](https nction. Additi ich also allow actional part: guments are se '%FT%T.%f%z'] | ard | nto a formatte in the strftime&sekti [%f]{.title-r ecision of elow). If no ions are used: | n=3) f} |
ram string inp string consi ordinary cha | t_string ting of zero or more conversion spe acters | ifications and | turn string with |
he formatted date and time informat | on | ype string Example: | ` tarantoolses rantool> dt = nsec |
ion atetime.new { = 123456789, | sec min hour | 20, 25, = 18, | day mont year |
20, = 8, = 2021, | tzof } . | set = 180 | rantool> dt:fo 20.08.21 18:25 . |
mat('%d.%m.%y %H:%M:%S.%5f') 20.12345 | rantool> dt:fo 2021-08-20T18: . | mat() 5:20.123456789+0300 | rantool> dt:fo 2021-08-20T18: . ` : |
mat('%FT%T.%f%z') 5:20.123456789+0300 | : method t( [{ units } | ] ) | date the field |
values in the existing | ject. | ram table unit Table of tim same as for | units. The [time units](datetime-n he |
w-args) are th | turn updated date | ime_object | ype cdata Example: |
` tarantoolses rantool> dt = nsec | ion atetime.new { = 123456789, | sec min hour | 20, 25, = 18, |
day mont year | 20, = 8, = 2021, | tzof } | set = 180 |
rantool> dt:se 2021-08-20T18: . | {msec = 567} 5:20.567+0300 | rantool> dt:se 2021-08-20T18: . ` : | {tzoffset = 60} 5:20.567+0100 |
: method b( { input[, | djust ] } ) | dify an existi gument. See al performed tak elds are set,
g datetime object by subtracting va o: | ues of the inp ). The subtrac fset |
t ion | ram table inpu an [interval Example) | object](#interval_obj) or an equival | nt table (see |
ram string adj defines how Possible val logic is sim [Example #2]
st o round days in a month after an ar es: ``none | thmetic operat ts to | on. he | turn datetime_obj |
ct | ype cdata Example: | ` tarantoolses rantool> dt = day mont year tzof } . | ion atetime.new { 26, = 8, = 2021, set = 180 |
rantool> iv = . | atetime.interval.new {day = 5} | rantool> dt, i 2021-08-26T00: +5 days .
0: | rantool> dt:su 2021-08-21T00: . |
(iv) 0:00+0300 | rantool> dt:su 2021-08-20T00: . ` : | { day = 1 } 0:00+0300 | ethod table() |
nvert the info e resulting ta | mation from a | the table for | at. |
Field name nsec | Description Nanoseconds. Number. | sec | Seconds. Number. |
min | Minutes. Number. | hour | Hours. Number. |
day | Day number. | month | Month number. |
year | Year. Number. | wday | Days since the beginning of is Sunday as for `os.date('* |
he week. Numbe ')`. | . 1 | yday | Days since the beginning of |
he year. Numbe | . | timestamp | Timestamp, in seconds. Numbe |
. | isdst | Is the DST (Daylight Saving the date, see a section [tim Boolean. | ime) applicabl zone](timezone |
for . | tzoffset | Time zone offset from UTC, s timezone. Number | e a section tz : |
Time zone name or abbreviati timezone. String | n, see a secti | n | turn table with t |
e date and time parameters | ype table Example: | ` tarantoolses rantool> dt = sec min hour | ion atetime.new { 20, 25, = 18, |
day mont year tz = } . | 20, = 8, = 2021, 'MAGT', | rantool> dt:to tz: 'MAGT' sec: 20 min: 25 yday: 232 day: 20 nsec: 0 isdst: false wday: 6 tzoffset: 600 month: 8 year: 2021 hour: 18 . ` | able() |
ect | nterval_object | ect. | |
ethod table() | nvert the info e resulting ta | mation from an | o the table fo |
mat. | Field name nsec | Description Nanoseconds | sec |
Seconds | min | Minutes | hour |
Hours | day | Day number | month |
Month number | year | Year | week |
Week number adjust : | Defines how to round days in arithmetic operation. | a month after | n |
turn table with t | e date and time parameters | ype table Example: | ` tarantoolses rantool> iv = . |
ion atetime.interval.new{month = 1, adj | st = 'last'} | rantool> iv:to adjust: last sec: 0 nsec: 0 day: 0 week: 0 hour: 0 month: 1 year: 0 min: 0 . ` | able() |
Datetime and | nterval arithmetic | e | dule enables creating of objects of nterval`. two types: |
you need to s e modifier met [datetime_obj ithmetic using | ift the | u can use eith dd()](datetime apply interval sub`) methods. | r add) |
atetime_object ject, but | add() | y the current operation resu | t. |
the interval quentially cal allest ( | peration, each of the interval subc ulated starting from the largest (` : | mponents is ear`) to the |
|
s ths s s es ds seconds | the results o mponents, an e | the operation exceed the allowed r ception is raised. | nge for any of |
the | e | d | in arithmetic |
The sum of two sum of each pa The result of interval objec of particular If you add dat object. The ad largest compon Subtraction of difference of the epoch seco is, years, mon An untyped tab datetime or in object with an | intervals is an interval object, wh ticular component of operands. ubtraction of two intervals is simi where each subcomponent is the res ields in the original operands. time and interval objects, the resu ition is performed in a determined nt ( | he on he f t d ed e matrix of th pes: |
|
their result | datetime | datetime interval unsupported datetime | table datetime |
interval : | datetime interval | interval e matrix of th pes: |
|
and their resu | t | datetime | datetime interval interval datetime |
table datetime | interval : | unsupported interval | interval |
e subtraction zdata` into ac | nd addition of datetime objects are ount | g |
|
ion me.new({tz='MSK'}) - datetime.new({ | z='UTC'}) | Datetime and | nterval comparison |
you need to c n use standard d | mpare the | object values, , | you |
pport for rela nce `2.11.0 </ | ional operators for | ed Example 1: | ` tarantoolses rantool> dt1 = . |
ion datetime.new({ year = 2010 }) | rantool> dt2 = . | datetime.new({ year = 2024 }) | rantool> dt1 = false . |
dt2 | rantool> dt1 < true . ` | dt2 Example 2: | ` tarantoolses rantool> iv1 = . |
ion datetime.interval.new({month = 1}) | rantool> iv2 = . | datetime.interval.new({month = 2}) | rantool> iv1 < true . ` |
iv2 | Leap second | eap seconds](h e-second adjus ep a system/'s rth/'s rotatio ents, and due predictable.
tps: | ond) are a per UTC) in order time. However, c and geologic arly spaced an |
odic o the l | rantool includ tabase](https: scription file dule [tarantoo | s the Time Zone /www.iana.org/time-zones) that besi also contains a leapseconds file. to get a used v | es the time zo ou can use the rsion of `tzda |
e Lua a`. | e | dule supports leap seconds at the m | st basic level |
The function [ input string w | atetime.parse()](#datetime-parse) co th 60 seconds: | rectly parses | n |
-
The datetime.new() function and the datetime_object:set() method accept a table with the
seckey set to 60 seconds:tarantool> datetime.new({ sec = 60 })---- 1970-01-01T00:01:00Z...
Meanwhile the following cases are NOT supported by the datetime
module:
-
With the datetime.new() function, the 60 leap seconds in the
seckey give an extra minute like regular seconds, and the result is represented in a regular manner, without leap seconds:datetime.new({ year = 1998, month = 12, day = 31, hour = 23, min = 59, sec = 60})---- 1999-01-01T00:00:00Z -
The function datetime.parse() produces an error when parsing a leap second input string with 60 seconds and a format that supports leap seconds ('rfc3339', 'iso8601'):
datetime.parse('1998-12-31T23:59:60Z', {format='rfc3339'})---- error: 'builtin/datetime.lua:885: could not parse ''1998-12-31T23:59:60Z'''
Full support has been added since 2.11.0.
Tarantool uses the Time Zone Database
(also known as the Olson database and supported by IANA) for timezone
support. You can use the Lua module tarantool to get
a used version of tzdata.
Every datetime object has three fields that represent timezone support:
tz, tzoffset, and isdst:
-
The field
isdstis calculated using tzindex and attributes of the selected timezone in the Olson DB timezone.tarantool> require('datetime').parse('2004-06-01T00:00 Europe/Moscow').isdst---- true... -
The field
tzfield can be set to a timezone name or abbreviation. A timezone name is a human-readable name based on the Time Zone Database, for example, "Europe/Moscow". Timezone abbreviations represent time zones by alphabetic abbreviations such as "EST", "WST", and "F". Both timezone names and abbreviations are available via the bidirectional array datetime.TZ. -
The field
tzoffsetis calculated automatically using the current Olson rule. This means that it takes into account summer time, leap year, and leap seconds information when a timezone name is set. However, thetzoffsetfield can be set manually when an appropriate timezone is not available.
The fields tz and tzoffset can be set in
datetime.new(), datetime.parse(), and
datetime_object:set(). The arithmetic on datetime
objects are performed taking tzdata into account, when tzoffset or
tz fields are set, see the interval_arithm section.
-
The supported date range is from
-5879610-06-22to+5879611-07-11. -
There were moments in past history when local mean time in some particular zone used a timezone offset not representable in a whole minutes but rather in seconds. For example, in Moscow before 1918 there used to be offset +2 hours 31 minutes and 19 seconds. See an Olson dump for this period:
$ zdump -c1880,1918 -i Europe/MoscowTZ="Europe/Moscow"- - +023017 MMT1916-07-03 00:01:02 +023119 MMT1917-07-02 00 +033119 MST 11917-12-27 23 +023119 MMTModern
tzdatarules do not use such a tiny fraction, and all timezones differ from UTC in units measured in minutes, not seconds. Tarantool datetime module uses minutes internally as units fortzoffset. So there might be some loss of precision if you try to operate with such ancient timestamps.