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 4FCBE6EC55; Thu, 29 Jul 2021 21:57:01 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4FCBE6EC55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627585021; bh=WWOv1YLh78ChsdOGP6bKRSVvkHv7YH7JgSOXbMLE2m4=; 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=Y5GqZMCQwdpTJdyRBNuNs1nFldbL4j48Snz/r8omC9QQx/HIQU5jG5cXPTAZ++tWP FwfFCEBf+ROdMIMM3BSzGCJtvMJeTSyU/WQP+4brsoi+8caniEKR+OtqBWR/mbomsL sG+dUNXUp8ce0+JG8LOSkS9riZXyA3wYoOdbZgA0= 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 959F16EC57 for ; Thu, 29 Jul 2021 21:55:52 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 959F16EC57 Received: by smtp57.i.mail.ru with esmtpa (envelope-from ) id 1m9BCB-0005hN-45; Thu, 29 Jul 2021 21:55:52 +0300 To: Timur Safin , v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org References: Message-ID: Date: Thu, 29 Jul 2021 21:55:50 +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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD941C43E597735A9C36A98DBA789EBB6AE26DB9A6C1D9BF7E0182A05F5380850402CE561F633BCF5083AAF1F6672DA87045F8C2E76AF9FD0E9EBA3495E54A80474 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7548C33FFD72831AFEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006378CCCB41504E044EE8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D84B4432C380ABC408809953B0EA69AEDB117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EEB28585415E75ADA9A9D86F9317F2E7ACD8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE30CABCCA60F52D7EB302FCEF25BFAB345C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637B8F435DEDE9E76EBEFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A54F14D4723508E48501A9B529CFB3DA75B75266FC8DB7D2FCD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA753530422897FB34C3410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D346C409ABC5F9C579B13FC32A8302F13BDB30DEF82E22892A925116FD9E5716C9179382FBEE615600F1D7E09C32AA3244C0D3667294421F8E58D096A63425D583005AB220A9D022EBCFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojPp/mPgZxawHn/SgLtunj8Q== X-Mailru-Sender: 583F1D7ACE8F49BD1042885CEC987B6BB2B6E69162C9B9A23AAF1F6672DA870460497F7EF99AAC8F7019711D9D5B048E1458020726E2BC9FD5ECBA0B92C0A936CDC7563AA7CEBD287402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH resend v2 04/11] lua, datetime: display datetime 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. It's not review at all. I have a question: why is datetime not scalar? Also I think that encoding and type system changes should live in separate patches. On 28.07.2021 13:34, Timur Safin via Tarantool-patches wrote: > * introduced output routine for converting datetime > to their default output format. > > * use this routine for tostring() in datetime.lua > > Part of #5941 > --- > src/exports.h | 1 + > src/lib/core/CMakeLists.txt | 1 + > src/lib/core/datetime.c | 85 +++++++++++++++++++++++++++++++++++++ > src/lib/core/datetime.h | 14 ++++++ > src/lua/datetime.lua | 18 ++++++-- > 5 files changed, 116 insertions(+), 3 deletions(-) > create mode 100755 src/lib/core/datetime.c > > diff --git a/src/exports.h b/src/exports.h > index db40c03a4..1a03db636 100644 > --- a/src/exports.h > +++ b/src/exports.h > @@ -552,3 +552,4 @@ EXPORT(dt_parse_iso_zone_extended) > EXPORT(dt_parse_iso_zone_lenient) > EXPORT(dt_from_struct_tm) > EXPORT(dt_to_struct_tm) > +EXPORT(datetime_to_string) > diff --git a/src/lib/core/CMakeLists.txt b/src/lib/core/CMakeLists.txt > index 2cd4d0b4f..8bc776b82 100644 > --- a/src/lib/core/CMakeLists.txt > +++ b/src/lib/core/CMakeLists.txt > @@ -30,6 +30,7 @@ set(core_sources > decimal.c > mp_decimal.c > cord_buf.c > + datetime.c > ) > > if (TARGET_OS_NETBSD) > diff --git a/src/lib/core/datetime.c b/src/lib/core/datetime.c > new file mode 100755 > index 000000000..65f813a70 > --- /dev/null > +++ b/src/lib/core/datetime.c > @@ -0,0 +1,85 @@ > +/* > + * Copyright 2021, Tarantool AUTHORS, please see AUTHORS file. > + * > + * Redistribution and use in source and binary forms, with or > + * without modification, are permitted provided that the following > + * conditions are met: > + * > + * 1. Redistributions of source code must retain the above > + * copyright notice, this list of conditions and the > + * following disclaimer. > + * > + * 2. Redistributions in binary form must reproduce the above > + * copyright notice, this list of conditions and the following > + * disclaimer in the documentation and/or other materials > + * provided with the distribution. > + * > + * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND > + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED > + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR > + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL > + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, > + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL > + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF > + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR > + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF > + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT > + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF > + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF > + * SUCH DAMAGE. > + */ > + > +#include > + > +#include "trivia/util.h" > +#include "datetime.h" > + > +int > +datetime_to_string(const struct datetime_t * date, char *buf, uint32_t len) > +{ > + char * src = buf; > + int offset = date->offset; > + int64_t secs = date->secs + offset * 60; > + dt_t dt = dt_from_rdn((secs / SECS_PER_DAY) + 719163); > + > + int year, month, day, sec, ns, sign; > + dt_to_ymd(dt, &year, &month, &day); > + > + int hour = (secs / 3600) % 24, > + minute = (secs / 60) % 60; > + ; > + sec = secs % 60; > + ns = date->nsec; > + uint32_t sz; > + sz = snprintf(buf, len, "%04d-%02d-%02dT%02d:%02d", > + year, month, day, hour, minute); > + buf += sz; len -= sz; > + if (sec || ns) { > + sz = snprintf(buf, len, ":%02d", sec); > + buf += sz; len -= sz; > + if (ns) { > + if ((ns % 1000000) == 0) > + sz = snprintf(buf, len, ".%03d", ns / 1000000); > + else if ((ns % 1000) == 0) > + sz = snprintf(buf, len, ".%06d", ns / 1000); > + else > + sz = snprintf(buf, len, ".%09d", ns); > + buf += sz; len -= sz; > + } > + } > + if (offset == 0) { > + strncpy(buf, "Z", len); > + buf++; > + len--; > + } > + else { > + if (offset < 0) > + sign = '-', offset = -offset; > + else > + sign = '+'; > + > + sz = snprintf(buf, len, "%c%02d:%02d", sign, offset / 60, offset % 60); > + buf += sz; len -= sz; > + } > + return (buf - src); > +} > diff --git a/src/lib/core/datetime.h b/src/lib/core/datetime.h > index 403bf1c64..fdaff2d27 100644 > --- a/src/lib/core/datetime.h > +++ b/src/lib/core/datetime.h > @@ -38,6 +38,11 @@ > extern "C" { > #endif /* defined(__cplusplus) */ > > +#ifndef SECS_PER_DAY > +#define SECS_PER_DAY 86400 > +#define NANOS_PER_SEC 1000000000 > +#endif > + > /** > * datetime structure consisting of: > */ > @@ -55,6 +60,15 @@ struct datetime_interval_t { > int32_t nsec; ///< nanoseconds delta > }; > > +/** > + * Convert datetime to string using default format > + * @param date source datetime value > + * @param buf output character buffer > + * @param len size ofoutput buffer > + */ > +int > +datetime_to_string(const struct datetime_t * date, char *buf, uint32_t len); > + > #if defined(__cplusplus) > } /* extern "C" */ > #endif /* defined(__cplusplus) */ > diff --git a/src/lua/datetime.lua b/src/lua/datetime.lua > index f4d2d7737..9ec06d8d8 100644 > --- a/src/lua/datetime.lua > +++ b/src/lua/datetime.lua > @@ -53,6 +53,12 @@ ffi.cdef [[ > dt_t dt_from_struct_tm (const struct tm *tm); > void dt_to_struct_tm (dt_t dt, struct tm *tm); > > + // mp_datetime.c > + > + int > + datetime_to_string(const struct datetime_t * date, char *buf, uint32_t len); > + > + > // > typedef long __kernel_long_t; > typedef unsigned long __kernel_ulong_t; > @@ -549,8 +555,13 @@ local function strftime(fmt, o) > return ffi.string(buff) > end > > --- strftime may be redirected to datetime:fmt("format") > -local function datetime_fmt() > +local function datetime_tostring(o) > + assert(ffi.typeof(o) == datetime_t) > + local sz = 48 > + local buff = ffi.new('char[?]', sz) > + local len = native.datetime_to_string(o, buff, sz) > + assert(len < sz) > + return ffi.string(buff) > end > > > @@ -566,7 +577,8 @@ return setmetatable( > parse_date = parse_date, > parse_time = parse_time, > parse_zone = parse_zone, > - fmt = datetime_fmt, > + > + tostring = datetime_tostring, > > now = local_now, > -- strptime = strptime;