I need to get the actual time in GMT+2 (Rome, Italy) in Go language.
I've used: time.Now() but it returns the actual date in GMT+0.
I've seen that there's a In(loc *Location) function but I can't understand how to use it. And also, do you know if setting GMT+2, it also consider the DST option in automatic?
Could you help me? Thanks
You can user the function FixedZone
or LoadLocation
to get a *Location
And then use that *Location
in func In
// get the location
location,_ := time.LoadLocation("Europe/Rome")
// this should give you time in location
t := time.Now().In(location)
fmt.Println(t)
Here is more of docs https://golang.org/pkg/time/#Time
Another method to get the current time is to get GMT time (UTC) and add the number of hours you need, but this way does not take into account DST, and yields precisely GMT+2 (GMT does not change with daylight saving time).
package main
import (
"fmt"
"time"
)
func main() {
when := time.Now().UTC().Add(2*time.Hour)
fmt.Println("GMT+2:", when)
}