How to format a local time object in Go with correct minutes? -
edit: have updated question code highlights why alleged duplicate's solution doesn't work me
i trying take utc (+0000) times , format them local times (eastern time in case) without hard coding timezone offsets (as avoid implementing dst correction).
i have following code demonstrates problem having
package main import ( "fmt" "time" ) func main() { // here load timezone timezone, _ := time.loadlocation("america/new_york") // parse time t, _ := time.parse("mon jan 2 15:04:05 +0000 2006", "tue jul 07 10:38:18 +0000 2015") // looks correct, it's still utc time fmt.println(t) // 2015-07-07 10:38:18 +0000 utc // seems fine - -4 hours convert est t = t.in(timezone) fmt.println(t) // 2015-07-07 06:38:18 -0400 edt // prints 6:07am, incorrect should 6:38am fmt.println(t.format("monday jan 2, 3:01pm")) // tuesday jul 7, 6:07am } (https://play.golang.org/p/e57slfhwfk)
so me seems parses , converts timezones fine, when output using format, gets rid of minutes , uses 07. doesn't matter set minutes to, comes out 07.
your layout (format) strings incorrect. noted in doc of time package, layout string must denote time:
mon jan 2 15:04:05 mst 2006 when parsing, use following format string:
t, _ := time.parse("mon jan 02 15:04:05 -0700 2006", "tue jul 07 10:38:18 +0000 2015") and when printing, use format string:
fmt.println(t.format("monday jan 2, 3:04pm")) this result in expected output:
tuesday jul 7, 6:38am try on go playground.
Comments
Post a Comment