Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract the value of my current local time offset?

Tags:

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

For 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 confess I don't fully understand), I can 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 is not, I think, exported, so this code doesn't work:

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

Is there a simple way to get this information?

like image 257
Brent.Longborough Avatar asked Jan 24 '16 11:01

Brent.Longborough


People also ask

How do I get timezone offset?

Definition and Usage. getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.

How do you use timezone offset?

The zone offset can be Z for UTC or it can be a value "+" or "-" from UTC. For example, the value 08:00-08:00 represents 8:00 AM in a time zone 8 hours behind UTC, which is the equivalent of 16:00Z (8:00 plus eight hours). The value 08:00+08:00 represents the opposite increment, or midnight (08:00 minus eight hours).

How do you read UTC time offset?

A UTC offset is the difference in hours and minutes between a particular time zone and UTC, the time at zero degrees longitude. For example, New York is UTC-05:00, which means it is five hours behind London, which is UTC±00:00.


2 Answers

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

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

Zone computes the time zone in effect at time t, returning the abbreviated name of the zone (such as "CET") and its offset in seconds east of UTC.

like image 198
olif Avatar answered Sep 17 '22 15:09

olif


Package 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) 

Zone computes the time zone in effect at time t, returning the abbreviated name of the zone (such as "CET") and its offset in seconds east of UTC.

type Location

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

A Location maps time instants to the zone in use at that time. Typically, the Location represents the collection of time offsets in use in a geographical area, such as CEST and CET for central Europe.

var Local *Location = &localLoc 

Local represents the system's local time zone.

var UTC *Location = &utcLoc 

UTC represents Universal Coordinated Time (UTC).

func (Time) In

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

In returns t with the location information set to loc.

In panics if loc is nil.

For example,

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 
like image 44
peterSO Avatar answered Sep 21 '22 15:09

peterSO