Skip to content

Instantly share code, notes, and snippets.

@Sl4vP0weR
Last active November 21, 2021 18:07
Show Gist options
  • Save Sl4vP0weR/73649bb8b06403b918333e2c007f59c0 to your computer and use it in GitHub Desktop.
Save Sl4vP0weR/73649bb8b06403b918333e2c007f59c0 to your computer and use it in GitHub Desktop.
Time since a certain period
public struct TimeSince
{
public TimeSince(DateTime? from = default)
{
From = from.HasValue ? from.Value : DateTime.Now;
}
readonly DateTime From;
public static implicit operator float(TimeSince @this) => (float)((TimeSpan)@this).TotalSeconds;
public static implicit operator TimeSince(float @this) => new TimeSince(DateTime.Now.AddSeconds(@this));
public static implicit operator TimeSpan(TimeSince @this) => (DateTime.Now - @this.From);
public static implicit operator DateTime(TimeSince @this) => @this.From;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment