dart-neats icon indicating copy to clipboard operation
dart-neats copied to clipboard

Always convert DateTime to UTC before sending to the database

Open jonasfj opened this issue 3 months ago • 0 comments

toExpr() will not call DateTime.toUtc(), but maybe it should!

Mixing UTC and local-time in a database is probably not a good idea.

I think postgres adapter already converts to UTC to avoid confusion.

And sqlite is probably timezone aware when comparing timestamps.

But perhaps it's safest to just always convert to UTC for sanity.


We have warnings in:

typed_sql/doc/02_inserting_rows.md:
 * `DateTime` (e.g. `toExpr(DateTime.now().toUtc())`)

typed_sql/lib/src/typed_sql.expr.dart:
/// Create an [Expr<T>] wrapping [value].
///
/// The type of [value] must be one of:
///  * [String],
///  * [int],
///  * [double],
///  * [bool],
///  * [DateTime] (consider using [DateTime.toUtc]),
///  * [Uint8List],
///  * `null`
///
/// > [!NOTE]
/// > If you want to use a [CustomDataType], use the `.asExpr`
/// > _extension method_ instead.
///
/// When wrapping a [DateTime] object using [toExpr], do consider converting to
/// UTC first using [DateTime.toUtc], some database adapters may already do so
/// implicitly.
///
/// {@category inserting_rows}
/// {@category writing_queries}
/// {@category update_and_delete}
Expr<T> toExpr<T extends Object?>(T value) => Literal(value);

jonasfj avatar Nov 20 '25 13:11 jonasfj