Skip to content

Commit 2a69804

Browse files
feat: deprecate GetAbbreviationsForTimeZone (#133)
1 parent 9a1f490 commit 2a69804

File tree

3 files changed

+13
-121
lines changed

3 files changed

+13
-121
lines changed

README.md

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Read [this blog post](https://codeofmatt.com/localized-time-zone-names-in-net/)
99
Note that if you are running .NET 6+ on Linux or macOS, the built-in names now stem from ICU and thus this library is no longer needed.
1010
See [the .NET blog post](https://devblogs.microsoft.com/dotnet/date-time-and-time-zone-enhancements-in-net-6/#time-zone-display-names-on-linux-and-macos) for more details.
1111

12+
**NOTE**:
13+
Methods for retrieving localized time zone abbreviations have been deprecated, as the source data for abbreviations is generally unreliable.
14+
1215
Nuget Installation
1316
=============================================================================
1417
```powershell
@@ -62,29 +65,6 @@ names.Standard == "Central European Standard Time"
6265
names.Daylight == "Central European Summer Time"
6366
```
6467

65-
### GetAbbreviationsForTimeZone
66-
Look up the localized abbreviations for a specific time zone:
67-
```csharp
68-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("America/Los_Angeles", "en-US");
69-
70-
abbreviations.Generic == "PT"
71-
abbreviations.Standard == "PST"
72-
abbreviations.Daylight == "PDT"
73-
```
74-
75-
You can pass a Windows time zone id instead, if you like:
76-
```csharp
77-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("Romance Standard Time", "en-GB");
78-
79-
names.Generic == "CET"
80-
abbreviations.Standard == "CET"
81-
abbreviations.Daylight == "CEST"
82-
```
83-
84-
**Note:** Time zone abbreviations are sometimes inconsistent, and are not necessarily
85-
localized correctly for every time zone. In most cases, you should use abbreviations
86-
for end-user display output only. Do not attempt to use abbreviations when parsing input.
87-
8868
## Methods for listing time zones
8969

9070
### GetDisplayNames
@@ -246,7 +226,7 @@ TODO: Add examples for this method.
246226

247227
### GetFixedTimeZoneAbbreviations
248228

249-
Gets the same list of zones as `GetFixedTimeZoneIds`, but includes localized abbreviations.
229+
Gets the same list of zones as `GetFixedTimeZoneIds`, but includes abbreviations.
250230

251231
TODO: Add examples for this method.
252232

src/TimeZoneNames/TZNames.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,15 @@ public static TimeZoneValues GetNamesForTimeZone(string timeZoneId, string langu
210210
/// <param name="timeZoneId">An IANA or Windows time zone identifier.</param>
211211
/// <param name="languageCode">The IETF language tag (culture code) to use when localizing the abbreviations.</param>
212212
/// <returns>A <see cref="TimeZoneValues"/> object containing the localized generic, standard, and daylight abbreviations.</returns>
213+
[Obsolete("WARNING! Time zone abbreviations are inconsistent and not well-defined in the source data. This method will be removed in a future release.")]
213214
public static TimeZoneValues GetAbbreviationsForTimeZone(string timeZoneId, string languageCode)
214215
{
215216
var langKey = GetLanguageKey(languageCode);
216217
if (langKey == null)
217218
{
218219
throw new ArgumentException("Invalid Language Code", nameof(languageCode));
219220
}
220-
221+
221222
if (TZConvert.TryWindowsToIana(timeZoneId, out var ianaId))
222223
{
223224
timeZoneId = ianaId;
@@ -298,7 +299,7 @@ public static IDictionary<string, string> GetDisplayNames(string languageCode, b
298299
{
299300
throw new ArgumentException("Invalid Language Code", nameof(languageCode));
300301
}
301-
302+
302303
var displayNames = Data.DisplayNames[langKey]
303304
.Where(x => !TimeZoneData.ObsoleteWindowsZones.Contains(x.Key))
304305
.ToList();
@@ -307,7 +308,7 @@ public static IDictionary<string, string> GetDisplayNames(string languageCode, b
307308
{
308309
return displayNames.ToOrderedDictionary(StringComparer.OrdinalIgnoreCase);
309310
}
310-
311+
311312
var languageCodeParts = languageCode.Split('_', '-');
312313
var territoryCode = languageCodeParts.Length < 2 ? "001" : languageCodeParts[1];
313314
return displayNames.ToOrderedDictionary(
@@ -334,7 +335,7 @@ public static ICollection<string> GetLanguageCodes()
334335
public static ICollection<string> GetLanguageCodes(bool forDisplayNames)
335336
{
336337
var keys = forDisplayNames
337-
? (IEnumerable<string>) Data.DisplayNames.Keys
338+
? (IEnumerable<string>)Data.DisplayNames.Keys
338339
: Data.CldrLanguageData.Keys;
339340

340341
return keys.OrderBy(x => x).ToList();
@@ -373,7 +374,7 @@ private static string GetLanguageKey(string languageCode, bool forDisplayNames =
373374

374375
if (key == null)
375376
{
376-
var keys = forDisplayNames ? (IEnumerable<string>) Data.DisplayNames.Keys : Data.CldrLanguageData.Keys;
377+
var keys = forDisplayNames ? (IEnumerable<string>)Data.DisplayNames.Keys : Data.CldrLanguageData.Keys;
377378
key = keys.FirstOrDefault(x =>
378379
x.Split('_')[0].Equals(languageCode.Split('-', '_')[0], StringComparison.OrdinalIgnoreCase));
379380

@@ -438,7 +439,7 @@ private static TimeZoneValues GetNames(string timeZoneId, string languageKey, bo
438439

439440
if (abbreviations && (timeZoneId == "Etc/GMT" || timeZoneId == "Etc/UTC"))
440441
{
441-
return new TimeZoneValues {Generic = "UTC", Standard = "UTC", Daylight = "UTC"};
442+
return new TimeZoneValues { Generic = "UTC", Standard = "UTC", Daylight = "UTC" };
442443
}
443444

444445
var metaZone = GetMetazone(timeZoneId);
@@ -626,7 +627,8 @@ private static TimeZoneValues GetNames(string timeZoneId, string languageKey, bo
626627
}
627628

628629
// last chance to make a generic name if it's missing
629-
if (values.Generic == null) {
630+
if (values.Generic == null)
631+
{
630632
values.Generic = values.Standard;
631633
}
632634

test/TimeZoneNames.Tests/TimeZoneNamesTest.cs

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ public void Can_Get_Names_For_US_Pacific()
2222
Assert.Equal("Pacific Daylight Time", names.Daylight);
2323
}
2424

25-
[Fact]
26-
public void Can_Get_Abbreviations_For_US_Pacific()
27-
{
28-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("America/Los_Angeles", "en-US");
29-
30-
Assert.Equal("PT", abbreviations.Generic);
31-
Assert.Equal("PST", abbreviations.Standard);
32-
Assert.Equal("PDT", abbreviations.Daylight);
33-
}
34-
3525
[Fact]
3626
public void Can_Get_French_Names_For_US_Pacific()
3727
{
@@ -42,16 +32,6 @@ public void Can_Get_French_Names_For_US_Pacific()
4232
Assert.Equal("heure avancée du Pacifique", names.Daylight);
4333
}
4434

45-
[Fact]
46-
public void Can_Get_French_Abbreviations_For_US_Pacific()
47-
{
48-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("America/Los_Angeles", "fr-CA");
49-
50-
Assert.Equal("HP", abbreviations.Generic);
51-
Assert.Equal("HNP", abbreviations.Standard);
52-
Assert.Equal("HAP", abbreviations.Daylight);
53-
}
54-
5535
[Fact]
5636
public void Can_Get_Names_For_US_Arizona()
5737
{
@@ -73,26 +53,6 @@ public void Can_Get_Names_For_UK()
7353
Assert.Equal("British Summer Time", names.Daylight);
7454
}
7555

76-
[Fact]
77-
public void Can_Get_Abbreviations_For_UK()
78-
{
79-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("Europe/London", "en-US");
80-
81-
Assert.Null(abbreviations.Generic);
82-
Assert.Equal("GMT", abbreviations.Standard);
83-
Assert.Equal("BST", abbreviations.Daylight);
84-
}
85-
86-
[Fact]
87-
public void Can_Get_Abbreviations_For_Central_Europe()
88-
{
89-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("Central European Standard Time", "en-US");
90-
91-
Assert.Equal("CET", abbreviations.Generic);
92-
Assert.Equal("CET", abbreviations.Standard);
93-
Assert.Equal("CEST", abbreviations.Daylight);
94-
}
95-
9656
[Fact]
9757
public void Can_Get_Names_For_IE()
9858
{
@@ -103,16 +63,6 @@ public void Can_Get_Names_For_IE()
10363
Assert.Equal("Irish Standard Time", names.Daylight);
10464
}
10565

106-
[Fact]
107-
public void Can_Get_Abbreviations_For_IE()
108-
{
109-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("Europe/Dublin", "en-US");
110-
111-
Assert.Null(abbreviations.Generic);
112-
Assert.Equal("GMT", abbreviations.Standard);
113-
Assert.Equal("IST", abbreviations.Daylight);
114-
}
115-
11666
[Fact]
11767
public void Can_Get_Names_For_IN1()
11868
{
@@ -123,16 +73,6 @@ public void Can_Get_Names_For_IN1()
12373
Assert.Equal("India Standard Time", names.Daylight);
12474
}
12575

126-
[Fact]
127-
public void Can_Get_Abbreviations_For_IN1()
128-
{
129-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("Asia/Calcutta", "en-US");
130-
131-
Assert.Equal("IST", abbreviations.Generic);
132-
Assert.Equal("IST", abbreviations.Standard);
133-
Assert.Equal("IST", abbreviations.Daylight);
134-
}
135-
13676
[Fact]
13777
public void Can_Get_Names_For_IN2()
13878
{
@@ -143,16 +83,6 @@ public void Can_Get_Names_For_IN2()
14383
Assert.Equal("India Standard Time", names.Daylight);
14484
}
14585

146-
[Fact]
147-
public void Can_Get_Abbreviations_For_IN2()
148-
{
149-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("Asia/Kolkata", "en-US");
150-
151-
Assert.Equal("IST", abbreviations.Generic);
152-
Assert.Equal("IST", abbreviations.Standard);
153-
Assert.Equal("IST", abbreviations.Daylight);
154-
}
155-
15686
[Fact]
15787
public void Can_Get_Names_For_CN()
15888
{
@@ -173,16 +103,6 @@ public void Can_Get_Names_For_CN()
173103
// Assert.Equal("Urumqi Daylight Time", names.Daylight);
174104
//}
175105

176-
[Fact]
177-
public void Can_Get_Abbreviations_For_Sao_Tome()
178-
{
179-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("Sao Tome Standard Time", "en-GB");
180-
181-
Assert.Equal("GMT", abbreviations.Generic);
182-
Assert.Equal("GMT", abbreviations.Standard);
183-
Assert.Equal("GMT", abbreviations.Daylight);
184-
}
185-
186106
[Fact]
187107
public void Can_Get_Names_For_UTC()
188108
{
@@ -225,16 +145,6 @@ public void Can_Get_Names_For_Windows_Timezone()
225145
Assert.Equal("Eastern Daylight Time", names.Daylight);
226146
}
227147

228-
[Fact]
229-
public void Can_Get_Abbreviations_For_Windows_Timezone()
230-
{
231-
var abbreviations = TZNames.GetAbbreviationsForTimeZone("AUS Eastern Standard Time", "en-US");
232-
233-
Assert.Equal("AET", abbreviations.Generic);
234-
Assert.Equal("AEST", abbreviations.Standard);
235-
Assert.Equal("AEDT", abbreviations.Daylight);
236-
}
237-
238148
//[Fact]
239149
//public void Can_Get_Names_For_CA_Pacific_From_MX()
240150
//{

0 commit comments

Comments
 (0)