Skip to content

Commit f0f123f

Browse files
committed
lua, datetime: display datetime
* introduced output routine for converting datetime to their default output format. * use this routine for tostring() in datetime.lua Part of tarantool#5941
1 parent fe56435 commit f0f123f

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed

src/exports.h

+1
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,4 @@ EXPORT(dt_parse_iso_zone_extended)
552552
EXPORT(dt_parse_iso_zone_lenient)
553553
EXPORT(dt_from_struct_tm)
554554
EXPORT(dt_to_struct_tm)
555+
EXPORT(datetime_to_string)

src/lib/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(core_sources
3030
decimal.c
3131
mp_decimal.c
3232
cord_buf.c
33+
datetime.c
3334
)
3435

3536
if (TARGET_OS_NETBSD)

src/lib/core/datetime.c

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2021, Tarantool AUTHORS, please see AUTHORS file.
3+
*
4+
* Redistribution and use in source and binary forms, with or
5+
* without modification, are permitted provided that the following
6+
* conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above
9+
* copyright notice, this list of conditions and the
10+
* following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following
14+
* disclaimer in the documentation and/or other materials
15+
* provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21+
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28+
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29+
* SUCH DAMAGE.
30+
*/
31+
32+
#include <string.h>
33+
34+
#include "trivia/util.h"
35+
#include "datetime.h"
36+
37+
int
38+
datetime_to_string(const struct datetime_t * date, char *buf, uint32_t len)
39+
{
40+
char * src = buf;
41+
int offset = date->offset;
42+
int64_t secs = date->secs + offset * 60;
43+
dt_t dt = dt_from_rdn((secs / SECS_PER_DAY) + 719163);
44+
45+
int year, month, day, sec, ns, sign;
46+
dt_to_ymd(dt, &year, &month, &day);
47+
48+
int hour = (secs / 3600) % 24,
49+
minute = (secs / 60) % 60;
50+
;
51+
sec = secs % 60;
52+
ns = date->nsec;
53+
uint32_t sz;
54+
sz = snprintf(buf, len, "%04d-%02d-%02dT%02d:%02d",
55+
year, month, day, hour, minute);
56+
buf += sz; len -= sz;
57+
if (sec || ns) {
58+
sz = snprintf(buf, len, ":%02d", sec);
59+
buf += sz; len -= sz;
60+
if (ns) {
61+
if ((ns % 1000000) == 0)
62+
sz = snprintf(buf, len, ".%03d", ns / 1000000);
63+
else if ((ns % 1000) == 0)
64+
sz = snprintf(buf, len, ".%06d", ns / 1000);
65+
else
66+
sz = snprintf(buf, len, ".%09d", ns);
67+
buf += sz; len -= sz;
68+
}
69+
}
70+
if (offset == 0) {
71+
strncpy(buf, "Z", len);
72+
buf++;
73+
len--;
74+
}
75+
else {
76+
if (offset < 0)
77+
sign = '-', offset = -offset;
78+
else
79+
sign = '+';
80+
81+
sz = snprintf(buf, len, "%c%02d:%02d", sign, offset / 60, offset % 60);
82+
buf += sz; len -= sz;
83+
}
84+
return (buf - src);
85+
}

src/lib/core/datetime.h

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
extern "C" {
3939
#endif /* defined(__cplusplus) */
4040

41+
#ifndef SECS_PER_DAY
42+
#define SECS_PER_DAY 86400
43+
#define NANOS_PER_SEC 1000000000
44+
#endif
45+
4146
/**
4247
* datetime structure consisting of:
4348
*/
@@ -55,6 +60,15 @@ struct datetime_interval_t {
5560
int32_t nsec; ///< nanoseconds delta
5661
};
5762

63+
/**
64+
* Convert datetime to string using default format
65+
* @param date source datetime value
66+
* @param buf output character buffer
67+
* @param len size ofoutput buffer
68+
*/
69+
int
70+
datetime_to_string(const struct datetime_t * date, char *buf, uint32_t len);
71+
5872
#if defined(__cplusplus)
5973
} /* extern "C" */
6074
#endif /* defined(__cplusplus) */

src/lua/datetime.lua

+15-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ ffi.cdef [[
5353
dt_t dt_from_struct_tm (const struct tm *tm);
5454
void dt_to_struct_tm (dt_t dt, struct tm *tm);
5555

56+
// mp_datetime.c
57+
58+
int
59+
datetime_to_string(const struct datetime_t * date, char *buf, uint32_t len);
60+
61+
5662
// <asm-generic/posix_types.h>
5763
typedef long __kernel_long_t;
5864
typedef unsigned long __kernel_ulong_t;
@@ -549,8 +555,13 @@ local function strftime(fmt, o)
549555
return ffi.string(buff)
550556
end
551557

552-
-- strftime may be redirected to datetime:fmt("format")
553-
local function datetime_fmt()
558+
local function datetime_tostring(o)
559+
assert(ffi.typeof(o) == datetime_t)
560+
local sz = 48
561+
local buff = ffi.new('char[?]', sz)
562+
local len = native.datetime_to_string(o, buff, sz)
563+
assert(len < sz)
564+
return ffi.string(buff)
554565
end
555566

556567

@@ -566,7 +577,8 @@ return setmetatable(
566577
parse_date = parse_date,
567578
parse_time = parse_time,
568579
parse_zone = parse_zone,
569-
fmt = datetime_fmt,
580+
581+
tostring = datetime_tostring,
570582

571583
now = local_now,
572584
-- strptime = strptime;

0 commit comments

Comments
 (0)