1 | #include "catch.hpp" |
2 | #include "duckdb/common/types/timestamp.hpp" |
3 | #include "duckdb/common/types/date.hpp" |
4 | #include "duckdb/common/types/time.hpp" |
5 | |
6 | using namespace duckdb; |
7 | using namespace std; |
8 | |
9 | static void VerifyTimestamp(date_t date, dtime_t time, int64_t epoch) { |
10 | // create the timestamp from the string |
11 | timestamp_t stamp = Timestamp::FromString(Date::ToString(date) + " " + Time::ToString(time)); |
12 | // verify that we can get the date and time back |
13 | REQUIRE(Timestamp::GetDate(stamp) == date); |
14 | REQUIRE(Timestamp::GetTime(stamp) == time); |
15 | // verify that the individual extract functions work |
16 | int32_t hour, min, sec, msec; |
17 | Time::Convert(time, hour, min, sec, msec); |
18 | REQUIRE(Timestamp::GetHours(stamp) == hour); |
19 | REQUIRE(Timestamp::GetMinutes(stamp) == min); |
20 | REQUIRE(Timestamp::GetSeconds(stamp) == sec); |
21 | |
22 | // verify that the epoch is correct |
23 | REQUIRE(epoch == (Date::Epoch(date) + time / 1000)); |
24 | REQUIRE(Timestamp::GetEpoch(stamp) == epoch); |
25 | } |
26 | |
27 | TEST_CASE("Verify that timestamp functions work" , "[timestamp]" ) { |
28 | VerifyTimestamp(Date::FromDate(2019, 8, 26), Time::FromTime(8, 52, 6), 1566809526); |
29 | VerifyTimestamp(Date::FromDate(1970, 1, 1), Time::FromTime(0, 0, 0), 0); |
30 | VerifyTimestamp(Date::FromDate(2000, 10, 10), Time::FromTime(10, 10, 10), 971172610); |
31 | } |
32 | |