Get UTC time in seconds - bash

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.

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

I bet this is what was intended as a result.
$ date -u --date=#1404372514
Thu Jul 3 07:28:34 UTC 2014

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

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'

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.

Related

How to pass hours and minutes to date command through -d option

I know how to format a date in bash using the date command
date -d 20160304 +%Y%m%d
for example. But now I want to pass a date and time and return the hours and minutes. I know the output format I need is +%H%M, but I don't know how to format the date string and it is not in the man pages.
For example if I try any of these:
date -d 201801010500 +%H%M
date -d 20180101_0500 +%H%M
date -d 2018-01-01_0500 +%H%M
date -d 2018:01:01-05:00 +%H%M
I get an "invalid date" error. When I search google I always find answer referring to the output format, not the input format...
GNU date accepts these format, among others I'm sure
$ date -d '2018-02-16 12:34'
Fri Feb 16 12:34:00 EST 2018
$ date -d '2018-02-16T12:34:56'
Fri Feb 16 12:34:56 EST 2018
$ date -d '2018-02-16T12:34:56Z'
Fri Feb 16 07:34:56 EST 2018
In general, can't go wrong with ISO8601 time formats
I'm in Canada/Eastern time zone
The date utility is pretty impressive in making sense of different arguments for the -d option.
Here is just one example:
$ date -d "20180101 05:00:00"
Mon Jan 1 05:00:00 +07 2018
Note +07 is the local timezone.

How to convert local date-time string to Unix timestamp (GMT)?

time_var="6/23/2016 3:20:00 AM"
(this is in EDT)
We need to get unix timestamp for this variable after converting its value to GMT.
Just use the -u flag while passing the date with -d:
$ time_var="6/23/2016 3:20:00 AM"
$ date -d"$time_var EDT" -u
Thu Jun 23 07:20:00 UTC 2016
Note I also appended EDT to your date.
From man date:
-d, --date=STRING
display time described by STRING, not 'now'
-u, --utc, --universal
print or set Coordinated Universal Time

Invalid date format in bash

I'm having trouble with checking time since EPOCH. (and late subtract it from another one).
I get the date like this:
var=$(date)
echo $var
wto, 1 mar 2016, 16:00:14 CET
and later I'm trying to turn it into seconds since epoch:
date -d "$var" +"%s"
date: invalid date ‘wto, 1 mar 2016, 16:00:14 CET’
I'm giving this just as an example. Actually I will be reading the date from file, written in default locale format (I'm operating on couple different machines).
if you type date -h there is the reason why you got this error.
the -d option MUST be declared only with TIME and not with complete DATE format
-d,--date TIME Display TIME, not 'now'
so
date -d "23:59:59"
then:
Tue Mar 1 23:59:59 2016
if you need get only the seconds from a date you have to execute this:
date +"%S"
if you use the -d the output will be deplyed in msec

Convert UTC time to GMT bash script

I am trying to convert a UTC time to GMT time in my small script, but it doesn't work:
TimestampUTC=$(date +"%s")
echo $TimestampUTC
dates=$(date -d #$TimestampUTC)
echo $dates
## 2 hours difference between UTC and GMT
Hours2=120
TimestampGMT=$((TimestampUTC - Hours2))
echo $TimestampGMT
diff=$((TimestampUTC - TimestampGMT))
echo $diff
dateGMT=$(date -d #$TimestampGMT)
echo $dateGMT
The displayed result for $dateGMT is the same as $dates.
Thanks in advance.
error in script.
Unix timestaps are given in seconds.
Hours2=120 means 120 seconds.
So your 2 timestaps are diverging by 2 minutes, not 2 hours.
This code is correct:
Hours2=7200
Also you claim having 2 hours between GMT and UTC, I'm sure you mean CET (central european time)
Note: there is nothing like a CET timestamp. It's just the normal unix timestamp displayed with a timezone offset. So independently of world location, the unix timestamp is always, worldwide, the same at the same instant.
You can replace all your code by just this
# get the timestamp 2 hours in the future from now
date2h=$(date -d "2 hours" +%s)
Which gives you the unix timestamp from the future. It is NOT the current timestamp in CET. The current CET timestamp is always the same as UTC.
How to get the time from UTC and CET? Set the environment variable TZ before the command.
$ TZ=UTC date
Mon Aug 17 11:44:05 UTC 2015
$ TZ=CET date
Mon Aug 17 13:44:05 CEST 2015
$ TZ=GMT date
Mon Aug 17 11:44:05 GMT 2015
but the timestap is always the same
$ TZ=UTC date +%s
1439812072
$ TZ=CET date +%s
1439812072
$ TZ=GMT date +%s
1439812072
GMT and UTC do not differ by 2 hours. In fact they don't differ at all. So displaying the dates of GMT and UTC will always show exactly the same number.
Also I don't know bash but I find it hard to believe that 2 hours is represented by 120 minutes. Normally when doing math with dates milliseconds are used.
In your favourite terminal use the following sequence
export TZ=GMT; date
date_format='+%d %B %Y %H:%M'
datatest="2021-11-21 12:00:00 UTC"
echo $(date -d "$datatest" "$date_format")
datatest="2021-11-21 12:00:00 CET"
echo $(date -d "$datatest" "$date_format")
datatest="2021-11-21 12:00:00 GMT"
echo $(date -d "$datatest" "$date_format")
Out:
21 November 2021 13:00
21 November 2021 12:00
21 November 2021 13:00

What does the at-sign # mean in the bash command: date --date #...?

Searching the internet I found explanations only for '$#', meaning 'expand to positional parameters'. But I couldn't find anything about the # sign by itself.
I stumbled over it in the third snipped of the accepted answer to this question:
https://superuser.com/questions/611538/is-there-a-way-to-display-a-countdown-or-stopwatch-timer-in-a-terminal
Specifically:
date -u --date #$((`date +%s` - $date1)) +%H:%M:%S
In the context you show, the # is in the beginning of the --date argument to the date command:
date -u --date #$((`date +%s` - $date1)) +%H:%M:%S
In that case it means that the argument should be treated as the number of seconds since epoch, see an example in man date:
Convert seconds since the epoch (1970-01-01 UTC) to a date
$ date --date='#2147483647'
or:
$ date -u -d #0
Thu Jan 1 00:00:00 UTC 1970
This meaning of # is defined by the date utility alone and not by bash.

Resources