2009年11月26日 星期四

C# Timestamp

1.Define a class, HiResDateTime

public class HiResDateTime
{
private static DateTime _startTime;
private static System.Diagnostics.Stopwatch _stopWatch = null;
private static TimeSpan _maxIdle = TimeSpan.FromSeconds(10);

public static DateTime UtcNow
{
get
{
if ((_stopWatch == null) ||
(_startTime.Add(_maxIdle) < DateTime.UtcNow))
{
Reset();
}
return _startTime.AddTicks(_stopWatch.Elapsed.Ticks);
}
}

private static void Reset()
{
_startTime = DateTime.UtcNow;
_stopWatch = System.Diagnostics.Stopwatch.StartNew();
}
}


2.How to use:

Console.WriteLine("{0}", HiResDateTime.UtcNow.ToString("yyyyMMddHHmmssffff"));

沒有留言: