Skip to content

fix: respect DateTimeKind in ToDateTimeOffset#335

Merged
w-ahmad merged 1 commit intow-ahmad:mainfrom
evanofficial:fix/utc-datetime-offset
Apr 7, 2026
Merged

fix: respect DateTimeKind in ToDateTimeOffset#335
w-ahmad merged 1 commit intow-ahmad:mainfrom
evanofficial:fix/utc-datetime-offset

Conversation

@evanofficial
Copy link
Copy Markdown
Contributor

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
Copy link
Copy Markdown
Owner

@w-ahmad w-ahmad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thank you for this fix!

@w-ahmad w-ahmad merged commit 03c7237 into w-ahmad:main Apr 7, 2026
1 of 2 checks passed
@evanofficial evanofficial deleted the fix/utc-datetime-offset branch April 8, 2026 22:35
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

2 participants