Timezone Abbreviations

2019-01-07 13:06发布

TimeZoneInfo does not provide abbreviation or a short name for a given Timezone. The only good way to do it is to have a dictionary that will map abbreviations to either Timezone.id, StandardName or DaylightName properties. However, all the sources I searched for list of abbreviations have different timezone names, i.e. not the same as in Windows.

How do you show a user not a full name, id or any other name in .NET? I don't want the UtcOffset but Timezone abbreviation - PST for Pacific, UTC - for Universal, EST - for Eastern Standard and etc. Is there any C# compatible list or database with all possible timezones and their abbreviations that compatible with those that TimeZoneInfo.GetSystemTimeZones() gives you?

标签: c# .net timezone
6条回答
该账号已被封号
2楼-- · 2019-01-07 13:31

UPDATED ANSWER

My original response is below, and is still valid. However there is now an easier way, using the TimeZoneNames library. After installing from Nuget, you can do the following:

string tzid = theTimeZoneInfo.Id;                // example: "Eastern Standard time"
string lang = CultureInfo.CurrentCulture.Name;   // example: "en-US"
var abbreviations = TZNames.GetAbbreviationsForTimeZone(tzid, lang);

The resulting object will have the properties similar to:

abbreviations.Generic == "ET"
abbreviations.Standard == "EST"
abbreviations.Daylight == "EDT"

You can also use this same library to get the fully localized names of the time zones. The library uses an embedded self-contained copy of the CLDR data.

ORIGINAL ANSWER

As others mentioned, Time zones abbreviations are ambiguous. But if you really want one for display, you need an IANA/Olson time zone database.

You can go from a Windows time zone to an IANA/Olson time zone and the other direction as well. But be aware that there could be multiple IANA/Olson zones for any given Windows zone. These mappings are maintained in the CLDR here.

NodaTime has both the database and the mappings. You can go from a .Net DateTime or DateTimeOffset with a TimeZoneInfo, to a NodaTime Instant and DateTimeZone. From there, you can get the abbreviation name.

// starting with a .Net TimeZoneInfo
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");

// You need to resolve to a specific instant in time - a noda Instant
// For illustrative purposes, I'll start from a regular .Net UTC DateTime
var dateTime = DateTime.UtcNow;
var instant = Instant.FromDateTimeUtc(dateTime);

// note that if we really wanted to just get the current instant,
// it's better and easier to use the following:
// var instant = SystemClock.Instance.Now;


// Now let's map the Windows time zone to an IANA/Olson time zone,
// using the CLDR mappings embedded in NodaTime.  This will use
// the *primary* mapping from the CLDR - that is, the ones marked
// as "territory 001".

// we need the NodaTime tzdb source.  In NodaTime 1.1.0+:
var tzdbSource = TzdbDateTimeZoneSource.Default;

// in previous NodaTime releases:
// var tzdbSource = new TzdbDateTimeZoneSource("NodaTime.TimeZones.Tzdb");

// map to the appropriate IANA/Olson tzid
var tzid = tzdbSource.MapTimeZoneId(timeZoneInfo);

// get a DateTimeZone from that id
var dateTimeZone = DateTimeZoneProviders.Tzdb[tzid];


// Finally, let's figure out what the abbreviation is
// for the instant and zone we have.

// now get a ZoneInterval for the zone and the instant
var zoneInterval = dateTimeZone.GetZoneInterval(instant);

// finally, you can get the correct time zone abbreviation
var abbreviation = zoneInterval.Name;

// abbreviation will be either PST or PDT depending
// on what instant was provided
Debug.WriteLine(abbreviation);
查看更多
霸刀☆藐视天下
3楼-- · 2019-01-07 13:31

Your question does not indicate what time zones your application must operate within, but in my particular instance, I only have a need to be concerned with United States time zones and UTC.

The abbreviations of U.S. time zones are always the first characters of each word in the time zone name. For instance, the abbreviation of "Mountain Standard Time" is "MST" and the abbreviation of "Eastern Daylight Time" is "EDT".

If you have similar requirements, you can easily derive the time zone abbreviation of the local time zone from the local time zone's name directly, as follows (note: here I'm determining the proper name based on the current date and time):

string timeZoneName = TimeZone.CurrentTimeZone.IsDaylightSavingTime(DateTime.Now)
                          ? TimeZone.CurrentTimeZone.DaylightName 
                          : TimeZone.CurrentTimeZone.StandardName;

string timeZoneAbbrev = GetTzAbbreviation(timeZoneName);

The code for the GetTzInitials() function is pretty straightforward. One thing worth mentioning is that some time zones may be set to Mexico or Canada, and the time zone names for these will come over with the country name in parenthesis, such as "Pacific Standard Time (Mexico)". To deal with this, any parenthesized data is passed back directly. The abbreviation returned for the above will be "PST(Mexico)", which works for me.

string GetTzAbbreviation(string timeZoneName) {
    string output = string.Empty;

    string[] timeZoneWords = timeZoneName.Split(' ');
    foreach (string timeZoneWord in timeZoneWords) {
        if (timeZoneWord[0] != '(') {
            output += timeZoneWord[0];
        } else {
            output += timeZoneWord;
        }
    }
    return output;
}
查看更多
孤傲高冷的网名
4楼-- · 2019-01-07 13:33

I had a similar issue, but didn't want to use a 3rd party library (as most of these answers suggest). If you only want to support .Net's timezone names, and decide to use a lookup table, take a look at my answer over here, which leans on PHP to help generate a list of abbreviations. You can adapt the snippet there and the table to suit your needs.

查看更多
Evening l夕情丶
5楼-- · 2019-01-07 13:34

This is is helpful for anyone using Xamarin for iOS or Android because according to the documentation "The display name is localized based on the culture installed with the Windows operating system." When using this in the Central Time Zone, for the DateTime "2016-09-01 12:00:00 GMT", this function returns "CDT" which is exactly what I needed when I came upon this question.

public static string GetTimeZoneAbbreviation(DateTime time)
{
    string timeZoneAbbr;
    if(time.IsDaylightSavingTime() == true)
    {
        timeZoneAbbr = TimeZoneInfo.Local.DaylightName;
    }
    else
    {
        timeZoneAbbr = TimeZoneInfo.Local.StandardName;
    }

    return timeZoneAbbr;
}
查看更多
放荡不羁爱自由
6楼-- · 2019-01-07 13:38

This is a tricky requirement, the best you can do is get the list of your choice and create a extension / helper method to get the abbreviation for the given TimeZoneInfo.

Once place to start is at http://www.timeanddate.com/library/abbreviations/timezones/ which has a version of list which covers the zones I am aware of.

The issue would be in selecting an appropriate abbreviation where more than one exists for a given timezone. For example UTC can be represented as UTC or WET (Western European Time) or WEZ (Westeuropäische Zeit) or WT (Western Sahara Standard Time).

You may want to agree with your stakeholders on the naming convention you are going to follow with the given choices.

查看更多
萌系小妹纸
7楼-- · 2019-01-07 13:39

Here's another snippet using NodaTime:

NodaTime.ZonedDateTime hereAndNow = NodaTime.SystemClock.Instance.Now.InZone(
    NodaTime.DateTimeZoneProviders.Tzdb.GetSystemDefault());

System.TimeSpan zoneOffset = hereAndNow.ToDateTimeOffset().Offset;

string sTimeDisplay = string.Format("{0:G} {1} (UTC{2}{3:hh\\:mm} {4})", 
    hereAndNow.ToDateTimeOffset(), 
    hereAndNow.Zone.GetZoneInterval(hereAndNow.ToInstant()).Name, 
    zoneOffset < TimeSpan.Zero ? "-" : "+", 
    zoneOffset, 
    hereAndNow.Zone.Id);

On my system this yields: "4/11/2013 5:03:23 PM CDT (UTC-05:00 America/Chicago)"

(Thanks to Matt Johnson's answer for the clue that the abbreviation lives in TimeZoneInterval)

It would be easier if NodaTime.ZonedDateTime had a GetZoneInterval method, but perhaps I'm missing something.

查看更多
登录 后发表回答