-
Notifications
You must be signed in to change notification settings - Fork 533
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
fix: jit-times - Timestamp arithmetic + add warnings for negative times #9484
Open
alexyakunin
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
alexyakunin:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+32
−85
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace jittimes { | ||
public struct Timestamp : IComparable { | ||
public Int64 seconds; | ||
public int milliseconds; | ||
public int nanoseconds; | ||
|
||
public record struct Timestamp(long nanoseconds) : IComparable { | ||
static readonly Regex regex = new Regex ("^([0-9]+)s:([0-9]+)::([0-9]+)$"); | ||
|
||
public static Timestamp Parse (string time) | ||
{ | ||
Timestamp ts = new Timestamp (); | ||
|
||
var match = regex.Match (time); | ||
if (!match.Success || match.Groups.Count <= 3) { | ||
ts.seconds = 0; | ||
ts.milliseconds = 0; | ||
ts.nanoseconds = 0; | ||
return ts; | ||
} | ||
if (!match.Success || match.Groups.Count <= 3) | ||
return default; | ||
|
||
ts.seconds = Convert.ToInt64 (match.Groups [1].Value); | ||
ts.milliseconds = Convert.ToInt32 (match.Groups [2].Value); | ||
ts.nanoseconds = Convert.ToInt32 (match.Groups [3].Value); | ||
|
||
return ts; | ||
var s = Convert.ToInt64 (match.Groups [1].Value); | ||
var ms = Convert.ToInt32 (match.Groups [2].Value); | ||
var ns = Convert.ToInt32 (match.Groups [3].Value); | ||
return new Timestamp(1000_000_000*s + 1000_000*ms + ns); | ||
} | ||
|
||
static public Timestamp operator - (Timestamp ts1, Timestamp ts2) | ||
{ | ||
Timestamp result = new Timestamp (); | ||
|
||
if (ts1.nanoseconds >= ts2.nanoseconds) | ||
result.nanoseconds = ts1.nanoseconds - ts2.nanoseconds; | ||
else { | ||
result.nanoseconds = 1000000 + ts1.nanoseconds - ts2.nanoseconds; | ||
result.milliseconds--; | ||
} | ||
|
||
if (ts1.milliseconds >= ts2.milliseconds) | ||
result.milliseconds += ts1.milliseconds - ts2.milliseconds; | ||
else { | ||
result.milliseconds += 1000 + ts1.milliseconds - ts2.milliseconds; | ||
result.seconds--; | ||
} | ||
|
||
result.seconds += ts1.seconds - ts2.seconds; | ||
|
||
return result; | ||
} | ||
=> new Timestamp(ts1.nanoseconds - ts2.nanoseconds); | ||
|
||
static public Timestamp operator + (Timestamp ts1, Timestamp ts2) | ||
{ | ||
Timestamp result = new Timestamp { | ||
nanoseconds = ts1.nanoseconds + ts2.nanoseconds | ||
}; | ||
|
||
if (result.nanoseconds > 1000000) { | ||
result.milliseconds += result.nanoseconds / 1000000; | ||
result.nanoseconds %= 1000000; | ||
} | ||
|
||
result.milliseconds += ts1.milliseconds + ts2.milliseconds; | ||
|
||
if (result.milliseconds > 1000) { | ||
result.seconds += result.milliseconds / 1000; | ||
result.milliseconds %= 1000; | ||
} | ||
|
||
return result; | ||
} | ||
=> new Timestamp(ts1.nanoseconds + ts2.nanoseconds); | ||
|
||
public override string ToString () | ||
{ | ||
var sec = seconds != 0 ? $"{seconds}(s):" : ""; | ||
|
||
return $"{sec}{milliseconds}::{nanoseconds}"; | ||
var remainder = Math.Abs(nanoseconds); | ||
var s = remainder / 1000_000_000; | ||
remainder -= 1000_000_000*s; | ||
var ms = remainder / 1000_000; | ||
var ns = remainder - 1000_000*ms; | ||
var sign = nanoseconds < 0 ? "-" : ""; | ||
var sec = s != 0 ? $"{s}(s):" : ""; | ||
return $"{sign}{sec}{ms}::{ns}"; | ||
} | ||
|
||
public Timestamp Positive () | ||
=> new Timestamp(Math.Max(0L, nanoseconds)); | ||
|
||
public double Milliseconds () | ||
{ | ||
return seconds * 1000.0 + (double)milliseconds + nanoseconds / 1000000.0; | ||
} | ||
=> nanoseconds / 1000_000; | ||
|
||
public int CompareTo (object o) | ||
public int CompareTo(object o) | ||
{ | ||
if (!(o is Timestamp other)) | ||
throw new ArgumentException ("Object is not a Timestamp"); | ||
|
||
if (seconds > other.seconds) | ||
return 1; | ||
|
||
if (seconds < other.seconds) | ||
return -1; | ||
|
||
if (milliseconds > other.milliseconds) | ||
return 1; | ||
|
||
if (milliseconds < other.milliseconds) | ||
return -1; | ||
|
||
if (nanoseconds > other.nanoseconds) | ||
return 1; | ||
|
||
if (nanoseconds < other.nanoseconds) | ||
return -1; | ||
|
||
return 0; | ||
return Comparer<long>.Default.Compare(this.nanoseconds, other.nanoseconds); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While moving to store only nanoseconds within the Unix epoch is a massive simplification, it also reduces the amount of time that can be tracked. As this is "nanoseconds of Unix epoch" in a signed 64-bit integer, the maximum time this approach can hold is around 2262-Apr-11. Any date after 23h47min16sec on that date results in a negative value:
Given that this is still 238 years away, this probably doesn't matter…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No disrespect, but if MS folks think it makes sense write this comment to PR + make others read it... I suspect I understand why there is zero progress with some huge issues like the one this PR is related to.