From: Timur Safin via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH resend v2 04/11] lua, datetime: display datetime
Date: Wed, 28 Jul 2021 13:34:06 +0300 [thread overview]
Message-ID: <d0dcefe99ae100656d3c9ad7a631bb3e90199e62.1627468002.git.tsafin@tarantool.org> (raw)
In-Reply-To: <cover.1627468002.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 | 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
next prev parent reply other threads:[~2021-07-28 10:35 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-28 10:34 [Tarantool-patches] [PATCH resend v2 00/11] Initial datetime support Timur Safin via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 01/11] build: add Christian Hansen c-dt to the build Timur Safin via Tarantool-patches
2021-07-29 23:40 ` Vladislav Shpilevoy via Tarantool-patches
2021-07-31 9:22 ` Timur Safin via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 02/11] lua: built-in module datetime Timur Safin via Tarantool-patches
2021-07-29 18:55 ` Oleg Babin via Tarantool-patches
2021-07-30 19:00 ` Timur Safin via Tarantool-patches
2021-07-31 6:29 ` Oleg Babin via Tarantool-patches
2021-07-31 16:51 ` Timur Safin via Tarantool-patches
2021-07-29 23:36 ` Vladislav Shpilevoy via Tarantool-patches
2021-07-30 15:39 ` Timur Safin via Tarantool-patches
2021-08-01 17:01 ` Vladislav Shpilevoy via Tarantool-patches
2021-08-01 20:23 ` Timur Safin via Tarantool-patches
2021-08-04 23:57 ` Vladislav Shpilevoy via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 03/11] lua, datetime: datetime tests Timur Safin via Tarantool-patches
2021-07-29 18:55 ` Oleg Babin via Tarantool-patches
2021-07-30 20:45 ` Timur Safin via Tarantool-patches
2021-07-28 10:34 ` Timur Safin via Tarantool-patches [this message]
2021-07-29 18:55 ` [Tarantool-patches] [PATCH resend v2 04/11] lua, datetime: display datetime Oleg Babin via Tarantool-patches
2021-07-30 21:48 ` Timur Safin via Tarantool-patches
2021-07-31 6:29 ` Oleg Babin via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 05/11] box, datetime: add messagepack support for datetime Timur Safin via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 06/11] box, datetime: datetime comparison for indices Timur Safin via Tarantool-patches
2021-07-29 18:56 ` Oleg Babin via Tarantool-patches
2021-07-30 22:18 ` Timur Safin via Tarantool-patches
2021-07-31 6:30 ` Oleg Babin via Tarantool-patches
2021-07-31 9:31 ` Timur Safin via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 07/11] lua, datetime: proper datetime encoding Timur Safin via Tarantool-patches
2021-07-29 18:57 ` Oleg Babin via Tarantool-patches
2021-07-30 22:20 ` Timur Safin via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 08/11] lua, datetime: calculated attributes for datetimes Timur Safin via Tarantool-patches
2021-07-29 18:57 ` Oleg Babin via Tarantool-patches
2021-07-30 22:30 ` Timur Safin via Tarantool-patches
2021-07-31 6:31 ` Oleg Babin via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 09/11] lua, datetime: time intervals support Timur Safin via Tarantool-patches
2021-07-29 18:58 ` Oleg Babin via Tarantool-patches
2021-07-30 22:58 ` Timur Safin via Tarantool-patches
2021-07-31 6:31 ` Oleg Babin via Tarantool-patches
2021-07-31 9:20 ` Timur Safin via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 10/11] lua, datetime: unixtime, timestamp setters in datetime.lua Timur Safin via Tarantool-patches
2021-07-29 18:58 ` Oleg Babin via Tarantool-patches
2021-07-30 23:11 ` Timur Safin via Tarantool-patches
2021-07-31 6:31 ` Oleg Babin via Tarantool-patches
2021-07-31 9:54 ` Timur Safin via Tarantool-patches
2021-07-28 10:34 ` [Tarantool-patches] [PATCH resend v2 11/11] datetime: changelog for datetime module Timur Safin via Tarantool-patches
2021-07-29 18:55 ` [Tarantool-patches] [PATCH resend v2 00/11] Initial datetime support Oleg Babin 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=d0dcefe99ae100656d3c9ad7a631bb3e90199e62.1627468002.git.tsafin@tarantool.org \
--to=tarantool-patches@dev.tarantool.org \
--cc=tsafin@tarantool.org \
--cc=v.shpilevoy@tarantool.org \
--subject='Re: [Tarantool-patches] [PATCH resend v2 04/11] 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