-4

I am new in programming and I'm practicing with a project and I need a DateTime for it. But as you know windows DateTime can be changed by user so I need a more solid time. So far I found this two different codes. (I don't need local time. I need a time that no matter where your location is it gives you the same time.)

So here is first way to get time :

private void button1_Click(object sender, EventArgs e)
    {
        DateTime dateTime = DateTime.MinValue;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
        request.Method = "GET";
        request.Accept = "text/html, application/xhtml+xml, */*";
        request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
        request.ContentType = "application/x-www-form-urlencoded";
        request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            StreamReader stream = new StreamReader(response.GetResponseStream());
            string html = stream.ReadToEnd();
            string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
            double milliseconds = Convert.ToInt64(time) / 1000.0;
            textBox1.Text = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToString();
        }

Here is the second way :

private void button2_Click(object sender, EventArgs e)
    {
        var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
        var response = myHttpWebRequest.GetResponse();
        string todaysDates = response.Headers["date"];
        DateTime dateTime = DateTime.ParseExact(todaysDates, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AssumeUniversal);
        textBox2.Text = dateTime.ToString();
        response.Close();          
    }

So here is the questions :

Which one of this codes you recommend me to use? (I really appreciate it if you give me a better code)

Is that code requires any changes?

Is the code no matter what your location is it still gives you the same time?

Is the code always and on every computer works? (I'v read some comments about port 13 and how the code might not work depends on its status)

Armin
  • 576
  • 6
  • 13
  • 4
    No matter how you get the time, the user can always manipulate it. Why not just use the local system time? –  Oct 11 '16 at 20:11
  • I want to store it in database. The user wont get the chance to manipulate it. – Armin Oct 11 '16 at 20:16
  • Both requests shown use Port 80, so what is your concern about Port 13? – Filburt Oct 11 '16 at 20:17
  • 5
    Why not just have the db stamp the time then? – Kirby Oct 11 '16 at 20:17
  • You misunderstand. Whether you use the system clock or a web request, the result can be manipulated by the user. You can't get away from that. –  Oct 11 '16 at 20:17
  • 2
    If you're storing the time in a *real* database, *use the database server's time*. Then its always consistent with everything else in the database. @Kirby is right. – 15ee8f99-57ff-4f92-890c-b56153 Oct 11 '16 at 20:17
  • You could query an NTP server as described in a [related post](http://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c). – Axel Kemper Oct 11 '16 at 20:19
  • @Amy Yeah, but while any retard can reset the clock, it takes a little more brains to fiddle the HTTP request (not a huge amount, but still). With enough force, you can violate any security arrangement. That doesn't mean security isn't worth doing at all. It means you figure what you need and what you can afford. – 15ee8f99-57ff-4f92-890c-b56153 Oct 11 '16 at 20:19
  • @Amy - if you're using the server time then no, the user isn't going to be able to manipulate it. – Darren Wainwright Oct 11 '16 at 20:27
  • @Darren Amy's obviously talking about the user's system clock. – itsme86 Oct 11 '16 at 20:29
  • @Darren HTTP requests can be intercepted and manipulated. The user doesn't have to manipulate the time on the server, just the response. This can be done with a basic HTTP proxy. –  Oct 11 '16 at 20:40

1 Answers1

0

Use the datatime that is stored on the server your database resides within. Most databases have a function you can call to get a timestap ie sql servers is simply GETDATE(). This will ensure that your times are always from the server no matter where the client makes the post from.

edit - Ah ed made a similar comment, same principle though and the best solution.

C Smith
  • 222
  • 1
  • 6