Tarantool development patches archive
 help / color / mirror / Atom feed
From: Timur Safin via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org, olegrok@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v3 4/9] lua, datetime: display datetime
Date: Mon,  2 Aug 2021 03:41:00 +0300	[thread overview]
Message-ID: <af326866aa7bb5aaffa83bd6dafa7d0b7ff04a7b.1627864075.git.tsafin@tarantool.org> (raw)
In-Reply-To: <cover.1627864075.git.tsafin@tarantool.org>

* 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     |  4 ++
 src/lua/datetime.lua        | 40 +++++++++++++++++
 5 files changed, 131 insertions(+)
 create mode 100755 src/lib/core/datetime.c

diff --git a/src/exports.h b/src/exports.h
index 2ec314369..ce8f74065 100644
--- a/src/exports.h
+++ b/src/exports.h
@@ -217,6 +217,7 @@ EXPORT(curl_url_set)
 EXPORT(curl_version)
 EXPORT(curl_version_info)
 #endif /* EXPORT_LIBCURL_SYMBOLS */
+EXPORT(datetime_to_string)
 EXPORT(decimal_unpack)
 EXPORT(dt_dow)
 EXPORT(dt_from_rdn)
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 <COPYRIGHT HOLDER> ``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
+ * <COPYRIGHT HOLDER> 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 <string.h>
+
+#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 84d6642ab..2c9530ad7 100644
--- a/src/lib/core/datetime.h
+++ b/src/lib/core/datetime.h
@@ -40,6 +40,10 @@ extern "C"
 {
 #endif /* defined(__cplusplus) */
 
+#ifndef SECS_PER_DAY
+#define SECS_PER_DAY	86400
+#endif
+
 /**
  * Full datetime structure representing moments
  * since Unix Epoch (1970-01-01).
diff --git a/src/lua/datetime.lua b/src/lua/datetime.lua
index 870edc541..e5b89768f 100644
--- a/src/lua/datetime.lua
+++ b/src/lua/datetime.lua
@@ -66,6 +66,12 @@ ffi.cdef [[
     dt_t    dt_from_struct_tm  (const struct tm *tm);
     void    dt_to_struct_tm    (dt_t dt, struct tm *tm);
 
+    // datetime.c
+
+    int
+    datetime_to_string(const struct datetime_t * date, char *buf, uint32_t len);
+
+
     // <asm-generic/posix_types.h>
     typedef long            __kernel_long_t;
     typedef unsigned long   __kernel_ulong_t;
@@ -467,6 +473,36 @@ local function datetime_new(o)
     return mk_timestamp(dt, secs, frac, offset)
 end
 
+local function datetime_tostring(o)
+    if ffi.typeof(o) == datetime_t then
+        local sz = 48
+        local buff = ffi.new('char[?]', sz)
+        local len = builtin.datetime_to_string(o, buff, sz)
+        assert(len < sz)
+        return ffi.string(buff)
+    elseif ffi.typeof(o) == interval_t then
+        local ts = o.timestamp
+        local sign = '+'
+
+        if ts < 0 then
+            ts = -ts
+            sign = '-'
+        end
+
+        if ts < 60 then
+            return ('%s%s secs'):format(sign, ts)
+        elseif ts < 60 * 60 then
+            return ('%+d minutes, %s seconds'):format(o.minutes, ts % 60)
+        elseif ts < 24 * 60 * 60 then
+            return ('%+d hours, %d minutes, %s seconds'):format(
+                    o.hours, o.minutes % 60, ts % 60)
+        else
+            return ('%+d days, %d hours, %d minutes, %s seconds'):format(
+                    o.days, o.hours % 24, o.minutes % 60, ts % 60)
+        end
+    end
+end
+
 local function date_first(lhs, rhs)
     if is_datetime(lhs) then
         return lhs, rhs
@@ -710,6 +746,7 @@ local function strftime(fmt, o)
 end
 
 local datetime_mt = {
+    __tostring = datetime_tostring,
     __serialize = datetime_serialize,
     __eq = datetime_eq,
     __lt = datetime_lt,
@@ -721,6 +758,7 @@ local datetime_mt = {
 }
 
 local interval_mt = {
+    __tostring = datetime_tostring,
     __serialize = interval_serialize,
     __eq = datetime_eq,
     __lt = datetime_lt,
@@ -743,6 +781,8 @@ return setmetatable(
         parse_time  = parse_time,
         parse_zone  = parse_zone,
 
+        tostring    = datetime_tostring,
+
         now         = local_now,
         strftime    = strftime,
         asctime     = asctime,
-- 
2.29.2


  parent reply	other threads:[~2021-08-02  0:43 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-02  0:40 [Tarantool-patches] [PATCH v3 0/9] Initial datetime support Timur Safin via Tarantool-patches
2021-08-02  0:40 ` [Tarantool-patches] [PATCH v3 1/9] build: add Christian Hansen c-dt to the build Timur Safin via Tarantool-patches
2021-08-04 23:58   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05  8:55     ` Safin Timur via Tarantool-patches
2021-08-08 14:34       ` Vladislav Shpilevoy via Tarantool-patches
2021-08-02  0:40 ` [Tarantool-patches] [PATCH v3 2/9] lua: built-in module datetime Timur Safin via Tarantool-patches
2021-08-04 23:58   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-06  0:23     ` Safin Timur via Tarantool-patches
2021-08-06  1:30       ` Oleg Babin via Tarantool-patches
2021-08-06 13:00         ` Safin Timur via Tarantool-patches
2021-08-06 17:24           ` Safin Timur via Tarantool-patches
2021-08-08 11:26       ` Vladislav Shpilevoy via Tarantool-patches
2021-08-08 16:35         ` Safin Timur via Tarantool-patches
2021-08-10 12:20           ` Vladislav Shpilevoy via Tarantool-patches
2021-08-10 12:21             ` Igor Munkin via Tarantool-patches
2021-08-12 20:47               ` Safin Timur via Tarantool-patches
2021-08-15 20:52                 ` Safin Timur via Tarantool-patches
2021-08-06  0:26   ` Safin Timur via Tarantool-patches
2021-08-08 14:34   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-08 16:47     ` Safin Timur via Tarantool-patches
2021-08-02  0:40 ` [Tarantool-patches] [PATCH v3 3/9] lua, datetime: datetime tests Timur Safin via Tarantool-patches
2021-08-06  0:25   ` Safin Timur via Tarantool-patches
2021-08-02  0:41 ` Timur Safin via Tarantool-patches [this message]
2021-08-02  0:41 ` [Tarantool-patches] [PATCH v3 5/9] box, datetime: messagepack support for datetime Timur Safin via Tarantool-patches
2021-08-03 13:38   ` Timur Safin via Tarantool-patches
2021-08-02  0:41 ` [Tarantool-patches] [PATCH v3 6/9] box, datetime: datetime comparison for indices Timur Safin via Tarantool-patches
2021-08-03 12:02   ` Serge Petrenko via Tarantool-patches
2021-08-03 12:59     ` Timur Safin via Tarantool-patches
2021-08-04 10:12     ` Timur Safin via Tarantool-patches
2021-08-02  0:41 ` [Tarantool-patches] [PATCH v3 7/9] lua, datetime: time intervals support Timur Safin via Tarantool-patches
2021-08-02  0:41 ` [Tarantool-patches] [PATCH v3 8/9] datetime: changelog for datetime module Timur Safin via Tarantool-patches
2021-08-02  0:41 ` [Tarantool-patches] [PATCH v3 9/9] lua, box, datetime: rename struct datetime_t Timur Safin via Tarantool-patches
2021-08-06  0:27   ` Safin Timur via Tarantool-patches
2021-08-03 21:23 ` [Tarantool-patches] [PATCH v3 1/2] datetime: update tests for macosx Timur Safin via Tarantool-patches
2021-08-06  0:28   ` Safin Timur via Tarantool-patches
2021-08-03 21:23 ` [Tarantool-patches] [PATCH v3 2/2] lua, datetime: introduce ctime, strftime wrappers Timur Safin via Tarantool-patches
2021-08-06  0:30   ` Safin Timur via Tarantool-patches
2021-08-03 21:26 ` [Tarantool-patches] [PATCH v3 0/9] Initial datetime support Timur Safin 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=af326866aa7bb5aaffa83bd6dafa7d0b7ff04a7b.1627864075.git.tsafin@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=olegrok@tarantool.org \
    --cc=tsafin@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v3 4/9] lua, datetime: display datetime' \
    /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