Skip to content

Commit

Permalink
Fix datetime extension [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
andrueastman committed Aug 30, 2022
1 parent 232f54b commit 53b7dcd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -154,4 +174,4 @@ private DateTime GetDateTimeFromDateTimeTimeZone(DateTimeTimeZone dateTimeTimeZo
return dateTime;
}
}
}
}

0 comments on commit 53b7dcd

Please sign in to comment.