Tarantool CE/EE Documentation portal logo
Support
Updated at July 17, 2026   02:08 PM

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.

API Reference

Below is a list of datetime functions, properties, and related objects.

Functions

datetime.new()

Create an object of the datetime type from a table of time units

datetime.now()

Create an object of the datetime type with the current date and time

datetime.is_datetime()

Check whether the specified value is a datetime object

datetime.parse()

Convert an input string with the date and time information into a datetime object

datetime.interval.is_interval()

Check whether the specified value is an interval object

datetime.interval.new()

Create an object of the interval type from a table of time units

Properties

datetime.TZ

A Lua table that maps timezone names and abbreviations to its index and vice-versa.

Methods

datetime_object:add()

Modify an existing datetime object by adding values of the input argument

datetime_object:format()

Convert the standard datetime object presentation into a formatted string

datetime_object:set()

Update the field values in the existing datetime object

datetime_object:sub()

Modify an existing datetime object by subtracting values of the input argument

datetime_object:totable()

Convert the information from a datetime object into the table format

interval_object:totable()

Functions

datetime.new( [{ units }] )

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, the datetime object with the default values corresponding to Unix Epoch is created: 1970-01-01T00:00:00Z.

Returns

datetime object

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 (nsec), or microseconds (usec), or milliseconds (msec). Specifying two of these units simultaneously or all three ones lead to an error.

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 -1 generates the last day of a particular month (see example below).

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 datetime object. If the fractional part for the last second is set via the nsec, usec, or msec units, the timestamp value should be integer otherwise an error occurs. The timestamp is not allowed if you already set time and/or date via specific units, namely, sec, min, hour, day, month, and year.

number

0

tzoffset

A time zone offset from UTC, in minutes. Value range: -720 - 840 inclusive. If both tzoffset and tz are specified, tz has the preference and the tzoffset value is ignored. See a section timezone.

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

datetime object

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:

  • ISO 8601
  • RFC 3339
  • extended strftime – see description of the format() for details.

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 the input_string format. Possible values: 'iso8601', 'rfc3339', or strptime-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'})
  • 2024-07-31T1

:30:19Z

nsec (usec, msec)

Fractional part of the last second. You can specify either nanoseconds (nsec), or microseconds (usec), or milliseconds (msec). Specifying two of these units simultaneously or all three ones lead to an error.

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

TZ

nce: ``2.11.0 <`

release/2.11.0>`

Lua table that breviations (l imezone](timez

maps timezone names (like Europe/M ke MSK`) to its index and vice-ver ne) section.

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

datetime_ob

ect

atetime_object datetime obj

ct.

: method d( input[, {

djust } ] )

dify an existi gument. See al rformed taking e set, see the g datetime object by adding values o: [interval_arithm](interval_arith tzdatainto account, whentzoffs timezone.`

f the input ). The additio tortz` fie

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, last, excess (see * 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:00+0300

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 datetime object presentation rsion specifications are the same a //www.freebsd.org/cgi/man.cgi?query nal specification for nanoseconds i a modifier to control the output p %5f (see the example for the method, the default conver .title-ref} (see the example below)

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 datetime o

ject.

ram table unit Table of tim same as for

units. The [time units](datetime-n he datetime.new() function.

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: [interval_arithm](interval_arith ng tzdatainto account, whentzo ee the timezone.`

ues of the inp ). The subtrac fsetortz`

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, last, excess. Defau lar to the one of the :add() meth datetime-add-example2).

thmetic operat ts to none. d -- see

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:00+0300

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 datetime object int le has the following fields:

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()

interval_ob

ect

nterval_object interval ob

ect.

ethod table()

nvert the info e resulting ta

mation from an interval object in le has the following fields:

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 datetime m atetimeand

dule enables creating of objects of nterval`. two types:

you need to s e modifier met [datetime_obj ithmetic using

ift the datetime object values, y ods, that is, the datetime_object: ct:sub() methods, or overloaded + (__add) or - (`_

u can use eith dd()](datetime apply interval sub`) methods.

r add)

atetime_object ject, but +/

add()/datetime_object:sub() modi - create copy of the object as the

y the current operation resu

t.

the interval quentially cal allest (nsec

peration, each of the interval subc ulated starting from the largest (` :

mponents is ear`) to the

year -- yea month -- mo week -- wee day -- days hour -- hou min -- minu sec -- seco nsec -- nan

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 datetime a erations:

d interval objects can participat

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 (year) to the smallest (nsec two datetime objects produces an in wo time values is performed not as ds, but as difference of all the su hs, days, hours, minutes, and secon e object can be used in each contex erval objects are used if the left overloaded operation of + or -. se fields are ar: it's an lt of subtract t is a datetim rder from the . erval object. he difference components, th s. where the typ perand is a ty

he on he f t d ed

e matrix of th pes:

addition operands eligibility an

their result

datetime

datetime interval unsupported datetime

table datetime

interval :

datetime interval

interval

e matrix of th pes:

subtraction operands eligibility

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 tzoffset or tz fields are performed taki et:

g

tarantoolses rantool> datet -180 minutes .

ion me.new({tz='MSK'}) - datetime.new({

z='UTC'})

Datetime and

nterval comparison

you need to c n use standard d <=. These tamethods to c

mpare the datetime and interval Lua relational operators: ==, ~= perators use the overloaded __eq`, mpare values.

object values, , >, <, > _lt, and

you , le

pport for rela nce `2.11.0 </

ional operators for interval obje elease/2.11.0>`.

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://en.wikipedia.org/wiki/Leap_se ment of Coordinated Universal Time time of day close to the mean solar speed varies in response to climat o this, UTC leap seconds are irregu

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 datetime m

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

- 1970-01-01T23:13:00Z - 8 ... ```
  • The datetime.new() function and the datetime_object:set() method accept a table with the sec key 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 sec key 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'''

Time zones

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 isdst is 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 tz field 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 tzoffset is 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, the tzoffset field 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.

Limitations

  • The supported date range is from -5879610-06-22 to +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 MMT

    Modern tzdata rules 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 for tzoffset. So there might be some loss of precision if you try to operate with such ancient timestamps.

References