Click here to Skip to main content
15,888,073 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I want to show clock to users regardless system time.it may system time is not set.
when i use this code:

C#
DateTime.UtcNow


and my system time is not correct,this time too is not correct.

how do i utcNow with a time zone that save in database.

i want to get utcNow.TimeOfDay and sum with timeZone and show to user,correct time.

how can do it?


thank you in advance.
Posted
Comments
Nathan Minier 15-Dec-15 7:43am    
NTP to the rescue!

There are a couple of sample methods that will get you rolling on this SO thread:
http://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c
Jochen Arndt 15-Dec-15 7:51am    
If you need a correct time source you must provide one. This is usually the system time. You may also get it from a time server using NTP. But the system is able to do the same and that is the preferred way.

Some time zone definition will not solve your problem. Handling with time zones is not necessary and will introduce additional imponderabilia.
Sergey Alexandrovich Kryukov 15-Dec-15 11:09am    
All correct, would make a fair formal answer.
I would start with this: System.DateTime.UtcNow does work correctly, but the system time might be wrong. Strictly speaking, time zone is irrelevant. A system can be set to any valid time zone, as soon as the time set is consistent with that zone. And it does not matter how the time is set with NTP or just manually; it just has to be correct.
—SA
NewWebDesigner 16-Dec-15 2:21am    
ok,but you say time can set manually!when time is not correct,utcNow too not correct.
is there other way to get right time,without using NTP or API?
If Server Time is correct,and client system Time is not,is it UTCNOW correct?

To do this functionality you will need a NTP client. There are several implementations, but over time it will drift, due to the internal task switcher in the OS, so periodically you will have to update the over NTP to get the correct value.

To answer your previous question, the server UTC time will be correct, regardless of your current system time. That's the whole point of NTP. Make your life easier: don't try to reimplement the wheel :)
 
Share this answer
 
Comments
NewWebDesigner 17-Dec-15 4:20am    
ok,can you give me an example of use NTP client?I dont know about it.
Code from http://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c[^]
C#
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
    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));
}
 
Share this answer
 
Comments
NewWebDesigner 17-Dec-15 6:41am    
Thank you.very good.
CHill60 17-Dec-15 8:55am    
Posting more than one solution to a question can be confusing for users that have got here via Google - they have to read all comments to understand why you have posted 2 solutions. Did you know you can use the "Improve Solution" link to add extra information to previous solutions you have posted?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900