bug-sh-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

date 2.0 -- converting seconds to a date


From: Charles Karney
Subject: date 2.0 -- converting seconds to a date
Date: Thu, 27 Sep 2001 15:40:07 -0400

CONFIGURATION

    Redhat Linux 6.2
    date 2.0

PROBLEM

    I would like to convert from seconds since epoch (date +%s) to a
    readable date (in order to decode the time in a tar incremental log
    file).

BUGS/MISFEATURES

    1 Info documentation on how to do the conversion is often wrong
    2 Getting it right is too messy

EXAMPLE

    TZ=EST5EDT export TZ

    SEC=`date -d '2001-09-11 09:00' +%s`
    echo $SEC ==> 1000213200

So I would like to convert $SEC to "2001-09-11 09:00:00 EDT".

The info documentation suggests

    date -d "1970-01-01 +$SEC sec" +"%Y-%m-%d %T %Z"

But this gives

    2001-09-11 14:00:00 EDT

which is 5 hours off.  So I try slipping in a UTC spec on the origin

    date -d "1970-01-01 UTC +$SEC sec" +"%Y-%m-%d %T %Z"

which gives

    2001-09-11 10:00:00 EDT

i.e., still 1 hour off!  The "problem" is that when adjusting for a time
delta, date compensates for a daylight savings time transition.  Thus

    date -d "2001-10-28 +1 hour" ==> Sun Oct 28 01:00:00 EDT 2001
    date -d "2001-10-28 +2 hour" ==> Sun Oct 28 02:00:00 EST 2001

But these times actually differ by 2 hours!

WORKAROUND

I can get around this feature by doing the delta calculation in a zone
without daylight savings time (viz UTC).  So the following works to convert
$SEC to a readable date:

    date -d "`env TZ=UTC date -d \"1970-01-01 +$SEC sec\"`" +"%Y-%m-%d %T %Z"

which gives the desired

    2001-09-11 09:00:00 EDT

SUGGESTION

Only do the daylight savings time adjustment on time deltas when the delta
includes time units of days or longer.  If the delta involves only hours,
minutes, and seconds, then DON'T do this adjustment, e.g.,

    date -d "2001-10-28 +2 bour" ==> Sun Oct 28 01:00:00 EST 2001

Then modify the documentation to use

    date -d "1970-01-01 UTC +$SEC sec" +"%Y-%m-%d %T %Z"

If this is too dangerous a change, then I suggest documenting the
workaround.

-- 
Charles Karney                  Email:  address@hidden
Sarnoff Corporation             Phone:  +1 609 734 2312
Princeton, NJ 08543-5300        Fax:    +1 609 734 2586




reply via email to

[Prev in Thread] Current Thread [Next in Thread]