Skip to content

Instantly share code, notes, and snippets.

@Mrnikbobjeff
Created February 24, 2025 10:59
Show Gist options
  • Save Mrnikbobjeff/4c7b9a14943072d40444171055d63067 to your computer and use it in GitHub Desktop.
Save Mrnikbobjeff/4c7b9a14943072d40444171055d63067 to your computer and use it in GitHub Desktop.
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