locked
[VB.NET] how to get current time from link using httprequest RRS feed

  • Question

  • hi guys..

    how to get current time from link using httprequest

    i founded this website https://www.timeanddate.com/worldclock/iraq/najaf

    just i need get this info in image



    Friday, June 9, 2017 2:48 PM

Answers

  • You should use documented APIs like Google Maps

    For Najaf like in your sample, you get the location with :

    http://maps.googleapis.com/maps/api/geocode/json?address=Najaf,Irak

    then you get the time offsets from the location

    https://maps.googleapis.com/maps/api/timezone/json?location=32.027376,44.3331165&timestamp=1497037528

    which gives 10800 = UTC + 3


    • Edited by Castorix31 Saturday, June 10, 2017 2:45 AM
    • Marked as answer by Reed KimbleMVP Monday, June 26, 2017 12:48 PM
    Friday, June 9, 2017 7:55 PM
  • KORG,

    You might want to have a look at this API which is free to use (non-commercial):

    https://timezonedb.com/

    So long as you don't hit the server any faster than once per second (I think that's it but look at the terms), it works pretty well.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, June 12, 2017 10:43 AM
  • Just hit a NIST server (time.nist.gov) with a NTP, DAYTIME, or TIME request and then adjust for the known time zone (which has already been determined to be UTC +3).

    The DAYTIME request is probably the simplest to implement, though NTP is encouraged and more robust.  However you probably only need second-resolution so DAYTIME is probably sufficient.

    You can get a DAYTIME result by simply opening a TCP connection on port 13 to a NIST server (or the default address above) and reading all ASCII data returned until the server closes the connection.  The resulting string contains the UTC time along with some other time format and service related info.  If you use the default time.nist.gov you may need to try several times until you get a response.  This should be used for regular polling.  If you are just getting the time once, or on demand, then you might pick a server closer to you or with low apparent utilization.

    Here's a little example code:

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim resultTime As Nullable(Of DateTime) = TryGetNistTime(3)
            If resultTime.HasValue Then
                TextBox1.Text = resultTime.Value.ToString
            Else
                TextBox1.Text = "Unable to get time... try again in a few moments."
            End If
        End Sub
    
        Private Function TryGetNistTime() As Nullable(Of DateTime)
            Return TryGetNistTime(0)
        End Function
    
        Private Function TryGetNistTime(utcOffset As Integer) As Nullable(Of DateTime)
            Dim resultText As New Text.StringBuilder
            Try
                Using client As New Net.Sockets.TcpClient("time-c.nist.gov", 13)
                    While client.Connected
                        Using reader As New IO.StreamReader(client.GetStream)
                            resultText.Append(reader.ReadToEnd)
                        End Using
                    End While
                End Using
                If resultText.Length > 0 Then
                    Try
                        Dim parts() As String = resultText.ToString.Split(" "c)
                        Return DateTime.ParseExact(String.Join(" ", parts(1), parts(2)), "yy-MM-dd HH:mm:ss", Globalization.CultureInfo.InvariantCulture.DateTimeFormat)
                    Catch ex As Exception
                        'a string was received, but cannot be interpreted as a date/time
                        'this error should generally not occur
                    End Try
                End If
                'the connection opened but no response was received
                'this error can occur when connecting to a busy server
            Catch ex As Exception
                'the connection failed to open
                'this error can occur when connecting in round-robin to the virtual
                'host address, or when the selected server is under load
            End Try
            Return Nothing
        End Function

    I chose the server time-c because it is geographically close to me and had low utilization at the time of this posting.

    The function might be better implemented with a custom return value which specifies the failure reason when a failure occurs, and should also offer an async version since the method can block and the blocking call has its own async version, but this should be enough of an example to get you started.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    • Edited by Reed KimbleMVP Monday, June 12, 2017 1:26 PM forgot a link and server choice info
    • Marked as answer by Reed KimbleMVP Monday, June 26, 2017 12:47 PM
    Monday, June 12, 2017 1:22 PM

All replies

  • Without paying for their service you would need to scrap the page which may very well be a violation. Even if it was not a violation (I viewed the source code) you would need to continuously download the page and parse for several elements to get the information you are after which would require a fair amount of parsing. 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, June 9, 2017 4:21 PM
  • You should use documented APIs like Google Maps

    For Najaf like in your sample, you get the location with :

    http://maps.googleapis.com/maps/api/geocode/json?address=Najaf,Irak

    then you get the time offsets from the location

    https://maps.googleapis.com/maps/api/timezone/json?location=32.027376,44.3331165&timestamp=1497037528

    which gives 10800 = UTC + 3


    • Edited by Castorix31 Saturday, June 10, 2017 2:45 AM
    • Marked as answer by Reed KimbleMVP Monday, June 26, 2017 12:48 PM
    Friday, June 9, 2017 7:55 PM
  • You should use documented APIs like Google Maps

    For Najaf like in your sample, you get the location with :

    http://maps.googleapis.com/maps/api/geocode/json?address=Najaf,Irak

    then you get the time offsets from the location

    https://maps.googleapis.com/maps/api/timezone/json?location=32.027376,44.3331165&timestamp=1497037528

    which gives 10800 = GMT + 3


    he get me:

    dstOffset
    0
    rawOffset 10800
    status "OK"
    timeZoneId "Asia/Baghdad"
    timeZoneName

    "Arabian Standard Time"

    where is the time ??

    i see this topic but i don't understand anything

    http://www.javascriptkit.com/dhtmltutors/local-time-google-time-zone-api.shtml

    Friday, June 9, 2017 8:53 PM
  • > where is the time ??

    rawOffset 10800

    10800 = 3 hours, so UTC + 3

    from

     DateTime utcTime = DateTime.UtcNow;

    Saturday, June 10, 2017 2:45 AM
  • > where is the time ??

    rawOffset 10800

    10800 = 3 hours, so UTC + 3

    from

     DateTime utcTime = DateTime.UtcNow;

    i'm sorry man but i'm very stupid :/

    please give me full code in vb.net

    Saturday, June 10, 2017 3:34 AM
  • Hi KORG,

    According to your description, your issue is more related about VB.NET. And this forum is discussing and asking questions about the Windows Form and ClickOnce,  I will move this thread to corresponding forum: VB.NET for dedicated information. You will get a more professional support from there, thank you for your understanding.

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 12, 2017 2:43 AM
  • KORG,

    You might want to have a look at this API which is free to use (non-commercial):

    https://timezonedb.com/

    So long as you don't hit the server any faster than once per second (I think that's it but look at the terms), it works pretty well.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, June 12, 2017 10:43 AM
  • > where is the time ??

    rawOffset 10800

    10800 = 3 hours, so UTC + 3

    from

     DateTime utcTime = DateTime.UtcNow;

    i'm sorry man but i'm very stupid :/

    please give me full code in vb.net

    Use WebRequest Class

    parse the result

    and add/subtract the number of hours  to DateTime.UtcNow


    Monday, June 12, 2017 10:49 AM
  • Just hit a NIST server (time.nist.gov) with a NTP, DAYTIME, or TIME request and then adjust for the known time zone (which has already been determined to be UTC +3).

    The DAYTIME request is probably the simplest to implement, though NTP is encouraged and more robust.  However you probably only need second-resolution so DAYTIME is probably sufficient.

    You can get a DAYTIME result by simply opening a TCP connection on port 13 to a NIST server (or the default address above) and reading all ASCII data returned until the server closes the connection.  The resulting string contains the UTC time along with some other time format and service related info.  If you use the default time.nist.gov you may need to try several times until you get a response.  This should be used for regular polling.  If you are just getting the time once, or on demand, then you might pick a server closer to you or with low apparent utilization.

    Here's a little example code:

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim resultTime As Nullable(Of DateTime) = TryGetNistTime(3)
            If resultTime.HasValue Then
                TextBox1.Text = resultTime.Value.ToString
            Else
                TextBox1.Text = "Unable to get time... try again in a few moments."
            End If
        End Sub
    
        Private Function TryGetNistTime() As Nullable(Of DateTime)
            Return TryGetNistTime(0)
        End Function
    
        Private Function TryGetNistTime(utcOffset As Integer) As Nullable(Of DateTime)
            Dim resultText As New Text.StringBuilder
            Try
                Using client As New Net.Sockets.TcpClient("time-c.nist.gov", 13)
                    While client.Connected
                        Using reader As New IO.StreamReader(client.GetStream)
                            resultText.Append(reader.ReadToEnd)
                        End Using
                    End While
                End Using
                If resultText.Length > 0 Then
                    Try
                        Dim parts() As String = resultText.ToString.Split(" "c)
                        Return DateTime.ParseExact(String.Join(" ", parts(1), parts(2)), "yy-MM-dd HH:mm:ss", Globalization.CultureInfo.InvariantCulture.DateTimeFormat)
                    Catch ex As Exception
                        'a string was received, but cannot be interpreted as a date/time
                        'this error should generally not occur
                    End Try
                End If
                'the connection opened but no response was received
                'this error can occur when connecting to a busy server
            Catch ex As Exception
                'the connection failed to open
                'this error can occur when connecting in round-robin to the virtual
                'host address, or when the selected server is under load
            End Try
            Return Nothing
        End Function

    I chose the server time-c because it is geographically close to me and had low utilization at the time of this posting.

    The function might be better implemented with a custom return value which specifies the failure reason when a failure occurs, and should also offer an async version since the method can block and the blocking call has its own async version, but this should be enough of an example to get you started.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    • Edited by Reed KimbleMVP Monday, June 12, 2017 1:26 PM forgot a link and server choice info
    • Marked as answer by Reed KimbleMVP Monday, June 26, 2017 12:47 PM
    Monday, June 12, 2017 1:22 PM