diff --git a/Tests/LibSQL/TestSqlValueAndTuple.cpp b/Tests/LibSQL/TestSqlValueAndTuple.cpp index 6526353133..970716895d 100644 --- a/Tests/LibSQL/TestSqlValueAndTuple.cpp +++ b/Tests/LibSQL/TestSqlValueAndTuple.cpp @@ -7,6 +7,7 @@ #include +#include #include #include #include @@ -471,6 +472,39 @@ TEST_CASE(serialize_boolean_value) EXPECT_EQ(v, v2); } +TEST_CASE(unix_date_time_value) +{ + auto now = UnixDateTime::now(); + { + SQL::Value value(now); + EXPECT_EQ(value.type(), SQL::SQLType::Integer); + + auto result = value.to_unix_date_time(); + VERIFY(result.has_value()); + EXPECT_EQ(result->milliseconds_since_epoch(), now.milliseconds_since_epoch()); + } + { + auto now_plus_10s = now + Duration::from_seconds(10); + + SQL::Value value(now_plus_10s); + EXPECT_EQ(value.type(), SQL::SQLType::Integer); + + auto result = value.to_unix_date_time(); + VERIFY(result.has_value()); + EXPECT_EQ(result->milliseconds_since_epoch(), now_plus_10s.milliseconds_since_epoch()); + } + { + auto now_minus_10s = now - Duration::from_seconds(10); + + SQL::Value value(now_minus_10s); + EXPECT_EQ(value.type(), SQL::SQLType::Integer); + + auto result = value.to_unix_date_time(); + VERIFY(result.has_value()); + EXPECT_EQ(result->milliseconds_since_epoch(), now_minus_10s.milliseconds_since_epoch()); + } +} + TEST_CASE(tuple_value) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); diff --git a/Userland/Libraries/LibSQL/Value.cpp b/Userland/Libraries/LibSQL/Value.cpp index a1da0aa0bd..83db129778 100644 --- a/Userland/Libraries/LibSQL/Value.cpp +++ b/Userland/Libraries/LibSQL/Value.cpp @@ -277,6 +277,15 @@ Optional Value::to_bool() const }); } +Optional Value::to_unix_date_time() const +{ + auto time = to_int(); + if (!time.has_value()) + return {}; + + return UnixDateTime::from_milliseconds_since_epoch(*time); +} + Optional> Value::to_vector() const { if (is_null() || (type() != SQLType::Tuple)) diff --git a/Userland/Libraries/LibSQL/Value.h b/Userland/Libraries/LibSQL/Value.h index 98852819aa..54654447b1 100644 --- a/Userland/Libraries/LibSQL/Value.h +++ b/Userland/Libraries/LibSQL/Value.h @@ -77,6 +77,7 @@ public: [[nodiscard]] ByteString to_byte_string() const; [[nodiscard]] Optional to_double() const; [[nodiscard]] Optional to_bool() const; + [[nodiscard]] Optional to_unix_date_time() const; [[nodiscard]] Optional> to_vector() const; template