采用这一方法(C#)使“团结时间”流出:
long UnixTime()
{
return (long) (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
}
<>strong> Question - 是否有办法在nanoseconds上获得不准确的时间?
Thanks in advance.
采用这一方法(C#)使“团结时间”流出:
long UnixTime()
{
return (long) (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
}
<>strong> Question - 是否有办法在nanoseconds上获得不准确的时间?
Thanks in advance.
计算本身并非硬性:
long UnixTime()
{
DateTime epochStart=new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return (DateTime.UtcNow - epochStart).Ticks*100;
}
<代码>Datetime和timeSpan
内部储存了一整套标准,其中一条标准为100人。 我也具体指明了时代开始是万国邮联的时间,因为我很想把<条码>Datetime与不同的<条码>Kind<>/code>相隔,即使该编码有效。
但<代码>Datetime.UtcNow的准确性很低。 它只是每几秒更新一次(图形数值在1米和16米之间有所不同)。
取得固定格式,可使用<代码>StopWatch。 由于你不需要绝对的时间。 但是,如果你走到这个道路,你就必须忙.地等待。 自Thread.Sleep
以来,时间者......受到同样的限制。
或者,你可以使用<条码>日码>(BeginPeriod(1),强迫窗户更新锁和操作时间,每秒钟。 但这是一种全球环境,增加了电力消费。 但这仍比忙.更有利。
为衡量时间差异,请以<条码>为基础。 QueryPerformanceCounter,但这涉及其自身的一系列问题,例如不同核心之间的代议。 I 看到的机器是。 履历 当你准备在另一个核心上进行时,我们会发生摩擦。
http://msdn.microsoft.com/en-us/library/system.timespan. Totalmilliseconds.aspx” rel=“nofollow>TotalMilliseconds 归还财产 包含全程
So, you only have to multiply its value by 1000000
to obtain nanoseconds:
return (long) ((DateTime.UtcNow
- new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds * 1000000.0);
I think this is not so easy (on trivial desktop x86 computer) doe to problems with precision. So first of all DateTime class is useless in this situation. You may use StopWatch
http://www.codeproject.com/KB/testing/stopwatch-ective-precise.aspx” rel=“nofollow noreferer” http://www.codeproject.com/KB/testing/stopwatch- Measures-precise.aspx
这一班子将提供帮助。 这使你能够从“九”改为“视窗”。 可能不需要更新评论意见,但所有意见都很完善。
public sealed class LinuxToWindowsFileTimeConverter : IValueConverter
{
static long ticksFrom1601To1970 = (long)(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) - DateTime.FromFileTimeUtc(0)).Ticks;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return new DateTime();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return new DateTime();
}
public static DateTime Convert(Int64 nanoSecsSince1970)
{
return Convert(nanoSecsSince1970, ScaleFactor.Billion);
}
/// <summary>
/// Converts from Linux seconds to Windows DateTime
/// </summary>
/// <param name="secs"></param><remarks> secs</remarks>
/// <param name="sf"></param><remarks>specifies scale factor.
/// Specify ScaleFactor.One for secs since 1970.
/// ScaleFactor.Thousand for milli (10^3) seconds since 1970.
/// ScaleFactor.Million for micro (10^6)seconds since 1970.
/// ScaleFactor.Billion for nano (10^9)seconds since 1970.
/// etc.</remarks>
/// <returns></returns>
public static DateTime Convert(Int64 secs, ScaleFactor sf)
{
long hndrdnsfrom1601 = 0;
switch(sf)
{
case ScaleFactor.Billion:
hndrdnsfrom1601 = ticksFrom1601To1970 + secs / 100;
break;
default:
// TODO: Correct for other cases.
hndrdnsfrom1601 = (long)ticksFrom1601To1970 + (secs * (long)ScaleFactor.TenMillion / (long)sf);
break;
}
return DateTime.FromFileTimeUtc(hndrdnsfrom1601);
}
public static long ConvertBack(DateTime dateTimeInUTC)
{
if (dateTimeInUTC == new DateTime())
dateTimeInUTC = new DateTime(1980, 1,1).ToUniversalTime();
long secsSince1970 = (dateTimeInUTC.ToFileTimeUtc() - ticksFrom1601To1970) * ((long)ScaleFactor.Billion / (long)ScaleFactor.TenMillion);
return secsSince1970;
}
public Int64 ConvertBack(DateTime dateTimeInUTC, CultureInfo culture)
{
return ConvertBack(dateTimeInUTC, culture, ScaleFactor.Billion);
}
/// <summary>
/// Converts from Windows file time to Linux seconds.
/// </summary>
/// <param name="dateTimeInUTC"></param>
/// <param name="culture"></param>
/// <param name="sf"></param><remarks>
/// Specify ScaleFactor.One for secs since 1970.
/// ScaleFactor.Thousand for milli (10^3) seconds since 1970.
/// ScaleFactor.Million for micro (10^6)seconds since 1970.
/// ScaleFactor.Billion for nano (10^9)seconds since 1970.
/// </remarks>
/// <returns></returns>
public Int64 ConvertBack(DateTime dateTimeInUTC, CultureInfo culture, ScaleFactor sf)
{
long secsSince1970 = (dateTimeInUTC.ToFileTimeUtc() - ticksFrom1601To1970) * ((long)sf / (long)ScaleFactor.TenMillion);
return secsSince1970;
}
}
public enum ScaleFactor : long
{
One = 1,
Ten = 10,
Hundred = 100,
Thousand = 1000,
TenThou = 10000,
HundredThou = 100000,
Million = 1000000,
TenMillion = 10000000,
HundredMillion = 100000000,
Billion = 1000000000,
TenBillion = 10000000000,
HundredBillion = 100000000000
}
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...