In Go, how can I extract the value of my current local time offset?

I'm new to Go, and I'm trying a bit to format and display some IBM TOD mainframe clock data. I want to format the data both by GMT and local time (by default - otherwise in the zone that the user specifies).

To do this, I need to get the value of the local time offset from GMT as a signed integer number of seconds.

In zoneinfo.go (which, I admit, I don't quite understand), I see

// A zone represents a single time zone such as CEST or CET. type zone struct { name string // abbreviated name, "CET" offset int // seconds east of UTC isDST bool // is this zone Daylight Savings Time? } 

but this, I think, is not exported, so this code does not work:

 package main import ( "time"; "fmt" ) func main() { l, _ := time.LoadLocation("Local") fmt.Printf("%v\n", l.zone.offset) } 

Is there an easy way to get this information?

+6
source share
3 answers

You can use the Zone () method for the time type:

 package main import ( "fmt" "time" ) func main() { t := time.Now() _, z := t.Zone() fmt.Println(z) } 

The zone calculates the time zone effective at time t, returning the abbreviated name of the zone (for example, β€œCET”) and its offset in seconds east of UTC.

+9
source

Packet time

func (Time) Local

 func (t Time) Local() Time 

Local returns t with the location set to local time.

func (Time) Zone

 func (t Time) Zone() (name string, offset int) 

The zone calculates the time zone valid at time t, returning the abbreviated name of the zone (for example, "CET") and its offset in seconds east of UTC.

Type Location

 type Location struct { // contains filtered or unexported fields } 

A Location displays the times in the area used at that time. Typically, a location is a collection of time delays to use in a geographic area, such as CEST and CET for Central Europe.

 var Local *Location = &localLoc 

Local represents the local time zone of the system.

 var UTC *Location = &utcLoc 

UTC is Universal Coordinated Time (UTC).

func (Time) At

 func (t Time) In(loc *Location) Time 

B returns t with the location information set to loc.

In a panic if loc is zero.

For instance,

 package main import ( "fmt" "time" ) func main() { t := time.Now() // For a time t, offset in seconds east of UTC (GMT) _, offset := t.Local().Zone() fmt.Println(offset) // For a time t, format and display as UTC (GMT) and local times. fmt.Println(t.In(time.UTC)) fmt.Println(t.In(time.Local)) } 

Output:

 -18000 2016-01-24 16:48:32.852638798 +0000 UTC 2016-01-24 11:48:32.852638798 -0500 EST 
+5
source

I don’t think it makes sense to manually convert the time to another TZ. Use the time.Time.In function :

 package main import ( "fmt" "time" ) func printTime(t time.Time) { zone, offset := t.Zone() fmt.Println(t.Format(time.Kitchen), "Zone:", zone, "Offset UTC:", offset) } func main() { printTime(time.Now()) printTime(time.Now().UTC()) loc, _ := time.LoadLocation("America/New_York") printTime(time.Now().In(loc)) } 
+4
source

All Articles