Golang : Get current time from the Internet time server(ntp) example
There are times when a program needs to have accurate machine time. For example, the make
command found in Unix/Linux. To get the most accurate time, your local machine time need to synchronize first with a time server to get the best accuracy.
Below is a Golang example code on how to get the time from pool.ntp.org
via the Network Time Protocol(NTP). There are couple of packages that allow us to get the time via NTP and we will use the github.com/beevik/ntp
package.
First, $go get -u github.com/beevik/ntp
and then run the code below.
package main
import (
"fmt"
"github.com/beevik/ntp"
"time"
)
func main() {
// ntpTime, err := ntp.Time("pool.ntp.org")
// time.apple.com will give the same result as pool.ntp.org
ntpTime, err := ntp.Time("time.apple.com")
if err != nil {
fmt.Println(err)
}
ntpTimeFormatted := ntpTime.Format(time.UnixDate)
fmt.Printf("Network time: %v\n", ntpTime)
fmt.Printf("Unix Date Network time: %v\n", ntpTimeFormatted)
fmt.Println("+++++++++++++++++++++++++++++++")
timeFormatted := time.Now().Local().Format(time.UnixDate)
fmt.Printf("System time: %v\n", time.Now())
fmt.Printf("Unix Date System time: %v\n", timeFormatted)
// so from here, use ntpTime instead of time.Now().Local()
// or set your machine time to sync with a network time server
// for example, on Mac OSX, you can tick to enable auto sync
// with time.apple.com
}
Output:
Network time: 2017-04-21 14:28:15.461293531 +0800 MYT
Unix Date Network time: Fri Apr 21 14:28:15 MYT 2017
+++++++++++++++++++++++++++++++
System time: 2017-04-21 14:30:13.041308829 +0800 MYT
Unix Date System time: Fri Apr 21 14:30:13 MYT 2017
NOTICE that my system time is 14:30 versus the network time 14:28. My local machine time is 2 minutes ahead.If my local machine is set to automagically sync with a time server ... such as time.apple.com
, then both the local and network time should be the same.
Happy coding!
References:
https://groups.google.com/forum/#!topic/golang-nuts/FlcdMU5fkLQ
See also : Golang : Get local time and equivalent time in different time zone
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.3k Linux/Unix/PHP : Restart PHP-FPM
+22k Golang : How to read JPG(JPEG), GIF and PNG files ?
+8.2k Golang : Convert(cast) []byte to io.Reader type
+10k Android Studio : Simple input textbox and intercept key example
+6.8k Golang : Get environment variable
+7.2k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+14.2k Golang : Rename directory
+17.2k Golang : Upload/Receive file progress indicator
+14.1k Golang : Find network of an IP address
+8.5k Golang : GMail API create and send draft with simple upload attachment example
+21.7k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+10.2k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example