Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Timur Safin via Tarantool-patches <tarantool-patches@dev.tarantool.org>
Cc: v.shpilevoy@tarantool.org
Subject: Re: [Tarantool-patches] [PATCH v5 6/8] lua, datetime: time intervals support
Date: Tue, 17 Aug 2021 21:52:25 +0300	[thread overview]
Message-ID: <20210817185225.qypvxdifvpvr7od2@esperanza> (raw)
In-Reply-To: <58bf0cbee84cccbe619c225aafa7d471a4c920ca.1629071531.git.tsafin@tarantool.org>

On Mon, Aug 16, 2021 at 02:59:40AM +0300, Timur Safin via Tarantool-patches wrote:
> diff --git a/src/lua/datetime.lua b/src/lua/datetime.lua
> index 4d946f194..5fd0565ac 100644
> --- a/src/lua/datetime.lua
> +++ b/src/lua/datetime.lua
> @@ -62,8 +75,23 @@ local DT_EPOCH_1970_OFFSET = 719163
>  local datetime_t = ffi.typeof('struct datetime')
>  local interval_t = ffi.typeof('struct datetime_interval')
>  
> +ffi.cdef [[
> +    struct interval_months {
> +        int m;
> +    };
> +
> +    struct interval_years {
> +        int y;
> +    };
> +]]
> +local interval_months_t = ffi.typeof('struct interval_months')
> +local interval_years_t = ffi.typeof('struct interval_years')

I disagree that this is required. interval_years and interval_months are
very vague notions - they depend on the date they are applied to.
Besides, supporting them complicates the code. I think that a time
interval must be exact. At the same time, I agree it may be convenient
to add a few years or months to a date. Why not add datetime methods
(add_years, add_months) instead of introducing two new kinds of
intervals?

> +
>  local function is_interval(o)
> -    return type(o) == 'cdata' and ffi.istype(interval_t, o)
> +    return type(o) == 'cdata' and
> +           (ffi.istype(interval_t, o) or
> +            ffi.istype(interval_months_t, o) or
> +            ffi.istype(interval_years_t, o))
>  end
>  
>  local function is_datetime(o)
> @@ -72,7 +100,10 @@ end
>  
>  local function is_date_interval(o)
>      return type(o) == 'cdata' and
> -           (ffi.istype(interval_t, o) or ffi.istype(datetime_t, o))
> +           (ffi.istype(datetime_t, o) or
> +            ffi.istype(interval_t, o) or
> +            ffi.istype(interval_months_t, o) or
> +            ffi.istype(interval_years_t, o))
>  end
>  
>  local function interval_new()
> @@ -80,6 +111,13 @@ local function interval_new()
>      return interval
>  end
>  
> +local function check_number(n, message)
> +    if type(n) ~= 'number' then
> +        return error(("%s: expected number, but received %s"):
> +                     format(message, n), 2)
> +    end
> +end
> +
>  local function check_date(o, message)
>      if not is_datetime(o) then
>          return error(("%s: expected datetime, but received %s"):
> @@ -87,6 +125,20 @@ local function check_date(o, message)
>      end
>  end
>  
> +local function check_date_interval(o, message)
> +    if not is_datetime(o) and not is_interval(o) then
> +        return error(("%s: expected datetime or interval, but received %s"):
> +                     format(message, o), 2)
> +    end
> +end
> +
> +local function check_interval(o, message)
> +    if not is_interval(o) then
> +        return error(("%s: expected interval, but received %s"):
> +                     format(message, o), 2)
> +    end
> +end
> +
>  local function check_str(s, message)
>      if not type(s) == 'string' then
>          return error(("%s: expected string, but received %s"):
> @@ -102,6 +154,77 @@ local function check_range(v, range, txt)
>      end
>  end
>  
> +local function interval_years_new(y)
> +    check_number(y, "years(number)")
> +    local o = ffi.new(interval_years_t)
> +    o.y = y
> +    return o
> +end
> +
> +local function interval_months_new(m)
> +    check_number(m, "months(number)")
> +    local o = ffi.new(interval_months_t)
> +    o.m = m
> +    return o
> +end
> +
> +local function interval_weeks_new(w)
> +    check_number(w, "weeks(number)")
> +    local o = ffi.new(interval_t)
> +    o.secs = w * SECS_PER_DAY * 7
> +    return o
> +end
> +
> +local function interval_days_new(d)
> +    check_number(d, "days(number)")
> +    local o = ffi.new(interval_t)
> +    o.secs = d * SECS_PER_DAY
> +    return o
> +end
> +
> +local function interval_hours_new(h)
> +    check_number(h, "hours(number)")
> +    local o = ffi.new(interval_t)
> +    o.secs = h * 60 * 60
> +    return o
> +end
> +
> +local function interval_minutes_new(m)
> +    check_number(m, "minutes(number)")
> +    local o = ffi.new(interval_t)
> +    o.secs = m * 60
> +    return o
> +end
> +
> +local function interval_seconds_new(s)
> +    check_number(s, "seconds(number)")
> +    local o = ffi.new(interval_t)
> +    o.nsec = s % 1 * 1e9
> +    o.secs = s - (s % 1)
> +    return o
> +end

These functions should have been added by the patch that introduced the
datetime library to Lua, because without them the library doesn't seem
to be complete. Doing this in a separate patch doesn't ease review - in
fact it only confuses me as a reviewer, because now I have to jump
between two patches to see the whole picture. In general, please try to
split a patch set in such a way that each patch may be applied before
the rest without breaking anything, introducing dead code or a
half-working feature.

IMO the series should be split as follows:

 1. Add datetime library to Lua + unit tests + Lua tests.
 2. Add datetime serialization to msgpack/yaml/json + Lua tests.
 3. Add datetime indexing support + Lua tests.

  parent reply	other threads:[~2021-08-17 18:52 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-15 23:59 [Tarantool-patches] [PATCH v5 0/8] Initial datetime implementation Timur Safin via Tarantool-patches
2021-08-15 23:59 ` [Tarantool-patches] [PATCH v5 1/8] build: add Christian Hansen c-dt to the build Timur Safin via Tarantool-patches
2021-08-17 12:15   ` Serge Petrenko via Tarantool-patches
2021-08-17 23:24     ` Safin Timur via Tarantool-patches
2021-08-18  8:56       ` Serge Petrenko via Tarantool-patches
2021-08-17 15:50   ` Vladimir Davydov via Tarantool-patches
2021-08-18 10:04     ` Safin Timur via Tarantool-patches
2021-08-15 23:59 ` [Tarantool-patches] [PATCH v5 2/8] lua: built-in module datetime Timur Safin via Tarantool-patches
2021-08-17 12:15   ` Serge Petrenko via Tarantool-patches
2021-08-17 23:30     ` Safin Timur via Tarantool-patches
2021-08-18  8:56       ` Serge Petrenko via Tarantool-patches
2021-08-17 16:52   ` Vladimir Davydov via Tarantool-patches
2021-08-17 19:16     ` Vladimir Davydov via Tarantool-patches
2021-08-18 13:38       ` Safin Timur via Tarantool-patches
2021-08-18 10:03     ` Safin Timur via Tarantool-patches
2021-08-18 10:06       ` Safin Timur via Tarantool-patches
2021-08-18 11:45       ` Vladimir Davydov via Tarantool-patches
2021-08-15 23:59 ` [Tarantool-patches] [PATCH v5 3/8] lua, datetime: display datetime Timur Safin via Tarantool-patches
2021-08-17 12:15   ` Serge Petrenko via Tarantool-patches
2021-08-17 23:32     ` Safin Timur via Tarantool-patches
2021-08-17 17:06   ` Vladimir Davydov via Tarantool-patches
2021-08-18 14:10     ` Safin Timur via Tarantool-patches
2021-08-15 23:59 ` [Tarantool-patches] [PATCH v5 4/8] box, datetime: messagepack support for datetime Timur Safin via Tarantool-patches
2021-08-16  0:20   ` Safin Timur via Tarantool-patches
2021-08-17 12:15     ` Serge Petrenko via Tarantool-patches
2021-08-17 12:16   ` Serge Petrenko via Tarantool-patches
2021-08-17 23:42     ` Safin Timur via Tarantool-patches
2021-08-18  9:01       ` Serge Petrenko via Tarantool-patches
2021-08-17 18:36   ` Vladimir Davydov via Tarantool-patches
2021-08-18 14:27     ` Safin Timur via Tarantool-patches
2021-08-15 23:59 ` [Tarantool-patches] [PATCH v5 5/8] box, datetime: datetime comparison for indices Timur Safin via Tarantool-patches
2021-08-17 12:16   ` Serge Petrenko via Tarantool-patches
2021-08-17 23:43     ` Safin Timur via Tarantool-patches
2021-08-18  9:03       ` Serge Petrenko via Tarantool-patches
2021-08-17 19:05   ` Vladimir Davydov via Tarantool-patches
2021-08-18 17:18     ` Safin Timur via Tarantool-patches
2021-08-15 23:59 ` [Tarantool-patches] [PATCH v5 6/8] lua, datetime: time intervals support Timur Safin via Tarantool-patches
2021-08-17 12:16   ` Serge Petrenko via Tarantool-patches
2021-08-17 23:44     ` Safin Timur via Tarantool-patches
2021-08-17 18:52   ` Vladimir Davydov via Tarantool-patches [this message]
2021-08-15 23:59 ` [Tarantool-patches] [PATCH v5 7/8] datetime: perf test for datetime parser Timur Safin via Tarantool-patches
2021-08-17 19:13   ` Vladimir Davydov via Tarantool-patches
2021-08-15 23:59 ` [Tarantool-patches] [PATCH v5 8/8] datetime: changelog for datetime module Timur Safin via Tarantool-patches
2021-08-17 12:16   ` Serge Petrenko via Tarantool-patches
2021-08-17 23:44     ` Safin Timur via Tarantool-patches
2021-08-18  9:04       ` Serge Petrenko via Tarantool-patches
2021-08-17 12:15 ` [Tarantool-patches] [PATCH v5 0/8] Initial datetime implementation Serge Petrenko via Tarantool-patches
     [not found] ` <20210818082222.mofgheciutpipelz@esperanza>
2021-08-18  8:25   ` Vladimir Davydov via Tarantool-patches
2021-08-18 13:24     ` Safin Timur via Tarantool-patches
2021-08-18 14:22       ` Vladimir Davydov 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=20210817185225.qypvxdifvpvr7od2@esperanza \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v5 6/8] lua, datetime: time intervals support' \
    /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