Skip to content

Commit cd0e07f

Browse files
trflynn89awesomekling
authored andcommitted
LibSQL: Add a helper to convert a SQL::Value to a UnixDateTime
Support for constructing a Value from a UnixDateTime was added in commit effcd08. That constructor just stores the value as the number of milliseconds since epoch. There's no way for outside users to know this, so this adds a helper to retrieve the value as a UnixDateTime and let SQL::Value be the source of truth for how the value is encoded/decoded.
1 parent 1205d39 commit cd0e07f

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

Tests/LibSQL/TestSqlValueAndTuple.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <unistd.h>
99

10+
#include <AK/Time.h>
1011
#include <LibSQL/Meta.h>
1112
#include <LibSQL/Row.h>
1213
#include <LibSQL/Tuple.h>
@@ -471,6 +472,39 @@ TEST_CASE(serialize_boolean_value)
471472
EXPECT_EQ(v, v2);
472473
}
473474

475+
TEST_CASE(unix_date_time_value)
476+
{
477+
auto now = UnixDateTime::now();
478+
{
479+
SQL::Value value(now);
480+
EXPECT_EQ(value.type(), SQL::SQLType::Integer);
481+
482+
auto result = value.to_unix_date_time();
483+
VERIFY(result.has_value());
484+
EXPECT_EQ(result->milliseconds_since_epoch(), now.milliseconds_since_epoch());
485+
}
486+
{
487+
auto now_plus_10s = now + Duration::from_seconds(10);
488+
489+
SQL::Value value(now_plus_10s);
490+
EXPECT_EQ(value.type(), SQL::SQLType::Integer);
491+
492+
auto result = value.to_unix_date_time();
493+
VERIFY(result.has_value());
494+
EXPECT_EQ(result->milliseconds_since_epoch(), now_plus_10s.milliseconds_since_epoch());
495+
}
496+
{
497+
auto now_minus_10s = now - Duration::from_seconds(10);
498+
499+
SQL::Value value(now_minus_10s);
500+
EXPECT_EQ(value.type(), SQL::SQLType::Integer);
501+
502+
auto result = value.to_unix_date_time();
503+
VERIFY(result.has_value());
504+
EXPECT_EQ(result->milliseconds_since_epoch(), now_minus_10s.milliseconds_since_epoch());
505+
}
506+
}
507+
474508
TEST_CASE(tuple_value)
475509
{
476510
NonnullRefPtr<SQL::TupleDescriptor> descriptor = adopt_ref(*new SQL::TupleDescriptor);

Userland/Libraries/LibSQL/Value.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ Optional<bool> Value::to_bool() const
277277
});
278278
}
279279

280+
Optional<UnixDateTime> Value::to_unix_date_time() const
281+
{
282+
auto time = to_int<i64>();
283+
if (!time.has_value())
284+
return {};
285+
286+
return UnixDateTime::from_milliseconds_since_epoch(*time);
287+
}
288+
280289
Optional<Vector<Value>> Value::to_vector() const
281290
{
282291
if (is_null() || (type() != SQLType::Tuple))

Userland/Libraries/LibSQL/Value.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class Value {
7777
[[nodiscard]] ByteString to_byte_string() const;
7878
[[nodiscard]] Optional<double> to_double() const;
7979
[[nodiscard]] Optional<bool> to_bool() const;
80+
[[nodiscard]] Optional<UnixDateTime> to_unix_date_time() const;
8081
[[nodiscard]] Optional<Vector<Value>> to_vector() const;
8182

8283
template<Integer T>

0 commit comments

Comments
 (0)