Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to work with dates in Tarantool?

Tags:

tarantool

There are several ways to get time in Tarantool:

  • Using the clock module
  • Using fiber.time function
  • Using os.date

But what is the correct way to work with dates?

like image 520
Andrey Avatar asked Jun 04 '20 09:06

Andrey


People also ask

Why Tarantool for OLTP?

Tarantool can be used in OLTP scenarios instead of relational databases, and such a solution will work many times faster. With Tarantool, you can replace the traditional bundle of database & cache and benefit from that by reducing operational costs. Tarantool is tolerant to write-heavy load.

Why is it so hard to work with dates and times?

It’s made more complicated by the fact that dates are also days of the week, like Monday or Friday, even though Excel doesn’t explicitly store that information in the cells. Here is the definitive guide to working with dates and times in Excel…

How to work with datetime in Python?

Python makes dealing with dates and time very easy, all we have to do is import a module named DateTime that comes along with python. It is a more efficient way to work with date without the need of being explicitly programmed.

Can dates and times be imported into worksheets as text?

Unfortunately, dates and times are often imported into worksheets as text. Most of the assorted functions like MONTH and HOUR are reasonably intelligent about converting on the fly.


2 Answers

For the first, there are several routines for Unix epoch:

  1. os.time() — classic Lua time function. Kinda slow and not very efficient. Not recommended to use inside tarantool for getting current epoch, but of course will work. May be used for getting epoch of arbitrary date (within local timezone). ex:
os.time({ year = 2020, month = 6, day = 4 })

will produce 1591261200, which is 12:00:00 in my GMT+3 timezone

  1. clock.time() (and clock.time64()) — High resolution timer, almost raw binding to clock_gettime. More information may be taken from doc
  2. fiber.time() (and also fiber.time64()) — Cached version of clock.time. Updated every event loop iteration. Recommended for use if absolute precision of clock is not required.

For converting epoch into different formats and timezones there are variants:

  1. os.date("<format>" [, epoch ]) — Convert epoch into local timezone.
  2. os.date("!<format>" [, epoch ]) (note ! prefix) — Convert epoch into GMT timezone.
  3. For getting components of a date as a table we may use os.date('*t') for local and os.date('!*t') for UTC
  4. icu-date may be considered it you need to work with different timezones and/or formats.

For example, if you need UTC time, it's ok to use cached fiber.time with os.date:

local fiber = require 'fiber'
os.date("!%Y-%m-%dT%H:%M:%SZ", fiber.time())

will return something like 2020-06-04T11:48:54Z independently on timezone

like image 109
Mons Anderson Avatar answered Dec 14 '22 05:12

Mons Anderson


It depends on your task.

If it's important for you to manipulate with timezones/formats etc. I suggest to use icu-data library (https://github.com/tarantool/icu-date)

like image 21
Oleg Babin Avatar answered Dec 14 '22 04:12

Oleg Babin