golang timestamp in RFC3339 format - go

how do we convert time.now() in time.Time( RFC3339) format?
Eg:
var t time.Time
timeNow= time.Now()
I want to assign timeNow to t

Golang has support for various time formats.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
r := t.Format(time.RFC3339)
fmt.Println("time.Now() ", t)
fmt.Println("RFC3339 ", r)
}

Related

How to parse this date 2018-10-22T2250?

How to parse this strange datetime 2018-10-22T2250 in golang?
I couldn't find date layout
You can create your own custom format. In production, you should also handle the error.
package main
import (
"fmt"
"time"
)
func main() {
timeString := "2018-10-22T2250"
timeFormat := "2006-01-02T1504"
t, _ := time.Parse(timeFormat, timeString)
fmt.Println(t)
}
Playground link
This returns the time in UTC. You may need to adjust to another timezone, depending on your source.
//init the location
loc, _ := time.LoadLocation("Asia/Shanghai")
//localize the time
localTime := t.In(loc)

How to parse a RFC3339 in Go?

My date looks like this
2019-03-29T09:32:52Z
Please help me parse it using Go's standard time package
Nevermind, I just had the wrong layout
package main
import (
"fmt"
"time"
)
func main() {
layout := "2006-01-02T15:04:05Z07:00"
t, err := time.Parse(layout, "2019-03-29T09:32:52Z")
fmt.Println(t, err)
}

Why these two time.Time instances are same for UTC and different for another location?

I'm expecting these two time.Time instances are the same. But, I'm not sure why I got the compare result is false.
package main
import (
"fmt"
"time"
)
func main() {
t := int64(1497029400000)
locYangon, _ := time.LoadLocation("Asia/Yangon")
dt := fromEpoch(t).In(locYangon)
locYangon2, _ := time.LoadLocation("Asia/Yangon")
dt2 := fromEpoch(t).In(locYangon2)
fmt.Println(dt2 == dt)
}
func fromEpoch(jsDate int64) time.Time {
return time.Unix(0, jsDate*int64(time.Millisecond))
}
Playground
If I change "Asia/Yangon" to "UTC", they are the same.
package main
import (
"fmt"
"time"
)
func main() {
t := int64(1497029400000)
locYangon, _ := time.LoadLocation("UTC")
dt := fromEpoch(t).In(locYangon)
locYangon2, _ := time.LoadLocation("UTC")
dt2 := fromEpoch(t).In(locYangon2)
fmt.Println(dt2 == dt)
}
func fromEpoch(jsDate int64) time.Time {
return time.Unix(0, jsDate*int64(time.Millisecond))
}
Playground
Note: I'm aware of Equal method (in fact, I fixed with Equal method.) But after more testing, I found some interesting case which is "UTC" location vs "Asia/Yangon" location. I'm expecting either both equal or both not equal.
Update: Add another code snippet with "UTC".
Update2: Update title to be more precise (I hope it will help to avoid duplication)
LoadLocation seems to return a pointer to a new value every time.
Anyway, the good way to compare dates is Equal:
fmt.Println(dt2.Equal(dt))
Playground: https://play.golang.org/p/9GW-LSF0wg.

Equivalent of python time.time()-started in golang

In python I can see how many seconds have elapsed during a specific process like,
started = time.time()
doProcess()
print(time.time()-started)
Whats the equivelent in golang?
import (
"fmt"
"time"
)
func main() {
begin := time.Now()
time.Sleep(10 * time.Millisecond)
end := time.Now()
duration := end.Sub(begin)
fmt.Println(duration)
}
import (
"fmt"
"time"
)
func main() {
started := time.Now()
doProcess()
fmt.Println(time.Now().Sub(started).Seconds())
}
Package time
func Since
func Since(t Time) Duration
Since returns the time elapsed since t. It is shorthand for
time.Now().Sub(t).
Your Python example in Go:
package main
import (
"fmt"
"time"
)
func main() {
started := time.Now()
time.Sleep(1 * time.Second)
fmt.Println(time.Since(started))
}
Output:
1s

Go printing date to console

I'm trying to pint the month, day, and year, separately to the console.
I need to be able to access each section of the date individually. I can get the whole thing using time.now() from the "time" package but I'm stuck after that.
Can anyone show me where I am going wrong please?
You're actually pretty close :) Then return value from time.Now() is a Time type, and looking at the package docs here will show you some of the methods you can call (for a quicker overview, go here and look under type Time). To get each of the attributes you mention above, you can do this:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Month())
fmt.Println(t.Day())
fmt.Println(t.Year())
}
If you are interested in printing the Month as an integer, you can use the Printf function:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Printf("%d\n", t.Month())
}
Day, Month and Year can be extracted from a time.Time type with the Date() method. It will return ints for both day and year, and a time.Month for the month. You can also extract the Hour, Minute and Second values with the Clock() method, which returns ints for all results.
For example:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
y, mon, d := t.Date()
h, m, s := t.Clock()
fmt.Println("Year: ", y)
fmt.Println("Month: ", mon)
fmt.Println("Day: ", d)
fmt.Println("Hour: ", h)
fmt.Println("Minute: ", m)
fmt.Println("Second: ", s)
}
Please remember that the Month variable (mon) is returned as a time.Month, and not as a string, or an int. You can still print it with fmt.Print() as it has a String() method.
Playground
You can just parse the string to get year, month, & day.
package main
import (
"fmt"
"strings"
)
func main() {
currTime := time.Now()
date := strings.Split(currTime.String(), " ")[0]
splits := strings.Split(date, "-")
year := splits[0]
month := splits[1]
day := splits[2]
fmt.Printf("%s-%s-%s\n", year, month, day)
}

Resources