Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
60 lines
2.5 KiB
C#
60 lines
2.5 KiB
C#
namespace Febe.Core.Domain;
|
|
|
|
/// <summary>
|
|
/// Calendar days in Alberto's time zone (spec §7.4: daily budgets use the local calendar day; §4.7: dates in file
|
|
/// names are local). The database stores UTC instants; these helpers convert at the edges.
|
|
/// </summary>
|
|
public static class LocalCalendar
|
|
{
|
|
/// <summary>Local calendar day of an instant.</summary>
|
|
public static DateOnly Today(DateTimeOffset instant, TimeZoneInfo zone) =>
|
|
DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(instant, zone).DateTime);
|
|
|
|
/// <summary>Local wall-clock time of an instant.</summary>
|
|
public static DateTime ToLocal(DateTimeOffset instant, TimeZoneInfo zone) =>
|
|
TimeZoneInfo.ConvertTime(instant, zone).DateTime;
|
|
|
|
/// <summary>
|
|
/// First instant of a local day. If local midnight does not exist (a daylight-saving jump at midnight, as in some
|
|
/// zones) the first valid local time after it is used.
|
|
/// </summary>
|
|
public static DateTimeOffset StartOfDay(DateOnly day, TimeZoneInfo zone) =>
|
|
FromLocal(day.ToDateTime(TimeOnly.MinValue, DateTimeKind.Unspecified), zone);
|
|
|
|
/// <summary>[start, end) of a local day, as UTC instants.</summary>
|
|
public static (DateTimeOffset Start, DateTimeOffset End) DayBounds(DateOnly day, TimeZoneInfo zone) =>
|
|
(StartOfDay(day, zone), StartOfDay(day.AddDays(1), zone));
|
|
|
|
/// <summary>[start, end) of the local day containing <paramref name="instant"/>.</summary>
|
|
public static (DateTimeOffset Start, DateTimeOffset End) DayBounds(DateTimeOffset instant, TimeZoneInfo zone) =>
|
|
DayBounds(Today(instant, zone), zone);
|
|
|
|
/// <summary>
|
|
/// Converts a local wall-clock time to an instant. Invalid times (inside a spring-forward gap) move forward to the
|
|
/// first valid minute; ambiguous times (fall-back) take the earlier instant (daylight time).
|
|
/// </summary>
|
|
public static DateTimeOffset FromLocal(DateTime local, TimeZoneInfo zone)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(zone);
|
|
var unspecified = DateTime.SpecifyKind(local, DateTimeKind.Unspecified);
|
|
var probe = unspecified;
|
|
for (var i = 0; i < 24 * 60 && zone.IsInvalidTime(probe); i++)
|
|
{
|
|
probe = probe.AddMinutes(1);
|
|
}
|
|
|
|
TimeSpan offset;
|
|
if (zone.IsAmbiguousTime(probe))
|
|
{
|
|
var offsets = zone.GetAmbiguousTimeOffsets(probe);
|
|
offset = offsets.Max();
|
|
}
|
|
else
|
|
{
|
|
offset = zone.GetUtcOffset(probe);
|
|
}
|
|
|
|
return new DateTimeOffset(probe, offset).ToUniversalTime();
|
|
}
|
|
}
|