Files
SIG-CM2.0/src/api/SIGCM2.Application/Common/TimeProviderArgentinaExtensions.cs

46 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace SIGCM2.Application.Common;
/// <summary>
/// Extension methods for <see cref="TimeProvider"/> that expose Argentina-localized
/// date helpers. Handles the UTC-3 offset cross-platform (IANA / Windows TZ IDs).
///
/// UDT-011: Cat2 fields (VigenciaDesde, etc.) must use civil Argentine date, never
/// raw UTC, to avoid date-creep during the 22:0023:59 window.
/// </summary>
public static class TimeProviderArgentinaExtensions
{
// IANA TZ id — Linux / macOS / .NET 8+ on Windows with ICU
public const string ArgentinaTimeZoneId = "America/Argentina/Buenos_Aires";
// Windows built-in TZ id — fallback for environments without ICU
public const string ArgentinaTimeZoneIdWindows = "Argentina Standard Time";
private static readonly TimeZoneInfo ArgentinaTz = LoadArgentinaTz();
/// <summary>
/// Returns today's civil date in Argentina timezone, computed from the
/// <see cref="TimeProvider"/> UTC clock.
/// Safe in tests via <c>FakeTimeProvider</c>; safe in production via
/// <c>TimeProvider.System</c>.
/// </summary>
public static DateOnly GetArgentinaToday(this TimeProvider timeProvider)
{
var utcNow = timeProvider.GetUtcNow();
var argentinaNow = TimeZoneInfo.ConvertTime(utcNow, ArgentinaTz);
return DateOnly.FromDateTime(argentinaNow.DateTime);
}
private static TimeZoneInfo LoadArgentinaTz()
{
try
{
return TimeZoneInfo.FindSystemTimeZoneById(ArgentinaTimeZoneId);
}
catch (TimeZoneNotFoundException)
{
// Windows without ICU: fall back to built-in Windows TZ name
return TimeZoneInfo.FindSystemTimeZoneById(ArgentinaTimeZoneIdWindows);
}
}
}