Timezone Abbreviations
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;
}
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);
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.
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.