time - Golang - Timezone parsing -
in example bellow result "[date] 05:00:00 +0000 utc" regardless timezone choose parseandprint function. wrong code? time should change depending on timezone choose. (go playground servers apparently configured in utc timezone).
http://play.golang.org/p/wp207bwyed
package main import ( "fmt" "time" ) func main() { := time.now() parseandprint(now, "brt") parseandprint(now, "edt") parseandprint(now, "utc") } func parseandprint(now time.time, timezone string) { test, err := time.parse("15:04:05 mst", fmt.sprintf("05:00:00 %s", timezone)) if err != nil { fmt.println(err) return } test = time.date( now.year(), now.month(), now.day(), test.hour(), test.minute(), test.second(), test.nanosecond(), test.location(), ) fmt.println(test.utc()) }
thanks!
when parse time, parsing in current location, ok long that's you're expecting, , timezone abbreviation known within location.
if can forgo timezones, it's far easier normalize times you're dealing utc.
the next easiest handling explicit offsets, -05:00
.
if want deal times originating in other timezones, need use time.location
. can load locations local timezone db time.loadlocation
, , parse times there time.parseinlocation
.
Comments
Post a Comment