summaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2024-01-05 19:04:27 +0100
committerSébastien Helleu <flashcode@flashtux.org>2024-01-05 19:09:19 +0100
commitf6ba789c3d33bdc1bcc6c00e9660ffbd9f2ba514 (patch)
tree3b1a78e26a482a306df8515d8a4385a095221037 /tests/unit
parente88882f0ae031c2c3400a517b751c53a66b1c79f (diff)
downloadweechat-f6ba789c3d33bdc1bcc6c00e9660ffbd9f2ba514.zip
api: add function util_parse_time (issue #649)
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/core/test-core-util.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/unit/core/test-core-util.cpp b/tests/unit/core/test-core-util.cpp
index a587274a6..a7fc00e9d 100644
--- a/tests/unit/core/test-core-util.cpp
+++ b/tests/unit/core/test-core-util.cpp
@@ -25,14 +25,23 @@
extern "C"
{
+#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
+#include <time.h>
#include <sys/time.h>
#include "src/core/wee-string.h"
#include "src/core/wee-util.h"
}
+#define WEE_PARSE_DATE(__result, __sec, __usec, __datetime) \
+ tv.tv_sec = 0; \
+ tv.tv_usec = 0; \
+ LONGS_EQUAL(__result, util_parse_time (__datetime, &tv)); \
+ LONGS_EQUAL(__sec, tv.tv_sec); \
+ LONGS_EQUAL(__usec, tv.tv_usec);
+
TEST_GROUP(CoreUtil)
{
};
@@ -231,6 +240,109 @@ TEST(CoreUtil, Strftimeval)
/*
* Tests functions:
+ * util_parse_time
+ */
+
+TEST(CoreUtil, ParseTime)
+{
+ struct timeval tv;
+ struct tm *local_time;
+ time_t date;
+ char str_time[128];
+
+ /* test with local timezone: UTC+1 */
+ setenv ("TZ", "UTC+1", 1);
+
+ LONGS_EQUAL(0, util_parse_time (NULL, NULL));
+ LONGS_EQUAL(0, util_parse_time (NULL, &tv));
+
+ WEE_PARSE_DATE(0, 0, 0, "");
+ WEE_PARSE_DATE(0, 0, 0, "invalid");
+
+ /*
+ * expected: 2023-12-25T00:00:00Z == 1703462400
+ * (local timezone UTC+1: 1703466000)
+ */
+ WEE_PARSE_DATE(1, 1703466000, 0, "2023-12-25");
+
+ /* expected: current date with specified local time */
+ date = time (NULL);
+ local_time = localtime (&date);
+ strftime (str_time, sizeof (str_time), "%H:%M:%S", 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.456789", 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);
+
+ /* expected: current date with specified UTC time */
+ date = time (NULL);
+ local_time = localtime (&date);
+ strftime (str_time, sizeof (str_time), "%H:%M:%SZ", 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.456789Z", 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: 2023-12-25T10:29:09.456789Z == 1703500149.456789
+ * (local timezone UTC+1: 1703503749.456789)
+ */
+ WEE_PARSE_DATE(1, 1703500149 + 3600, 0, "2023-12-25T10:29:09");
+ WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25T10:29:09Z");
+ 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
+ * with timezone offset
+ */
+ WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25T10:29:09+00");
+ WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25T10:29:09+0000");
+ WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25T10:29:09+00:00");
+ WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25T10:29:09-00");
+ WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25T10:29:09-0000");
+ WEE_PARSE_DATE(1, 1703500149, 0, "2023-12-25T10:29:09-00:00");
+ WEE_PARSE_DATE(1, 1703500149, 456789, "2023-12-25T10:29:09.456789-00:00");
+
+ WEE_PARSE_DATE(1, 1703500149 + 7200, 0, "2023-12-25T10:29:09+02");
+ WEE_PARSE_DATE(1, 1703500149 + 60, 0, "2023-12-25T10:29:09+0001");
+ WEE_PARSE_DATE(1, 1703500149 + 60, 456789, "2023-12-25T10:29:09.456789+0001");
+
+ WEE_PARSE_DATE(1, 1703500149 + 7200, 0, "2023-12-25T10:29:09+0200");
+ WEE_PARSE_DATE(1, 1703500149 + 7200, 0, "2023-12-25T10:29:09+02:00");
+ WEE_PARSE_DATE(1, 1703500149 + 3600 + 1800, 0, "2023-12-25T10:29:09+0130");
+ 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");
+
+ WEE_PARSE_DATE(1, 1703500149 - 60, 0, "2023-12-25T10:29:09-0001");
+ WEE_PARSE_DATE(1, 1703500149 - 3600 - 1800, 0, "2023-12-25T10:29:09-0130");
+ 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 */
+ WEE_PARSE_DATE(1, 1703500149, 0, "1703500149");
+ WEE_PARSE_DATE(1, 1703500149, 456000, "1703500149.456");
+ WEE_PARSE_DATE(1, 1703500149, 456789, "1703500149.456789");
+ WEE_PARSE_DATE(1, 1703500149, 456000, "1703500149,456");
+ WEE_PARSE_DATE(1, 1703500149, 456789, "1703500149,456789");
+
+ setenv ("TZ", "", 1);
+}
+
+/*
+ * Tests functions:
* util_get_time_diff
*/