diff options
author | Jan Kotas <jkotas@microsoft.com> | 2018-01-19 13:46:22 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-19 13:46:22 -0800 |
commit | 643a3966300a0d509e99629c3b6056117b89720a (patch) | |
tree | 7d614c48a5b04984161cffae249239d9bb848626 /src | |
parent | 516b973b52d16843c5c7f20c53bd246d62c55d4e (diff) | |
download | coreclr-643a3966300a0d509e99629c3b6056117b89720a.tar.gz coreclr-643a3966300a0d509e99629c3b6056117b89720a.tar.bz2 coreclr-643a3966300a0d509e99629c3b6056117b89720a.zip |
Move platform-neutral part of TimeZone to shared CoreLib partition (#15926)
Prep work for moving the platform specific parts
Diffstat (limited to 'src')
-rw-r--r-- | src/mscorlib/System.Private.CoreLib.csproj | 4 | ||||
-rw-r--r-- | src/mscorlib/shared/System.Private.CoreLib.Shared.projitems | 4 | ||||
-rw-r--r-- | src/mscorlib/shared/System/TimeZoneInfo.AdjustmentRule.cs (renamed from src/mscorlib/src/System/TimeZoneInfo.AdjustmentRule.cs) | 0 | ||||
-rw-r--r-- | src/mscorlib/shared/System/TimeZoneInfo.StringSerializer.cs (renamed from src/mscorlib/src/System/TimeZoneInfo.StringSerializer.cs) | 0 | ||||
-rw-r--r-- | src/mscorlib/shared/System/TimeZoneInfo.TransitionTime.cs (renamed from src/mscorlib/src/System/TimeZoneInfo.TransitionTime.cs) | 0 | ||||
-rw-r--r-- | src/mscorlib/shared/System/TimeZoneInfo.cs (renamed from src/mscorlib/src/System/TimeZoneInfo.cs) | 0 | ||||
-rw-r--r-- | src/mscorlib/src/System/IO/File.cs | 28 | ||||
-rw-r--r-- | src/mscorlib/src/System/TimeZoneInfo.Unix.cs | 68 | ||||
-rw-r--r-- | src/mscorlib/src/System/TimeZoneInfo.Win32.cs | 64 |
9 files changed, 67 insertions, 101 deletions
diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj index 26ca98845a..dcfe620643 100644 --- a/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/mscorlib/System.Private.CoreLib.csproj @@ -347,10 +347,6 @@ <Compile Include="$(BclSourcesRoot)\System\RuntimeHandles.cs" /> <Compile Include="$(BclSourcesRoot)\System\SharedStatics.cs" /> <Compile Include="$(BclSourcesRoot)\System\StubHelpers.cs" /> - <Compile Include="$(BclSourcesRoot)\System\TimeZoneInfo.AdjustmentRule.cs" /> - <Compile Include="$(BclSourcesRoot)\System\TimeZoneInfo.cs" /> - <Compile Include="$(BclSourcesRoot)\System\TimeZoneInfo.StringSerializer.cs" /> - <Compile Include="$(BclSourcesRoot)\System\TimeZoneInfo.TransitionTime.cs" /> <Compile Include="$(BclSourcesRoot)\System\Type.CoreCLR.cs" /> <Compile Include="$(BclSourcesRoot)\System\TypeNameParser.cs" /> <Compile Include="$(BclSourcesRoot)\System\TypedReference.cs" /> diff --git a/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems b/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems index 58e65804e0..e49edfd367 100644 --- a/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems @@ -551,6 +551,10 @@ <Compile Include="$(MSBuildThisFileDirectory)System\ThreadStaticAttribute.cs" /> <Compile Include="$(MSBuildThisFileDirectory)System\TimeoutException.cs" /> <Compile Include="$(MSBuildThisFileDirectory)System\TimeZone.cs" /> + <Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.cs" /> + <Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.AdjustmentRule.cs" /> + <Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.StringSerializer.cs" /> + <Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.TransitionTime.cs" /> <Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneNotFoundException.cs" /> <Compile Include="$(MSBuildThisFileDirectory)System\Tuple.cs" /> <Compile Include="$(MSBuildThisFileDirectory)System\TupleExtensions.cs" /> diff --git a/src/mscorlib/src/System/TimeZoneInfo.AdjustmentRule.cs b/src/mscorlib/shared/System/TimeZoneInfo.AdjustmentRule.cs index 0e949a30ec..0e949a30ec 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.AdjustmentRule.cs +++ b/src/mscorlib/shared/System/TimeZoneInfo.AdjustmentRule.cs diff --git a/src/mscorlib/src/System/TimeZoneInfo.StringSerializer.cs b/src/mscorlib/shared/System/TimeZoneInfo.StringSerializer.cs index e3e9ddbf58..e3e9ddbf58 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.StringSerializer.cs +++ b/src/mscorlib/shared/System/TimeZoneInfo.StringSerializer.cs diff --git a/src/mscorlib/src/System/TimeZoneInfo.TransitionTime.cs b/src/mscorlib/shared/System/TimeZoneInfo.TransitionTime.cs index b93794262c..b93794262c 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.TransitionTime.cs +++ b/src/mscorlib/shared/System/TimeZoneInfo.TransitionTime.cs diff --git a/src/mscorlib/src/System/TimeZoneInfo.cs b/src/mscorlib/shared/System/TimeZoneInfo.cs index f9b5ce872a..f9b5ce872a 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.cs +++ b/src/mscorlib/shared/System/TimeZoneInfo.cs diff --git a/src/mscorlib/src/System/IO/File.cs b/src/mscorlib/src/System/IO/File.cs index 2b2506cd80..6640c6506f 100644 --- a/src/mscorlib/src/System/IO/File.cs +++ b/src/mscorlib/src/System/IO/File.cs @@ -107,34 +107,6 @@ namespace System.IO return bytes; } -#if PLATFORM_UNIX - public static String[] ReadAllLines(String path) - { - if (path == null) - throw new ArgumentNullException(nameof(path)); - if (path.Length == 0) - throw new ArgumentException(SR.Argument_EmptyPath); - - return InternalReadAllLines(path, Encoding.UTF8); - } - - private static String[] InternalReadAllLines(String path, Encoding encoding) - { - Debug.Assert(path != null); - Debug.Assert(encoding != null); - Debug.Assert(path.Length != 0); - - String line; - List<String> lines = new List<String>(); - - using (StreamReader sr = new StreamReader(path, encoding)) - while ((line = sr.ReadLine()) != null) - lines.Add(line); - - return lines.ToArray(); - } -#endif // PLATFORM_UNIX - // Returns 0 on success, otherwise a Win32 error code. Note that // classes should use -1 as the uninitialized state for dataInitialized. internal static int FillAttributeInfo(String path, ref Win32Native.WIN32_FILE_ATTRIBUTE_DATA data, bool tryagain, bool returnErrorOnNotFound) diff --git a/src/mscorlib/src/System/TimeZoneInfo.Unix.cs b/src/mscorlib/src/System/TimeZoneInfo.Unix.cs index 504167b3e0..b1090b8c02 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.Unix.cs +++ b/src/mscorlib/src/System/TimeZoneInfo.Unix.cs @@ -241,54 +241,50 @@ namespace System /// </remarks> private static List<string> GetTimeZoneIds(string timeZoneDirectory) { - string[] zoneTabFileLines = null; - try - { - zoneTabFileLines = File.ReadAllLines(Path.Combine(timeZoneDirectory, ZoneTabFileName)); - } - catch (IOException) { } - catch (UnauthorizedAccessException) { } + List<string> timeZoneIds = new List<string>(); - if (zoneTabFileLines == null) - { - return new List<string>(); - } - - List<string> timeZoneIds = new List<string>(zoneTabFileLines.Length); - - foreach (string zoneTabFileLine in zoneTabFileLines) + try { - if (!string.IsNullOrEmpty(zoneTabFileLine) && zoneTabFileLine[0] != '#') + using (StreamReader sr = new StreamReader(Path.Combine(timeZoneDirectory, ZoneTabFileName), Encoding.UTF8)) { - // the format of the line is "country-code \t coordinates \t TimeZone Id \t comments" - - int firstTabIndex = zoneTabFileLine.IndexOf('\t'); - if (firstTabIndex != -1) + string zoneTabFileLine; + while ((zoneTabFileLine = sr.ReadLine()) != null) { - int secondTabIndex = zoneTabFileLine.IndexOf('\t', firstTabIndex + 1); - if (secondTabIndex != -1) + if (!string.IsNullOrEmpty(zoneTabFileLine) && zoneTabFileLine[0] != '#') { - string timeZoneId; - int startIndex = secondTabIndex + 1; - int thirdTabIndex = zoneTabFileLine.IndexOf('\t', startIndex); - if (thirdTabIndex != -1) - { - int length = thirdTabIndex - startIndex; - timeZoneId = zoneTabFileLine.Substring(startIndex, length); - } - else - { - timeZoneId = zoneTabFileLine.Substring(startIndex); - } + // the format of the line is "country-code \t coordinates \t TimeZone Id \t comments" - if (!string.IsNullOrEmpty(timeZoneId)) + int firstTabIndex = zoneTabFileLine.IndexOf('\t'); + if (firstTabIndex != -1) { - timeZoneIds.Add(timeZoneId); + int secondTabIndex = zoneTabFileLine.IndexOf('\t', firstTabIndex + 1); + if (secondTabIndex != -1) + { + string timeZoneId; + int startIndex = secondTabIndex + 1; + int thirdTabIndex = zoneTabFileLine.IndexOf('\t', startIndex); + if (thirdTabIndex != -1) + { + int length = thirdTabIndex - startIndex; + timeZoneId = zoneTabFileLine.Substring(startIndex, length); + } + else + { + timeZoneId = zoneTabFileLine.Substring(startIndex); + } + + if (!string.IsNullOrEmpty(timeZoneId)) + { + timeZoneIds.Add(timeZoneId); + } + } } } } } } + catch (IOException) { } + catch (UnauthorizedAccessException) { } return timeZoneIds; } diff --git a/src/mscorlib/src/System/TimeZoneInfo.Win32.cs b/src/mscorlib/src/System/TimeZoneInfo.Win32.cs index a2f43917ee..08fc921751 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.Win32.cs +++ b/src/mscorlib/src/System/TimeZoneInfo.Win32.cs @@ -9,8 +9,12 @@ using System.IO; using System.Security; using System.Text; using System.Threading; + using Microsoft.Win32; using Microsoft.Win32.SafeHandles; +using REG_TZI_FORMAT = Microsoft.Win32.Win32Native.RegistryTimeZoneInformation; +using TIME_ZONE_INFORMATION = Microsoft.Win32.Win32Native.TimeZoneInformation; +using TIME_DYNAMIC_ZONE_INFORMATION = Microsoft.Win32.Win32Native.DynamicTimeZoneInformation; namespace System { @@ -38,7 +42,7 @@ namespace System private static TimeZoneInfo GetCurrentOneYearLocal() { // load the data from the OS - Win32Native.TimeZoneInformation timeZoneInformation; + TIME_ZONE_INFORMATION timeZoneInformation; long result = UnsafeNativeMethods.GetTimeZoneInformation(out timeZoneInformation); return result == Win32Native.TIME_ZONE_ID_INVALID ? CreateCustomTimeZone(LocalId, TimeSpan.Zero, LocalId, LocalId) : @@ -107,7 +111,7 @@ namespace System } } - private TimeZoneInfo(Win32Native.TimeZoneInformation zone, bool dstDisabled) + private TimeZoneInfo(in TIME_ZONE_INFORMATION zone, bool dstDisabled) { if (string.IsNullOrEmpty(zone.StandardName)) { @@ -122,7 +126,7 @@ namespace System if (!dstDisabled) { // only create the adjustment rule if DST is enabled - Win32Native.RegistryTimeZoneInformation regZone = new Win32Native.RegistryTimeZoneInformation(zone); + REG_TZI_FORMAT regZone = new REG_TZI_FORMAT(zone); AdjustmentRule rule = CreateAdjustmentRuleFromTimeZoneInformation(regZone, DateTime.MinValue.Date, DateTime.MaxValue.Date, zone.Bias); if (rule != null) { @@ -142,7 +146,7 @@ namespace System /// This check returns true when the DaylightDate == StandardDate. /// This check is only meant to be used for "Local". /// </summary> - private static bool CheckDaylightSavingTimeNotSupported(Win32Native.TimeZoneInformation timeZone) => + private static bool CheckDaylightSavingTimeNotSupported(in TIME_ZONE_INFORMATION timeZone) => timeZone.DaylightDate.Year == timeZone.StandardDate.Year && timeZone.DaylightDate.Month == timeZone.StandardDate.Month && timeZone.DaylightDate.DayOfWeek == timeZone.StandardDate.DayOfWeek && @@ -153,9 +157,9 @@ namespace System timeZone.DaylightDate.Milliseconds == timeZone.StandardDate.Milliseconds; /// <summary> - /// Converts a Win32Native.RegistryTimeZoneInformation (REG_TZI_FORMAT struct) to an AdjustmentRule. + /// Converts a REG_TZI_FORMAT struct to an AdjustmentRule. /// </summary> - private static AdjustmentRule CreateAdjustmentRuleFromTimeZoneInformation(Win32Native.RegistryTimeZoneInformation timeZoneInformation, DateTime startDate, DateTime endDate, int defaultBaseUtcOffset) + private static AdjustmentRule CreateAdjustmentRuleFromTimeZoneInformation(in REG_TZI_FORMAT timeZoneInformation, DateTime startDate, DateTime endDate, int defaultBaseUtcOffset) { bool supportsDst = timeZoneInformation.StandardDate.Month != 0; @@ -212,7 +216,7 @@ namespace System /// Helper function that searches the registry for a time zone entry /// that matches the TimeZoneInformation struct. /// </summary> - private static string FindIdFromTimeZoneInformation(Win32Native.TimeZoneInformation timeZone, out bool dstDisabled) + private static string FindIdFromTimeZoneInformation(in TIME_ZONE_INFORMATION timeZone, out bool dstDisabled) { dstDisabled = false; @@ -245,22 +249,20 @@ namespace System { Debug.Assert(Monitor.IsEntered(cachedData)); - string id = null; - // // Try using the "kernel32!GetDynamicTimeZoneInformation" API to get the "id" // - var dynamicTimeZoneInformation = new Win32Native.DynamicTimeZoneInformation(); + var dynamicTimeZoneInformation = new TIME_DYNAMIC_ZONE_INFORMATION(); // call kernel32!GetDynamicTimeZoneInformation... - long result = UnsafeNativeMethods.GetDynamicTimeZoneInformation(out dynamicTimeZoneInformation); + int result = UnsafeNativeMethods.GetDynamicTimeZoneInformation(out dynamicTimeZoneInformation); if (result == Win32Native.TIME_ZONE_ID_INVALID) { // return a dummy entry return CreateCustomTimeZone(LocalId, TimeSpan.Zero, LocalId, LocalId); } - var timeZoneInformation = new Win32Native.TimeZoneInformation(dynamicTimeZoneInformation); + var timeZoneInformation = new TIME_ZONE_INFORMATION(dynamicTimeZoneInformation); bool dstDisabled = dynamicTimeZoneInformation.DynamicDaylightTimeDisabled; @@ -278,7 +280,7 @@ namespace System } // the key name was not returned or it pointed to a bogus entry - search for the entry ourselves - id = FindIdFromTimeZoneInformation(timeZoneInformation, out dstDisabled); + string id = FindIdFromTimeZoneInformation(timeZoneInformation, out dstDisabled); if (id != null) { @@ -299,9 +301,9 @@ namespace System /// <summary> /// Helper function used by 'GetLocalTimeZone()' - this function wraps a bunch of /// try/catch logic for handling the TimeZoneInfo private constructor that takes - /// a Win32Native.TimeZoneInformation structure. + /// a TIME_ZONE_INFORMATION structure. /// </summary> - private static TimeZoneInfo GetLocalTimeZoneFromWin32Data(Win32Native.TimeZoneInformation timeZoneInformation, bool dstDisabled) + private static TimeZoneInfo GetLocalTimeZoneFromWin32Data(in TIME_ZONE_INFORMATION timeZoneInformation, bool dstDisabled) { // first try to create the TimeZoneInfo with the original 'dstDisabled' flag try @@ -407,11 +409,11 @@ namespace System } /// <summary> - /// Converts a Win32Native.RegistryTimeZoneInformation (REG_TZI_FORMAT struct) to a TransitionTime + /// Converts a REG_TZI_FORMAT struct to a TransitionTime /// - When the argument 'readStart' is true the corresponding daylightTransitionTimeStart field is read /// - When the argument 'readStart' is false the corresponding dayightTransitionTimeEnd field is read /// </summary> - private static bool TransitionTimeFromTimeZoneInformation(Win32Native.RegistryTimeZoneInformation timeZoneInformation, out TransitionTime transitionTime, bool readStartDate) + private static bool TransitionTimeFromTimeZoneInformation(in REG_TZI_FORMAT timeZoneInformation, out TransitionTime transitionTime, bool readStartDate) { // // SYSTEMTIME - @@ -526,10 +528,10 @@ namespace System /// <summary> /// Helper function that takes: /// 1. A string representing a <time_zone_name> registry key name. - /// 2. A RegistryTimeZoneInformation struct containing the default rule. + /// 2. A REG_TZI_FORMAT struct containing the default rule. /// 3. An AdjustmentRule[] out-parameter. /// </summary> - private static bool TryCreateAdjustmentRules(string id, Win32Native.RegistryTimeZoneInformation defaultTimeZoneInformation, out AdjustmentRule[] rules, out Exception e, int defaultBaseUtcOffset) + private static bool TryCreateAdjustmentRules(string id, in REG_TZI_FORMAT defaultTimeZoneInformation, out AdjustmentRule[] rules, out Exception e, int defaultBaseUtcOffset) { e = null; @@ -553,11 +555,8 @@ namespace System // Last year in the table. If the current year is greater than this value, // this entry will be used for DST boundaries" // * "<year1>" REG_BINARY REG_TZI_FORMAT - // See Win32Native.RegistryTimeZoneInformation // * "<year2>" REG_BINARY REG_TZI_FORMAT - // See Win32Native.RegistryTimeZoneInformation // * "<year3>" REG_BINARY REG_TZI_FORMAT - // See Win32Native.RegistryTimeZoneInformation // using (RegistryKey dynamicKey = Registry.LocalMachine.OpenSubKey(TimeZonesRegistryHive + "\\" + id + "\\Dynamic DST", writable: false)) { @@ -587,14 +586,14 @@ namespace System } // read the first year entry - Win32Native.RegistryTimeZoneInformation dtzi; + REG_TZI_FORMAT dtzi; byte[] regValue = dynamicKey.GetValue(first.ToString(CultureInfo.InvariantCulture), null, RegistryValueOptions.None) as byte[]; if (regValue == null || regValue.Length != RegByteLength) { rules = null; return false; } - dtzi = new Win32Native.RegistryTimeZoneInformation(regValue); + dtzi = new REG_TZI_FORMAT(regValue); if (first == last) { @@ -627,7 +626,7 @@ namespace System rules = null; return false; } - dtzi = new Win32Native.RegistryTimeZoneInformation(regValue); + dtzi = new REG_TZI_FORMAT(regValue); AdjustmentRule middleRule = CreateAdjustmentRuleFromTimeZoneInformation( dtzi, new DateTime(i, 1, 1), // January 01, <Year> @@ -642,7 +641,7 @@ namespace System // read the last year entry regValue = dynamicKey.GetValue(last.ToString(CultureInfo.InvariantCulture), null, RegistryValueOptions.None) as byte[]; - dtzi = new Win32Native.RegistryTimeZoneInformation(regValue); + dtzi = new REG_TZI_FORMAT(regValue); if (regValue == null || regValue.Length != RegByteLength) { rules = null; @@ -693,7 +692,7 @@ namespace System /// Helper function that compares the StandardBias and StandardDate portion a /// TimeZoneInformation struct to a time zone registry entry. /// </summary> - private static bool TryCompareStandardDate(Win32Native.TimeZoneInformation timeZone, Win32Native.RegistryTimeZoneInformation registryTimeZoneInfo) => + private static bool TryCompareStandardDate(in TIME_ZONE_INFORMATION timeZone, in REG_TZI_FORMAT registryTimeZoneInfo) => timeZone.Bias == registryTimeZoneInfo.Bias && timeZone.StandardBias == registryTimeZoneInfo.StandardBias && timeZone.StandardDate.Year == registryTimeZoneInfo.StandardDate.Year && @@ -708,7 +707,7 @@ namespace System /// <summary> /// Helper function that compares a TimeZoneInformation struct to a time zone registry entry. /// </summary> - private static bool TryCompareTimeZoneInformationToRegistry(Win32Native.TimeZoneInformation timeZone, string id, out bool dstDisabled) + private static bool TryCompareTimeZoneInformationToRegistry(in TIME_ZONE_INFORMATION timeZone, string id, out bool dstDisabled) { dstDisabled = false; @@ -719,10 +718,10 @@ namespace System return false; } - Win32Native.RegistryTimeZoneInformation registryTimeZoneInfo; + REG_TZI_FORMAT registryTimeZoneInfo; byte[] regValue = key.GetValue(TimeZoneInfoValue, null, RegistryValueOptions.None) as byte[]; if (regValue == null || regValue.Length != RegByteLength) return false; - registryTimeZoneInfo = new Win32Native.RegistryTimeZoneInformation(regValue); + registryTimeZoneInfo = new REG_TZI_FORMAT(regValue); // // first compare the bias and standard date information between the data from the Win32 API @@ -955,7 +954,6 @@ namespace System // Indirect string to localized resource for the Display, // add "%windir%\system32\" after "@" // * TZI, REG_BINARY REG_TZI_FORMAT - // See Win32Native.RegistryTimeZoneInformation // using (RegistryKey key = Registry.LocalMachine.OpenSubKey(TimeZonesRegistryHive + "\\" + id, writable: false)) { @@ -965,7 +963,7 @@ namespace System return TimeZoneInfoResult.TimeZoneNotFoundException; } - Win32Native.RegistryTimeZoneInformation defaultTimeZoneInformation; + REG_TZI_FORMAT defaultTimeZoneInformation; byte[] regValue = key.GetValue(TimeZoneInfoValue, null, RegistryValueOptions.None) as byte[]; if (regValue == null || regValue.Length != RegByteLength) { @@ -973,7 +971,7 @@ namespace System value = null; return TimeZoneInfoResult.InvalidTimeZoneException; } - defaultTimeZoneInformation = new Win32Native.RegistryTimeZoneInformation(regValue); + defaultTimeZoneInformation = new REG_TZI_FORMAT(regValue); AdjustmentRule[] adjustmentRules; if (!TryCreateAdjustmentRules(id, defaultTimeZoneInformation, out adjustmentRules, out e, defaultTimeZoneInformation.Bias)) |