encodeTimestamp static method

String encodeTimestamp(
  1. DateTime value
)

A fixed-width UTC ISO-8601 timestamp.

SQLite has no date type: WHERE available_at <= ? is a text comparison, which is only equivalent to a chronological one while every stored value has the same shape. DateTime.toIso8601String breaks that — it emits three fractional digits when the microsecond component happens to be zero and six otherwise:

2026-01-01T12:00:00.123Z      // earlier in time
2026-01-01T12:00:00.123456Z   // later in time

Compared as text the shorter one sorts after the longer ('Z' is 0x5A, '4' is 0x34), inverting the order. DateTime.now() lands on a zero microsecond roughly once in a thousand calls, so the effect is a rare, unreproducible wrong answer rather than an obvious break.

Padding to a constant six digits makes the two orders agree. Rows written by an earlier version keep their original width; a table that mixes the two still compares wrongly for those rows, and has to be rewritten to be fully correct.

Implementation

static String encodeTimestamp(DateTime value) => value
    .toUtc()
    .toIso8601String()
    .replaceFirstMapped(_millisecondPrecision, (match) => '.${match[1]}000Z');