-3

We can't use the offset returned by Zone():

package main

import "fmt"
import "time"

func main() {
    loc, _ := time.LoadLocation("America/Los_Angeles")
    t := time.Date(2015,04,12,19,23,0,0,loc)
    t2 := time.Date(2015,03,1,19,23,0,0,loc)
    _, offset := t.Zone()
    _, offset2 := t.Zone()
    fmt.Printf("t1: %v  offset: %d\n", t, offset)
    fmt.Printf("t2: %v  offset: %d\n", t2, offset2)
}

This returns:

t1: 2015-04-12 19:23:00 -0700 PDT  offset: -25200
t2: 2015-03-01 19:23:00 -0800 PST  offset: -25200

The offset does not reflect daylight savings. Is parsing manually the offset after formatting a time instance the only option (-0700 and -0800)?

We can retrieve the current time with time.Now(), but using .In() simply changes the location without adjusting the hours and minutes.

miguelv
  • 2,856
  • 1
  • 23
  • 22

1 Answers1

3

Fix the bug in your program. For example,

package main

import "fmt"
import "time"

func main() {
    loc, _ := time.LoadLocation("America/Los_Angeles")
    t := time.Date(2015, 04, 12, 19, 23, 0, 0, loc)
    t2 := time.Date(2015, 03, 1, 19, 23, 0, 0, loc)
    _, offset := t.Zone()
    _, offset2 := t2.Zone()
    fmt.Printf("t1: %v  offset: %d\n", t, offset)
    fmt.Printf("t2: %v  offset: %d\n", t2, offset2)
}

Output:

t1: 2015-04-12 19:23:00 -0700 PDT  offset: -25200
t2: 2015-03-01 19:23:00 -0800 PST  offset: -28800
peterSO
  • 134,261
  • 26
  • 231
  • 232