From: Timur Safin via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Subject: [Tarantool-patches] [RFC PATCH 09/13] lua: calculated attributes for date Date: Thu, 15 Jul 2021 11:18:15 +0300 [thread overview] Message-ID: <06d6c106b99e2461e093a7449be12dc4ef8f8687.1626335241.git.tsafin@tarantool.org> (raw) In-Reply-To: <cover.1626335241.git.tsafin@tarantool.org> * introduced numerous calculated attributes to data object, like: - timestamp, seconds, microseconds, minute, or hours * added new test case for that --- src/lua/datetime.lua | 51 ++++++++++++++++++++++------------ test/app-tap/datetime.test.lua | 17 +++++++++++- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/lua/datetime.lua b/src/lua/datetime.lua index 086c97697..5cb53c6f2 100644 --- a/src/lua/datetime.lua +++ b/src/lua/datetime.lua @@ -218,6 +218,36 @@ local function duration_serialize(self) return { secs = self.secs, nsec = self.nsec } end +local datetime_index = function(self, key) + local attributes = { + timestamp = function(self) + return tonumber(self.secs + self.nsec / 1e9) + end, + nanoseconds = function(self) + return tonumber(self.secs * 1e9 + self.nsec) + end, + microseconds = function(self) + return tonumber(self.secs * 1e6 + self.nsec / 1e3) + end, + milliseconds = function(self) + return tonumber(self.secs * 1e3 + self.nsec / 1e6) + end, + seconds = function(self) + return tonumber(self.secs + self.nsec / 1e9) + end, + minutes = function(self) + return tonumber((self.secs + self.nsec / 1e9) / 60 % 60) + end, + hours = function(self) + return tonumber((self.secs + self.nsec / 1e9) / (60 * 60)) + end, + days = function(self) + return tonumber((self.secs + self.nsec / 1e9) / (60 * 60)) / 24 + end, + } + return attributes[key] ~= nil and attributes[key](self) or nil +end + local datetime_mt = { -- __tostring = datetime_tostring, __serialize = datetime_serialize, @@ -226,23 +256,7 @@ local datetime_mt = { __le = datetime_le, __sub = datetime_sub, __add = datetime_add, - - nanoseconds = function(self) - return tonumber(self.secs*NANOS_PER_SEC + self.nsec) - end, - microseconds = function(self) - return tonumber(self.secs*1e6 + self.nsec*1e3) - end, - seconds = function(self) - return tonumber(self.secs + self.nsec*1e3) - end, - minutes = function(self) - return tonumber((self._ticks/(1e6*60))%60) - end, - hours = function(self) - return tonumber(self._ticks/(1e6*60*60)) - end, - + __index = datetime_index, } local duration_mt = { @@ -251,6 +265,9 @@ local duration_mt = { __eq = datetime_eq, __lt = datetime_lt, __le = datetime_le, + __sub = datetime_sub, + __add = datetime_add, + __index = datetime_index, } local function datetime_new_raw(secs, nsec, offset) diff --git a/test/app-tap/datetime.test.lua b/test/app-tap/datetime.test.lua index b79450ecc..06720b241 100755 --- a/test/app-tap/datetime.test.lua +++ b/test/app-tap/datetime.test.lua @@ -4,7 +4,7 @@ local tap = require('tap') local test = tap.test("errno") local date = require('datetime') -test:plan(5) +test:plan(6) test:test("Simple tests for parser", function(test) test:plan(2) @@ -188,4 +188,19 @@ test:test("Parse iso date - invalid strings", function(test) end end) +test:test("Parse tiny date into seconds and other parts", function(test) + test:plan(9) + local str = '19700101 00:00:30.528' + local tiny = date(str) + test:ok(tiny.secs, 30, ("secs of '%s'"):format(str)) + test:ok(tiny.nsec, 528000000, ("nsec of '%s'"):format(str)) + test:ok(tiny.nanoseconds, 30528000000, "nanoseconds") + test:ok(tiny.microseconds, 30528000, "microseconds") + test:ok(tiny.milliseconds, 30528, "milliseconds") + test:ok(tiny.seconds, 30.528, "seconds") + test:ok(tiny.timestamp, 30.528, "timestamp") + test:ok(tiny.minutes, 0.5088, "minuts") + test:ok(tiny.hours, 0.00848, "hours") +end) + os.exit(test:check() and 0 or 1) -- 2.29.2
next prev parent reply other threads:[~2021-07-15 8:23 UTC|newest] Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-07-15 8:18 [Tarantool-patches] [RFC PATCH 00/13] Initial datetime support Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 01/13] build: add Christian Hansen c-dt to the build Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 02/13] lua: built-in module datetime Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 03/13] test: datetime test Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 04/13] test: datetime string formatting Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 05/13] box: add messagepack support for datetime Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 06/13] lua: positive/negative cases in datetime test Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 07/13] lua: asctime and strfime fixed Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 08/13] box, lua: renamed t_datetime_tz structure to datetime_t Timur Safin via Tarantool-patches 2021-07-15 8:18 ` Timur Safin via Tarantool-patches [this message] 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 10/13] lua: tostring formatization in datetime.lua Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 11/13] test: allow relaxed date format without tz Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 12/13] lua: initial time duration support Timur Safin via Tarantool-patches 2021-07-15 8:18 ` [Tarantool-patches] [RFC PATCH 13/13] lua: complete " Timur Safin via Tarantool-patches 2021-07-15 16:56 ` [Tarantool-patches] [RFC PATCH 00/13] Initial datetime support Oleg Babin via Tarantool-patches 2021-07-15 23:03 ` Timur Safin via Tarantool-patches 2021-07-16 6:58 ` Oleg Babin via Tarantool-patches 2021-07-22 10:01 ` Igor Munkin via Tarantool-patches 2021-07-22 12:54 ` Timur Safin via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=06d6c106b99e2461e093a7449be12dc4ef8f8687.1626335241.git.tsafin@tarantool.org \ --to=tarantool-patches@dev.tarantool.org \ --cc=tsafin@tarantool.org \ --cc=v.shpilevoy@tarantool.org \ --subject='Re: [Tarantool-patches] [RFC PATCH 09/13] lua: calculated attributes for date' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox