How should I populate a list of IANA / Olson time zones from Noda Time?
Solution 1:
Noda Time 1.1 has the zone.tab data, so you can now do the following:
/// <summary>
/// Returns a list of valid timezones as a dictionary, where the key is
/// the timezone id, and the value can be used for display.
/// </summary>
/// <param name="countryCode">
/// The two-letter country code to get timezones for.
/// Returns all timezones if null or empty.
/// </param>
public IDictionary<string, string> GetTimeZones(string countryCode)
{
var now = SystemClock.Instance.Now;
var tzdb = DateTimeZoneProviders.Tzdb;
var list =
from location in TzdbDateTimeZoneSource.Default.ZoneLocations
where string.IsNullOrEmpty(countryCode) ||
location.CountryCode.Equals(countryCode,
StringComparison.OrdinalIgnoreCase)
let zoneId = location.ZoneId
let tz = tzdb[zoneId]
let offset = tz.GetZoneInterval(now).StandardOffset
orderby offset, zoneId
select new
{
Id = zoneId,
DisplayValue = string.Format("({0:+HH:mm}) {1}", offset, zoneId)
};
return list.ToDictionary(x => x.Id, x => x.DisplayValue);
}
Alternative Approach
Instead of providing a drop down at all, you can use a map-based timezone picker.
Solution 2:
Getting the standard offset is easy - tz.GetZoneInterval(now).StandardOffset
. That will give you the "current" standard offset (it's possible for a zone to change over time).
The filtering may be appropriate for you - I wouldn't like to say for sure. It's certainly not ideal in that the IDs aren't really designed for display. Ideally you'd use the Unicode CLDR "example" places, but we don't have any CLDR integration on that front at the moment.