Skip to content
← All posts
· Delta1 Labs Time ZonesKeyright

Showing license expiry in the right time zone

A license expires at one instant on the timeline, but "November 30" is a civil date that depends on where you stand. Confuse the two and a customer in Auckland sees "expired" on a key a customer in Los Angeles still shows as valid. This post builds the correct expiry display in .NET: store the expiry as a UTC instant, project it into the viewer's own time zone with DateTimeOffset and TimeZoneInfo, label the zone, and compute "days remaining" so daylight-saving transitions never make a day 23 or 25 hours long.

A support ticket arrives: a customer in Auckland says your app reports their license as expired today, but their renewal receipt clearly says it runs through the end of the month. You check the key. It is signed, it is theirs, and the expiry instant is comfortably in the future. Then a second customer, in Los Angeles, with the same expiry on their key, reports no problem at all — their copy says “expires tomorrow.” Nothing is wrong with either license. Your UI rendered one instant onto two different calendars.

This is not the clock-skew problem, where two machines honestly disagree about what time it is, and it is not an attacker rolling the clock back. Both clocks here are correct. The bug is in how Keyright’s expiry — a UTC instant — gets turned into a human-readable date. This post builds that conversion properly.

An instant is not a date

A Keyright license stores its expiry as a single point on the global timeline, in UTC:

public sealed class LicenseInfo
{
    public DateTimeOffset ExpiryUtc { get; }   // one instant, e.g. 2026-11-30T07:59:59+00:00
    // ...
}

DateTimeOffset is the right type: it pins an instant and remembers that its offset is zero (UTC). The trouble starts the moment you try to show it to a person, because “when does this expire” has two different honest answers. As an instant it is one value for everyone on Earth. As a civil date — the thing a human reads on a calendar — it depends entirely on where the reader is standing.

// The bug. Renders the UTC instant as a bare date.
var label = info.ExpiryUtc.ToString("D");   // "Monday, 30 November 2026" — in UTC

That label is correct only for a viewer who happens to live on the UTC meridian. For the Los Angeles customer, the instant 2026-11-30T07:59:59Z is 2026-11-29T23:59:59 local — it is still November 29 where they are. For the Auckland customer, the same instant is 2026-11-30T20:59:59 local — well into November 30. Format the instant as a bare date and you have told one of them the wrong day.

One UTC instant, two calendar datesexpiry = 2026-11-30 07:59:59ZAmerica/Los_Angeles (UTC−8)Nov 29Nov 3023:59 on Nov 29 — still the 29thPacific/Auckland (UTC+13)Nov 29Nov 3020:59 on Nov 30 — already the 30th
The orange marks are local midnight. The single expiry instant sits before midnight in Los Angeles and after it in Auckland, so a bare-date render disagrees by a day.

Project the instant into the viewer’s zone

The fix is to convert the instant into the zone of whoever is reading it, then format. TimeZoneInfo.ConvertTime takes a DateTimeOffset and returns a new DateTimeOffset carrying that zone’s offset at that instant:

public static string FormatExpiry(DateTimeOffset expiryUtc, string viewerZoneId)
{
    // viewerZoneId is an IANA id such as "Pacific/Auckland" or "America/Los_Angeles".
    var zone  = TimeZoneInfo.FindSystemTimeZoneById(viewerZoneId);
    var local = TimeZoneInfo.ConvertTime(expiryUtc, zone);

    // Include the zone so the date is unambiguous to the reader.
    return $"{local:dddd, d MMMM yyyy 'at' h:mm tt} ({zone.Id})";
    // Auckland  -> "Monday, 30 November 2026 at 8:59 PM (Pacific/Auckland)"
    // Los Angeles -> "Sunday, 29 November 2026 at 11:59 PM (America/Los_Angeles)"
}

Two things make this correct. First, ConvertTime on a DateTimeOffset is always well defined — projecting an instant into a zone can never be ambiguous, because an instant is a single point and every zone has exactly one local representation of it. (The ambiguity of daylight saving only bites when you go the other way, turning a typed-in wall-clock time back into an instant — a trap for the admin UI that sets an expiry, not for the client that displays one.) Second, labelling the zone means neither customer can misread the date: “November 29 (America/Los_Angeles)” and “November 30 (Pacific/Auckland)” are both true, and now visibly describe the same moment.

Since .NET 6, FindSystemTimeZoneById accepts IANA ids like America/Los_Angeles on Windows as well as Linux and macOS, because the runtime uses ICU and ships the IANA-to-Windows mapping. Store one canonical IANA id per user — it is exactly what a browser reports from Intl.DateTimeFormat().resolvedOptions().timeZone — and resolve it identically wherever your code runs. Never store a bare numeric offset: an offset does not know when daylight saving starts, so a value of -08:00 saved in winter is simply wrong for a customer in Los Angeles all summer.

“Days remaining” is two different questions

The expiry banner usually also says how long is left, and here is a second place the naive answer drifts. Decide which question you are answering, because across a daylight-saving transition they give different numbers.

A duration — “4 days, 3 hours left” — is the true gap between two instants. Subtract the DateTimeOffset values directly:

TimeSpan remaining = info.ExpiryUtc - DateTimeOffset.UtcNow;   // exact elapsed time

A calendar count — “expires on the 30th, which is 3 days from today” — is about civil dates in the viewer’s zone, and it must be computed on the dates, not on a raw duration:

static int CalendarDaysUntilExpiry(DateTimeOffset expiryUtc, TimeZoneInfo zone)
{
    var expiryLocalDate = TimeZoneInfo.ConvertTime(expiryUtc, zone).Date;
    var todayLocalDate  = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, zone).Date;
    return (expiryLocalDate - todayLocalDate).Days;   // whole calendar days in the viewer's zone
}

The tempting shortcut — (int)(remaining.TotalDays) or remaining.Days — is wrong precisely on the days that matter, the ones around a clock change. On the spring-forward day a local calendar day is only 23 hours, and on fall-back it is 25. Divide a TimeSpan by a flat 24 hours across one of those and you land a day early or a day late. Stripping both instants to their local .Date first sidesteps the problem entirely: midnight-to-midnight date subtraction counts calendar days regardless of how many hours each day actually held.

When it breaks, and what to watch

Three honest caveats. First, the zone database moves: governments change daylight-saving rules, and a future expiry you render today can shift by an hour once the host’s tzdata is updated — which is another reason the instant is the source of truth and the local projection is only a view. Second, resolve the viewer’s zone deliberately: prefer the user’s saved profile zone, fall back to what the browser reports, and never silently use the server’s zone — that is the original bug wearing a different hat. Third, do not round-trip through a local string: if you ever persist local.DateTime with Kind == Unspecified and read it back, you have lost the instant, and no amount of later conversion can recover which offset was in force.

The takeaway

A license expiry is one instant on the timeline; the date a human reads is that instant seen from somewhere. Keep the signed value in UTC as a DateTimeOffset, convert it into the viewer’s IANA zone with TimeZoneInfo only at render time, and always print the zone next to the date so two customers an ocean apart can see that “the 29th here” and “the 30th there” are the same moment. Compute a countdown from the instant difference, but compute “days remaining” from local .Date subtraction so a daylight-saving day of 23 or 25 hours never makes your banner off by one. Get those two conversions right and the Auckland ticket never opens.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.