Reputation: 63
I am to get a time struct from a string. I am using the function time.ParseTime()
with the layout "2006-01-02 15:04"
.
When I execute the function with any valid time string I get a time struct pointing to that time stamp but it is in UTC.
How can I change it to a different time zone? To be clear I want the same timestamp but with a different time zone. I don't want to convert between timezones; I just want to get the same time object but not in UTC.
Upvotes: 4
Views: 4959
Reputation: 164829
Use time.ParseInLocation
to parse time in a given Location when there's no time zone given. time.Local
is your local time zone, pass that in as your Location.
package main
import (
"fmt"
"time"
)
func main() {
// This will honor the given time zone.
// 2012-07-09 05:02:00 +0000 CEST
const formWithZone = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.ParseInLocation(formWithZone, "Jul 9, 2012 at 5:02am (CEST)", time.Local)
fmt.Println(t)
// Lacking a time zone, it will use your local time zone.
// Mine is PDT: 2012-07-09 05:02:00 -0700 PDT
const formWithoutZone = "Jan 2, 2006 at 3:04pm"
t, _ = time.ParseInLocation(formWithoutZone, "Jul 9, 2012 at 5:02am", time.Local)
fmt.Println(t)
}
Upvotes: 9