1833780 Members
2259 Online
110063 Solutions
New Discussion

Re: Date conversion

 
Jayson B. Hurd
Advisor

Date conversion

Is there a command that will convert the output from `date` to seconds since the epoch and then back?
6 REPLIES 6
Mel Burslan
Honored Contributor

Re: Date conversion

The answer to all your date conversion woes is here if you prefer shell version:

http://mirrors.develooper.com/hpux/caljd-2.23.sh

and here if you prefer perl version:

http://mirrors.develooper.com/hpux/caljd-2.2.pl

thanks to A. Clay Stephenson of these forums.
________________________________
UNIX because I majored in cryptology...
A. Clay Stephenson
Acclaimed Contributor

Re: Date conversion

You don't really need it. See my answer to your previos post. The answer to this question

is Perl's time and localtime functions.
If it ain't broke, I can fix that.
Mel Burslan
Honored Contributor

Re: Date conversion

If you really really need it in a dedicated script, here is how I do it:

#!/usr/bin/ksh
#
# display # of seconds since 1/1/1970 00:00:00 GMT
#

/usr/bin/date -u '+%y:%j:%H:%M:%S' | IFS=: read YR DAY HR MIN SEC

print $(((YR*365+YR/4+((YR%4)>0)+DAY+10956)*86400+(HR*3600+MIN*60+SEC)))


and in case last line breaks due to length, print and everything after that is on one single line
________________________________
UNIX because I majored in cryptology...
A. Clay Stephenson
Acclaimed Contributor

Re: Date conversion

Try this:

typeset -i NOW=$(perl -e 'print time()')
echo "Epoch seconds = ${NOW}"
(( NOW -= 300 )) # subtract 5 minutes
echo "Epoch seconds - 5 minutes = ${NOW}"

typeset DT=$(perl "print scalar localtime(${NOW})")
echo "Date 5 minutes ago = ${DT}"
If it ain't broke, I can fix that.
vinod_25
Valued Contributor

Re: Date conversion

Hi Hurd,

if i have taken your question right...

try

# date +%s

if this served ur query update me

Regards

Vinod K
vinod_25
Valued Contributor

Re: Date conversion

hi hurd

added to my previous mail

The following PERL one-liner also prints the seconds since the start of
the epoch:


/opt/perl/bin/perl -e '$epoch = time; print "$epoch\n";'

Regards

Vinod K