Reputation: 459
I tried parsing
Tue Apr 07 2020 11:17:47 GMT+0530 (India Standard Time)
But got the following error
parsing time "Tue Apr 07 2020 11:17:47 GMT+0530 (India Standard Time)" as "2006-01-02T15:04:05Z07:00": cannot parse "Tue Apr 07 2020 11:17:47 GMT+0530 (India Standard Time)" as "2006" 0001-01-01 00:00:00 +0000 UTC
From the frontend i can get timestamp in any format depending on the location or zone.But, in go lang i'm trying to parse any timestamp and convert it to UTC to store in ledger.What is the correct way to handle this case?
t, err := time.Parse(time.RFC3339, str)
if err != nil {
fmt.Println(err)
}
fmt.Println(t)
Upvotes: 0
Views: 473
Reputation: 85895
The reference time used in the Go
time layouts is the specific time, Mon Jan 2 15:04:05 MST 2006
. To define your own format, write down what the reference time would look like formatted your way.
Since MST
is GMT-0700
, use your ref string that way in the first argument.
package main
import (
"fmt"
"time"
)
func main() {
// Parsing your custom time format by using the reference time in your format.
t1, err := time.Parse(
"Mon Jan 02 2006 15:04:05 GMT-0700",
"Tue Apr 07 2020 11:17:47 GMT+0530")
if err != nil {
fmt.Println(err)
}
fmt.Println(t1.UTC())
}
Upvotes: 2