Last active
November 21, 2021 18:07
-
-
Save Sl4vP0weR/73649bb8b06403b918333e2c007f59c0 to your computer and use it in GitHub Desktop.
Time since a certain period
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
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