开发者

Best way to retrieve accurate date?

What is the best way to retrieve the current date? Currently I am storing the date like this:

string date = DateTime.Now.ToString("yyyyMMdd");

Obviously, if the user c开发者_运维问答hanges the date on their system, this will also be affected.

I need a way to access an accurate date (preferable in central time) and store it as a string within my application. Any ideas?


Define "accurate"? If you cannot trust the system date, then you need to get the time from an outside source (ie, atomic clock). But the user may block your program from getting it (ie, unplugging the network cable, blocking your time query with a software firewall).

So what is it that you really want?


EDIT: This works, give it a try:

private string GetCurrentDateTime()
{
    WebRequest request = WebRequest.Create(@"http://developer.yahooapis.com/TimeService/V1/getTime?appid=Test");
    // request.Proxy = new WebProxy("PROXYSERVERNAME", 8080); // You may or may not need this
    WebResponse response = request.GetResponse();      

    Double currentTimeStamp = 0;
    using (Stream stream = response.GetResponseStream())
    {
        using (XmlTextReader xmlReader = new XmlTextReader(stream))
        {
            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (xmlReader.Name == "Timestamp")
                        {
                            currentTimeStamp = Convert.ToDouble(xmlReader.ReadInnerXml());
                        }
                        break;
                }
            }

            xmlReader.Close();
        }
    }

    DateTime yahooDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return yahooDateTime.AddSeconds(currentTimeStamp).AddHours(2).ToString("yyyyMMdd");     
}


Whatever date/time you get locally, depends on the current system's time accuracy. If you want independent accurate time, you'll need to get it externally from a time web service like Yahoo's Server Time.


You could implement a very simple web service or RESTful call on a server that would provide the result. This would work as long as you trusted that the server's date/time were correct.


Since the user can change the time on the bios you can't trust that the computer will be accurate.

Your only other choice requires that they have access to the Internet, then you can call out to a webservice, and make a call, but if you are going to be paranoid, and to limit how much they know is going on, encrypt it with RSA encryption, to verify that your service gave the date.


Always store in UTC time - you'll save yourself a load of trouble later on. Store in UTC and convert to whatever local time you need, including daylight saving time, at the time you need it for your calculation.

Also, if the time doesn't need to be human readable in storage, you could store it in Ticks to be least error-prone:

string ticks = DateTimeOffset.UtcNow.Ticks.ToString();

DateTimeOffset stampUtc = new DateTimeOffset(long.Parse(ticks), TimeSpan.Zero);

DateTimeOffset stampLocal = stampUtc.ToLocalTime();


If you need to capture the current time and don't want to be coupled to the machine the code is running on having a correct date/time set, you can use NTP to retrieve the current time from the network (or internet) ... assuming the machine is actively connected to one.

I believe the US Navy maintains a group of NTP servers. Check out: http://tycho.usno.navy.mil/ntp.html

If you need super-secure, highly resilient access to the current time, you're going to need to have some level of control over the hardware you're running on. After all, users can pull their network cable, spoof IP addresses on a network, block ports and IP addresses, change their HOSTS file, all sorts of things that could interfere with a network request for the time.


This is an old problem. I believe you're looking for NTP (Network Time Protocol). Browse around http://time.gov/about.html and you can find a public server that will give you the standard time.

Also, to use NTP, you may want to look at this. How to Query an NTP Server using C#?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜