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 2BEA16EC57; Thu, 29 Jul 2021 21:59:34 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2BEA16EC57 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627585174; bh=0fNUhDnzp2LWzeuj53CqjSWJ1G14KLGehmgIyK5uiHI=; h=To:Cc:References:Date:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=wATZFRugaijtKj3RGl5s7uOlfqFD2mw6o8yaMYDlieFMW6AiYz1V+ldmwdhHE2bDZ 0AA9ZIWTl6fKazvvQfnXJ2NlVD66iMbkzgtGmSPYYWcfD8nBpf1eRigMKH3D7LAVzM MzLJQWHVabCMJcWlNd+1jRkYo7FVGMFqJdO7/tuo= Received: from smtp37.i.mail.ru (smtp37.i.mail.ru [94.100.177.97]) (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 C48B16F3C9 for ; Thu, 29 Jul 2021 21:58:39 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C48B16F3C9 Received: by smtp37.i.mail.ru with esmtpa (envelope-from ) id 1m9BEs-0003zd-RO; Thu, 29 Jul 2021 21:58:39 +0300 To: Timur Safin , v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org References: Message-ID: <531544e8-0dd8-8d71-ccfc-4dae2805d498@tarantool.org> Date: Thu, 29 Jul 2021 21:58:38 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-GB X-7564579A: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD941C43E597735A9C34755E0A9F196FCB739C645213AB7C8E0182A05F5380850404371F29C56E7272BD7EABE606E81601CF3B0DCB52180BB44B0B75209A7910909 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7F2393C4755A27B53EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006375121C8F3070B83748638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8596A6F77846979B2B58EA15A163A2C69117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE7B96B19DC4093321CE99E4F20BAF2718D8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE389DDFE3E282F3DD1AD7EC71F1DB88427C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F79006379FC65886B7F75608EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B50546BEDFC03AC585BABDD72E3AF16C6AED X-C1DE0DAB: 0D63561A33F958A52D82A3A79FE582E2F80341D8F8A12ED54233B10E65CF72F2D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7536C62C4FBC402878410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3453037B5638665721F602FF5D6CF93EECB1265E4934AA79E9B37D5630A92899668BF141ABFF9B34981D7E09C32AA3244C1503A7414426CDC3D44F0419CE7ED82464EE5813BBCA3A9DFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojPp/mPgZxawGHVYZsVptdgQ== X-Mailru-Sender: 583F1D7ACE8F49BD1042885CEC987B6B4A0461C269D1C4B6D7EABE606E81601C7958A49548C749F67019711D9D5B048E1458020726E2BC9FD5ECBA0B92C0A936CDC7563AA7CEBD287402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH resend v2 10/11] lua, datetime: unixtime, timestamp setters in datetime.lua 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: Oleg Babin via Tarantool-patches Reply-To: Oleg Babin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thanks for your patch. The comment is primarly the same as in previous patch. Why do you use such slow approach and creates redundant tables and function (temporary metatable in fact) to use it only once? Please fix it. Unixtime and timestamp is great but they loss precision. I think it should be possible go get timestamp with nanoseconds precision since datetime has nanoseconds precision. Also still it's hard how to convert timestamp to datetime value back. On 28.07.2021 13:34, Timur Safin via Tarantool-patches wrote: > * implemented proper range checks for date attributes values; > > * created `.unixtime` attribute, which is alias to `.secs`, > with corresponding setter/getter; > > * similarly to `unixtime`, created virtual `timestamp` attribute > setter. Which is a convenient way to simultaneously assign > unixtime (seconds since epoch) and nanoseconds > > Part of #5941 > --- > src/lua/datetime.lua | 90 ++++++++++++++++++++++++++++++-------------- > 1 file changed, 61 insertions(+), 29 deletions(-) > > diff --git a/src/lua/datetime.lua b/src/lua/datetime.lua > index 1466b923f..cc2ae119b 100644 > --- a/src/lua/datetime.lua > +++ b/src/lua/datetime.lua > @@ -342,12 +342,33 @@ local function _normalize_nsec(secs, nsec) > return secs, nsec > end > > +local function seconds_fraction(v) > + local seconds, fraction > + fraction = v % 1 > + if fraction > 0 then > + seconds = v - (v % 1) > + else > + seconds = v > + end > + return seconds, fraction > +end > + > +local function check_range(v, range, txt) > + assert(#range == 2) > + if not (v >= range[1] and v <= range[2]) then > + error(('value %d of %s is out of allowed range [%d, %d]'): > + format(v, txt, range[1], range[2])) > + end > +end > + > -- addition or subtraction from date/time of a given interval > -- described via table direction should be +1 or -1 > local function interval_increment(self, o, direction) > assert(direction == -1 or direction == 1) > - check_date(self, "interval_increment(date, object, -+1)") > - assert(type(o) == 'table') > + check_date(self, "interval_increment(date, object, direction)") > + if type(o) ~= 'table' then > + error('interval_increment(date, object, direction) - object expected', 2) > + end > > local ym_updated = false > local dhms_updated = false > @@ -359,49 +380,43 @@ local function interval_increment(self, o, direction) > for key, value in pairs(o) do > local handlers = { > years = function(v) > - assert(v > 0 and v < 10000) > + check_range(v, {0, 9999}, key) > dt = cdt.dt_add_years(dt, direction * v, cdt.DT_LIMIT) > ym_updated = true > end, > > months = function(v) > - assert(v > 0 and v < 13 ) > + check_range(v, {0, 12}, key) > dt = cdt.dt_add_months(dt, direction * v, cdt.DT_LIMIT) > ym_updated = true > end, > > weeks = function(v) > - assert(v > 0 and v < 32) > + check_range(v, {0, 52}, key) > secs = secs + direction * 7 * v * SECS_PER_DAY > dhms_updated = true > end, > > days = function(v) > - assert(v > 0 and v < 32) > + check_range(v, {0, 31}, key) > secs = secs + direction * v * SECS_PER_DAY > dhms_updated = true > end, > > hours = function(v) > - assert(v >= 0 and v < 24) > + check_range(v, {0, 23}, key) > secs = secs + direction * 60 * 60 * v > dhms_updated = true > end, > > minutes = function(v) > - assert(v >= 0 and v < 60) > + check_range(v, {0, 59}, key) > secs = secs + direction * 60 * v > end, > > seconds = function(v) > - assert(v >= 0 and v < 61) > - local s, frac > - frac = v % 1 > - if frac > 0 then > - s = v - (v % 1) > - else > - s = v > - end > + check_range(v, {0, 60}, key) > + local s, frac = seconds_fraction(v) > secs = secs + direction * s > nsec = nsec + direction * frac * 1e9 -- convert fraction to nanoseconds > dhms_updated = true > @@ -429,6 +444,9 @@ end > > local datetime_index = function(self, key) > local attributes = { > + unixtime = function(self) > + return self.secs > + end, > timestamp = function(self) > return tonumber(self.secs) + self.nsec / 1e9 > end, > @@ -467,6 +485,24 @@ local datetime_index = function(self, key) > return attributes[key] ~= nil and attributes[key](self) or nil > end > > +local function datetime_newindex(self, key, value) > + local attributes = { > + unixtime = function(self, value) > + self.secs = value > + self.nsec, self.offset = 0, 0 > + end, > + timestamp = function(self, value) > + local secs, frac = seconds_fraction(value) > + self.secs = secs > + self.nsec = frac * 1e9 > + self.offset = 0 > + end, > + } > + if attributes[key] ~= nil then > + attributes[key](self, value) > + end > +end > + > local function datetime_new_raw(secs, nsec, offset) > local dt_obj = ffi.new(datetime_t) > dt_obj.secs = secs > @@ -518,50 +554,45 @@ local function datetime_new(o) > end, > > year = function(v) > - assert(v > 0 and v < 10000) > + check_range(v, {1, 9999}, key) > y = v > ymd = true > end, > > month = function(v) > - assert(v > 0 and v < 13 ) > + check_range(v, {1, 12}, key) > M = v > ymd = true > end, > > day = function(v) > - assert(v > 0 and v < 32) > + check_range(v, {1, 31}, key) > d = v > ymd = true > end, > > hour = function(v) > - assert(v >= 0 and v < 24) > + check_range(v, {0, 23}, key) > h = v > hms = true > end, > > minute = function(v) > - assert(v >= 0 and v < 60) > + check_range(v, {0, 59}, key) > m = v > hms = true > end, > > second = function(v) > - assert(v >= 0 and v < 61) > - frac = v % 1 > - if frac > 0 then > - s = v - (v % 1) > - else > - s = v > - end > + check_range(v, {0, 60}, key) > + s, frac = seconds_fraction(v) > frac = frac * 1e9 -- convert fraction to nanoseconds > hms = true > end, > > -- tz offset in minutes > tz = function(v) > - assert(v >= 0 and v <= 720) > + check_range(v, {0, 720}, key) > offset = v > end > } > @@ -918,6 +949,7 @@ local datetime_mt = { > __sub = datetime_sub, > __add = datetime_add, > __index = datetime_index, > + __newindex = datetime_newindex, > add = function(self, o) > self = interval_increment(self, o, 1) > return self