Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can get DateTime From Internet (External Resource - Not From Server)

how can i get current time From Internet (External Resource - Not From Server)?
Edited
For Example From the Below WebSite :
http://www.timeanddate.com/worldclock
Reason
i will redirect my pages to a Lock page after one month (by checking DateTime.Now) and in that page user should input activation code for comming back...
for some security reasons i want to get the current Date/Time From Out Of My Server...

thanks in advance

like image 945
SilverLight Avatar asked Jul 13 '11 08:07

SilverLight


2 Answers

I think this will help you out of it.Apply the below mentioned code for retrieving the date from Internet.

public static DateTime GetFastestNISTDate()
{
    var result = DateTime.MinValue;

    // Initialize the list of NIST time servers
    // http://tf.nist.gov/tf-cgi/servers.cgi
    string[] servers = new string[] {
        "nist1-ny.ustiming.org",
        "nist1-nj.ustiming.org",
        "nist1-pa.ustiming.org",
        "time-a.nist.gov",
        "time-b.nist.gov",
        "nist1.aol-va.symmetricom.com",
        "nist1.columbiacountyga.gov",
        "nist1-chi.ustiming.org",
        "nist.expertsmi.com",
        "nist.netservicesgroup.com"
};

        // Try 5 servers in random order to spread the load
        Random rnd = new Random();
        foreach (string server in servers.OrderBy(s => rnd.NextDouble()).Take(5))
        {
            try
            {
                // Connect to the server (at port 13) and get the response
                string serverResponse = string.Empty;
                using (var reader = new StreamReader(new System.Net.Sockets.TcpClient(server, 13).GetStream()))
                {
                    serverResponse = reader.ReadToEnd();
                }

                // If a response was received
                if (!string.IsNullOrEmpty(serverResponse))
                {
                    // Split the response string ("55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST) *")
                    string[] tokens = serverResponse.Split(' ');

                    // Check the number of tokens
                    if (tokens.Length >= 6)
                    {
                        // Check the health status
                        string health = tokens[5];
                        if (health == "0")
                        {
                            // Get date and time parts from the server response
                            string[] dateParts = tokens[1].Split('-');
                            string[] timeParts = tokens[2].Split(':');

                            // Create a DateTime instance
                            DateTime utcDateTime = new DateTime(
                                Convert.ToInt32(dateParts[0]) + 2000,
                                Convert.ToInt32(dateParts[1]), Convert.ToInt32(dateParts[2]),
                                Convert.ToInt32(timeParts[0]), Convert.ToInt32(timeParts[1]),
                                Convert.ToInt32(timeParts[2]));

                            // Convert received (UTC) DateTime value to the local timezone
                            result = utcDateTime.ToLocalTime();

                            return result;
                            // Response successfully received; exit the loop

                        }
                    }

                }

            }
            catch
            {
                // Ignore exception and try the next server
            }
        }
        return result;
    }
like image 78
palaniappan Avatar answered Nov 15 '22 08:11

palaniappan


You can't, not by using DateTime.Now. It is a property of the DateTime structure that can't be overridden.

You can use an NTP server to get time (though there is not support in the BCL for one).

like image 45
Oded Avatar answered Nov 15 '22 09:11

Oded