Golang : time.Time.Equal() function example
package time
Golang : time.Time.Equal() function usage example.
NOTE : Comparing time that are same but from different location will be true.
package main
import (
"fmt"
"time"
)
func main() {
timeString := "Aug 6, 2015 at 6:00am (UTC)"
layOut := "Jan 2, 2006 at 3:04pm (MST)"
timePresentMYT, err := time.Parse(layOut, timeString)
if err != nil {
fmt.Println(err)
}
// see also :
// https://www.socketloop.com/tutorials/golang-get-local-time-and-equivalent-time-in-different-time-zone
est, _ := time.LoadLocation("EST")
timePresentEST := time.Date(2015, 8, 6, 1, 0, 0, 0, est)
future := time.Now().Add(48 * time.Hour)
fmt.Println("Present MYT: ", timePresentMYT)
fmt.Println("Present EST : ", timePresentEST)
fmt.Println("Both time is equal even in different location.")
fmt.Println("Present MYT is equal to Present EST ? : ", timePresentMYT.Equal(timePresentEST))
fmt.Println("Future : ", future)
fmt.Println("Present MYT is equal to future ? : ", timePresentMYT.Equal(future))
}
Output :
Present MYT: 2015-08-06 06:00:00 +0000 UTC
Present EST : 2015-08-06 01:00:00 -0500 EST
Both time is equal even in different location.
Present MYT is equal to Present EST ? : true
Future : 2015-08-08 11:20:25.300126445 +0800 MYT
Present MYT is equal to future ? : false
Reference :
Advertisement
Something interesting
Tutorials
+16.1k Golang : Generate universally unique identifier(UUID) example
+8.3k Golang : Find relative luminance or color brightness
+9.7k Golang : Sort and reverse sort a slice of floats
+12k Golang : Convert(cast) bigint to string
+19.8k Golang : Archive directory with tar and gzip
+23.6k Golang : Get ASCII code from a key press(cross-platform) example
+25.7k Golang : convert rune to integer value
+18.1k Golang : Check if a directory exist or not
+11k Golang : Get UDP client IP address and differentiate clients by port number
+17.1k Golang : Get number of CPU cores
+7.3k CloudFlare : Another way to get visitor's real IP address
+14.9k Golang : Normalize unicode strings for comparison purpose