The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
epoch time ali560045 Shell Programming and Scripting 3 06-02-2008 01:41 AM
script to convert epoch into human-readable snoman1 Shell Programming and Scripting 3 04-18-2008 12:59 PM
epoch time in shell script robsonde SUN Solaris 12 04-07-2008 08:04 PM
Convert milliseconds to standard time chiru_h Shell Programming and Scripting 1 07-19-2007 10:45 AM
how to convert epoch time to readible format? cin2000 Shell Programming and Scripting 11 12-19-2005 04:14 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-16-2005
Registered User
 

Join Date: May 2004
Posts: 45
Convert from standard epoch time from a shell script?

Is there an easy method to do an on the fly conversion of a standard epoch time (seconds from 1970) to more readable date format?

Does Unix have anything built in to do this?
Reply With Quote
Forum Sponsor
  #2  
Old 09-16-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
Look at datecalc. That should have what you are looking for.

You can do this as well. But I doubt it will be supported in all versions of linux.

Code:
date -d '1970-01-01 UTC 1126884183 seconds' +"%Y-%m-%d %T %z"
It gives me

Code:
2005-09-16 08:23:03 -0700
vino
Reply With Quote
  #3  
Old 09-16-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,615
Each version of unix handles timezones a little differently. Ignoring timezones more or less, this comes fairly close



Code:
#! /usr/bin/ksh


#
#  convert unix time to a string
#
#   time="$(unixsecond2timestring $seconds)" is
#      similiar to the c construct:
#               strcpy(time,ctime(&xdate));
#      except that it ignores timezone considerations.
#      This means that it is exactly like:
#          strcpy(time,asctime(gmtime(&xdate)));
#
#      The only way to handle timezones is to figure out
#      your local number of seconds difference from GMT and
#      adjust the value of seconds before passing it.  This
#      means that for small values of "seconds" you may adjust
#      it to a negative number.  That's ok, this routine can
#      handle numbers in the range -86400 to 2147483647.
#
unixsecond2timestring() {
        integer uxsec mjd daysecond hour hoursecond minute second
        typeset -Z2 val
        typeset -R2 val2
        typeset -L3 fdow
        typeset dow time year month day
        typeset months
        set -A months xxx Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
        uxsec=$1

#
#  Calculate
#    mjd=modified julian day number  (range is 40586 - 65442)
#      Dec 31, 1969 has  mjd=40586
#      Jan 19, 2038 has  mjd=65442
#    daysecond=number of second during the day (range is 0 - 86399)
#    hoursecond=number of second during hour   (range is 0 - 3599)
#    hour, minute, second represent current time
#
        ((mjd=(uxsec/86400)+40587))
        ((daysecond=uxsec%86400))
        ((hour=daysecond/3600))
        ((hoursecond=daysecond-(hour*3600)))
        ((minute=hoursecond/60))
        ((second=hoursecond%60))

#
#    Adjust things if we are negative
        if ((uxsec<0)) ; then
                ((mjd=mjd-1))
                ((hour=(hour+24)%24))
        fi

#
#    Convert mjd to year, month day and get dow (day of week)
        datecalc -j $mjd | read year month day
        dow=$(datecalc -D $year $month $day)

#
#    Format the date
        val=$hour
        time="${val}:"
        val=$minute
        time="${time}${val}:"
        val=$second
        time="${time}${val} $year"
        fdow=$dow
        val2=$day
        time="${fdow} ${months[month]} $val2 $time"
        echo "$time"
        return
}


integer unixsecond
typeset -R11 dsecond

while (($#)) ; do
        unixsecond=$1
        shift
        time1=$(unixsecond2timestring $unixsecond)
        dsecond=$unixsecond
        ((unixsecond2=unixsecond-(5*3600)))
        time2=$(unixsecond2timestring $unixsecond2)

        print "arg = $dsecond     ${time1}    ${time2}"
done
exit 0
Reply With Quote
  #4  
Old 09-16-2005
Registered User
 

Join Date: Jul 2005
Posts: 137
Code:
ruby -e 'puts Time.at(1126884183).strftime("%Y-%m-%d")'

2005-09-16
Reply With Quote
  #5  
Old 09-18-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,248
Code:
gawk 'BEGIN{print strftime("%Y-%m-%d",1126884183)}'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0