Get UTC time in seconds
Asked Answered
P

6

79

It looks like I can't manage to get the bash UTC date in second. I'm in Sydney so + 10hours UTC time

 date
 Thu Jul 3 17:28:19 WST 2014

 date -u
 Thu Jul 3 07:28:20 UTC 2014

But when I tried to convert it, I'm getting the same result which is not UTC time

 date +%s
 1404372514

 date -u +%s
 1404372515

What am I missing here?


After getting an answer saying date +%s was returning UTC time, here are more details about the problem I'm facing now.

I'm trying to compare a date written in a file with python. This date is written in seconds in UTC time. And the bash date +%s doesn't give me the same one. Actually if I'm doing in python time.asctime(time.localtime(date_in_seconds_from_bash)), I get the current time of Sydney, not UTC. I don't understand.

Preindicate answered 3/7, 2014 at 7:33 Comment(1)
Return value of date +%s is indeed UTCBoomer
N
43

I believe +%s is seconds since epoch. It's timezone invariant.

Nitrile answered 3/7, 2014 at 7:37 Comment(4)
anyway to get date in UTC seconds then?Preindicate
@RomanzoCriminale According to date --help: %s seconds since 1970-01-01 00:00:00 UTC. Adding -u doesn't give a difference. It would still print based from UTC. This is the reason why you were getting the same values.Vibrato
Both your answers above are correct. UTC Seconds is the same anywhere in the world, and so is useful for unambigious timestamps. Seconds since 1970 midnight in Sydney could be calculated by taking the difference between that time and UTC zero time (-neg 11 or so hours I suppose), but that number is probably not what you want.Nitrile
My problem is i'm trying to compare a date written in a file with python.This date is written in seconds in UTC time. And the bash 'date +%s doesn't give me the same one. Actually if i'm doing in python time.asctime(time.localtime(date_in_seconds_from_bash)), I get the current time of sydney not UTC. I don't understandPreindicate
C
27

I bet this is what was intended as a result.

$ date -u --date=@1404372514
Thu Jul  3 07:28:34 UTC 2014
Coffer answered 8/9, 2014 at 19:28 Comment(0)
W
14

You say you're using:

time.asctime(time.localtime(date_in_seconds_from_bash))

where date_in_seconds_from_bash is presumably the output of date +%s.

The time.localtime function, as the name implies, gives you local time.

If you want UTC, use time.gmtime() rather than time.localtime().

As JamesNoonan33's answer says, the output of date +%s is timezone invariant, so date +%s is exactly equivalent to date -u +%s. It prints the number of seconds since the "epoch", which is 1970-01-01 00:00:00 UTC. The output you show in your question is entirely consistent with that:

date -u
Thu Jul 3 07:28:20 UTC 2014

date +%s
1404372514   # 14 seconds after "date -u" command

date -u +%s
1404372515   # 15 seconds after "date -u" command
Welldisposed answered 4/7, 2014 at 0:11 Comment(0)
P
6

One might consider adding this line to ~/.bash_profile (or similar) in order to can quickly get the current UTC both as current time and as seconds since the epoch.

alias utc='date -u && date -u +%s'

Philologian answered 17/4, 2018 at 15:24 Comment(0)
F
1

Based on the answer from the other @Adam, here is a one-liner with the UTC date-time and seconds since epoch start:

alias utc='printf "%s : %s\n" "$(date -u)" "$(date -u +%s)"'

The epoch start time is defined as the number of seconds since January 1, 1970 at 00:00 Greenwich Mean Time (GMT), a timezone now known as UTC±00:00 or simply UTC.

Frontolysis answered 24/6, 2022 at 14:14 Comment(0)
R
0

time.asctime(time.localtime(date_in_seconds_from_bash)), I get the current time of Sydney, not UTC. I don't understand.

You're kind of answering your own question here, but missing the important bits from the documentation. You got a good UTC time from the shell with date command. Then you're passing it to python's time.localtime() ... which explicitly accepts a UTC timestamp in seconds and returns a Python time value that's localized to the timezone, or at least whatever Python thinks is the timezone:

https://docs.python.org/3/library/time.html#time.localtime

If you want it to remain in UTC, then probably what you are after is time.gmtime() instead of time.localtime():

https://docs.python.org/3/library/time.html#time.gmtime

Ruminate answered 20/12, 2023 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.