Cause of many mistakes. Applies anywhere, I am going to provide a shell examples.
So this is what can twist your brain:
1 2 3 4 |
$ date +"%s" 1321920962 $ date -d "1970/01/01 +1321920962 sec" +"%s" 1321917362 |
Unixtime (POSIX time) is a number of seconds passed since the beginning of year 1970. So in the example above I get this number with date +"%s", then add this number to the epoch (1970/01/01) and get a different number!
The reason of this is very simple:
1 2 3 4 |
$ date -d "1970/01/01" +"%s" -3600 $ date -u -d "1970/01/01" +"%s" 0 |
Unixtime is always UTC
It is a well-known fact, but it’s so easy to miss:
1 2 3 4 5 6 |
$ date ; date -u Tue Nov 22 01:31:42 CET 2011 Tue Nov 22 00:31:42 UTC 2011 $ date +"%s" ; date -u +"%s" 1321921903 1321921903 |
That is, for example, you might have local and UTC datetime variables defined in your code, perfectly distinguishable. And then at some point you decide to just format it with +"%s" to unixtime, and they become equal :)
BUT:
1 2 3 4 5 |
$ dt='Tue Nov 22 01:31:42' $ date -d "$dt" +"%s" 1321921902 $ date -u -d "$dt" +"%s" 1321925502 |
Note that the second number is bigger. Seems wrong because my local CET time is one hour ahead. But it is absolutely correct. It is bigger because in the first case to be converted to unixtime this variable must be converted to UTC first (minus 1 hour), so it becomes ‘Tue Nov 22 00:31:42 UTC 2011’ while in the second case it is considered as ‘Tue Nov 22 01:31:42 UTC 2011’. In other words, $dt is a string, not a date. This is why the -u option converts it to date like this:
1 2 3 4 |
$ date -d "$dt" Tue Nov 22 01:31:42 CET 2011 $ date -u -d "$dt" Tue Nov 22 01:31:42 UTC 2011 |
String can’t be converted to unixtime, so it is converted to date first, then the result is converted to UTC if needed and this result is then converted to unixtime.
Home
Contacts
Downloads
RoR
Linux
Notes
ERR
Servers
Русский