[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Unexpected behaviour in date; calculates daylight savings tim e in c
From: |
Karl Zaryski |
Subject: |
RE: Unexpected behaviour in date; calculates daylight savings tim e in correctly... |
Date: |
Sun, 3 Jun 2001 13:59:30 -0400 |
Howdy... Since I'd already worked up a workaround, I took your word for it
below. Today, though, I'm working on a log-processing script that deals
with times, and I've been running into related trouble. Trying the command
line you suggested...
date ; date -d '01/01/1970 utc '`date -u +%s`' secs'
Sun Jun 3 12:45:20 EDT 2001
Sun Jun 3 13:45:20 EDT 2001
So, putting in the -u flag didn't help. I tried testing several systems
with various architectures and operating systems, and -u didn't make a
difference anywhere. The only system that gave the correct date both times
was a recent Sun box; an older Sun box and all PC boxes gave times one hour
off, regardless of use of the -u flag.
While searching on the net, I found evidence that other people have noticed
this problem... see
http://www.moongroup.com/archives/shell.scripting/2000-06/msg00047.html
Here are the results from trying this on various systems:
-=-
bash-2.03# uname -a
SunOS brother 5.8 Generic_108528-06 sun4u sparc SUNW,UltraSPARC-IIi-cEngine
bash-2.03# pwd
/tmp/sh-utils-2.0
bash-2.03# src/date --version
date (GNU sh-utils) 2.0
Written by David MacKenzie.
Copyright (C) 1999 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
bash-2.03# src/date ; src/date -d '01/01/1970 utc '`src/date -u +%s`' secs'
Sun Jun 3 13:12:31 EDT 2001
Sun Jun 3 13:12:31 EDT 2001
bash-2.03# src/date ; src/date -d '01/01/1970 utc '`src/date +%s`' secs'
Sun Jun 3 13:12:36 EDT 2001
Sun Jun 3 13:12:36 EDT 2001
-=-
bash-2.03# uname -a
FreeBSD big.blackberry.net 4.0-RELEASE FreeBSD 4.0-RELEASE #0: Fri Jun 23
14:10:31 EDT 2000 address@hidden:/usr/src/sys/compile/LOCAL
i386
bash-2.03# pwd
/tmp/sh-utils-2.0
bash-2.03# src/date --version
date (GNU sh-utils) 2.0
Written by David MacKenzie.
Copyright (C) 1999 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
bash-2.03# src/date ; src/date -d '01/01/1970 utc '`src/date -u +%s`' secs'
Sun Jun 3 13:10:20 EDT 2001
Sun Jun 3 14:10:21 EDT 2001
bash-2.03# src/date ; src/date -d '01/01/1970 utc '`src/date +%s`' secs'
Sun Jun 3 13:10:46 EDT 2001
Sun Jun 3 14:10:46 EDT 2001
-=-
address@hidden bb]$ uname -a
Linux grp01ykf.blackberry.net 2.4.5 #2 SMP Sun Jun 3 03:22:05 EDT 2001 i686
unknown
address@hidden bb]$ date --version
date (GNU sh-utils) 2.0
Written by David MacKenzie.
Copyright (C) 1999 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
address@hidden bb]$ date ; date -d '01/01/1970 utc '`date -u +%s`' secs'
Sun Jun 3 13:16:28 EDT 2001
Sun Jun 3 14:16:28 EDT 2001
address@hidden bb]$ date ; date -d '01/01/1970 utc '`date +%s`' secs'
Sun Jun 3 13:16:33 EDT 2001
Sun Jun 3 14:16:33 EDT 2001
-=-
leela [371]$ uname -a
SunOS leela 5.5.1 Generic_103641-31 i86pc i386 i86pc
leela [372]$ pwd
/tmp/knz/sh-utils-2.0
leela [373]$ src/date --version
date (GNU sh-utils) 2.0
Written by David MacKenzie.
Copyright (C) 1999 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
leela [374]$ src/date ; src/date -d '01/01/1970 utc '`src/date -u +%s`'
secs'
Sun Jun 3 13:23:48 EDT 2001
Sun Jun 3 14:23:48 EDT 2001
leela [375]$ src/date ; src/date -d '01/01/1970 utc '`src/date +%s`' secs'
Sun Jun 3 13:23:53 EDT 2001
Sun Jun 3 14:23:53 EDT 2001
-=-
@calum[375]$ uname -a
SunOS calum 5.7 Generic_106541-10 sun4m sparc SUNW,SPARCstation-10
@calum[376]$ pwd
/tmp/knz/sh-utils-2.0
@calum[377]$ src/date --version
date (GNU sh-utils) 2.0
Written by David MacKenzie.
Copyright (C) 1999 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
@calum[378]$ src/date ; src/date -d '01/01/1970 utc '`src/date -u +%s`'
secs'
Sun Jun 3 13:55:51 EDT 2001
Sun Jun 3 14:55:51 EDT 2001
@calum[379]$ src/date ; src/date -d '01/01/1970 utc '`src/date +%s`' secs'
Sun Jun 3 13:55:57 EDT 2001
Sun Jun 3 14:55:57 EDT 2001
-----Original Message-----
From: Jim Meyering [mailto:address@hidden
Sent: Saturday, May 19, 2001 2:39 AM
To: Karl Zaryski
Cc: 'address@hidden'
Subject: Re: Unexpected behaviour in date; calculates daylight savings
time in correctly...
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
- RE: Unexpected behaviour in date; calculates daylight savings tim e in correctly...,
Karl Zaryski <=