代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Pub.Class
{
/// <summary>
/// 日期操作类
/// </summary>
public class Date
{

#region ExactTimeSpan
/// <summary>
/// 精确的时间差
/// </summary>
/// <example>
/// <code>
/// context.items["beginrequesttickcount"]=a.gettickcount();
/// 代码段
/// new TimeSpan(A.GetTickCount()-(long)Context.Items["BeginRequestTickCount"]).TotalMilliseconds
/// </code>
/// </example>
public class ExactTimeSpan {
[DllImport(
"kernel32.dll")]
static extern bool QueryPerformanceCounter([In, Out] ref long lpPerformanceCount);
[DllImport(
"kernel32.dll")]
static extern bool QueryPerformanceFrequency([In, Out] ref long lpFrequency);
static long _f = 0;
/// <summary>
/// 来纪录程序开始时的TickCount
/// </summary>
/// <returns>TickCount</returns>
static public long GetTickCount() {
long f = _f;
if (f == 0)
if (QueryPerformanceFrequency(ref f)) _f = f; else _f = -1;

if (f == -1) return Environment.TickCount * 10000;
long c = 0;
QueryPerformanceCounter(
ref c);
return (long)(((double)c) * 1000 * 10000 / ((double)f));
}

static long _tc = 0;//GetTickCount()为0时的DateTime.Ticks值

/// <summary>
/// 这个返回的不是真正的精确时间,但时间与时间的差是精确的。
/// GetExactNow与DateTime.Now的偏差比DateTime.Now的精度还要小
/// </summary>
/// <returns>时间与时间差</returns>
static public DateTime GetExactNow() {
if (_tc == 0) {
long tc = GetTickCount();
DateTime dt
= DateTime.Now;
_tc
= dt.Ticks - tc;
return dt;
}
return new DateTime(_tc + GetTickCount());
}
}
#endregion

}
}

 

相关文章:

  • 2021-08-19
  • 2021-05-18
  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2021-11-04
  • 2021-06-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-07
  • 2021-09-02
  • 2021-10-08
  • 2021-11-13
相关资源
相似解决方案