From 53b7dcd83c8744ca0559b41989e0816808544f9b Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 30 Aug 2022 16:23:41 +0300 Subject: [PATCH] Fix datetime extension [skip ci] --- .../Extensions/DateTimeZoneExtensions.cs | 2 +- .../Extensions/DateTimeZoneExtensionsTests.cs | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Graph/Models/Extensions/DateTimeZoneExtensions.cs b/src/Microsoft.Graph/Models/Extensions/DateTimeZoneExtensions.cs index 36788e2b27c..933b032fb30 100644 --- a/src/Microsoft.Graph/Models/Extensions/DateTimeZoneExtensions.cs +++ b/src/Microsoft.Graph/Models/Extensions/DateTimeZoneExtensions.cs @@ -90,7 +90,7 @@ internal static DateTimeOffset ToDateTimeOffset(this DateTime dateTime, TimeZone } else { - dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); + dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified); offset = timeZoneInfo.GetUtcOffset(dateTime); } diff --git a/tests/Microsoft.Graph.DotnetCore.Test/Models/Extensions/DateTimeZoneExtensionsTests.cs b/tests/Microsoft.Graph.DotnetCore.Test/Models/Extensions/DateTimeZoneExtensionsTests.cs index 5b932cd9d09..3b730296442 100644 --- a/tests/Microsoft.Graph.DotnetCore.Test/Models/Extensions/DateTimeZoneExtensionsTests.cs +++ b/tests/Microsoft.Graph.DotnetCore.Test/Models/Extensions/DateTimeZoneExtensionsTests.cs @@ -45,6 +45,26 @@ public void ToDateTimeOffset_Should_Convert_DateTimeTimeZone_To_DateTimeOffset() Assert.Equal(expectedDateTimeOffset, actualDateTimeOffset); } + [Fact] + public void ToDateTimeOffset_Should_Convert_DateTimeTimeZone_To_DateTimeOffset_With_Non_UTC() + { + DateTimeTimeZone dateTimeTimeZone = new DateTimeTimeZone + { + TimeZone = "Eastern Standard Time", + DateTime = "2019-01-25T06:37:39.8058788" + }; + + DateTime dateTime = DateTime.ParseExact(dateTimeTimeZone.DateTime, DateTimeFormat, CultureInfo.InvariantCulture); + TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(dateTimeTimeZone.TimeZone); + TimeSpan offset = timeZoneInfo.GetUtcOffset(dateTime); + dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified); + + var expectedDateTimeOffset = new DateTimeOffset(dateTime, offset); + var actualDateTimeOffset = dateTimeTimeZone.ToDateTimeOffset(); + + Assert.Equal(expectedDateTimeOffset, actualDateTimeOffset); + } + [Fact] public void FromDateTime_Should_Convert_DateTime_To_DateTimeTimeZone() { @@ -154,4 +174,4 @@ private DateTime GetDateTimeFromDateTimeTimeZone(DateTimeTimeZone dateTimeTimeZo return dateTime; } } -} \ No newline at end of file +}