Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static ctor for MonotonicTimeStamp throws ArgumentOutOfRangeException #28

Closed
Dnawrkshp opened this issue Jan 10, 2022 · 5 comments
Closed
Assignees

Comments

@Dnawrkshp
Copy link

This just started happening. Not sure why. To reproduce simply run the ExampleCode project.

The line that errors is

var ret = DateTime.SpecifyKind(UtcReference + ((TimeSpan)ElapsedSinceUtcReference), DateTimeKind.Utc);

Sum of UtcReference and ElapsedSinceUtcReference:

UtcReference + ((TimeSpan)ElapsedSinceUtcReference)
{11/1/9999 3:12:52 AM}
    Date: {11/1/9999 12:00:00 AM}
    Day: 0x00000001
    DayOfWeek: Monday
    DayOfYear: 0x00000131
    Hour: 0x00000003
    Kind: Utc
    Millisecond: 0x000001d3
    Minute: 0x0000000c
    Month: 0x0000000b
    Second: 0x00000034
    Ticks: 0x2bc9f8a1cabef2f8
    TimeOfDay: {03:12:52.4677880}
    Year: 0x0000270f

Exception:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'HpTimeStamps.MonotonicTimeStamp`1' threw an exception.
  Source=HighPrecisionTimeStamps
  StackTrace:
   at HpTimeStamps.MonotonicTimeStamp`1.CreateFromRefTicks(Int64 referenceTicks) in /_/MonotonicTimeStamp.cs:line 111
   at HpTimeStamps.MonotonicTimeStampUtil`1.get_StampNow() in /_/MonotonicTimeStampUtil.cs:line 19
   at ExampleTimestamps.Program.PrintMonotonicInfo() in M:\VS\High-Precision-Time-Stamps\ExampleCode\Program.cs:line 81
   at ExampleTimestamps.Program.Main() in M:\VS\High-Precision-Time-Stamps\ExampleCode\Program.cs:line 28

  This exception was originally thrown at this call stack:
    [External Code]
    HpTimeStamps.MonotonicTimeStamp<TStampContext>.ToUtcDateTime() in MonotonicTimeStamp.cs
    HpTimeStamps.MonotonicTimeStamp<TStampContext>.CalculateMaximumAndMinimumImportableDateTimes() in MonotonicTimeStamp.cs
    HpTimeStamps.MonotonicTimeStamp<TStampContext>.MonotonicTimeStamp() in MonotonicTimeStamp.cs

Inner Exception 1:
ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime. (Parameter 't')
@cpsusie cpsusie self-assigned this Jan 10, 2022
@cpsusie
Copy link
Owner

cpsusie commented Jan 10, 2022

This just started happening. Not sure why. To reproduce simply run the ExampleCode project.

The line that errors is

var ret = DateTime.SpecifyKind(UtcReference + ((TimeSpan)ElapsedSinceUtcReference), DateTimeKind.Utc);

Sum of UtcReference and ElapsedSinceUtcReference:

UtcReference + ((TimeSpan)ElapsedSinceUtcReference)
{11/1/9999 3:12:52 AM}
    Date: {11/1/9999 12:00:00 AM}
    Day: 0x00000001
    DayOfWeek: Monday
    DayOfYear: 0x00000131
    Hour: 0x00000003
    Kind: Utc
    Millisecond: 0x000001d3
    Minute: 0x0000000c
    Month: 0x0000000b
    Second: 0x00000034
    Ticks: 0x2bc9f8a1cabef2f8
    TimeOfDay: {03:12:52.4677880}
    Year: 0x0000270f

Exception:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'HpTimeStamps.MonotonicTimeStamp`1' threw an exception.
  Source=HighPrecisionTimeStamps
  StackTrace:
   at HpTimeStamps.MonotonicTimeStamp`1.CreateFromRefTicks(Int64 referenceTicks) in /_/MonotonicTimeStamp.cs:line 111
   at HpTimeStamps.MonotonicTimeStampUtil`1.get_StampNow() in /_/MonotonicTimeStampUtil.cs:line 19
   at ExampleTimestamps.Program.PrintMonotonicInfo() in M:\VS\High-Precision-Time-Stamps\ExampleCode\Program.cs:line 81
   at ExampleTimestamps.Program.Main() in M:\VS\High-Precision-Time-Stamps\ExampleCode\Program.cs:line 28

  This exception was originally thrown at this call stack:
    [External Code]
    HpTimeStamps.MonotonicTimeStamp<TStampContext>.ToUtcDateTime() in MonotonicTimeStamp.cs
    HpTimeStamps.MonotonicTimeStamp<TStampContext>.CalculateMaximumAndMinimumImportableDateTimes() in MonotonicTimeStamp.cs
    HpTimeStamps.MonotonicTimeStamp<TStampContext>.MonotonicTimeStamp() in MonotonicTimeStamp.cs

Inner Exception 1:
ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime. (Parameter 't')

Thanks for the bug report. It looks like you are using version 1.0.0.6. Is this correct? When I switched from using this in a .NET 4.8 environment to 5.0, I started encountering a similar problem. See issue 21 resolved (for me) with Pull Request 23. Could something similar in your case? Checklist:

  • Version of .NET Framework / Core being used:
  • Operating System
  • Value of System.Diagnostics.Stopwatch.Frequency
  • Value of System.Diagnostics.Stopwatch.IsHighResolution
  • Value of DateTime.MinValue.ToUniversalTime() on your system
  • Value of DateTime.MaxValue.ToUniversalTime() on your system

Thank you for this extra information and for your bug report.

@Dnawrkshp
Copy link
Author

Dnawrkshp commented Jan 10, 2022

Thanks for the reply. I'm on the latest master branch which looks like it's 1.0.0.6. It's strange because I've been using the nuget package for awhile now on .net core 3.1 and just today it started giving me this error. I don't have 4.8 or any .net framework version installed but I can install and test on it if needed. It does seem like the issue in #21 is the same problem I'm experiencing.

  • .NET 5.0
  • Windows 10
  • System.Diagnostics.Stopwatch.Frequency = 10000000
  • System.Diagnostics.Stopwatch.IsHighResolution = true
DateTime.MinValue.ToUniversalTime()
{1/1/0001 8:00:00 AM}
    Date: {1/1/0001 12:00:00 AM}
    Day: 0x00000001
    DayOfWeek: Monday
    DayOfYear: 0x00000001
    Hour: 0x00000008
    Kind: Utc
    Millisecond: 0x00000000
    Minute: 0x00000000
    Month: 0x00000001
    Second: 0x00000000
    Ticks: 0x000000430e234000
    TimeOfDay: {08:00:00}
    Year: 0x00000001
DateTime.MaxValue.ToUniversalTime()
{12/31/9999 11:59:59 PM}
    Date: {12/31/9999 12:00:00 AM}
    Day: 0x0000001f
    DayOfWeek: Friday
    DayOfYear: 0x0000016d
    Hour: 0x00000017
    Kind: Utc
    Millisecond: 0x000003e7
    Minute: 0x0000003b
    Month: 0x0000000c
    Second: 0x0000003b
    Ticks: 0x2bca2875f4373fff
    TimeOfDay: {23:59:59.9999999}
    Year: 0x0000270f

@cpsusie
Copy link
Owner

cpsusie commented Jan 10, 2022

  • 000

Ok. Hopefully I can look into this and find a fix sometime this or next week (during the week usually doing work / non-open-source stuff for employer, sometimes get time during week). If you look over the issue 21 stuff and think it's the same issue with slightly different parameters and can come up with a fix before then, please let me know by opening a PR. I'm currently working on an extension to this library that allows for conversion to and from Protobuf Timestamps. If one of us can get a fix in soon for your bug, I'll try to release the fix together with the Protobuf conversion support.

@Dnawrkshp
Copy link
Author

Hey it started working again a few days ago on my machine. Might have been a random Windows issue since it never threw an exception on my linux server. Not really sure though.

I'll leave this up for you to close if you like. Really appreciate the quick response.

Cheers!

@cpsusie
Copy link
Owner

cpsusie commented Jan 30, 2022

Hey it started working again a few days ago on my machine. Might have been a random Windows issue since it never threw an exception on my linux server. Not really sure though.

I'll leave this up for you to close if you like. Really appreciate the quick response.

Cheers!

No problem. I tried without success to reproduce your problem. I typically test on a Windows Server, a Windows desktop and a linux desktop (kubuntu, 20.11 I think). I would love to know what was going on in your case but probably don't have time to track it down, especially since different systems behave differently. I would love to start building a collection of combinations of NET version, hardware and OS listing min/max utc date time and high resolution event timer ticks so I can set the min and max values in the static CTOR appropriately rather than just keep trimming the ends (increasing min and decreasing max) until it works everywhere I test and use the software. I'll close it for now. Thanks for the report!

@cpsusie cpsusie closed this as completed Jan 30, 2022
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

No branches or pull requests

2 participants