1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-05 09:13:14 +02:00

api: add support of date like ISO 8601 but with spaces in function util_parse_time

So for example the format "2024-01-04 22:01:02.123456 +0100" is supported in
addition to ""2024-01-04T22:01:02.123456+0100".
This commit is contained in:
Sébastien Helleu
2025-08-30 11:51:20 +02:00
parent f630c36af0
commit 7980a6d100
8 changed files with 275 additions and 49 deletions
+115
View File
@@ -300,6 +300,9 @@ TEST(CoreUtil, ParseTime)
WEE_PARSE_DATE(0, 0, 0, "");
WEE_PARSE_DATE(0, 0, 0, "invalid");
/* invalid: negative microseconds */
WEE_PARSE_DATE(0, 0, 0, "1703500149.-456789");
/*
* expected: 2023-12-25T00:00:00Z == 1703462400
* (local timezone UTC+1: 1703466000)
@@ -321,6 +324,49 @@ TEST(CoreUtil, ParseTime)
CHECK((tv.tv_sec >= date) && (tv.tv_sec <= date + 10));
LONGS_EQUAL(456789, tv.tv_usec);
/* expected: current date with specified local time + timezone offset */
date = time (NULL);
local_time = localtime (&date);
strftime (str_time, sizeof (str_time), "%H:%M:%S+0100", local_time);
LONGS_EQUAL(1, util_parse_time (str_time, &tv));
CHECK((tv.tv_sec >= date) && (tv.tv_sec <= date + 10));
LONGS_EQUAL(0, tv.tv_usec);
date = time (NULL);
local_time = localtime (&date);
strftime (str_time, sizeof (str_time), "%H:%M:%S +01:00", local_time);
LONGS_EQUAL(1, util_parse_time (str_time, &tv));
CHECK((tv.tv_sec >= date) && (tv.tv_sec <= date + 10));
LONGS_EQUAL(0, tv.tv_usec);
date = time (NULL);
local_time = localtime (&date);
strftime (str_time, sizeof (str_time), "%H:%M:%S +02:00", local_time);
LONGS_EQUAL(1, util_parse_time (str_time, &tv));
CHECK((tv.tv_sec >= date + 3600) && (tv.tv_sec <= date + 3600 + 10));
LONGS_EQUAL(0, tv.tv_usec);
date = time (NULL);
local_time = localtime (&date);
strftime (str_time, sizeof (str_time), "%H:%M:%S.456789+0100", local_time);
LONGS_EQUAL(1, util_parse_time (str_time, &tv));
CHECK((tv.tv_sec >= date) && (tv.tv_sec <= date + 10));
LONGS_EQUAL(456789, tv.tv_usec);
date = time (NULL);
local_time = localtime (&date);
strftime (str_time, sizeof (str_time), "%H:%M:%S.456789 +01:00", local_time);
LONGS_EQUAL(1, util_parse_time (str_time, &tv));
CHECK((tv.tv_sec >= date) && (tv.tv_sec <= date + 10));
LONGS_EQUAL(456789, tv.tv_usec);
date = time (NULL);
local_time = localtime (&date);
strftime (str_time, sizeof (str_time), "%H:%M:%S.456789 +02:00", local_time);
LONGS_EQUAL(1, util_parse_time (str_time, &tv));
CHECK((tv.tv_sec >= date + 3600) && (tv.tv_sec <= date + 3600 + 10));
LONGS_EQUAL(456789, tv.tv_usec);
/* expected: current date with specified UTC time */
date = time (NULL);
local_time = localtime (&date);
@@ -345,6 +391,16 @@ TEST(CoreUtil, ParseTime)
WEE_PARSE_DATE(1, 1703500149, 456000, "2023-12-25T10:29:09.456Z");
WEE_PARSE_DATE(1, 1703500149, 456789, "2023-12-25T10:29:09.456789Z");
/*
* expected: 2023-12-25T10:29:09.456789Z == 1703500149.456789
* (local timezone UTC+1: 1703503749.456789)
* with space instead of "T"
*/
WEE_PARSE_DATE(1, 1703500149 + 3600, 0, "2023-12-25 10:29:09");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09Z");
WEE_PARSE_DATE(1, 1703500149, 456000, "2023-12-25 10:29:09.456Z");
WEE_PARSE_DATE(1, 1703500149, 456789, "2023-12-25 10:29:09.456789Z");
/*
* expected: 2023-12-25T10:29:09.456789Z == 1703500149.456789
* with timezone offset
@@ -372,6 +428,60 @@ TEST(CoreUtil, ParseTime)
WEE_PARSE_DATE(1, 1703500149 - 3600 - 1800, 0, "2023-12-25T10:29:09-01:30");
WEE_PARSE_DATE(1, 1703500149 - 3600 - 1800, 456789, "2023-12-25T10:29:09.456789-01:30");
/*
* expected: 2023-12-25T10:29:09.456789Z == 1703500149.456789
* with space instead of "T" and timezone offset
*/
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09+00");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09+0000");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09+00:00");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09-00");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09-0000");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09-00:00");
WEE_PARSE_DATE(1, 1703500149, 456789, "2023-12-25 10:29:09.456789-00:00");
WEE_PARSE_DATE(1, 1703500149 + 7200, 0, "2023-12-25 10:29:09+02");
WEE_PARSE_DATE(1, 1703500149 + 60, 0, "2023-12-25 10:29:09+0001");
WEE_PARSE_DATE(1, 1703500149 + 60, 456789, "2023-12-25 10:29:09.456789+0001");
WEE_PARSE_DATE(1, 1703500149 + 7200, 0, "2023-12-25 10:29:09+0200");
WEE_PARSE_DATE(1, 1703500149 + 7200, 0, "2023-12-25 10:29:09+02:00");
WEE_PARSE_DATE(1, 1703500149 + 3600 + 1800, 0, "2023-12-25 10:29:09+0130");
WEE_PARSE_DATE(1, 1703500149 + 3600 + 1800, 0, "2023-12-25 10:29:09+01:30");
WEE_PARSE_DATE(1, 1703500149 + 3600 + 1800, 456789, "2023-12-25 10:29:09.456789+01:30");
WEE_PARSE_DATE(1, 1703500149 - 60, 0, "2023-12-25 10:29:09-0001");
WEE_PARSE_DATE(1, 1703500149 - 3600 - 1800, 0, "2023-12-25 10:29:09-0130");
WEE_PARSE_DATE(1, 1703500149 - 3600 - 1800, 0, "2023-12-25 10:29:09-01:30");
WEE_PARSE_DATE(1, 1703500149 - 3600 - 1800, 456789, "2023-12-25 10:29:09.456789-01:30");
/*
* expected: 2023-12-25T10:29:09.456789Z == 1703500149.456789
* with space instead of "T" and timezone offset after a space
*/
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09 +00");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09 +0000");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09 +00:00");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09 -00");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09 -0000");
WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25 10:29:09 -00:00");
WEE_PARSE_DATE(1, 1703500149, 456789, "2023-12-25 10:29:09.456789 -00:00");
WEE_PARSE_DATE(1, 1703500149 + 7200, 0, "2023-12-25 10:29:09 +02");
WEE_PARSE_DATE(1, 1703500149 + 60, 0, "2023-12-25 10:29:09 +0001");
WEE_PARSE_DATE(1, 1703500149 + 60, 456789, "2023-12-25 10:29:09.456789 +0001");
WEE_PARSE_DATE(1, 1703500149 + 7200, 0, "2023-12-25 10:29:09 +0200");
WEE_PARSE_DATE(1, 1703500149 + 7200, 0, "2023-12-25 10:29:09 +02:00");
WEE_PARSE_DATE(1, 1703500149 + 3600 + 1800, 0, "2023-12-25 10:29:09 +0130");
WEE_PARSE_DATE(1, 1703500149 + 3600 + 1800, 0, "2023-12-25 10:29:09 +01:30");
WEE_PARSE_DATE(1, 1703500149 + 3600 + 1800, 456789, "2023-12-25 10:29:09.456789 +01:30");
WEE_PARSE_DATE(1, 1703500149 - 60, 0, "2023-12-25 10:29:09 -0001");
WEE_PARSE_DATE(1, 1703500149 - 3600 - 1800, 0, "2023-12-25 10:29:09 -0130");
WEE_PARSE_DATE(1, 1703500149 - 3600 - 1800, 0, "2023-12-25 10:29:09 -01:30");
WEE_PARSE_DATE(1, 1703500149 - 3600 - 1800, 456789, "2023-12-25 10:29:09.456789 -01:30");
/* expected: 2023-12-25T10:29:09.456789Z == 1703500149.456789 */
WEE_PARSE_DATE(1, 1703500149, 0, "1703500149");
WEE_PARSE_DATE(1, 1703500149, 456000, "1703500149.456");
@@ -379,6 +489,11 @@ TEST(CoreUtil, ParseTime)
WEE_PARSE_DATE(1, 1703500149, 456000, "1703500149,456");
WEE_PARSE_DATE(1, 1703500149, 456789, "1703500149,456789");
/* expected: 2023-12-25T10:29:09.456789Z == 1703500149.456789 */
/* with extra digits after microseconds */
WEE_PARSE_DATE(1, 1703500149, 456789, "1703500149.4567891");
WEE_PARSE_DATE(1, 1703500149, 456789, "1703500149.456789123456789123456789123456789");
setenv ("TZ", "", 1);
}