[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Unexpected behaviour in date; calculates daylight savings time in co
From: |
Jim Meyering |
Subject: |
Re: Unexpected behaviour in date; calculates daylight savings time in correctly... |
Date: |
19 May 2001 08:39:10 +0200 |
User-agent: |
Gnus/5.090003 (Oort Gnus v0.03) Emacs/21.0.104 |
Karl Zaryski <address@hidden> wrote:
| Hello! I may have found a bug in the GNU date. I expected that the
| following statements would give the same result:
|
| date ; date -d '01/01/1970 utc '`date +%s`' secs'
|
| But the results are off by an hour:
|
| Wed May 16 12:38:31 EDT 2001
| Wed May 16 13:38:31 EDT 2001
|
| I haven't delved into the source code for date (I'm no expert programmer)
| but I suspect that the problem may be because I'm starting with a date out
| of daylight savings time (start of epoch) and adding a number of seconds to
| arrive at a result within daylight savings time... somewhere it adds the
| extra hour. I've worked out a workaround for my scripts, but it's a bit of
| a kludge.
|
| Is this a bug? If not, can you explain this unexpected behaviour?
It's not a bug.
But it is a little tricky. Your inner date command needs -u.
$ date ; date -d '01/01/1970 utc '`date -u +%s`' secs'
Sat May 19 08:34:39 CEST 2001
Sat May 19 08:34:39 CEST 2001
BTW, there are some examples in the documentation that show how to do that.
[the following is at the bottom of the section you see if you run
the command `info date example': ]
* To convert a date string to the number of seconds since the epoch
(which is 1970-01-01 00:00:00 UTC), use the `--date' option with
the `%s' format. That can be useful in sorting and/or graphing
and/or comparing data by date. The following command outputs the
number of the seconds since the epoch for the time two minutes
after the epoch:
date --date='1970-01-01 00:02:00 +0000' +%s
120
If you do not specify time zone information in the date string,
`date' uses your computer's idea of the time zone when
interpreting the string. For example, if your computer's time
zone is that of Cambridge, Massachusetts, which was then 5 hours
(i.e., 18,000 seconds) behind UTC:
# local time zone used
date --date='1970-01-01 00:02:00' +%s
18120
* If you're sorting or graphing dated data, your raw date values may
be represented as seconds since the epoch. But few people can
look at the date `946684800' and casually note "Oh, that's the
first second of the year 2000 in Greenwich, England."
date --date='2000-01-01 UTC' +%s
946684800
To convert such an unwieldy number of seconds back to a more
readable form, use a command like this:
# local time zone used
date -d '1970-01-01 UTC 946684800 seconds' +"%Y-%m-%d %T %z"
1999-12-31 19:00:00 -0500