Linux local time versus UTC in seconds
Could you please explain what is wrong with this 2 syntax for getting the elapsed time in seconds?
If I run both of these commands at the same time:
date -u +%s # - for UTC - returns e.g. 1303430843.
date +%s # - for local time - mine is set to pacific time
# - also returns e.g. 1303430843
I get exactly the same number (e.g. 1303430843) seconds, no difference, but I would expect 25200 seconds - a 7 hour difference, as if I ran:
date -u "+%Y-%m-%d %H:%M:%S" # returns: 2011-04-22 00:01:14
date "+%Y-%m-%d %H:%M:%S" # returns: 2011-04-21 17:01:14 - 7 hours difference
What's the trick here?
thank
+3
source to share
4 answers
This is because GMT uses UTC (they are mostly used interchangeably) and PDT is currently 7 hours behind .
To get the current UTC time in seconds:
date -u +%s
To get the current PDT time in seconds:
TZ=":US/Pacific" date +%s
It's smart enough to pick up DST so you don't have to worry about it.
0
source to share