Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timedifference using only sh 1

Status
Not open for further replies.

lhg1

IS-IT--Management
Mar 29, 2005
134
DK
Hi

I am currently using perl to look at time difference f.ex between files.

But due to a monitoring tool I need to convert them into simple shell

The only thing this scripts does is to turn a date into secounds from 1970, is there any one that knows a way to do this in sh.

What I need is the ability to convert different types of timestamps into secounds.



Code:
# TimeDiff.pl '2004 Mar 22 15:00' '22032004-14:00'

$Var1 = "$ARGV[0]\n";
@TmpArrayTid1 = split /\s|:/, $Var1;
#@TmpArray vil så se sådan ud: (Mar,22,11,27)
#print "$TmpArrayTid1[0]\n";
# TmpArrayTid1 0=Year 1=Mrd 2=Day 3=Hour 4=Min

$Var2 = "$ARGV[1]\n";
$Var2 =~ /(\d{2})(\d{2})(\d{4})-(\d{2}):(\d{2})/;
#print "$1\n";

# $1=day $2=mrd $3=Year $4=hour $5=Min


# Mar 22 12:32 2004

$Mdr = {
'Jan' => 1,
'Feb' => 2,
'Mar' => 3,
'Apr' => 4,
'May' => 5,
'Jun' => 6,
'Jul' => 7,
'Aug' => 8,
'Sep' => 9,
'Oct' => 10,
'Nov' => 11,
'Dec' => 12,
};

#print "Month number for February is $Mdr->{("$TmpArrayTid1[1]")}\n";



use Time::Local;

#Converts a date into EPOCH:
# 2004 Mar 22 15:17   # "$TmpArrayTid1[0]\n" 0=Year 1=Mrd 2=Day 3=Hour 4=Min
 $sec		 = "0";
 $min		 = "$TmpArrayTid1[4]\n";
 $hour		 = "$TmpArrayTid1[3]\n";
 $day		 = "$TmpArrayTid1[2]\n"; 
 $mon		 = $Mdr->{("$TmpArrayTid1[1]")};  
# print "$mon\n";
 $year		 = "$TmpArrayTid1[0]\n";
$RunEPOCH =  timelocal($sec,$min,$hour,$day,($mon-1),($year-1900));

#print "RunEpoch =\t [$RunEPOCH]\n";


#Converts a date into EPOCH:
# 22032004-11:23   $1=day $2=mrd $3=Year $4=hour $5=Min
 $sec		 = "0";
 $min		 = "$5\n";
 $hour 		 = "$4\n";
 $day		 = "$1\n";
 $mon		 = "$2\n"; 
# print "$mon\n";
 $year		 = "$3\n";

$EpochSec = timelocal($sec,$min,$hour,$day,($mon-1),($year-1900));
#print "DateEpoch =\t [$EpochSec]\n";

#Finds diff. between the 2 Epoch sekund vars:
$Diff = (($RunEPOCH - $EpochSec) / 60 );
print "$Diff\n";


Hope someone can help.

/Lhg
 
Maybe you needn't convert at all?
Have a look at your ls man pages; maybe there is something like this:
ls -l --time-style=+%s

At least this works for SuSE Linux.
What is your flavour of Unix?
 
Hi

I'm using Tru64 and HP-UX

I can'tseem to get the ls you have to work.

And the conversion would be the best coice for me.

/Lhg
 
If you have the date command,
I think you can use it to convert back and forth...

First we create a timestamp so we have something to convert:
Code:
geir@debian:~$ mytime=`date +%F" "%R`
Print it to se what it looks like:
Code:
geir@debian:~$ echo $mytime
2008-02-11 19:28
Now we try to convert it to Unix timestamp:
Code:
geir@debian:~$ date -d "$mytime" +%s
1202754480

Is it something like this you'r looking for?
:)


 
Hi

From the article (great stuff) I made this simple function to get minutes from 1 jan 4713 BC

Code:
get_minutes_from_JDD()
{
#Imput parm definition ex.  get_minutes_from_EPOCH 2008 12 31 23 59
# $1 = year    (yyyy)
# $2 = month   (01-12)
# $3 = day     (01-31)
# $4 = hour    (01-24)
# $5 = minutes (00-59)
 
year=$1
month=$2
day=$3
hh=$4
mi=$5
 
typeset -i JDD
 
#Gets the number of days from 1 January 4713 BC
JDD=$(($day-32075+1461*($year+4800+($month-14)/12)/4+367*($month-2-($month-14)/12*12)/12-3*(($year+4900+($month-14)/12)/100)/4))
 
#Makes it into minutes and adds minutes in timestamp
(( minutes=${JDD}*1440+${hh}*60+${mi}  ))
 
echo $minutes
}
 
 
get_minutes_from_JDD 2008 12 31 23 58

Thanks for the help.

/lhg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top