diff options
Diffstat (limited to '')
-rw-r--r-- | test/test_date_time_scanner.cc | 76 |
1 files changed, 72 insertions, 4 deletions
diff --git a/test/test_date_time_scanner.cc b/test/test_date_time_scanner.cc index 8a85993..c19d9c8 100644 --- a/test/test_date_time_scanner.cc +++ b/test/test_date_time_scanner.cc @@ -30,11 +30,24 @@ #include <assert.h> #include <locale.h> +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "../src/lnav_util.hh" #include "base/date_time_scanner.hh" #include "config.h" +#include "doctest/doctest.h" +#include "lnav_config.hh" +#include "ptimec.hh" static const char* GOOD_TIMES[] = { + "2023-08-11T00:59:36.208491Z", + "09/Aug/2023:21:41:44 +0000", + "2022-08-27T17:22:01.694554+03:00", + "2022-08-27T17:22:01.694554+0300", + "2022-08-27T17:22:01.694554+00:00", + "2022-08-27T17:22:01.694554+0000", + "2022-08-27T17:22:01.694554Z", + "2022-08-27 17:22:01.694554 UTC", + "2022-08-27 17:22:01.694554 GMT", "2017 May 08 Mon 18:57:57.578", "May 01 00:00:01", "May 10 12:00:01", @@ -53,11 +66,11 @@ static const char* BAD_TIMES[] = { "@4000000043", }; -int -main(int argc, char* argv[]) +TEST_CASE("date_time_scanner") { setenv("TZ", "UTC", 1); + lnav_config.lc_log_date_time.c_zoned_to_local = false; for (const auto* good_time : GOOD_TIMES) { date_time_scanner dts; struct timeval tv; @@ -65,16 +78,71 @@ main(int argc, char* argv[]) const char* rc; rc = dts.scan(good_time, strlen(good_time), nullptr, &tm, tv); + CHECK(dts.dts_zoned_to_local == false); printf("ret %s %p\n", good_time, rc); assert(rc != nullptr); char ts[64]; - gmtime_r(&tv.tv_sec, &tm.et_tm); dts.ftime(ts, sizeof(ts), nullptr, tm); + printf("fmt %s\n", PTIMEC_FORMATS[dts.dts_fmt_lock].pf_fmt); printf("orig %s\n", good_time); printf("loop %s\n", ts); - assert(strcmp(ts, good_time) == 0); + CHECK(std::string(ts) == std::string(good_time)); + } + + { + const auto sf + = string_fragment::from_const("2014-02-11 16:12:34.123.456"); + struct timeval tv; + struct exttm tm; + date_time_scanner dts; + const auto* rc = dts.scan(sf.data(), sf.length(), nullptr, &tm, tv); + CHECK((tm.et_flags & ETF_MILLIS_SET)); + CHECK(std::string(rc) == sf.substr(23).to_string()); + + char ts[64]; + dts.ftime(ts, sizeof(ts), nullptr, tm); + + CHECK(std::string(ts) == std::string("2014-02-11 16:12:34.123")); + } + + { + const auto sf + = string_fragment::from_const("2014-02-11 16:12:34.12345Z"); + struct timeval tv; + struct exttm tm; + date_time_scanner dts; + const auto* rc = dts.scan(sf.data(), sf.length(), nullptr, &tm, tv); + printf("fmt %s\n", PTIMEC_FORMAT_STR[dts.dts_fmt_lock]); + CHECK(rc != nullptr); + CHECK((tm.et_flags & ETF_MICROS_SET)); + CHECK(*rc == '\0'); + + char ts[64]; + dts.ftime(ts, sizeof(ts), nullptr, tm); + + CHECK(std::string(ts) == std::string("2014-02-11 16:12:34.123450Z")); + } + + { + const auto sf + = string_fragment::from_const("Tue Jul 25 12:01:01 AM UTC 2023"); + struct timeval tv; + struct exttm tm; + date_time_scanner dts; + const auto* rc = dts.scan(sf.data(), sf.length(), nullptr, &tm, tv); + printf("fmt %s\n", PTIMEC_FORMAT_STR[dts.dts_fmt_lock]); + CHECK(rc != nullptr); + CHECK((tm.et_flags & ETF_ZONE_SET)); + CHECK((tm.et_flags & ETF_Z_IS_UTC)); + CHECK(*rc == '\0'); + + char ts[64]; + dts.ftime(ts, sizeof(ts), nullptr, tm); + + CHECK(std::string(ts) + == std::string("Tue Jul 25 12:01:01 AM UTC 2023")); } { |