Skip to content

Instantly share code, notes, and snippets.

@gidiyorum
Forked from mattjohnsonpint/Program.cs
Created August 21, 2020 09:32
Show Gist options
  • Save gidiyorum/677708a67fd8dff11650e2ae0536c1b1 to your computer and use it in GitHub Desktop.
Save gidiyorum/677708a67fd8dff11650e2ae0536c1b1 to your computer and use it in GitHub Desktop.
Just playing with clocks
using System;
using System.Runtime.InteropServices;
namespace PreciseTimeTest
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("------------------------------------");
Console.WriteLine("DateTime.UtcNow");
Console.WriteLine("------------------------------------");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(DateTime.UtcNow.ToString("o"));
}
Console.WriteLine();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Console.WriteLine("------------------------------------");
Console.WriteLine("GetSystemTimeAsFileTime");
Console.WriteLine("------------------------------------");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(GetNowWindows().ToString("o"));
}
Console.WriteLine();
Console.WriteLine("------------------------------------");
Console.WriteLine("GetSystemTimePreciseAsFileTime");
Console.WriteLine("------------------------------------");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(GetPreciseNowWindows().ToString("o"));
}
Console.WriteLine();
}
else
{
Console.WriteLine("------------------------------------");
Console.WriteLine("clock_gettime(CLOCK_REALTIME_COARSE)");
Console.WriteLine("------------------------------------");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(GetTimeLinuxTime(CLOCK_REALTIME_COARSE).ToString("o"));
}
Console.WriteLine();
Console.WriteLine("------------------------------------");
Console.WriteLine("clock_gettime(CLOCK_REALTIME)");
Console.WriteLine("------------------------------------");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(GetTimeLinuxTime(CLOCK_REALTIME).ToString("o"));
}
Console.WriteLine();
}
}
private const int CLOCK_REALTIME = 0;
private const int CLOCK_REALTIME_COARSE = 5;
private static DateTime GetTimeLinuxTime(int clockId)
{
timespec ts;
clock_gettime(clockId, out ts);
var dto = DateTimeOffset.FromUnixTimeSeconds(ts.tv_sec).AddTicks(ts.tv_nsec / 100);
return dto.UtcDateTime;
}
private static DateTime GetPreciseNowWindows()
{
long timeStamp;
GetSystemTimePreciseAsFileTime(out timeStamp);
return DateTime.FromFileTimeUtc(timeStamp);
}
private static DateTime GetNowWindows()
{
long timeStamp;
GetSystemTimeAsFileTime(out timeStamp);
return DateTime.FromFileTimeUtc(timeStamp);
}
[DllImport("kernel32.dll")]
static extern void GetSystemTimeAsFileTime(out long timeStamp);
[DllImport("kernel32.dll")]
static extern void GetSystemTimePreciseAsFileTime(out long timeStamp);
[DllImport("libc")]
static extern void clock_gettime(int clk_id, out timespec ts);
[StructLayout(LayoutKind.Sequential)]
struct timespec
{
public long tv_sec;
public long tv_nsec;
}
}
}
------------------------------------
DateTime.UtcNow
------------------------------------
2017-01-30T22:56:30.9340520Z
2017-01-30T22:56:31.5351510Z
2017-01-30T22:56:31.5351510Z
2017-01-30T22:56:31.5355800Z
2017-01-30T22:56:31.5355800Z
2017-01-30T22:56:31.5355800Z
2017-01-30T22:56:31.5355800Z
2017-01-30T22:56:31.5355800Z
2017-01-30T22:56:31.5355800Z
2017-01-30T22:56:31.5360790Z
------------------------------------
clock_gettime(CLOCK_REALTIME_COARSE)
------------------------------------
2017-01-30T22:56:31.5390800Z
2017-01-30T22:56:31.5390800Z
2017-01-30T22:56:31.5390800Z
2017-01-30T22:56:31.5390800Z
2017-01-30T22:56:31.5390800Z
2017-01-30T22:56:31.5395800Z
2017-01-30T22:56:31.5395800Z
2017-01-30T22:56:31.5395800Z
2017-01-30T22:56:31.5395800Z
2017-01-30T22:56:31.5395800Z
------------------------------------
clock_gettime(CLOCK_REALTIME)
------------------------------------
2017-01-30T22:56:31.5402917Z
2017-01-30T22:56:31.5403714Z
2017-01-30T22:56:31.5404506Z
2017-01-30T22:56:31.5405293Z
2017-01-30T22:56:31.5406270Z
2017-01-30T22:56:31.5407252Z
2017-01-30T22:56:31.5408059Z
2017-01-30T22:56:31.5408890Z
2017-01-30T22:56:31.5409740Z
2017-01-30T22:56:31.5410542Z
------------------------------------
DateTime.UtcNow
------------------------------------
2017-01-30T22:57:10.0354444Z
2017-01-30T22:57:10.0359440Z
2017-01-30T22:57:10.0359440Z
2017-01-30T22:57:10.0359440Z
2017-01-30T22:57:10.0359440Z
2017-01-30T22:57:10.0359440Z
2017-01-30T22:57:10.0359440Z
2017-01-30T22:57:10.0359440Z
2017-01-30T22:57:10.0359440Z
2017-01-30T22:57:10.0359440Z
------------------------------------
GetSystemTimeAsFileTime
------------------------------------
2017-01-30T22:57:10.0369431Z
2017-01-30T22:57:10.0369431Z
2017-01-30T22:57:10.0369431Z
2017-01-30T22:57:10.0369431Z
2017-01-30T22:57:10.0369431Z
2017-01-30T22:57:10.0369431Z
2017-01-30T22:57:10.0369431Z
2017-01-30T22:57:10.0369431Z
2017-01-30T22:57:10.0369431Z
2017-01-30T22:57:10.0374442Z
------------------------------------
GetSystemTimePreciseAsFileTime
------------------------------------
2017-01-30T22:57:10.0378719Z
2017-01-30T22:57:10.0379613Z
2017-01-30T22:57:10.0380116Z
2017-01-30T22:57:10.0389834Z
2017-01-30T22:57:10.0397068Z
2017-01-30T22:57:10.0404611Z
2017-01-30T22:57:10.0411557Z
2017-01-30T22:57:10.0418727Z
2017-01-30T22:57:10.0426010Z
2017-01-30T22:57:10.0433040Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment