5
var easternTimeZone = DateTimeZoneProviders.Tzdb[timeZoneIdentifier];

Using NodaTime how should I validate that when the string timeZoneIdentifier is set to a valid IANA string like "Europe/Stockholm" it is deemed valid but an unsupported IANA string is rejected.

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Dizzle
  • 1,026
  • 13
  • 26
  • 1
    What's the difference between an *unsupported* string and an invalid string? Just catch the `TimeZoneNotFound` exception, or try for the null reference from `GetZoneOrNull` and consider that good enough? – Anya Shenanigans Oct 17 '15 at 20:36
  • Thank you for the comment, I will accept Matt Johnson's answer as it has a code sample. – Dizzle Oct 18 '15 at 05:12

1 Answers1

7
DateTimeZone tz = DateTimeZoneProviders.Tzdb.GetZoneOrNull(timeZoneIdentifier);
bool valid = tz != null;

Or, as an extension method:

public static bool IsValidTimeZone(this IDateTimeZoneProvider provider, string timeZoneId)
{
    return provider.GetZoneOrNull(timeZoneId) != null;
}
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575