Click here to Skip to main content
15,891,704 members
Articles / Programming Languages / Visual Basic 10
Tip/Trick

Get NTP Time (and Extras)

Rate me:
Please Sign up or sign in to vote.
4.57/5 (8 votes)
28 Jan 2020CPOL2 min read 8.9K   4   1
A quick Function to get UTC / NTP time

Introduction

This function will make it easy to get 15 pieces of information about date and time from the internet concerning the computer location requesting it. It will return JSON or plain Text. Can be used for something as simple as checking the local computer time for accuracy or any other reason that the computer time, might not be enough.

Background

I needed this information for a program I wrote, and saw other articles in locations that did not give enough information to do it or took a bit more to do it and received less. This is straight forward and easy to use.

Using the Code

I am including two functions for this. One is just a support and if you have another method to get the local IP (public) or if you want a JSON return, you can leave out FN_GETIP and " + MY_IP + ".txt". You will also need to adjust the TIME_IN variable to handle a JSON data return as well as the rest of the parsing area. To keep it simple and clear, I think the string return is the best way to display this.

To CALL the function, you create a multi- element variable.

C++
//
// DIM I_TIME() AS STRING = fn_NTPTIME()
//

When run, I_TIME() will have 15 elements in it. If you only need the date and time, you only need element at index #2. (element 3) (See list below). Here are both functions.

C++
// 
Public Function fn_NTPTIME() As String()
    'Created by Doug_Strkyer
        Dim Time_IN as string
        Dim TIME_DATA() As String
        Dim client As New System.Net.WebClient
        Dim MY_IP As String = fn_GETIP()
        Dim Time_request As String = "http://worldtimeapi.org/api/ip/" + MY_IP + ".txt"
        Time_in = client.DownloadString(Time_request)
        TIME_DATA = Split(Time_IN, Chr(10))
        Return TIME_DATA
End Function

Public Function fn_GETIP() As String
        Dim MY_IP As String
        Dim client As New System.Net.WebClient
        MY_IP = client.DownloadString("https://api.ipify.org")
        Return MY_IP
End Function
// 
//

Here is a printout from debug as to the elements in I_TIME() will be. I have added the "ITEM # =" just for clarity of the index. Item 1 will contain the public address of the requestor.

ITEM 0 = abbreviation: CST
ITEM 1 = client_ip: ###.###.###.###
ITEM 2 = datetime: 2020-01-27T19:57:43.223214-06:00
ITEM 3 = day_of_week: 1
ITEM 4 = day_of_year: 27
ITEM 5 = dst: false
ITEM 6 = dst_from: 
ITEM 7 = dst_offset: 0
ITEM 8 = dst_until: 
ITEM 9 = raw_offset: -21600
ITEM 10 = timezone: America/Chicago
ITEM 11 = unixtime: 1580176663
ITEM 12 = utc_datetime: 2020-01-28T01:57:43.223214+00:00
ITEM 13 = utc_offset: -06:00
ITEM 14 = week_number: 5

Here is a some quick code to show in debug, the above information. Just create a project and past this and the 2 functions on the main form. I know, I should have changed the form name to something useful.

VB.NET
// 
 Private Sub Form1_Load(ByVal sender As System.Object, _
             ByVal e As System.EventArgs) Handles MyBase.Load
        Dim I_TIME() As String = fn_NTPTIME()
        Dim INFO As String
        For I = 0 To UBound(I_TIME)
            INFO = "ITEM " + I.ToString + " = " + I_TIME(I)
            Debug.Print(INFO)
        Next
    End Sub
// 
//

Points of Interest

I know there are a couple of lines of that which could be combined, however I did not do so for clarity.

In Item 2 and Item 12, the date and time are separated by a "T". This is not my doing, I am posting the raw data, except in array form. It is returned to TIME_IN as a single string.

While I have been coding for many years, this is my first full submission. Let me know what you think.

History

  • 27th January, 2020 - First submitted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNTP Time Pin
Member 1033071129-Jan-20 7:16
professionalMember 1033071129-Jan-20 7:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.