Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Time Zone Abbreviations in ASP.NET

Do they even exist? I am really sick of having to display "Romance Standard Time" to my European users and trying to explain why their "Central European Time" can't be displayed as "CET". If I parsed "Romance Standard Time" and presented them with "RST" that would be meaningless to them, or possibly confused as "Russian Standard Time" or other nonsense.

Is there not a way to get generally accepted abbreviations out of Windows? I need something that works across North America, South America, Europe, Africa, and Asia.

UPDATE: This is a total hack, I know, but it will likely get me 90% of the cases I care about. If you have suggestions, I'm still all ears ;)

    /// <summary>
    /// Gets the current time zone abbreviation, corrected for Daylight Saving Time (if applicable)
    /// </summary>
    /// <param name="timeZoneId">The time zone id.</param>
    /// <returns></returns>
    public static string GetTZAbbrev(string timeZoneId)
    {
      var timeZoneInfos = TimeZoneInfo.GetSystemTimeZones();
      var abbrev = string.Empty;

      foreach (var timeZoneInfo in timeZoneInfos)
      {
        if (timeZoneInfo.Id != timeZoneId) continue;

        string[] words;

        if (timeZoneInfo.IsDaylightSavingTime(DateTime.UtcNow))
        {
          words = timeZoneInfo.DaylightName.Split(' ');         
        }
        else
        {
          words = timeZoneInfo.StandardName.Split(' ');         
        }

        foreach (var word in words)
        {
          abbrev += word[0];
        }
      }
      return abbrev;
    }
like image 541
Mark Richman Avatar asked Nov 16 '10 15:11

Mark Richman


2 Answers

Well, even the "generally accepted abbreviations" aren't really a good idea - because they're so ambiguous. If you say "EDT" do you mean Eastern Daylight Time in the US, or Eastern Daylight Time in Australia?

A more generally accepted technical solution is to use Olson (zoneinfo) names such as "Europe/London" as identifiers. How these are presented to users is a different matter, but usually I see user interfaces with place names for particular time zones along with their standard (or current; it varies) offset from UTC. See Google Calendar's time zone settings for example.

This is really a matter of internationalizing (or localizing, I can never remember) identifiers to user-friendly names, however you do it.

like image 116
Jon Skeet Avatar answered Oct 19 '22 13:10

Jon Skeet


You could always build a crosswalk method and convert them yourself. You would just need to get a list of each time, then crosswalk it yourself. I don't know of anything in windows that has this for you, but you're not talking a lot of code to do it yourself...

like image 24
Brosto Avatar answered Oct 19 '22 13:10

Brosto