Path: redb/redb.SQLite/Data/SqliteJulian.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.SQLite/Data/SqliteJulian.cs ---- using System; namespace redb.SQLite.Data { /// /// SQLite stores REDB datetimes as REAL Julian day numbers in UTC — the native /// SQLite datetime representation. SQLite's own date/time functions /// (datetime(), strftime(), julianday(), date()) consume a /// REAL Julian day directly, and because it is a plain number, range comparisons /// (</>) are numeric, correct, and index-friendly (sargable) — /// unlike the previous TEXT-ISO storage whose lexical comparison broke on format drift. /// /// Offset is normalized to UTC, matching REDB's contract (stored datetimes are UTC /// instants — see ) and how PostgreSQL /// keeps timestamptz in UTC. /// /// The day count is linear in the tick count, truncated to the millisecond - the precision the /// other providers keep and the hash canon assumes. It was computed through the OLE automation /// date (ToOADate), which is not linear: its zero stands for , /// so a default date came back as 1899-12-30, and a day before its epoch is encoded as sign and /// magnitude, so any time of day before 1899-12-30 moved by a day - while the native extension /// writes through SQLite's own julianday(), which is linear. /// internal static class SqliteJulian { // 1899-12-30 00:00 UTC, the same epoch the OLE date used: for every instant from it on the number // is bit for bit what was stored before, so existing rows and equality filters keep matching. private const double EpochJulianDay = 2415018.5; private const long EpochMillis = 599264352000000000L / TimeSpan.TicksPerMillisecond; private const double MillisPerDay = 86_400_000.0; /// DateTimeOffset → UTC Julian day (REAL). Uses the true UTC instant. public static double ToJulian(DateTimeOffset dto) => ToJulian(dto.UtcDateTime); /// /// DateTime → UTC Julian day (REAL). The clock value is treated as UTC (REDB's /// contract: /// specifies Kind=Utc without converting), so Kind is ignored here. /// public static double ToJulian(DateTime dt) { var millis = dt.Ticks / TimeSpan.TicksPerMillisecond - EpochMillis; return millis / MillisPerDay + EpochJulianDay; } /// UTC Julian day (REAL) → DateTimeOffset (+00:00), to the millisecond. public static DateTimeOffset FromJulian(double julian) { var millis = (long)Math.Round((julian - EpochJulianDay) * MillisPerDay, MidpointRounding.AwayFromZero); var ticks = (millis + EpochMillis) * TimeSpan.TicksPerMillisecond; return new DateTimeOffset(new DateTime(ticks, DateTimeKind.Utc), TimeSpan.Zero); } } }