Created
February 24, 2025 10:59
-
-
Save Mrnikbobjeff/4c7b9a14943072d40444171055d63067 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Graph.Models; | |
using System.Globalization; | |
namespace MsGraph.Reproduction; | |
public class MsGraphReproduction | |
{ | |
[Fact] | |
public void ZonedDateTime_Maps_Time_UTC() | |
{ | |
// Arrange | |
DateTime start = new DateTime(2023, 6, 1, 10, 0, 0, DateTimeKind.Utc); | |
var zoned = new DateTimeTimeZone | |
{ | |
DateTime = start.ToString("o"), | |
TimeZone = "UTC" | |
}; | |
//Fails as ToDateTimeOffset does not use DateTimeStyles.RoundtripKind | |
var offset = zoned.ToDateTimeOffset(); | |
Assert.Equal(offset.UtcDateTime, start); | |
} | |
// The main path we took, changing the DateTimeStyles to RoundtripKind works. | |
DateTimeOffset ToOffset(DateTimeTimeZone dateTimeTimeZone) | |
{ | |
DateTime dateTime = DateTime.ParseExact(dateTimeTimeZone.DateTime, "yyyy-MM-ddTHH:mm:ss.fffffffK", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind); | |
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(dateTimeTimeZone.TimeZone); | |
dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); | |
var offset = timeZoneInfo.GetUtcOffset(dateTime); | |
return new DateTimeOffset(dateTime, offset); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment