[Tarantool-patches] [PATCH resend v2 04/11] lua, datetime: display datetime

Timur Safin tsafin at tarantool.org
Wed Jul 28 13:34:06 MSK 2021


* 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 <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 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);
+
+
     // <asm-generic/posix_types.h>
     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;
-- 
2.29.2



More information about the Tarantool-patches mailing list