0

I am using time.Time in go. to get time in seconds (the number of seconds elapsed since 1970's January 1) I am using

now := Time.now()
loc, err := time.LoadLocation(country.Timezone) // time zone in Asia/Dhaka
now = now.In(loc)

then,

seconds := now.Unix()

but the seconds giving seconds without adding the time zone seconds. it actually giving the seconds in UTC. My question is, how can I get seconds with added 6 hours ( asia/dhaka time zone is UTC+6)?

  • 1
    Unix timestamps are timezone independent - https://stackoverflow.com/questions/23062515/do-unix-timestamps-change-across-timezones. It is supposed to be same seconds. –  Sep 15 '19 at 06:10
  • It will be same amount of seconds. If you offset with your timezone at time.Now(), the unix beginning time at Jan 1 1970 will offset by same amount of hours too. Unixtime doesn't change across locations, that's the very reason it's used in systems – kiddorails Sep 15 '19 at 06:21

2 Answers2

0

If you want current clock time's second part only use below code:

loc := time.FixedZone("some_common_name", 6*60*60)
ts := time.Now().In(loc).Second()
fmt.Println(ts)

If you want seconds from start of current year(like 01.01.1970)

loc := time.FixedZone("some_common_name", 6*60*60)
tp := time.Date(1970, 1, 1, 0, 0, 0, 0, loc)
ts := time.Now().Sub(tp).Seconds()
fmt.Printf("%v", ts)

In case, you want different time zone information, change offset value of time.FixedZone() functions. As, if you want GMT +5, then use 5*60*60 as offset value

Abu Hanifa
  • 2,857
  • 2
  • 22
  • 38
  • Second() methon returns the second offset within the minute specified by t, in the range [0, 59] but my purpose is to get the number of seconds elapsed since January 1 – Shakhawat Hossain Sep 15 '19 at 06:16
  • also, is it current year's January 1 or 1970's January 1? – Abu Hanifa Sep 15 '19 at 06:28
  • As time zone is dynamic. In that case I only know the time zone string like "Asia/Dhaka". but don't know the value like 6 hours. – Shakhawat Hossain Sep 15 '19 at 06:38
  • if its asia/dhaka, it should be +6 hours with UTC – Abu Hanifa Sep 15 '19 at 06:43
  • also in `time.FixedZone("Asia/Dhaka", 6*60*60)` "Asia/Dhaka" is name, if you need some other timezone's information, then you need to provide different offset value. for "asia/dhaka" its 6*60*60. if this time zone if dynamic use name in more genarelize manner and change offset value according to your need – Abu Hanifa Sep 15 '19 at 06:47
  • as I said it's dynamic in previous comment. it's not always asia/dhaka. it can be another time zone also. – Shakhawat Hossain Sep 15 '19 at 06:48
0

I've read a number of posts and most of them are rightfully biased that Unix() time should be exactly that; meaning UTC. However, In my particular case others, like the TCL team, are a little loose. Both input and output allow the user to override the TZ. Furthermore the default TZ has it's own rules.

In my case I was not even using times... only dates. However, the closer to the date boundary the more likely it change days and thus bang up the date expressions etc.

In TCL there is UTC seconds from 1/1/1970 but there is also TZ adjusted seconds from 1/1/1970. (right or wrong I need some compatibility)

// parse the time string (the value does not have the TZ)
t, _ := time.Parse(format, value)
// set the location.
t = t.In(location)
// get the offset seconds from TZ
_, offset := t.Zone()
// adjust the  Unix() seconds by the offset.
retval = fmt.Sprintf("%d", t.Unix()-int64(offset))

I'm in EST5EDT and it works here when location is EST5EDT and Local. I did not try anything on the other side of the UTC.

UPDATE: Well... Someone once said show me a programmer who knows dates and times and I'll show you someone who doesn't. The code above worked just fine as Local and UTC were on the same calendar day. But as soon as UTC moved into the next calendar day the Seconds were exactly 24hrs apart. I can squeeze the last second out of this so that TCL and my app work similarly but I'm better off doing this in the app code rather than in the libs.

Richard
  • 10,122
  • 10
  • 42
  • 61