diff --git a/src/query/interpret/awesome_memgraph_functions.cpp b/src/query/interpret/awesome_memgraph_functions.cpp index cd68e83e2..353015168 100644 --- a/src/query/interpret/awesome_memgraph_functions.cpp +++ b/src/query/interpret/awesome_memgraph_functions.cpp @@ -1,4 +1,4 @@ -// Copyright 2021 Memgraph Ltd. +// Copyright 2022 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -1080,7 +1080,7 @@ void MapNumericParameters(auto ¶meter_mappings, const auto &input_parameters TypedValue Date(const TypedValue *args, int64_t nargs, const FunctionContext &ctx) { FType>>("date", args, nargs); if (nargs == 0) { - return TypedValue(utils::UtcToday(), ctx.memory); + return TypedValue(utils::LocalDateTime(ctx.timestamp).date, ctx.memory); } if (args[0].IsString()) { @@ -1103,7 +1103,7 @@ TypedValue LocalTime(const TypedValue *args, int64_t nargs, const FunctionContex FType>>("localtime", args, nargs); if (nargs == 0) { - return TypedValue(utils::UtcLocalTime(), ctx.memory); + return TypedValue(utils::LocalDateTime(ctx.timestamp).local_time, ctx.memory); } if (args[0].IsString()) { @@ -1130,7 +1130,7 @@ TypedValue LocalDateTime(const TypedValue *args, int64_t nargs, const FunctionCo FType>>("localdatetime", args, nargs); if (nargs == 0) { - return TypedValue(utils::UtcLocalDateTime(), ctx.memory); + return TypedValue(utils::LocalDateTime(ctx.timestamp), ctx.memory); } if (args[0].IsString()) { diff --git a/src/query/interpret/awesome_memgraph_functions.hpp b/src/query/interpret/awesome_memgraph_functions.hpp index 3a0728b08..b07a541de 100644 --- a/src/query/interpret/awesome_memgraph_functions.hpp +++ b/src/query/interpret/awesome_memgraph_functions.hpp @@ -1,4 +1,4 @@ -// Copyright 2021 Memgraph Ltd. +// Copyright 2022 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source diff --git a/src/query/procedure/mg_procedure_impl.cpp b/src/query/procedure/mg_procedure_impl.cpp index a380eebdf..0ddefa6b2 100644 --- a/src/query/procedure/mg_procedure_impl.cpp +++ b/src/query/procedure/mg_procedure_impl.cpp @@ -1,4 +1,4 @@ -// Copyright 2021 Memgraph Ltd. +// Copyright 2022 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -1178,7 +1178,7 @@ mgp_error mgp_date_timestamp(mgp_date *date, int64_t *timestamp) { } mgp_error mgp_date_now(mgp_memory *memory, mgp_date **date) { - return WrapExceptions([memory] { return NewRawMgpObject(memory, utils::UtcToday()); }, date); + return WrapExceptions([memory] { return NewRawMgpObject(memory, utils::CurrentDate()); }, date); } mgp_error mgp_date_add_duration(mgp_date *date, mgp_duration *dur, mgp_memory *memory, mgp_date **result) { @@ -1241,7 +1241,7 @@ mgp_error mgp_local_time_timestamp(mgp_local_time *local_time, int64_t *timestam } mgp_error mgp_local_time_now(mgp_memory *memory, mgp_local_time **local_time) { - return WrapExceptions([memory] { return NewRawMgpObject(memory, utils::UtcLocalTime()); }, + return WrapExceptions([memory] { return NewRawMgpObject(memory, utils::CurrentLocalTime()); }, local_time); } @@ -1334,8 +1334,9 @@ mgp_error mgp_local_date_time_timestamp(mgp_local_date_time *local_date_time, in } mgp_error mgp_local_date_time_now(mgp_memory *memory, mgp_local_date_time **local_date_time) { - return WrapExceptions([memory] { return NewRawMgpObject(memory, utils::UtcLocalDateTime()); }, - local_date_time); + return WrapExceptions( + [memory] { return NewRawMgpObject(memory, utils::CurrentLocalDateTime()); }, + local_date_time); } mgp_error mgp_local_date_time_add_duration(mgp_local_date_time *local_date_time, mgp_duration *dur, mgp_memory *memory, diff --git a/src/query/procedure/mg_procedure_impl.hpp b/src/query/procedure/mg_procedure_impl.hpp index fc7168259..df8653d2b 100644 --- a/src/query/procedure/mg_procedure_impl.hpp +++ b/src/query/procedure/mg_procedure_impl.hpp @@ -1,4 +1,4 @@ -// Copyright 2021 Memgraph Ltd. +// Copyright 2022 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source diff --git a/src/utils/temporal.cpp b/src/utils/temporal.cpp index 45529c0c8..ddd64c697 100644 --- a/src/utils/temporal.cpp +++ b/src/utils/temporal.cpp @@ -1,4 +1,4 @@ -// Copyright 2021 Memgraph Ltd. +// Copyright 2022 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -79,36 +79,14 @@ Date::Date(const DateParameters &date_parameters) { day = date_parameters.day; } -namespace { -tm GetUtcFromSystemClockOrThrow() { +Date CurrentDate() { return CurrentLocalDateTime().date; } + +LocalTime CurrentLocalTime() { return CurrentLocalDateTime().local_time; } + +LocalDateTime CurrentLocalDateTime() { namespace chrono = std::chrono; - const auto today = chrono::system_clock::to_time_t(chrono::system_clock::now()); - tm utc_today; - if (!gmtime_r(&today, &utc_today)) { - throw temporal::InvalidArgumentException("Can't access clock's UTC time"); - } - return utc_today; -} - -int64_t TMYearToUtcYear(int year) { return year + 1900; } - -int64_t TMMonthToUtcMonth(int month) { return month + 1; } -} // namespace - -Date UtcToday() { - const auto utc_today = GetUtcFromSystemClockOrThrow(); - return Date({TMYearToUtcYear(utc_today.tm_year), TMMonthToUtcMonth(utc_today.tm_mon), utc_today.tm_mday}); -} - -LocalTime UtcLocalTime() { - const auto utc_today = GetUtcFromSystemClockOrThrow(); - return LocalTime({utc_today.tm_hour, utc_today.tm_min, utc_today.tm_sec}); -} - -LocalDateTime UtcLocalDateTime() { - const auto utc_today = GetUtcFromSystemClockOrThrow(); - return LocalDateTime({TMYearToUtcYear(utc_today.tm_year), TMMonthToUtcMonth(utc_today.tm_mon), utc_today.tm_mday}, - {utc_today.tm_hour, utc_today.tm_min, utc_today.tm_sec}); + auto ts = chrono::time_point_cast(chrono::system_clock::now()); + return LocalDateTime(ts.time_since_epoch().count()); } namespace { diff --git a/src/utils/temporal.hpp b/src/utils/temporal.hpp index c93f473a7..d2f3b6eac 100644 --- a/src/utils/temporal.hpp +++ b/src/utils/temporal.hpp @@ -1,4 +1,4 @@ -// Copyright 2021 Memgraph Ltd. +// Copyright 2022 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -313,7 +313,7 @@ struct LocalDateTimeHash { size_t operator()(const LocalDateTime &local_date_time) const; }; -Date UtcToday(); -LocalTime UtcLocalTime(); -LocalDateTime UtcLocalDateTime(); +Date CurrentDate(); +LocalTime CurrentLocalTime(); +LocalDateTime CurrentLocalDateTime(); } // namespace utils diff --git a/tests/unit/query_expression_evaluator.cpp b/tests/unit/query_expression_evaluator.cpp index bdff1561f..db69e868a 100644 --- a/tests/unit/query_expression_evaluator.cpp +++ b/tests/unit/query_expression_evaluator.cpp @@ -1,4 +1,4 @@ -// Copyright 2021 Memgraph Ltd. +// Copyright 2022 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -48,7 +48,7 @@ class ExpressionEvaluatorTest : public ::testing::Test { AstStorage storage; utils::MonotonicBufferResource mem{1024}; - EvaluationContext ctx{&mem}; + EvaluationContext ctx{.memory = &mem, .timestamp = query::QueryTimestamp()}; SymbolTable symbol_table; Frame frame{128}; @@ -1916,7 +1916,7 @@ TEST_F(FunctionTest, Date) { const auto map_param = TypedValue( std::map{{"year", TypedValue(1970)}, {"month", TypedValue(1)}, {"day", TypedValue(1)}}); EXPECT_EQ(EvaluateFunction("DATE", map_param).ValueDate(), unix_epoch); - const auto today = utils::UtcToday(); + const auto today = utils::CurrentDate(); EXPECT_EQ(EvaluateFunction("DATE").ValueDate(), today); EXPECT_THROW(EvaluateFunction("DATE", "{}"), utils::BasicException); @@ -1938,7 +1938,7 @@ TEST_F(FunctionTest, LocalTime) { {"millisecond", TypedValue(4)}, {"microsecond", TypedValue(5)}}); EXPECT_EQ(EvaluateFunction("LOCALTIME", map_param).ValueLocalTime(), utils::LocalTime({1, 2, 3, 4, 5})); - const auto today = utils::UtcLocalTime(); + const auto today = utils::CurrentLocalTime(); EXPECT_NEAR(EvaluateFunction("LOCALTIME").ValueLocalTime().MicrosecondsSinceEpoch(), today.MicrosecondsSinceEpoch(), one_sec_in_microseconds); @@ -1956,7 +1956,7 @@ TEST_F(FunctionTest, LocalTime) { TEST_F(FunctionTest, LocalDateTime) { const auto local_date_time = utils::LocalDateTime({1970, 1, 1}, {13, 3, 2, 0, 0}); EXPECT_EQ(EvaluateFunction("LOCALDATETIME", "1970-01-01T13:03:02").ValueLocalDateTime(), local_date_time); - const auto today = utils::UtcLocalDateTime(); + const auto today = utils::CurrentLocalDateTime(); const auto one_sec_in_microseconds = 1000000; const auto map_param = TypedValue(std::map{{"year", TypedValue(1972)}, {"month", TypedValue(2)},