简体   繁体   English

C#中的Java System.currentTimeMillis()等效

[英]Java System.currentTimeMillis() equivalent in C#

在C#中,Java的System.currentTimeMillis()相当于什么?

An alternative: 替代:

private static readonly DateTime Jan1st1970 = new DateTime
    (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static long CurrentTimeMillis()
{
    return (long) (DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}

A common idiom in Java is to use the currentTimeMillis() for timing or scheduling purposes, where you're not interested in the actual milliseconds since 1970, but instead calculate some relative value and compare later invocations of currentTimeMillis() to that value. Java中常见的习惯用法是使用currentTimeMillis()进行计时或调度,你不会对自1970年以来的实际毫秒数感兴趣,而是计算一些相对值并将currentTimeMillis()后续调用与该值进行比较。

If that's what you're looking for, the C# equivalent is Environment.TickCount . 如果这就是你要找的东西,那么C#等价物就是Environment.TickCount

If you are interested in TIMING, add a reference to System.Diagnostics and use a Stopwatch. 如果您对TIMING感兴趣,请添加对System.Diagnostics的引用并使用秒表。

For example: 例如:

var sw = Stopwatch.StartNew();
...
var elapsedStage1 = sw.ElapsedMilliseconds;
...
var elapsedStage2 = sw.ElapsedMilliseconds;
...
sw.Stop();

the System.currentTimeMillis() in java returns the current time in milliseconds from 1/1/1970 java中的System.currentTimeMillis()返回从1970年1月1日起的当前时间(以毫秒为单位)

c# that would be c#将是

public static double GetCurrentMilli()
    {
        DateTime Jan1970 = new DateTime(1970, 1, 1, 0, 0,0,DateTimeKind.Utc);
        TimeSpan javaSpan = DateTime.UtcNow - Jan1970;
        return javaSpan.TotalMilliseconds;
    }

edit: made it utc as suggested :) 编辑:按建议制作utc :)

We could also get a little fancy and do it as an extension method, so that it hangs off the DateTime class: 我们也可以稍微看一下它作为扩展方法,以便它挂起DateTime类:

public static class DateTimeExtensions
{
    private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    public static long currentTimeMillis(this DateTime d)
    {
        return (long) ((DateTime.UtcNow - Jan1st1970).TotalMilliseconds);
    }
}

Here is a simple way to approximate the Unix timestamp. 这是一种近似Unix时间戳的简单方法。 Using UTC is closer to the unix concept, and you need to covert from double to long . 使用UTC更接近unix概念,你需要从doublelong

TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
long millis = (long)ts.TotalMilliseconds;
Console.WriteLine("millis={0}", millis);

prints: 打印:

millis=1226674125796
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()

我只是考虑如何实现你一直在努力的最直接的方式如下:

DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond

该框架不包括自1970年以来的旧秒(或毫秒)。您得到的最接近的是DateTime.Ticks,它是自0001年1月1日以来100纳秒的数量。

If you want a timestamp to be compared between different processes, different languages (Java, C, C#), under GNU/Linux and Windows (Seven at least): 如果你想在不同的进程,不同的语言(Java,C,C#)之间比较时间戳,在GNU / Linux和Windows下(至少七个):

C#: C#:

private static long nanoTime() {
   long nano = 10000L * Stopwatch.GetTimestamp();
   nano /= TimeSpan.TicksPerMillisecond;
   nano *= 100L;
   return nano;
}

Java: Java的:

java.lang.System.nanoTime();

C GNU/Linux: C GNU / Linux:

static int64_t hpms_nano() {
   struct timespec t;
   clock_gettime( CLOCK_MONOTONIC, &t );
   int64_t nano = t.tv_sec;
   nano *= 1000;
   nano *= 1000;
   nano *= 1000;
   nano += t.tv_nsec;
   return nano;
}

C Windows: C Windows:

static int64_t hpms_nano() {
   static LARGE_INTEGER ticksPerSecond;
   if( ticksPerSecond.QuadPart == 0 ) {
      QueryPerformanceFrequency( &ticksPerSecond );
   }
   LARGE_INTEGER ticks;
   QueryPerformanceCounter( &ticks );
   uint64_t nano = ( 1000*1000*10UL * ticks.QuadPart ) / ticksPerSecond.QuadPart;
   nano *= 100UL;
   return nano;
}

I know question asks for equivalent but since I use those 2 for the same tasks I throw in GetTickCount . 我知道问题要求等价,但是因为我使用那些2来执行相同的任务,所以我会把它放在GetTickCount中 I might be nostalgic but System.currentTimeMillis() and GetTickCount() are the only ones I use for getting ticks. 我可能怀旧,但System.currentTimeMillis()和GetTickCount()是我用来获取刻度的唯一。

[DllImport("kernel32.dll")]
static extern uint GetTickCount();

// call
uint ticks = GetTickCount();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM