Computing.Net > Forums > Unix > script to convert date/time to seco

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

script to convert date/time to seco

Reply to Message Icon

Original Message
Name: steve
Date: September 12, 2002 at 02:33:44 Pacific
Subject: script to convert date/time to seco
OS: solaris/kshell
CPU/Ram: dl580
Comment:

perused entire forum and subject came up many times but was never addressed explicitly. i need a short script written entirely in korn shell (no C or perl, etc) to convert a files date/time into seconds. referencing the 1/1/70 epoch would be great. script must ls -l, awk/nawk the date/time field out, and convert the resulting into a pure seconds...any reference date/time in the past is suitable. if someone has something canned they can post or a prolific expert can whip one out they would be helping like 5 or 6 people here on the forum. i can't really script competently at this point but i know some of the ins/outs and could easily adapt it.

appreciatively, s


Report Offensive Message For Removal


Response Number 1
Name: LANkrypt0
Date: September 12, 2002 at 13:48:44 Pacific
Reply: (edit)

Meh gimme a little bit, Ill give it a shot :)


Report Offensive Follow Up For Removal

Response Number 2
Name: LANkrypt0
Date: September 12, 2002 at 15:02:42 Pacific
Reply: (edit)

Ok try this:

#!/bin/ksh
### STATIC INFO ###
refdatey=1970
refdatem=1
refdated=1
chkdatey=2002
###################

function timetosec {
hrtse=$(($chkdateh*3600))
mitse=$(($chkdatem*60))
totse=$(($hrtse+$mitse))
totaldays
}

function totaldays {
yrdiff=$(($chkdatey-$refdatey))
yrtda=$(($yrdiff*365))
pttda=$(($yrtda+$chkdated))
modays
fitda=$(($pttda+$dta))
daystosec
}

function daystosec {
datse=$(($fitda*24*60*60))
febextda
}

function febextda {
lepyrs=$(($yrdiff/4))
febadd=$(($lepyrs*24*60*60))
totalsec
}

function totalsec {
totalseconds=$(($datse+$totse+$febadd))
}

function modays {
case $chkdatemo in
Jan)
dta=0
;;
Feb)
dta=31
;;
Mar)
dta=59
;;
Apr)
dta=90
;;
May)
dta=120
;;
Jun)
dta=151
;;
Jul)
dta=181
;;
Aug)
dta=212
;;
Sep)
dta=243
;;
Oct)
dta=273
;;
Nov)
dta=304
;;
Dec)
dta=334
;;
esac
}

list=`ls`
for var in $list;do
chkdatemo=`ls -l $var | awk '{print $6}'`
chkdated=`ls -l $var | awk '{print $7}'`
chkdateh=`ls -l $var | awk '{print $8}' | awk -F : '{print $1}'`
chkdatem=`ls -l $var | awk '{print $8}' | awk -F : '{print $2}'`
timetosec
echo "$var - $totalseconds" >> secondsoutput
done

This works fine for me. It takes the list of files in, then goes through the list, picking out what it needs (date/time) then processes that info through the function, then dumps the results into a file (secondsoutput).

This one also adjusts and adds seconds on for leap years.



Report Offensive Follow Up For Removal

Response Number 3
Name: Jerry Lemieux
Date: September 12, 2002 at 16:08:58 Pacific
Reply: (edit)

This is a little shorter.

#!/bin/ksh

function calcYears {

if (( $1 % 100 ))
then
if (( $1 % 4 ))
then
(( seconds += 31536000 )) ## non-leap year
else
(( seconds += 31622400 )) ## leap year
fi
else
(( seconds += 31536000 )) ## non-leap year
fi

}

epoch=1970
seconds=0
processYear=$epoch
(( days=$(date "+%j") - 1 ))
(( seconds=days*86400 ))
(( seconds=seconds + ( $(date "+%H") * 3600) ))
(( seconds=seconds + ( $(date "+%M") * 60) ))
(( seconds=seconds + $(date "+%S") ))


currYear=$(date "+%Y")

while [[ $processYear -lt currYear ]]
do
calcYears $processYear
(( processYear += 1 ))
done

print "As of $(date "+%H:%M:%S on %d %h, %Y"), $seconds seconds have elapsed since 1 January, 1970."


Report Offensive Follow Up For Removal

Response Number 4
Name: Jerry Lemieux
Date: September 12, 2002 at 16:55:47 Pacific
Reply: (edit)

Misread your post. However, I believe there is sufficient logic here to modify the script to fit your needs.

Jerry


Report Offensive Follow Up For Removal

Response Number 5
Name: jimbo
Date: September 12, 2002 at 20:54:01 Pacific
Reply: (edit)

there should be no space after the "F" and the ":" in the awk commands:

chkdateh=`ls -l $var | awk '{print $8}' | awk -F: '{print $1}'`
chkdatem=`ls -l $var | awk '{print $8}' | awk -F: '{print $2}'`

Script works with this fix.

-jim


Report Offensive Follow Up For Removal


Response Number 6
Name: ben
Date: September 15, 2002 at 04:37:01 Pacific
Reply: (edit)

i need the script that can convert the second to date/time.


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software