integrate new query parser

This commit is contained in:
djcb
2017-10-24 22:57:57 +03:00
parent b75f9f508b
commit 5e9cafea59
17 changed files with 373 additions and 979 deletions

View File

@ -53,23 +53,59 @@ test_cases(const CaseVec& cases, ProcFunc proc)
}
static void
test_date ()
test_date_basic ()
{
g_setenv ("TZ", "Europe/Helsinki", TRUE);
CaseVec cases = {
{ "2015-09-18T09:10:23", true, "001442556623" },
{ "1972-12-14T09:10:23", true, "000093165023" },
{ "1854-11-18T17:10:23", true, "000000000000" },
{ "fnorb", true, "000000000000" },
{ "fnorb", false, "999999999999" },
{ "", false, "999999999999" },
{ "", true, "000000000000" }
{ "2015-09-18T09:10:23", true, "1442556623" },
{ "1972-12-14T09:10:23", true, "0093165023" },
{ "1854-11-18T17:10:23", true, "0000000000" },
{ "2016", true, "1451599200" },
{ "2016", false, "1483221599" },
{ "fnorb", true, "0000000000" },
{ "fnorb", false, "9999999999" },
{ "", false, "9999999999" },
{ "", true, "0000000000" }
};
test_cases (cases, [](auto s, auto f){ return date_to_time_t_string(s,f); });
}
static void
test_date_ymwdhMs (void)
{
struct {
std::string expr;
long diff;
int tolerance;
} tests[] = {
{ "3h", 3 * 60 * 60, 1 },
{ "21d", 21 * 24 * 60 * 60, 1 },
{ "2w", 2 * 7 * 24 * 60 * 60, 1 },
{ "2y", 2 * 365 * 24 * 60 * 60, 24 * 3600 + 1 },
{ "3m", 3 * 30 * 24 * 60 * 60, 3 * 24 * 3600 + 1 }
};
for (auto i = 0; i != G_N_ELEMENTS(tests); ++i) {
const auto diff = time(NULL) -
strtol(Mux::date_to_time_t_string(tests[i].expr, true).c_str(),
NULL, 10);
if (g_test_verbose())
std::cerr << tests[i].expr << ' '
<< diff << ' '
<< tests[i].diff << std::endl;
g_assert_true (tests[i].diff - diff <= tests[i].tolerance);
}
g_assert_true (strtol(Mux::date_to_time_t_string("-1y", true).c_str(),
NULL, 10) == 0);
}
static void
test_size ()
{
@ -88,8 +124,9 @@ main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/utils/process-date", test_date);
g_test_add_func ("/utils/process-size", test_size);
g_test_add_func ("/utils/date-basic", test_date_basic);
g_test_add_func ("/utils/date-ymwdhMs", test_date_ymwdhMs);
g_test_add_func ("/utils/size", test_size);
return g_test_run ();
}

View File

@ -144,11 +144,11 @@ Mux::quote (const std::string& str)
return str;
}
constexpr const auto InternalDateFormat = "%012" G_GINT64_FORMAT;
constexpr const char InternalDateMin[] = "000000000000";
constexpr const char InternalDateMax[] = "999999999999";
static_assert(sizeof(InternalDateMin) == 12 + 1);
static_assert(sizeof(InternalDateMax) == 12 + 1);
constexpr const auto InternalDateFormat = "%010" G_GINT64_FORMAT;
constexpr const char InternalDateMin[] = "0000000000";
constexpr const char InternalDateMax[] = "9999999999";
static_assert(sizeof(InternalDateMin) == 10 + 1);
static_assert(sizeof(InternalDateMax) == 10 + 1);
static std::string
date_boundary (bool is_first)
@ -204,7 +204,6 @@ delta_ymwdhMs (const std::string& expr)
return date_to_time_t_string (t);
}
static std::string
special_date (const std::string& d, bool is_first)
{
@ -235,9 +234,8 @@ special_date (const std::string& d, bool is_first)
return date_boundary (is_first);
}
constexpr const char UserDateMin[] = "19700101000000";
constexpr const char UserDateMax[] = "29993112235959";
constexpr const char UserDateMax[] = "29991231235959";
std::string
Mux::date_to_time_t_string (const std::string& dstr, bool is_first)
@ -249,7 +247,10 @@ Mux::date_to_time_t_string (const std::string& dstr, bool is_first)
/* one-sided dates */
if (dstr.empty())
return date_boundary (is_first);
else if (is_first && dstr.find_first_of("ymdwhMs") != std::string::npos)
else if (dstr == "today" || dstr == "now")
return special_date (dstr, is_first);
else if (dstr.find_first_of("ymdwhMs") != std::string::npos)
return delta_ymwdhMs (dstr);
std::string date (is_first ? UserDateMin : UserDateMax);
@ -261,7 +262,7 @@ Mux::date_to_time_t_string (const std::string& dstr, bool is_first)
!strptime (date.c_str(), "%Y%m%d", &tbuf) &&
!strptime (date.c_str(), "%Y%m", &tbuf) &&
!strptime (date.c_str(), "%Y", &tbuf))
return special_date (date, is_first);
return date_boundary (is_first);
dtime = g_date_time_new_local (tbuf.tm_year + 1900,
tbuf.tm_mon + 1,