From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 2D4316EC5A; Thu, 15 Jul 2021 11:23:25 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2D4316EC5A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1626337405; bh=T0takU5H8yU3lY42WtPc4HnOc/hwTw7zQI/HONw+63c=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=e5XwFnAEP18lTlunB9oatPv2j7AHmdQmaKqa/tsE823wmr2k6FNoXM35QUO4dDUBr RrlzwBBNOss6u0aMV7sryXgYDLLSr/rVQNKUUAL7Yu1yapDx/6a2g6J4STdk6rS8em qbKIzkX+8iYnFw7gQxEfNrhVI06OsjYLnepaneCM= Received: from smtp57.i.mail.ru (smtp57.i.mail.ru [217.69.128.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id E15876EC5C for ; Thu, 15 Jul 2021 11:18:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E15876EC5C Received: by smtp57.i.mail.ru with esmtpa (envelope-from ) id 1m3waA-00070N-M1; Thu, 15 Jul 2021 11:18:59 +0300 To: v.shpilevoy@tarantool.org Date: Thu, 15 Jul 2021 11:18:15 +0300 Message-Id: <06d6c106b99e2461e093a7449be12dc4ef8f8687.1626335241.git.tsafin@tarantool.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD941C43E597735A9C38DCC94ADF53B46036ECF04D6E32A6D03182A05F538085040991E0B263B058DFB76146CFCB7726E6E37DE0BCA1FACE6E936066EF64D0718FB X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE70A10A23A3B64B805EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637737629E79DA7E02D8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8657074910D0E815A6E76A9D0DB82AA08117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC2EE5AD8F952D28FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18CB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EEC24E1E72F37C03A020DA3B6C150F7642D8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3735DFC8FA7AC1207040F9FF01DFDA4A8C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637AD0424077D726551EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CE68746B1F2AB10C6F3E8913E0252CECDF951D77622AB11499C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF3033054805BDE987699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D343D50AEDB859DBAD9A981820745182AC3E3F64FA405D431269C16538806057CB7BBD868E6944E098C1D7E09C32AA3244C319A78A839BB6918933451997F4B71AA33C9DC155518937F83B48618A63566E0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXjnYxxC9kpVb0FtMxrUVdDx X-Mailru-Sender: B5B6A6EBBD94DAD8DA84A184D75F19DB2E7B7B20C3E75D0F4D5E5A485575C7B6AD49838AFA339EA01EC9E4A2C82A33BC8C24925A86E657CE0C70AEE3C9A96FBAB3D7EE8ED63280BE112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [RFC PATCH 09/13] lua: calculated attributes for date X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Timur Safin via Tarantool-patches Reply-To: Timur Safin Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" * 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