Skip to content

Instantly share code, notes, and snippets.

@Jalalx
Created December 28, 2017 10:41
Show Gist options
  • Save Jalalx/b69b0d1f672869e874ced56bb3beba69 to your computer and use it in GitHub Desktop.
Save Jalalx/b69b0d1f672869e874ced56bb3beba69 to your computer and use it in GitHub Desktop.
/// <summary>
/// Read more at: https://stackoverflow.com/a/14369695/375958
/// </summary>
public static class HighResolutionDateTime
{
private static long lastTimeStamp = DateTime.UtcNow.Ticks;
public static long UtcNowTicks
{
get
{
long original, newValue;
do
{
original = lastTimeStamp;
long now = DateTime.UtcNow.Ticks;
newValue = Math.Max(now, original + 1L);
} while (Interlocked.CompareExchange(ref lastTimeStamp, newValue, original) != original);
return newValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment