diff options
author | Marcos Henrich <marcos.henrich@xamarin.com> | 2015-04-02 17:52:00 +0300 |
---|---|---|
committer | Marcos Henrich <marcos.henrich@xamarin.com> | 2015-04-03 12:27:51 +0300 |
commit | 4420113b52fb5a772c8eea291dc1c77173c3e6a3 (patch) | |
tree | 49cf842b643f6f323ad0d17e5c30ac9db5ecd2ce /mcs/class/corlib | |
parent | f7c051a73c31b842bf9695099d365b050372380c (diff) |
[corlib] Removed referencesource duplicated code.
Diffstat (limited to 'mcs/class/corlib')
-rw-r--r-- | mcs/class/corlib/System/TimeZoneInfo.cs | 370 |
1 files changed, 43 insertions, 327 deletions
diff --git a/mcs/class/corlib/System/TimeZoneInfo.cs b/mcs/class/corlib/System/TimeZoneInfo.cs index 34a5b373365..09277b45262 100644 --- a/mcs/class/corlib/System/TimeZoneInfo.cs +++ b/mcs/class/corlib/System/TimeZoneInfo.cs @@ -867,6 +867,23 @@ namespace System throw new NotImplementedException (); } + public bool IsInvalidTime (DateTime dateTime) + { + if (dateTime.Kind == DateTimeKind.Utc) + return false; + if (dateTime.Kind == DateTimeKind.Local && this != Local) + return false; + + AdjustmentRule rule = GetApplicableRule (dateTime); + if (rule != null) { + DateTime tpoint = TransitionPoint (rule.DaylightTransitionStart, dateTime.Year); + if (dateTime >= tpoint && dateTime < tpoint + rule.DaylightDelta) + return true; + } + + return false; + } + void IDeserializationCallback.OnDeserialization (object sender) { try { @@ -1276,333 +1293,32 @@ namespace System } #region reference sources - // Shortcut for TimeZoneInfo.Local.GetUtcOffset - internal static TimeSpan GetLocalUtcOffset(DateTime dateTime, TimeZoneInfoOptions flags) - { - bool dst; - return Local.GetUtcOffset (dateTime, out dst); - } - - internal TimeSpan GetUtcOffset(DateTime dateTime, TimeZoneInfoOptions flags) - { - bool dst; - return GetUtcOffset (dateTime, out dst); - } - - // used by GetUtcOffsetFromUtc (DateTime.Now, DateTime.ToLocalTime) for max/min whole-day range checks - private static DateTime s_maxDateOnly = new DateTime(9999, 12, 31); - private static DateTime s_minDateOnly = new DateTime(1, 1, 2); - - static internal TimeSpan GetUtcOffsetFromUtc (DateTime time, TimeZoneInfo zone, out Boolean isDaylightSavings, out Boolean isAmbiguousLocalDst) - { - isDaylightSavings = false; - isAmbiguousLocalDst = false; - TimeSpan baseOffset = zone.BaseUtcOffset; - Int32 year; - AdjustmentRule rule; - - if (time > s_maxDateOnly) { - rule = zone.GetAdjustmentRuleForTime(DateTime.MaxValue); - year = 9999; - } - else if (time < s_minDateOnly) { - rule = zone.GetAdjustmentRuleForTime(DateTime.MinValue); - year = 1; - } - else { - DateTime targetTime = time + baseOffset; - year = time.Year; - rule = zone.GetAdjustmentRuleForTime(targetTime); - } - - if (rule != null) { - isDaylightSavings = GetIsDaylightSavingsFromUtc(time, year, zone.baseUtcOffset, rule, out isAmbiguousLocalDst); - baseOffset += (isDaylightSavings ? rule.DaylightDelta : TimeSpan.Zero /* */); - } - - return baseOffset; - } - - // assumes dateTime is in the current time zone's time - private AdjustmentRule GetAdjustmentRuleForTime(DateTime dateTime) { - if (adjustmentRules == null || adjustmentRules.Length == 0) { - return null; - } - -#if WINXP_AND_WIN2K3_SUPPORT - // On pre-Vista versions of Windows if you run "cmd /c date" or "cmd /c time" to update the system time - // the operating system doesn't pick up the correct time zone adjustment rule (it stays on the currently loaded rule). - // We need to use the OS API data in this scenario instead of the loaded adjustment rules from the registry for - // consistency. Otherwise DateTime.Now might not match the time displayed in the system tray. - if (!Environment.IsWindowsVistaOrAbove && s_cachedData.GetCorrespondingKind(this) == DateTimeKind.Local) { - return s_cachedData.GetOneYearLocalFromLocal(dateTime.Year).rule; - } -#endif - // Only check the whole-date portion of the dateTime - - // This is because the AdjustmentRule DateStart & DateEnd are stored as - // Date-only values {4/2/2006 - 10/28/2006} but actually represent the - // time span {4/2/2006@00:00:00.00000 - 10/28/2006@23:59:59.99999} - DateTime date = dateTime.Date; - - for (int i = 0; i < adjustmentRules.Length; i++) { - if (adjustmentRules[i].DateStart <= date && adjustmentRules[i].DateEnd >= date) { - return adjustmentRules[i]; - } - } - - return null; - } - - // - // GetIsDaylightSavingsFromUtc - - // - // Helper function that checks if a given dateTime is in Daylight Saving Time (DST) - // This function assumes the dateTime is in UTC and AdjustmentRule is in a different time zone - // - static private Boolean GetIsDaylightSavingsFromUtc(DateTime time, Int32 Year, TimeSpan utc, AdjustmentRule rule, out Boolean isAmbiguousLocalDst) { - isAmbiguousLocalDst = false; - - if (rule == null) { - return false; - } - - // Get the daylight changes for the year of the specified time. - TimeSpan offset = utc; /* */ - DaylightTime daylightTime = GetDaylightTime(Year, rule); - - // The start and end times represent the range of universal times that are in DST for that year. - // Within that there is an ambiguous hour, usually right at the end, but at the beginning in - // the unusual case of a negative daylight savings delta. - DateTime startTime = daylightTime.Start - offset; - DateTime endTime = daylightTime.End - offset - rule.DaylightDelta; /* */ - DateTime ambiguousStart; - DateTime ambiguousEnd; - if (daylightTime.Delta.Ticks > 0) { - ambiguousStart = endTime - daylightTime.Delta; - ambiguousEnd = endTime; - } else { - ambiguousStart = startTime; - ambiguousEnd = startTime - daylightTime.Delta; - } - - Boolean isDst = CheckIsDst(startTime, time, endTime); - - // See if the resulting local time becomes ambiguous. This must be captured here or the - // DateTime will not be able to round-trip back to UTC accurately. - if (isDst) { - isAmbiguousLocalDst = (time >= ambiguousStart && time < ambiguousEnd); - - if (!isAmbiguousLocalDst && ambiguousStart.Year != ambiguousEnd.Year) { - // there exists an extreme corner case where the start or end period is on a year boundary and - // because of this the comparison above might have been performed for a year-early or a year-later - // than it should have been. - DateTime ambiguousStartModified; - DateTime ambiguousEndModified; - try { - ambiguousStartModified = ambiguousStart.AddYears(1); - ambiguousEndModified = ambiguousEnd.AddYears(1); - isAmbiguousLocalDst = (time >= ambiguousStart && time < ambiguousEnd); - } - catch (ArgumentOutOfRangeException) {} - - if (!isAmbiguousLocalDst) { - try { - ambiguousStartModified = ambiguousStart.AddYears(-1); - ambiguousEndModified = ambiguousEnd.AddYears(-1); - isAmbiguousLocalDst = (time >= ambiguousStart && time < ambiguousEnd); - } - catch (ArgumentOutOfRangeException) {} - } - - } - } - - return isDst; - } - - - static private Boolean CheckIsDst(DateTime startTime, DateTime time, DateTime endTime) { - Boolean isDst; - - int startTimeYear = startTime.Year; - int endTimeYear = endTime.Year; - - if (startTimeYear != endTimeYear) { - endTime = endTime.AddYears(startTimeYear - endTimeYear); - } - - int timeYear = time.Year; - - if (startTimeYear != timeYear) { - time = time.AddYears(startTimeYear - timeYear); - } - - if (startTime > endTime) { - // In southern hemisphere, the daylight saving time starts later in the year, and ends in the beginning of next year. - // Note, the summer in the southern hemisphere begins late in the year. - isDst = (time < endTime || time >= startTime); - } - else { - // In northern hemisphere, the daylight saving time starts in the middle of the year. - isDst = (time >= startTime && time < endTime); - } - return isDst; - } - - // - // GetDaylightTime - - // - // Helper function that returns a DaylightTime from a year and AdjustmentRule - // - static private DaylightTime GetDaylightTime(Int32 year, AdjustmentRule rule) { - TimeSpan delta = rule.DaylightDelta; - DateTime startTime = TransitionTimeToDateTime(year, rule.DaylightTransitionStart); - DateTime endTime = TransitionTimeToDateTime(year, rule.DaylightTransitionEnd); - return new DaylightTime(startTime, endTime, delta); - } - - // - // TransitionTimeToDateTime - - // - // Helper function that converts a year and TransitionTime into a DateTime - // - static private DateTime TransitionTimeToDateTime(Int32 year, TransitionTime transitionTime) { - DateTime value; - DateTime timeOfDay = transitionTime.TimeOfDay; - - if (transitionTime.IsFixedDateRule) { - // create a DateTime from the passed in year and the properties on the transitionTime - - // if the day is out of range for the month then use the last day of the month - Int32 day = DateTime.DaysInMonth(year, transitionTime.Month); - - value = new DateTime(year, transitionTime.Month, (day < transitionTime.Day) ? day : transitionTime.Day, - timeOfDay.Hour, timeOfDay.Minute, timeOfDay.Second, timeOfDay.Millisecond); - } - else { - if (transitionTime.Week <= 4) { - // - // Get the (transitionTime.Week)th Sunday. - // - value = new DateTime(year, transitionTime.Month, 1, - timeOfDay.Hour, timeOfDay.Minute, timeOfDay.Second, timeOfDay.Millisecond); - - int dayOfWeek = (int)value.DayOfWeek; - int delta = (int)transitionTime.DayOfWeek - dayOfWeek; - if (delta < 0) { - delta += 7; - } - delta += 7 * (transitionTime.Week - 1); - - if (delta > 0) { - value = value.AddDays(delta); - } - } - else { - // - // If TransitionWeek is greater than 4, we will get the last week. - // - Int32 daysInMonth = DateTime.DaysInMonth(year, transitionTime.Month); - value = new DateTime(year, transitionTime.Month, daysInMonth, - timeOfDay.Hour, timeOfDay.Minute, timeOfDay.Second, timeOfDay.Millisecond); - - // This is the day of week for the last day of the month. - int dayOfWeek = (int)value.DayOfWeek; - int delta = dayOfWeek - (int)transitionTime.DayOfWeek; - if (delta < 0) { - delta += 7; - } - - if (delta > 0) { - value = value.AddDays(-delta); - } - } - } - return value; - } - - // - // IsInvalidTime - - // - // returns true when dateTime falls into a "hole in time". - // - public Boolean IsInvalidTime(DateTime dateTime) { - Boolean isInvalid = false; - - if ( (dateTime.Kind == DateTimeKind.Unspecified) - || (dateTime.Kind == DateTimeKind.Local && this == Local) ) { - - // only check Unspecified and (Local when this TimeZoneInfo instance is Local) - AdjustmentRule rule = GetAdjustmentRuleForTime(dateTime); - - - if (rule != null) { - DaylightTime daylightTime = GetDaylightTime(dateTime.Year, rule); - isInvalid = GetIsInvalidTime(dateTime, rule, daylightTime); - } - else { - isInvalid = false; - } - } - - return isInvalid; - } - - // - // GetIsInvalidTime - - // - // Helper function that checks if a given DateTime is in an invalid time ("time hole") - // A "time hole" occurs at a DST transition point when time jumps forward; - // For example, in Pacific Standard Time on Sunday, April 2, 2006 time jumps from - // 1:59:59.9999999 to 3AM. The time range 2AM to 2:59:59.9999999AM is the "time hole". - // A "time hole" is not limited to only occurring at the start of DST, and may occur at - // the end of DST as well. - // - static private Boolean GetIsInvalidTime(DateTime time, AdjustmentRule rule, DaylightTime daylightTime) { - Boolean isInvalid = false; - if (rule == null || rule.DaylightDelta == TimeSpan.Zero) { - return isInvalid; - } - - DateTime startInvalidTime; - DateTime endInvalidTime; - - // if at DST start we transition forward in time then there is an ambiguous time range at the DST end - if (rule.DaylightDelta < TimeSpan.Zero) { - startInvalidTime = daylightTime.End; - endInvalidTime = daylightTime.End - rule.DaylightDelta; /* */ - } - else { - startInvalidTime = daylightTime.Start; - endInvalidTime = daylightTime.Start + rule.DaylightDelta; /* */ - } - - isInvalid = (time >= startInvalidTime && time < endInvalidTime); - - if (!isInvalid && startInvalidTime.Year != endInvalidTime.Year) { - // there exists an extreme corner case where the start or end period is on a year boundary and - // because of this the comparison above might have been performed for a year-early or a year-later - // than it should have been. - DateTime startModifiedInvalidTime; - DateTime endModifiedInvalidTime; - try { - startModifiedInvalidTime = startInvalidTime.AddYears(1); - endModifiedInvalidTime = endInvalidTime.AddYears(1); - isInvalid = (time >= startModifiedInvalidTime && time < endModifiedInvalidTime); - } - catch (ArgumentOutOfRangeException) {} - - if (!isInvalid) { - try { - startModifiedInvalidTime = startInvalidTime.AddYears(-1); - endModifiedInvalidTime = endInvalidTime.AddYears(-1); - isInvalid = (time >= startModifiedInvalidTime && time < endModifiedInvalidTime); - } - catch (ArgumentOutOfRangeException) {} - } - } - return isInvalid; - } + // Shortcut for TimeZoneInfo.Local.GetUtcOffset + internal static TimeSpan GetLocalUtcOffset(DateTime dateTime, TimeZoneInfoOptions flags) + { + bool dst; + return Local.GetUtcOffset (dateTime, out dst); + } + + internal TimeSpan GetUtcOffset(DateTime dateTime, TimeZoneInfoOptions flags) + { + bool dst; + return GetUtcOffset (dateTime, out dst); + } + + static internal TimeSpan GetUtcOffsetFromUtc (DateTime time, TimeZoneInfo zone, out Boolean isDaylightSavings, out Boolean isAmbiguousLocalDst) + { + isDaylightSavings = false; + isAmbiguousLocalDst = false; + TimeSpan baseOffset = zone.BaseUtcOffset; + + if (zone.IsAmbiguousTime (time)) { + isAmbiguousLocalDst = true; + return baseOffset; + } + + return zone.GetUtcOffset (time, out isDaylightSavings); + } #endregion } |