74

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.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Romanzo Criminale
  • 1,289
  • 2
  • 14
  • 21

5 Answers5

41

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

slm
  • 15,396
  • 12
  • 109
  • 124
JamesNoonan33
  • 554
  • 3
  • 4
  • anyway to get date in UTC seconds then? – Romanzo Criminale Jul 03 '14 at 07:41
  • 5
    @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. – konsolebox Jul 03 '14 at 07:49
  • 2
    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. – JamesNoonan33 Jul 03 '14 at 07:50
  • 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 understand – Romanzo Criminale Jul 03 '14 at 23:48
27

I bet this is what was intended as a result.

$ date -u --date=@1404372514
Thu Jul  3 07:28:34 UTC 2014
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Derek Boonstra
  • 279
  • 3
  • 2
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
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
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'

Adam
  • 81
  • 1
  • 9
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.

Adam Erickson
  • 6,027
  • 2
  • 46
  • 33