[Tarantool-patches] [PATCH resend v2 08/11] lua, datetime: calculated attributes for datetimes

Oleg Babin olegrok at tarantool.org
Thu Jul 29 21:57:14 MSK 2021


Thanks for your patch. See one comment below.

Also this change should be squashed into the first patch.


On 28.07.2021 13:34, Timur Safin via Tarantool-patches wrote:
> * introduced a set of calculated attributes to data object, e.g.:
>    - timestamp, seconds, microseconds, minute, or hours
>
> Part of #5941
> ---
>   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 9ec06d8d8..7a208cef9 100644
> --- a/src/lua/datetime.lua
> +++ b/src/lua/datetime.lua
> @@ -224,6 +224,36 @@ local function interval_serialize(self)
>       return { secs = self.secs, nsec = self.nsec }
>   end
>   
> +local datetime_index = function(self, key)
> +    local attributes = {

This change looks like pessimization because currently we need to 
recreate "attributes" table

for each function call. I suggest to use `attributes` as `__index` but 
not such function.


> +        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
> +        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,
> @@ -232,23 +262,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 interval_mt = {
> @@ -257,6 +271,9 @@ local interval_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 09c968858..407d89556 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)


More information about the Tarantool-patches mailing list