Path: redb/redb.Export/Providers/SqliteJulianDay.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.Export/Providers/SqliteJulianDay.cs
----
namespace redb.Export.Providers;
///
/// The stored form of a date in a redb SQLite database: a UTC Julian day number (REAL), in
/// _objects._date_*, _objects._value_datetime, _users._date_* and
/// _values._DateTimeOffset.
///
/// A copy of redb.SQLite/Data/SqliteJulian.cs - same epoch, same millisecond rounding - because
/// this package does not reference the provider. The two must stay identical: the provider binds
/// every date parameter of a query as this number, and in SQLite a REAL never equals, and always
/// sorts before, a TEXT. A date written in any other form is invisible to every date filter.
///
///
internal static class SqliteJulianDay
{
// 1899-12-30 00:00 UTC.
private const double EpochJulianDay = 2415018.5;
private const long EpochMillis = 599264352000000000L / TimeSpan.TicksPerMillisecond;
private const double MillisPerDay = 86_400_000.0;
/// The UTC instant of as a Julian day.
public static double ToJulian(DateTimeOffset value)
{
var millis = value.UtcDateTime.Ticks / TimeSpan.TicksPerMillisecond - EpochMillis;
return millis / MillisPerDay + EpochJulianDay;
}
/// A Julian day as a at +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);
}
}