如果电脑本地时间错误,如何获得正确的时区日期时间



如何获取带有时区信息的日期时间(GMT/UTC+7)?

假设在实时中是06:00 PM,然后有人将本地PC时间更改为01:00 PM。如何获取日期时间06:00 PM

我试过这个:

System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time");
DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tzi);
Console.WriteLine(localTime);

但我还是得了01:00 PM

只需联系NTP服务器,慢慢来。在stackoverflow上找到代码。当然,你需要连接互联网才能做到这一点。

public static DateTime GetNetworkTime() {
//default Windows time server
const string ntpServer = "time.windows.com";
// NTP message size - 16 bytes of the digest (RFC 2030)
var ntpData = new byte[48];
//Setting the Leap Indicator, Version Number and Mode values
ntpData[0] = 0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
var addresses = Dns.GetHostEntry(ntpServer).AddressList;
//The UDP port number assigned to NTP is 123
var ipEndPoint = new IPEndPoint(addresses[0], 123);
//NTP uses UDP
using(var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
socket.Connect(ipEndPoint);
//Stops code hang if NTP is blocked
socket.ReceiveTimeout = 3000;     
socket.Send(ntpData);
socket.Receive(ntpData);
socket.Close();
}
//Offset to get to the "Transmit Timestamp" field (time at which the reply 
//departed the server for the client, in 64-bit timestamp format."
const byte serverReplyTime = 40;
//Get the seconds part
ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);
//Get the seconds fraction
ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);
//Convert From big-endian to little-endian
intPart = SwapEndianness(intPart);
fractPart = SwapEndianness(fractPart);
var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
//**UTC** time
var networkDateTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
return networkDateTime.ToLocalTime(); }
// stackoverflow.com/a/3294698/162671 static uint SwapEndianness(ulong x) {
return (uint) (((x & 0x000000ff) << 24) +
((x & 0x0000ff00) << 8) +
((x & 0x00ff0000) >> 8) +
((x & 0xff000000) >> 24)); }

如果你根本不想信任用户电脑上的信息,那么你需要:

  1. 时区数据的备用来源
  2. 网络时间源,例如通过NTP

您可以使用Noda Time实现第一个,使用我的NodaTime.NetworkClock插件实现第二个。在内部,它使用与Martin的回答类似的NTP代码。

public static DateTime GetRealTimeInZone(string timeZoneId)
{
var clock = NetworkClock.Instance;
var now = clock.GetCurrentInstant();
var tz = DateTimeZoneProviders.Tzdb[timeZoneId];
return now.InZone(tz).ToDateTimeUnspecified();
}

用法:

DateTime dt = GetRealTimeInZone("Asia/Bangkok");

或者,如果你确实信任本地时区设置,只是不信任时钟,那么:

public static DateTime GetRealTimeInZone()
{
var clock = NetworkClock.Instance;
var now = clock.GetCurrentInstant();
var tz = DateTimeZoneProviders.Tzdb.GetSystemDefault();
return now.InZone(tz).ToDateTimeUnspecified();
}

用法:

DateTime dt = GetRealTimeInZone();

相关内容

  • 没有找到相关文章

最新更新