How to get Timestamp of UTC time with Golang?

61,725

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.

Share:
61,725
LeMoussel
Author by

LeMoussel

Problem solver. Je recherche, je développe, je teste, je veille, ..., Passionné par toutes les techniques de Dév. (.NET, C#, PHP, JavaScript, ...).

Updated on July 15, 2022

Comments

  • LeMoussel
    LeMoussel almost 2 years

    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

  • John Balvin Arias
    John Balvin Arias almost 6 years
    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? youtube.com/watch?v=pOGL7oqZqEc
  • Zunino
    Zunino over 5 years
    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.