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 :

http://golang.org/pkg/time/#Time.Equal

Advertisement