0

I have a program that send me a time for any account.

For some people, the program sends the datetime to the database like 2020-05-17 11:33:00.

For some people, the program sends the datetime to the database like 0000-00-00 00:00:00.

A photo to prove this:

DateTime Photo

The code which get the datetime is:

Imports System.Globalization
Imports System.Net

    Public Shared Function GetNistTime() As DateTime
        Dim myHttpWebRequest = CType(WebRequest.Create("http://www.microsoft.com"), HttpWebRequest)

        Dim response = myHttpWebRequest.GetResponse()
        Dim todaysDates As String = response.Headers("date")
        Return DateTime.ParseExact(todaysDates, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AssumeUniversal)
    End Function

So what is the solution? What can these people install on their machines to prevent this, like install .NET Framework 3.5?

I don't know if installing .NET Framework 3.5 can solve this problem!

I think it can solve the problem because I found it on my PC, and on another PC where I get the datetime like 0000-00-00 00:00:00 doesn't have it installed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Do you need to get the time from the Microsoft servers? Can't you use `DateTime.UtcNow`? – Jimi May 17 '20 at 10:14
  • It's not clear whether this date/time is sent to you or a server of yours, from your clients or you receive it from an online service that stores what Users send to it (which would make more sense if this *procedure* is some sort of validation). – Jimi May 17 '20 at 10:20
  • it just take the datetime to make a membership datetime to the customer @Jimi – Eslam Alawy May 18 '20 at 08:42
  • @Jimi which package support that ! like `.Net 3.5` – Eslam Alawy May 18 '20 at 08:43
  • Supports what? You clients send you an empty DateTime when you cannot reach the server or the server doesn't answer. Use the DateTime.UtcNow instead of opening an Internet connection. – Jimi May 18 '20 at 09:18
  • 2
    It's bad manners (at minimum) and a subpar design (to put it extremely kindly) to pull the Microsoft home page just to grab the date from the headers. If you want an authoritative date/time, get it from a time server, which is designed and intended to provide an authoritative date/time. – Craig May 18 '20 at 15:16
  • Make sure that `response.Headers("date")` is not returning a blank string, and that it is formatted the way you are expecting. The HTTP spec allows the [`Date`](https://tools.ietf.org/html/rfc2616#section-14.18) header to be specified in [several different formats](https://tools.ietf.org/html/rfc2616#section-3.3), but you are only parsing one format and ignoring the others. – Remy Lebeau May 18 '20 at 17:15
  • I suggest that instead of getting the time from the client, you write the server code to use the database's time/date function. – Andrew Morton May 18 '20 at 17:34
  • @AndrewMorton i got the time by this query `SELECT NOW() FROM DUAL` how i can save the value of this query in my tabel variable like `insert into table (date) values ('SELECT NOW() FROM DUAL')` – Eslam Alawy May 20 '20 at 16:51
  • @EslamAlawy Please see [In Oracle SQL: How do you insert the current date + time into a table?](https://stackoverflow.com/q/32939206/1115360) – Andrew Morton May 20 '20 at 16:59
  • @AndrewMorton i got some errors , bro i don`t understand anything can you explain a little bit , please – Eslam Alawy May 20 '20 at 17:10
  • @AndrewMorton i want to insert the value which came from this command `SELECT NOW() FROM DUAL` to my table – Eslam Alawy May 20 '20 at 17:17
  • @EslamAlawy I am not familiar with PL/SQL, but I guess `insert into tablename (date) values (SELECT NOW() FROM DUAL)` - without the single quotes - would work. – Andrew Morton May 20 '20 at 17:18
  • @AndrewMorton i used it and got this Exception message https://ibb.co/RTKJkyK – Eslam Alawy May 20 '20 at 17:22
  • @Jimi can you help ? with this , read the comments – Eslam Alawy May 20 '20 at 17:24

1 Answers1

0

According to your saying, you should firstly verify if it's just due to problem related to .NET Framework 3.5 installation. If it isn't, then try this code:

Try
    Dim client = New TcpClient("time.nist.gov", 13)
    Dim myDateTime As Date

    Using streamReader = New StreamReader(client.GetStream())
        Dim response = streamReader.ReadToEnd()
        Dim utcDateTimeString = response.Substring(7, 17)

        Dim localDateTime = Date.ParseExact(utcDateTimeString, "yy-MM-dd HH:mm:ss",
                    CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal)

        myDateTime = localDateTime
    End Using

    MsgBox(myDateTime.ToString)

    Catch ex As SocketException
        MsgBox("Unable to connect to the time server.", "Connection Failed!")
End Try
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34