From ee32973394c3b328dcf21952ab7aebcc25954839 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Mon, 26 Feb 2018 12:31:21 -0800 Subject: [PATCH] Support .NET Framework 4.0 and 3.5 --- README.md | 2 +- src/TimeZoneNames/TZNames.cs | 43 ++++++++++++++++++++++++-- src/TimeZoneNames/TimeZoneData.cs | 5 +++ src/TimeZoneNames/TimeZoneNames.csproj | 12 ++++--- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c756b27..0b1e8da 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Nuget Installation PM> Install-Package TimeZoneNames ``` -This library is targeting .NET Standard 1.1+, .NET Standard 2.0+, and .NET Framework 4.5+. +This library is targeting .NET Standard 2.0, 1.1, and .NET Framework 4.5, 4.0 and 3.5. See the [.NET Standard Platform Support Matrix](https://docs.microsoft.com/en-us/dotnet/articles/standard/library) for further details. Demo diff --git a/src/TimeZoneNames/TZNames.cs b/src/TimeZoneNames/TZNames.cs index 1c5e37a..4032f64 100644 --- a/src/TimeZoneNames/TZNames.cs +++ b/src/TimeZoneNames/TZNames.cs @@ -1,10 +1,13 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; using System.Linq; using TimeZoneConverter; +#if !NET35 +using System.Collections.Concurrent; +#endif + namespace TimeZoneNames { /// @@ -14,8 +17,15 @@ public static class TZNames { private static readonly TimeZoneData Data = TimeZoneData.Load(); +#if NET35 + private static readonly Dictionary> Comparers = + new Dictionary>(StringComparer.OrdinalIgnoreCase); + + private static readonly object SyncLock = new object(); +#else private static readonly ConcurrentDictionary> Comparers = new ConcurrentDictionary>(StringComparer.OrdinalIgnoreCase); +#endif /// /// Gets an array of IANA time zone identifiers for a specific country. @@ -158,7 +168,11 @@ private static IDictionary GetFixedTimeZoneNames(string language private static string AppendCity(this string name, string city) { +#if NET35 + return string.IsNullOrEmpty(city.Trim()) ? name : $"{name} ({city})"; +#else return string.IsNullOrWhiteSpace(city) ? name : $"{name} ({city})"; +#endif } /// @@ -229,15 +243,38 @@ public static ICollection GetLanguageCodes() private static IComparer GetComparer(string langKey) { + +#if NET35 + if (Comparers.TryGetValue(langKey, out var comparer)) + { + return comparer; + } + + lock (SyncLock) + { + if (!Comparers.TryGetValue(langKey, out comparer)) + { + var culture = new CultureInfo(langKey.Replace('_', '-')); + comparer = StringComparer.Create(culture, true); + } + + return comparer; + } + +#elif NETSTANDARD1_1 return Comparers.GetOrAdd(langKey, key => { var culture = new CultureInfo(langKey.Replace('_', '-')); -#if NETSTANDARD1_1 return new CultureAwareStringComparer(culture, CompareOptions.IgnoreCase); + }); + #else + return Comparers.GetOrAdd(langKey, key => + { + var culture = new CultureInfo(langKey.Replace('_', '-')); return StringComparer.Create(culture, true); -#endif }); +#endif } private static string GetLanguageKey(string languageCode) diff --git a/src/TimeZoneNames/TimeZoneData.cs b/src/TimeZoneNames/TimeZoneData.cs index d7d11e2..2f18156 100644 --- a/src/TimeZoneNames/TimeZoneData.cs +++ b/src/TimeZoneNames/TimeZoneData.cs @@ -27,7 +27,12 @@ internal class TimeZoneData [SecuritySafeCritical] public static TimeZoneData Load() { + +#if NET35 || NET40 + var assembly = typeof(TimeZoneData).Assembly; +#else var assembly = typeof(TimeZoneData).GetTypeInfo().Assembly; +#endif using (var compressedStream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.data.json.gz")) using (var stream = new GZipStream(compressedStream, CompressionMode.Decompress)) using (var reader = new StreamReader(stream)) diff --git a/src/TimeZoneNames/TimeZoneNames.csproj b/src/TimeZoneNames/TimeZoneNames.csproj index 84ee426..81d9922 100644 --- a/src/TimeZoneNames/TimeZoneNames.csproj +++ b/src/TimeZoneNames/TimeZoneNames.csproj @@ -3,14 +3,14 @@ Provides localized time zone names. Matt Johnson - netstandard2.0;netstandard1.1;net45 + netstandard2.0;netstandard1.1;net45;net40;net35 TimeZoneNames timezone;time;zone;time zone;iana;tzdb;olson;timezoneinfo;globalization;international;localization https://github.com/mj1856/TimeZoneNames https://raw.githubusercontent.com/mj1856/TimeZoneNames/master/LICENSE True True - 3.2.0 + 3.3.0 @@ -41,12 +41,16 @@ - + + + $(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client + + - +