Skip to content

Instantly share code, notes, and snippets.

@oktopus1959
Created June 13, 2023 12:29
Show Gist options
  • Save oktopus1959/09061a702ba7a41cb7c2eb3dbf0f59e5 to your computer and use it in GitHub Desktop.
Save oktopus1959/09061a702ba7a41cb7c2eb3dbf0f59e5 to your computer and use it in GitHub Desktop.
High resolution DateTime.Now
using System;
using System.Runtime.InteropServices;
namespace Utils
{
/// <summary>
/// High Resolution DateTime
/// </summary>
public static class HRDateTime
{
// cf. https://stackoverflow.com/questions/54889988/what-is-the-time-reference-for-getsystemtimepreciseasfiletime
// cf. https://qiita.com/5view5q/items/6f08aaea7d961fd4cad7
[DllImport("kernel32.dll")]
static extern void GetSystemTimePreciseAsFileTime(out long filetime);
static TimeSpan adjustTs;
static DateTime systemHiResNow()
{
long filetime;
GetSystemTimePreciseAsFileTime(out filetime);
return DateTime.FromFileTimeUtc(filetime).ToLocalTime();
}
/// <summary>
/// システムクロックと現在時刻(DateTime.Now)にはズレがあるので、それを補正する。<br/>
/// プログラム開始時を含め、適当なタイミングで呼び出すこと。
/// </summary>
public static void AdjustHiResNow()
{
adjustTs = DateTime.Now - systemHiResNow();
}
/// <summary>
/// High Resolution な DateTime.Now
/// </summary>
public static DateTime Now => systemHiResNow() + adjustTs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment