You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There appears to be an 11-year-old bug in the .NET DateTime.Parse() methods for handling time-only strings when passing in DateTimeStyle.AssumeUnivesal. Per the documentation, the Parse() method should be honoring the passed in style, but when stepping through .NET code it clearly ignores this.
Reproduction Steps
Use a debugger to step through DateTime.Parse("13:30", null, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal) in a debugger.
When stepping through, due to using a time-only string, it lands within the following method inside DateTimeParse.cs:
privatestaticDateTimeGetDateTimeNow(scopedrefDateTimeResultresult,scopedrefDateTimeStylesstyles){if((result.flags&ParseFlags.CaptureOffset)!=0){if((result.flags&ParseFlags.TimeZoneUsed)!=0){// use the supplied offset to calculate 'Now'returnnewDateTime(DateTime.UtcNow.Ticks+result.timeZoneOffset.Ticks,DateTimeKind.Unspecified);}elseif((styles&DateTimeStyles.AssumeUniversal)!=0){// assume the offset is UtcreturnDateTime.UtcNow;}}// assume the offset is LocalreturnDateTime.Now;}
The input resulting parameters from the DateTime.Parse() call above contain result.flags = ParseFlags.HaveTime and styles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal. Note that with this combination, the entire branch is skipped because a time-only string doesn't contain a capture offset. The requested styles are completely ignored and local time is used, which breaks documented convention.
Expected behavior
The expectation here is to use UTC when DateTimeStyles.AssumeUniversal is used instead of Local, per documentation:
Description
There appears to be an 11-year-old bug in the .NET DateTime.Parse() methods for handling time-only strings when passing in DateTimeStyle.AssumeUnivesal. Per the documentation, the Parse() method should be honoring the passed in style, but when stepping through .NET code it clearly ignores this.
Reproduction Steps
Use a debugger to step through DateTime.Parse("13:30", null, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal) in a debugger.
When stepping through, due to using a time-only string, it lands within the following method inside DateTimeParse.cs:
The input resulting parameters from the DateTime.Parse() call above contain
result.flags = ParseFlags.HaveTime
andstyles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal
. Note that with this combination, the entire branch is skipped because a time-only string doesn't contain a capture offset. The requested styles are completely ignored and local time is used, which breaks documented convention.Expected behavior
The expectation here is to use UTC when DateTimeStyles.AssumeUniversal is used instead of Local, per documentation:
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parse?view=net-9.0#system-datetime-parse(system-string-system-iformatprovider-system-globalization-datetimestyles)
https://learn.microsoft.com/en-us/dotnet/api/system.globalization.datetimestyles?view=net-9.0#system-globalization-datetimestyles-assumeuniversal
Actual behavior
The observed behavior is that Local time is used even when DateTimeStyles.AssumeUniversal is specified.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: