Search code examples
c#nodatimedatetime-conversion

Noda Time zone Format


I am using the Noda Time library. My code is

var timeZone = NodaTime.DateTimeZoneProviders.Tzdb.GetZoneOrNull("Europe/Amsterdam");

above line of code gives me value like this which I don't want.

MaxOffset: +2:00
MinOffset: +00:19:32

when I use the same zone and query using the timezonedb.com its give me result like this

UTC/GMT +2.00 hours

But I need result in this format:

(UTC+02:00) Europe/Amsterdam

How can I achieve this using Noda Time?


Solution

  • Well, the result of the line of code you've given is a DateTimeZone. That has a MinOffset and a MaxOffset, but that's a different matter.

    You can find out the current standard and wall offset using:

    IClock clock = // some clock, e.g. SystemClock.Instance;
    Instant now = clock.Now;
    ZoneInterval interval = zone.GetZoneInterval(now);
    Offset wallOffset = interval.WallOffset;
    Offset standardOffset = interval.StandardOffset;
    

    So depending on whether you want the UTC+08:00 to represent the current standard or wall offset, you can format that. For example:

    string text = string.Format("(UTC{0:+m}) {1}", interval.WallOffset, zone.Id);