[Tarantool-patches] [PATCH v5 6/8] lua, datetime: time intervals support

Serge Petrenko sergepetrenko at tarantool.org
Tue Aug 17 15:16:25 MSK 2021



16.08.2021 02:59, Timur Safin via Tarantool-patches пишет:
> * created few entry points (months(N), years(N), days(N), etc.)
>    for easier datetime arithmetic;
> * additions/subtractions of years/months use `dt_add_years()`
>    and `dt_add_months()` from 3rd party c-dt library;
> * also there are `:add{}` and `:sub{}` methods in datetime
>    object to add or substract more complex intervals;
> * introduced `is_datetime()` and `is_interval()` helpers for checking
>    of validity of passed arguments;
> * human-readable stringization implemented for interval objects.
>
> Note, that additions/subtractions completed for all _reasonable_
> combinations of values of date and interval types;
>
> 	Time + Interval	=> Time
> 	Interval + Time => Time
> 	Time - Time 	=> Interval
> 	Time - Interval => Time
> 	Interval + Interval => Interval
> 	Interval - Interval => Interval
>
> Part of #5941
> ---
>   extra/exports                  |   3 +
>   src/lua/datetime.lua           | 349 ++++++++++++++++++++++++++++++++-
>   test/app-tap/datetime.test.lua | 159 ++++++++++++++-
>   3 files changed, 506 insertions(+), 5 deletions(-)
>
> diff --git a/extra/exports b/extra/exports
> index c34a5c2b5..421dda51f 100644
> --- a/extra/exports
> +++ b/extra/exports
> @@ -157,6 +157,9 @@ datetime_to_string
>   datetime_unpack
>   decimal_from_string
>   decimal_unpack
> +tnt_dt_add_years
> +tnt_dt_add_quarters
> +tnt_dt_add_months
>   tnt_dt_dow
>   tnt_dt_from_rdn
>   tnt_dt_from_struct_tm
> 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
> @@ -24,6 +24,17 @@ ffi.cdef [[
>   
>       int      tnt_dt_rdn          (dt_t dt);
>   
> +    // dt_arithmetic.h
> +    typedef enum {
> +        DT_EXCESS,
> +        DT_LIMIT,
> +        DT_SNAP
> +    } dt_adjust_t;
> +
> +    dt_t    tnt_dt_add_years        (dt_t dt, int delta, dt_adjust_t adjust);
> +    dt_t    tnt_dt_add_quarters     (dt_t dt, int delta, dt_adjust_t adjust);
> +    dt_t    tnt_dt_add_months       (dt_t dt, int delta, dt_adjust_t adjust);
> +


Same about comments inside ffi.cdef. Better avoid them.

Please, split the cdef into reasonable blocks with
comments (when you need them)
between the blocks.


>       // dt_parse_iso.h
>       size_t tnt_dt_parse_iso_date          (const char *str, size_t len, dt_t *dt);
>       size_t tnt_dt_parse_iso_time          (const char *str, size_t len, int *sod, int *nsec);
> @@ -50,8 +61,10 @@ ffi.cdef [[
>   
>   local builtin = ffi.C
>   local math_modf = math.modf
> +local math_floor = math.floor
>   
>   local SECS_PER_DAY     = 86400
> +local NANOS_PER_SEC    = 1000000000
>   
>   -- c-dt/dt_config.h
>   
>
>
>      

Everything else looks fine.

-- 
Serge Petrenko



More information about the Tarantool-patches mailing list