TimeZoneNames

repository·main·Indexed 42 days ago

https://github.com/mattjohnsonpint/timezonenames

A C# library providing localized time zone names using CLDR and TZDB sources. It offers the TZNames class to retrieve generic, standard, and daylight names via IANA or Windows IDs, list time zones by country, and provide localized country names. Version 7.0.0 supports .NET 6+, .NET Core 2.0+, and .NET Framework 4.6.2+.

Tokens
1.6K
Snippets
9
Records
10
Agent score
3%

What's inside TimeZoneNames

  1. Install TimeZoneNames via NuGet

    main

    Install the TimeZoneNames package using the NuGet Package Manager Console:

    PM> Install-Package TimeZoneNames

    Compatibility: As of version 7.0.0, the library supports:

    • .NET 6 or greater
    • .NET Core 2.0 or greater
    • .NET Framework 4.6.2 and greater

    Note: .NET Framework versions less than 4.6.2 are no longer supported. If you are running .NET 6+ on Linux or macOS, the built-in names now stem from ICU and this library may no longer be needed.

  2. Use the TZNames class for localized time zone names

    main

    All functionality is provided through static methods in the TZNames class. To use it, import the TimeZoneNames namespace.

    using TimeZoneNames;
    
    // Access methods via TZNames
    var names = TZNames.GetNamesForTimeZone("America/Los_Angeles", "en-US");
    using TimeZoneNames;
  3. Get a single display name for a time zone with GetDisplayNameForTimeZone

    main

    Use TZNames.GetDisplayNameForTimeZone to get a single formatted string representing the time zone, including its UTC offset.

    string displayName = TZNames.GetDisplayNameForTimeZone("America/Vancouver", "fr");
    // => "(UTC-08:00) Pacifique (É.-U. et Canada)"
    string displayName = TZNames.GetDisplayNameForTimeZone("India Standard Time", "zh-CN");
    // => "(UTC+05:30) 钦奈,加尔各答,孟买,新德里"
  4. Get time zones for a specific country with GetTimeZonesForCountry

    main

    Use TZNames.GetTimeZonesForCountry to get a list of time zones for a specific country, ideal for user selection UI. It returns a dictionary where the key is the IANA identifier and the value is the localized generic name. If names are identical, a localized city name is appended in parentheses to disambiguate.

    Parameters:

    • countryCode (string): The ISO country code (e.g., "BR").
    • locale (string): The language/locale (e.g., "pt-BR").
    • threshold (DateTime/DateTimeOffset, optional): Limits the list to time zones that vary only after this specific date. Passing DateTimeOffset.UtcNow returns only time zones that differ in the future.
    // Basic usage
    var zones = TZNames.GetTimeZonesForCountry("BR", "pt-BR");
    
    // Using a threshold date
    var zones = TZNames.GetTimeZonesForCountry("BR", "pt-BR", new DateTime(2010, 1, 1));
    
    // Using UtcNow to only show future differences
    var zones = TZNames.GetTimeZonesForCountry("BR", "pt-BR", DateTimeOffset.UtcNow);
  5. Get time zone identifiers for a country with GetTimeZoneIdsForCountry

    main

    Use TZNames.GetTimeZoneIdsForCountry to get an array of IANA time zone identifiers for a specific country without localized names.

    Parameters:

    • countryCode (string): The ISO country code (e.g., "AU").
    • threshold (DateTime/DateTimeOffset, optional): Limits the list to zones that vary only after this specific date.
    // Get all IDs
    string[] zones = TZNames.GetTimeZoneIdsForCountry("AU");
    
    // Get IDs with a threshold
    string[] zones = TZNames.GetTimeZoneIdsForCountry("AU", DateTimeOffset.Now);
  6. Get localized country names with GetCountryNames

    main

    Use TZNames.GetCountryNames to get a dictionary of localized country names, suitable for a country-selection dropdown.

    Returns: A dictionary where the key is the ISO country code and the value is the localized name.

    var countries = TZNames.GetCountryNames("en-GB");
    // Key: "AF", Value: "Afghanistan"
  7. List all time zone display names with GetDisplayNames

    main

    Use TZNames.GetDisplayNames to get a dictionary where the key is a time zone identifier and the value is the localized name. This is useful for providing localized names regardless of the operating system's language.

    Parameters:

    • locale (string): The language/locale (e.g., "fr-CA").
    • useIanaIds (bool, optional): If true, returns IANA time zone IDs as keys (though the list remains limited to valid Windows time zones). If a country code is present in the locale (e.g., CA in fr-CA), it is used for the Windows-to-IANA mapping.
    var displayNames = TZNames.GetDisplayNames("fr-CA");
  8. Get fixed offset time zone IDs, names, and abbreviations

    main

    The following methods return lists of time zone IDs that represent a fixed offset from UTC (including UTC itself):

    • GetFixedTimeZoneIds(): Returns the IDs.
    • GetFixedTimeZoneNames(): Returns the localized names.
    • GetFixedTimeZoneAbbreviations(): Returns the abbreviations.

    Note: Time zones in the form Etc/GMT[+/-]n use an inverted sign from usual conventions.

    Warning: Methods for retrieving localized time zone abbreviations are deprecated due to unreliable source data.

  9. Get localized names for a specific time zone with GetNamesForTimeZone

    main

    Use TZNames.GetNamesForTimeZone to look up the localized names for a specific time zone using either an IANA ID or a Windows time zone ID. It returns an object containing Generic, Standard, and Daylight names.

    // Using IANA ID
    var names = TZNames.GetNamesForTimeZone("America/Los_Angeles", "en-US");
    // names.Generic == "Pacific Time"
    // names.Standard == "Pacific Standard Time"
    // names.Daylight == "Pacific Daylight Time"
    
    // Using Windows ID
    var names = TZNames.GetNamesForTimeZone("Romance Standard Time", "en-GB");
    // names.Generic == "Central European Time"
    // names.Standard == "Central European Standard Time"
    // names.Daylight == "Central European Summer Time"
    var names = TZNames.GetNamesForTimeZone("America/Los_Angeles", "en-US");
    
    names.Generic == "Pacific Time"
    names.Standard == "Pacific Standard Time"
    names.Daylight == "Pacific Daylight Time"