Initial commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
namespace PowderCoating.Web.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Converts UTC DateTimes to a specific IANA or Windows timezone for display.
|
||||
/// .NET 6+ accepts both timezone ID formats on all platforms.
|
||||
/// Falls back to the original value when the ID is null, empty, or unrecognised.
|
||||
/// </summary>
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
/// <summary>Converts a UTC DateTime to the specified timezone (IANA or Windows ID).</summary>
|
||||
public static DateTime Tz(this DateTime utc, string? ianaTimeZone)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ianaTimeZone)) return utc;
|
||||
try
|
||||
{
|
||||
var tzi = TimeZoneInfo.FindSystemTimeZoneById(ianaTimeZone);
|
||||
var normalized = utc.Kind == DateTimeKind.Unspecified
|
||||
? DateTime.SpecifyKind(utc, DateTimeKind.Utc)
|
||||
: utc.ToUniversalTime();
|
||||
return TimeZoneInfo.ConvertTimeFromUtc(normalized, tzi);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return utc;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user