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


From the man page:

% s seconds since 1970-01-01 00:00:00 UTC



so it %s

returns the time in seconds since a specific point in time specified in UTC, which means it doesn't depend on time zones at all.

+5


source


Don't catch. The clock is set and starts in UTC format. The date command takes local time zone and day savings into account. This means that you can display the date / time for any time zone in the world.



0


source


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


%s     seconds since 1970-01-01 00:00:00 UTC

      

This does not change with the time zone. The calculation is the same.

0


source







All Articles