C# 获取当前 GMT 时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/516788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:27:11  来源:igfitidea点击:

Getting current GMT time

c#.nettimezoneutcntp

提问by user62958

Is there a method in C# that returns the UTC (GMT) time zone? Not based on the system's time.

C# 中是否有返回 UTC (GMT) 时区的方法?不是基于系统的时间。

Basically I want to get the correct UTC time even if my system time is not right.

基本上,即使我的系统时间不正确,我也想获得正确的 UTC 时间。

回答by Jon Skeet

Not based on the system's time? You'd need to make a call to a network time service or something similar. You could write an NTPclient, or just screenscrape World Clock;)

不是基于系统的时间?您需要拨打网络时间服务或类似的电话。你可以写一个NTP客户端,或者只是屏幕抓取世界时钟;)

I don't believe .NET has an NTP client built in, but there are quite a few available.

我不相信 .NET 有内置的 NTP 客户端,但有很多可用的.

回答by Sciolist

If I were to wager a guess for how to get a guaranteed accurate time, you'd have to find / write some NNTP class to get the time off of a time server.

如果我要猜测如何获得有保证的准确时间,您必须找到/编写一些 NNTP 类才能从时间服务器中获取时间。

If you search C# NTPon google you can find a few implementations, otherwise check the NTPprotocol.

如果您在 google 上搜索C# NTP,您可以找到一些实现,否则请检查NTP协议。

回答by Rob Prouse

If your system time is not right, nothing that you get out of the DateTime class will help. Your system can sync the time with time servers though, so if that is turned on, the various DateTime UTC methods/properties will return the correct UTC time.

如果您的系统时间不正确,您从 DateTime 类中获得的任何内容都无济于事。不过,您的系统可以与时间服务器同步时间,因此如果启用该功能,各种 DateTime UTC 方法/属性将返回正确的 UTC 时间。

回答by Nicki

I use this from UNITY

我用这个来自 UNITY

//Get a NTP time from NIST
//do not request a nist date more than once every 4 seconds, or the connection will be refused.
//more servers at tf.nist.goc/tf-cgi/servers.cgi
public static DateTime GetDummyDate()
{
    return new DateTime(1000, 1, 1); //to check if we have an online date or not.
}
public static DateTime GetNISTDate()
{
    Random ran = new Random(DateTime.Now.Millisecond);
    DateTime date = GetDummyDate();
    string serverResponse = string.Empty;

    // Represents the list of NIST servers
    string[] servers = new string[] {
        "nist1-ny.ustiming.org",
        "time-a.nist.gov",
        "nist1-chi.ustiming.org",
        "time.nist.gov",
        "ntp-nist.ldsbc.edu",
        "nist1-la.ustiming.org"                         
    };

    // Try each server in random order to avoid blocked requests due to too frequent request
    for (int i = 0; i < 5; i++)
    {
        try
        {
            // Open a StreamReader to a random time server
            StreamReader reader = new StreamReader(new System.Net.Sockets.TcpClient(servers[ran.Next(0, servers.Length)], 13).GetStream());
            serverResponse = reader.ReadToEnd();
            reader.Close();

            // Check to see that the signature is there
            if (serverResponse.Length > 47 && serverResponse.Substring(38, 9).Equals("UTC(NIST)"))
            {
                // Parse the date
                int jd = int.Parse(serverResponse.Substring(1, 5));
                int yr = int.Parse(serverResponse.Substring(7, 2));
                int mo = int.Parse(serverResponse.Substring(10, 2));
                int dy = int.Parse(serverResponse.Substring(13, 2));
                int hr = int.Parse(serverResponse.Substring(16, 2));
                int mm = int.Parse(serverResponse.Substring(19, 2));
                int sc = int.Parse(serverResponse.Substring(22, 2));

                if (jd > 51544)
                    yr += 2000;
                else
                    yr += 1999;

                date = new DateTime(yr, mo, dy, hr, mm, sc);

                // Exit the loop
                break;
            }
        }
        catch (Exception ex)
        {
            /* Do Nothing...try the next server */
        }
    }
return date;
}

回答by Alex

Instead of calling

而不是打电话

DateTime.Now.ToUniversalTime()

you can call

你可以打电话

DateTime.UtcNow

Same thing but shorter :) Documentation here.

同样的事情,但更短:) 文档在这里