Skip to content

fix: respect DateTimeKind in ToDateTimeOffset#335

Open
evanofficial wants to merge 1 commit intow-ahmad:mainfrom
evanofficial:fix/utc-datetime-offset
Open

fix: respect DateTimeKind in ToDateTimeOffset#335
evanofficial wants to merge 1 commit intow-ahmad:mainfrom
evanofficial:fix/utc-datetime-offset

Conversation

@evanofficial
Copy link
Copy Markdown

Description

DateTimeExtensions.ToDateTimeOffset() always used TimeZoneInfo.Local.GetUtcOffset(), which throws 0x80070057 ("The UTC Offset for Utc DateTime instances must be 0") when passed a DateTime with DateTimeKind.Utc.

The fix checks dateTime.Kind and uses TimeSpan.Zero for UTC DateTimes, preserving existing behavior for Unspecified and Local kinds.

Changes

  • DateTimeExtensions.cs — Check DateTimeKind.Utc before selecting the offset
  • DateTimeExtensionsTests.cs — Added ToDateTimeOffset_FromUtcDateTime_UsesZeroOffset test

Fixes #330

ToDateTimeOffset always used TimeZoneInfo.Local offset, which
throws 0x80070057 when passed a DateTime with DateTimeKind.Utc.
Now checks DateTimeKind and uses TimeSpan.Zero for UTC DateTimes.

Fixes w-ahmad#330
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DateTime is always initialized as non UTC kind and fails with UTC DateTime

1 participant