-
Notifications
You must be signed in to change notification settings - Fork 2
Dylan/feat: Static TimeConvert util #35
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
base: master
Are you sure you want to change the base?
Dylan/feat: Static TimeConvert util #35
Conversation
Scripts/TimeConvert.cs
Outdated
/// </summary> | ||
public static class TimeConvert | ||
{ | ||
private static readonly DateTimeOffset unixEpoch = new( |
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.
This is built-in in .NET under DateTime.UnixEpoch
: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.unixepoch?view=netstandard-2.1
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.
TIL! I'll adjust this
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.
@RReverser After editing this out, there's not much left! It may be still worth it just to provide an "official" way to convert back and forth (with some inline docs to explain better) since there are always questions about the best practices for time converts.
This is especially emphasized since I personally got it wrong when using SpacetimeDB by using ISO timestamp strings at first, not realizing it doesn't get as low as microsecs (in addition to long
being more efficient than string
).
public static long MicrosecondsTimestamp() | ||
{ | ||
TimeSpan elapsed = DateTimeOffset.Now - DateTime.UnixEpoch; | ||
return elapsed.Ticks / 10; | ||
} |
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.
Still not sure about the class itself and going to leave it to others to review, but another small nit is that we can reuse the more general method here:
public static long MicrosecondsTimestamp() | |
{ | |
TimeSpan elapsed = DateTimeOffset.Now - DateTime.UnixEpoch; | |
return elapsed.Ticks / 10; | |
} | |
public static long MicrosecondsTimestamp() => ToMicrosecondsTimestamp(DateTimeOffset.Now); |
About
Add a static
TimeConvert
helper class to the Unity SDKWhy?
In Rust,
.timestamp()
is pretty straight-forward. However, for C#? There are a million ways to do it, and almost all of the ones most people would initially think are probably wrong (including my own theories when I was eating my own dogfood converting scripts from rs to c#).I was originally using an ISO timestamp since it's accepted by most DBs, for example - that was not in microseconds. However, it can also get messy quite fast and can inaccurate as things get parsed back-and-forth.
To simplify things, this helper class can help keep things consistent and makes it as close as the Rust timestamp() call as possible, converting both ways.
EDIT: After Ingvar pointing out there's an official DateTime.UnixEpoch, this makes this PR way less needed. However, it may still be useful to provide official convert consistency (with inline docs) since DateTime-like converts always raise questions/concerns.
Feats
public static DateTimeOffset FromMicrosecondsTimestamp(long microseconds)
public static long ToMicrosecondsTimestamp(DateTimeOffset dateTimeOffset)
public static long MicrosecondsTimestamp()
[Obsolete] public static string ToTimestampIso8601(DateTimeOffset dateTimeOffset)
Additional Note
I recently updated Zeke's Demo mini upgrade PR to have a duplicate TimeConvert class to ensure synchronicity (which will dupe into the new demo docs later).