1

While trying to differentiate between the Mountain time zone and Arizona (I realize they are both Mountain Time) I found that GetNamesForTimeZone("America/Phoenix", "en-us") returned a daylight name.

Should i be relying on external code(noda time) to figure out if the time is daylight for that timezone or is there a way to know just from TimeZoneNames functions if a zone doesn't have daylight savings?

On that same note, what the the use-case for using the "generic" name for a timezone name instead of standard vs daylight?

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
DavidG
  • 469
  • 3
  • 12
  • Do you mean using https://github.com/mj1856/TimeZoneNames? It's unclear what your code looks like at the moment. A [mcve] would be helpful. – Jon Skeet Sep 21 '16 at 15:43

2 Answers2

2

As far as I can tell, TimeZoneNames is all about just the time zone names themselves - it doesn't know anything about the time zone data itself.

If you want to know whether America/Phoenix is currently observing daylight savings, I'd definitely use Noda Time itself:

// Usually pass in System.Clock.Instance as the clock...
// or take an Instant instead.
public bool IsCurrentlyObservingDaylightSavings(string id, IClock clock)
{
    var zone = DateTimeZoneProviders.Tzdb[id];
    var now = clock.Now;
    var zoneInterval = zone.GetZoneInterval(now);
    return zoneInterval.Savings != Offset.Zero;
}

I may well add a DateTimeZone.InDaylightSaving(Instant) method to 2.0...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

As the author of TimeZoneNames library, I can explain why this happens.

First, America/Phoenix maps to the America_Mountain metazone in the CLDR metaZones.xml file

<timezone type="America/Phoenix">
    <usesMetazone mzone="America_Mountain"/>
</timezone>

Other time zones are also in the same meta zone:

<timezone type="America/Denver">
    <usesMetazone mzone="America_Mountain"/>
</timezone>

Then, in each CLDR language file, such as the en.xml file for English, you'll see the America_Mountain metazone entry with its localized strings:

<metazone type="America_Mountain">
    <long>
        <generic>Mountain Time</generic>
        <standard>Mountain Standard Time</standard>
        <daylight>Mountain Daylight Time</daylight>
    </long>
    <short>
        <generic>MT</generic>
        <standard>MST</standard>
        <daylight>MDT</daylight>
    </short>
</metazone>

So, there's no information in CLDR as to whether or not DST is applicable in Arizona or not. Keep in mind that Arizona did indeed have DST in the past - last in 1967. So without TZDB data like that you would find in Noda Time, you can't make the determination of whether DST is in effect or not for some particular point in time.

Jon's answer shows how you would test for if DST is in effect using Noda Time. Once you have the result, you can choose the standard or daylight string from TimeZoneNames accordingly.

As for the Generic name, that is typically used when a human being is referring to the time zone in general, and not to either the daylight time portion or the standard time portion. One use case for this is in time zone selection, such as is demonstrated here.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks Matt. If I have another question about your TimeZoneNames library (I'm getting an error while in silverlight when NOT in elevated trust). What tag should i use so that you will look at it, or should i post it straight to the github for TimeZoneNames? Just wanted to make sure you had visibility to it. Since Silverlight isn't supported anymore you may not even consider it(our WPF and Silverlight has some shared code), but I thought I would ask. – DavidG Sep 23 '16 at 16:25
  • Hmmm.. Post it, sure. I'll take a look when I can. No promises though. :) – Matt Johnson-Pint Sep 23 '16 at 16:45
  • Logged as an issue on the git repo for TimeZoneNames library. – DavidG Sep 28 '16 at 14:50