Path: redb/redb.Core/Utils/RedbTemporalFormat.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.Core/Utils/RedbTemporalFormat.cs
----
using System;
using System.Globalization;
namespace redb.Core.Utils
{
///
/// Invariant, round-trip text form for the zone-less temporal types
/// (, , ).
///
///
/// Both the JSON converters and the _values._String write path go through here, so the
/// two can never disagree and neither depends on the ambient .
///
///
///
/// Why this exists. Three distinct culture leaks, only the first of which is obvious:
///
/// - A bare DateOnly.ToString() / TimeOnly.ToString() uses the CURRENT
/// culture's short date/time pattern: "23.08.2026" under ru-RU, "8/23/2026" under
/// en-US, "2:30 PM" for a time. This is what the _values._String write path used
/// to do, so a value written under one culture failed to parse, or silently mis-parsed with day
/// and month swapped, under another.
/// - Parse / TryParse without an explicit reads
/// back through the current culture, so the read side drifted with the write side.
/// - Even an explicit custom pattern is only partly safe: in a custom format string
/// ':' resolves to the culture's TIME SEPARATOR ('-' and '.' are literals,
/// only '/' is the date separator). Most cultures use ':', which is exactly why
/// this one hid.
///
/// The standard round-trip specifiers used here ("O" for the two date/time types,
/// "c" for ) are culture-independent by definition.
///
///
///
/// additionally used to be written as @"hh\:mm\:ss", which drops
/// the day component and the sign: 3.02:00:00 came back as 02:00:00. "c"
/// is the constant format [-][d'.']hh':'mm':'ss['.'fffffff] and keeps both.
///
///
///
/// Parsing accepts the invariant form first and falls back to the current culture last, so rows
/// written by an earlier build under a non-invariant culture keep loading on a host that still
/// runs under that culture.
///
///
public static class RedbTemporalFormat
{
/// Round-trip format: yyyy-MM-dd.
public const string DateOnlyFormat = "O";
/// Round-trip format: HH:mm:ss.fffffff.
public const string TimeOnlyFormat = "O";
/// Constant format: [-][d.]hh:mm:ss[.fffffff].
public const string TimeSpanFormat = "c";
public static string ToText(DateOnly value) =>
value.ToString(DateOnlyFormat, CultureInfo.InvariantCulture);
public static string ToText(TimeOnly value) =>
value.ToString(TimeOnlyFormat, CultureInfo.InvariantCulture);
public static string ToText(TimeSpan value) =>
value.ToString(TimeSpanFormat, CultureInfo.InvariantCulture);
public static bool TryParseDateOnly(string? text, out DateOnly value)
{
value = default;
if (string.IsNullOrWhiteSpace(text)) return false;
// PostgreSQL sentinels. Npgsql maps DateTime.MinValue/MaxValue to -infinity/infinity,
// so an unset DateOnly (0001-01-01, its default) comes back as the text "-infinity".
// The DateTime converters have handled this for a long time; DateOnly did not, and
// simply leaving a DateOnly property unassigned threw on load.
if (text == "-infinity") { value = DateOnly.MinValue; return true; }
if (text == "infinity") { value = DateOnly.MaxValue; return true; }
if (DateOnly.TryParseExact(text, DateOnlyFormat, CultureInfo.InvariantCulture,
DateTimeStyles.None, out value))
return true;
if (DateOnly.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.None, out value))
return true;
// A full ISO timestamp also resolves to a date (the JSON side may carry one).
if (DateTime.TryParse(text, CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out var asDateTime))
{
value = DateOnly.FromDateTime(asDateTime);
return true;
}
// Legacy: written by an earlier build through the current culture.
return DateOnly.TryParse(text, CultureInfo.CurrentCulture, DateTimeStyles.None, out value);
}
public static bool TryParseTimeOnly(string? text, out TimeOnly value)
{
value = default;
if (string.IsNullOrWhiteSpace(text)) return false;
if (TimeOnly.TryParseExact(text, TimeOnlyFormat, CultureInfo.InvariantCulture,
DateTimeStyles.None, out value))
return true;
if (TimeOnly.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.None, out value))
return true;
// Older rows carry a TimeSpan-shaped string ("14:30:00").
if (TryParseTimeSpan(text, out var asSpan)
&& asSpan >= TimeSpan.Zero && asSpan < TimeSpan.FromDays(1))
{
value = TimeOnly.FromTimeSpan(asSpan);
return true;
}
return TimeOnly.TryParse(text, CultureInfo.CurrentCulture, DateTimeStyles.None, out value);
}
public static bool TryParseTimeSpan(string? text, out TimeSpan value)
{
value = default;
if (string.IsNullOrWhiteSpace(text)) return false;
if (TimeSpan.TryParseExact(text, TimeSpanFormat, CultureInfo.InvariantCulture, out value))
return true;
if (TimeSpan.TryParse(text, CultureInfo.InvariantCulture, out value))
return true;
return TimeSpan.TryParse(text, CultureInfo.CurrentCulture, out value);
}
}
}