Recipe 15.3 · C#
Time Zones That Survive Daylight Saving
Problem
Users in several time zones need to see times in their own zone, and reports need "orders placed on March 8" to mean March 8 where the business is, not in UTC. The code converts with hard-coded offsets, and twice a year something is off by an hour.
The way it's often written
Anti-pattern: shown so you can recognize it. Don't copy this one.
// Not to copy: fixed offsets and the server's zone.
public static DateTime ToEastern(DateTime utc)
{
return utc.AddHours(-5); // wrong half the year
}
public static DateTime ToUserTime(DateTime value, string zone)
{
var tz = TimeZoneInfo.FindSystemTimeZoneById(zone);
return TimeZoneInfo.ConvertTime(value, tz); // value's Kind?
}
The first method is wrong from March to November. The second depends on the Kind of the incoming DateTime: if it is Local, the conversion starts from the server's zone, and if the zone name came from a browser on Linux it might not be found at all.
The improved recipe
using System;
using System.Linq;
namespace ImproveCode.Utilities;
public static class ZoneTime
{
public static TimeZoneInfo Find(string id)
{
ArgumentException.ThrowIfNullOrWhiteSpace(id);
if (TimeZoneInfo.TryFindSystemTimeZoneById(
id, out TimeZoneInfo? zone))
{
return zone;
}
throw new ArgumentException(
$"Unknown time zone '{id}'.", nameof(id));
}
public static DateTimeOffset ToZone(
DateTimeOffset instant, TimeZoneInfo zone) =>
TimeZoneInfo.ConvertTime(instant, zone);
public static DateTimeOffset FromLocal(
DateTime localTime, TimeZoneInfo zone)
{
DateTime local = DateTime.SpecifyKind(
localTime, DateTimeKind.Unspecified);
if (zone.IsInvalidTime(local))
{
// Inside a spring-forward gap: move past it.
TimeSpan before = zone.GetUtcOffset(local.AddHours(-6));
TimeSpan after = zone.GetUtcOffset(local.AddHours(6));
local = local.Add(after - before);
}
TimeSpan offset = zone.IsAmbiguousTime(local)
? zone.GetAmbiguousTimeOffsets(local).Max()
: zone.GetUtcOffset(local);
return new DateTimeOffset(local, offset);
}
public static (DateTimeOffset Start, DateTimeOffset End) DayRange(
DateOnly day, TimeZoneInfo zone)
{
DateTime start = day.ToDateTime(TimeOnly.MinValue);
DateTime end = day.AddDays(1).ToDateTime(TimeOnly.MinValue);
return (FromLocal(start, zone), FromLocal(end, zone));
}
}
Usage snippet:
TimeZoneInfo eastern = ZoneTime.Find("America/New_York");
DateTimeOffset shown = ZoneTime.ToZone(order.CreatedUtc, eastern);
var (start, end) = ZoneTime.DayRange(new DateOnly(2026, 3, 8), eastern);
// WHERE CreatedUtc >= @start AND CreatedUtc < @end
// pass start.UtcDateTime and end.UtcDateTime as parameters
How it works
Find wraps TimeZoneInfo.FindSystemTimeZoneById through its non-throwing sibling, TryFindSystemTimeZoneById (added in .NET 8), and gives a clear error with the bad ID in it.
There are two families of time zone IDs. Windows uses names like Eastern Standard Time; Linux, macOS, browsers and most APIs use IANA names like America/New_York. Before .NET 6 you could only look up the native kind for the operating system. Since .NET 6, FindSystemTimeZoneById accepts either kind on any platform, converting between them with ICU data. TimeZoneInfo.TryConvertIanaIdToWindowsId and TryConvertWindowsIdToIanaId are there when you need to translate explicitly. I store IANA IDs in user profiles, because that is what a browser reports and what every other system understands.
ToZone converts an instant. Because the input is a DateTimeOffset, there is no Kind to misread; the result carries the zone's offset at that moment, daylight saving included.
FromLocal handles the hard direction: a local wall-clock time typed by a user, turned into an instant. Two local times are special. In the spring, the hour from 2:00 to 3:00 doesn't exist in US Eastern time; IsInvalidTime catches it and the code moves the time forward by the size of the gap, the same thing a wall clock does. In the autumn, 1:30 happens twice; IsAmbiguousTime catches it and the code chooses the larger offset, which is the first occurrence. TimeZoneInfo.ConvertTimeToUtc would throw on the first case and silently pick standard time on the second.
DayRange returns a half-open range, start inclusive and end exclusive, for querying UTC columns. A day is not always 24 hours: March 8, 2026 in New York is 23 hours long, and the range reflects that.
Variations and pitfalls
- IANA lookups on Windows rely on the ICU library that ships with Windows 10 version 1903 and later. If an app runs in globalization-invariant mode, test your zone lookups; conversion between ID families may not be available.
- Never store an offset as if it were a zone.
-05:00tells you nothing about what the offset will be next July. Store the zone ID and the UTC instant. - For scheduling future local events ("9 a.m. every Monday in Chicago"), store the local time and the zone ID, and compute the instant each time. Time zone rules change by law, and a precomputed UTC value will drift.
- If you need richer time zone handling, such as historical rules or calendars beyond the BCL, look at NodaTime. For most business apps,
TimeZoneInfowith the rules above is enough.