0

Edit: I have updated the question with code that highlights why the alleged duplicate's solution doesn't work for me

I am trying to take UTC (+0000) times and format them into local times (eastern time in my case) without hard coding any timezone offsets (as to avoid implementing dst correction).

I have the following code which demonstrates the problem I am having

package main

import (
    "fmt"
    "time"
)

func main() {
    // Here I load the timezone
    timezone, _ := time.LoadLocation("America/New_York")

    // I parse the time
    t, _ := time.Parse("Mon Jan 2 15:04:05 +0000 2006", "Tue Jul 07 10:38:18 +0000 2015")

    // This looks correct, it's still a utc time
    fmt.Println(t)
    // 2015-07-07 10:38:18 +0000 UTC

    // This seems to be fine - -4 hours to convert to est
    t = t.In(timezone)
    fmt.Println(t)
    // 2015-07-07 06:38:18 -0400 EDT

    // This prints 6:07am, completely incorrect as it should be 6:38am
    fmt.Println(t.Format("Monday Jan 2, 3:01pm"))
    // Tuesday Jul 7, 6:07am
}

(https://play.golang.org/p/e57slFhWFk)

So to me it seems that it parses and converts timezones fine, but when I output it using format, it gets rid of the minutes and uses 07. It doesn't matter what I set the minutes to, it always comes out as 07.

Stephen Smith
  • 485
  • 1
  • 6
  • 14
  • Both the answers in the marked duplicate contains your solution (using `Time.In()`). – icza Jul 07 '15 at 21:16
  • The other question was about how to make a new time instead of parse into a timezone which I am trying to do with ParseInLocation – Stephen Smith Jul 07 '15 at 21:18
  • You are printing the time wihtout the location. You can do `.Println(t.Format("Monday Jan 2, 3:01pm (MST)"))` to print the time with the Location information. Also t.UTC() works. Your math is reversed. `// This looks correct, 20 - 0400 = 16 = 4pm` The time should be added to get the UTC time. So UTC time is 12am next day rather than UTC minus 8 [Updated playground link](https://play.golang.org/p/WL9DQtvcBh) See also the linked answer for `time.In()` – reedobrien Jul 07 '15 at 21:27
  • My math can't be reversed... I used a date returned by twitter at 4pm, and the twitter date said 8pm +0000, then golang converted it to 8pm -0400 – Stephen Smith Jul 08 '15 at 21:26
  • 1
    @icza I have updated the question explaining the exact problem I have, including the solution from the "duplicate", which doesn't work for me. – Stephen Smith Jul 22 '15 at 20:31
  • @Stephen Yes, it is good now. Reopened and answered your question. – icza Jul 22 '15 at 21:25

1 Answers1

1

Your layout (format) strings are incorrect. As noted in the doc of the time package, the layout string must denote this time:

Mon Jan 2 15:04:05 MST 2006

When parsing, use the 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 this format string:

fmt.Println(t.Format("Monday Jan 2, 3:04pm"))

This will result in your expected output:

Tuesday Jul 7, 6:38am

Try it on the Go Playground.

icza
  • 389,944
  • 63
  • 907
  • 827