Skip to content

Always convert DateTime to UTC before sending to the database #302

@jonasfj

Description

@jonasfj

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);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions