Skip to content

Commit da8cf83

Browse files
authored
Fix TimeZoneInfo to handle Yukon zone (#43550)
1 parent 839b134 commit da8cf83

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.AdjustmentRule.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ internal static AdjustmentRule CreateAdjustmentRule(
119119
internal bool IsStartDateMarkerForBeginningOfYear() =>
120120
!NoDaylightTransitions &&
121121
DaylightTransitionStart.Month == 1 && DaylightTransitionStart.Day == 1 &&
122-
DaylightTransitionStart.TimeOfDay.TimeOfDay.Ticks < TimeSpan.TicksPerSecond && // < 12:00:01 AM
123-
_dateStart.Year == _dateEnd.Year;
122+
DaylightTransitionStart.TimeOfDay.TimeOfDay.Ticks < TimeSpan.TicksPerSecond; // < 12:00:01 AM
124123

125124
//
126125
// When Windows sets the daylight transition end Jan 1st at 12:00 AM, it means the year ends with the daylight saving on.
@@ -129,8 +128,7 @@ internal bool IsStartDateMarkerForBeginningOfYear() =>
129128
internal bool IsEndDateMarkerForEndOfYear() =>
130129
!NoDaylightTransitions &&
131130
DaylightTransitionEnd.Month == 1 && DaylightTransitionEnd.Day == 1 &&
132-
DaylightTransitionEnd.TimeOfDay.TimeOfDay.Ticks < TimeSpan.TicksPerSecond && // < 12:00:01 AM
133-
_dateStart.Year == _dateEnd.Year;
131+
DaylightTransitionEnd.TimeOfDay.TimeOfDay.Ticks < TimeSpan.TicksPerSecond; // < 12:00:01 AM
134132

135133
/// <summary>
136134
/// Helper function that performs all of the validation checks for the factory methods and deserialization callback.

src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,40 @@ public static void LibyaTimeZone()
119119
Assert.True(libyaLocalTime.Equals(expectResult), string.Format("Expected {0} and got {1}", expectResult, libyaLocalTime));
120120
}
121121

122+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsWindows))]
123+
public static void TestYukunTZ()
124+
{
125+
try
126+
{
127+
TimeZoneInfo yukon = TimeZoneInfo.FindSystemTimeZoneById("Yukon Standard Time");
128+
129+
// First, ensure we have the updated data
130+
TimeZoneInfo.AdjustmentRule [] rules = yukon.GetAdjustmentRules();
131+
if (rules.Length <= 0 || rules[rules.Length - 1].DateStart.Year != 2021 || rules[rules.Length - 1].DateEnd.Year != 9999)
132+
{
133+
return;
134+
}
135+
136+
TimeSpan minus7HoursSpan = new TimeSpan(-7, 0, 0);
137+
138+
DateTimeOffset midnight = new DateTimeOffset(2021, 1, 1, 0, 0, 0, 0, minus7HoursSpan);
139+
DateTimeOffset beforeMidnight = new DateTimeOffset(2020, 12, 31, 23, 59, 59, 999, minus7HoursSpan);
140+
DateTimeOffset before1AM = new DateTimeOffset(2021, 1, 1, 0, 59, 59, 999, minus7HoursSpan);
141+
DateTimeOffset at1AM = new DateTimeOffset(2021, 1, 1, 1, 0, 0, 0, minus7HoursSpan);
142+
DateTimeOffset midnight2022 = new DateTimeOffset(2022, 1, 1, 0, 0, 0, 0, minus7HoursSpan);
143+
144+
Assert.Equal(minus7HoursSpan, yukon.GetUtcOffset(midnight));
145+
Assert.Equal(minus7HoursSpan, yukon.GetUtcOffset(beforeMidnight));
146+
Assert.Equal(minus7HoursSpan, yukon.GetUtcOffset(before1AM));
147+
Assert.Equal(minus7HoursSpan, yukon.GetUtcOffset(at1AM));
148+
Assert.Equal(minus7HoursSpan, yukon.GetUtcOffset(midnight2022));
149+
}
150+
catch (TimeZoneNotFoundException)
151+
{
152+
// Some Windows versions don't carry the complete TZ data. Ignore the tests on such versiosn.
153+
}
154+
}
155+
122156
[Fact]
123157
public static void RussianTimeZone()
124158
{

0 commit comments

Comments
 (0)