46

I want to convert UTC time string to unix timestamp. I do this

fmt.Printf("%s %d\n", time.Now().String(), time.Now().Unix())
fmt.Printf("%s %s\n", time.Now().UTC().String(), time.Now().UTC().Unix())

But I got same unix timestamp 1499018765

2017-07-02 20:06:05.5582802 +0200 CEST 1499018765

2017-07-02 18:06:05.791337 +0000 UTC 1499018765

LeMoussel
  • 5,290
  • 12
  • 69
  • 122

1 Answers1

93

Unix() always returns the number of seconds elapsed since January 1, 1970 UTC. So it does not matter whether you give it time.Now() or time.Now().UTC(), it is the same UTC time, just in different places on Earth. What you get as the result is correct.

Constantin S. Pan
  • 1,362
  • 11
  • 4
  • 3
    could you please explain the frase " it is the same UTC time, just in different places on Earth" UTC wasn't meant to be universal? https://www.youtube.com/watch?v=pOGL7oqZqEc – John Balvin Arias Jul 17 '18 at 03:56
  • 3
    A given moment or point in time is a unique instance which can be represented in various ways, according to the point of reference. E.g. some event taking place at noon time somewhere where the timezone is GMT-2 would be referred to or represented as 15 hours (3pm) by someone on GMT+1 timezone, as 17 hours (5pm) by someone on GMT+3 timezone and so on. So, different references to a single point in time. I hope this helps somewhat. – Zunino Nov 27 '18 at 20:15