About date & time difference

Hello All,

I was having a look on threads on the Forum about time calculation but didn't find exactly this issue.

For instance, if we have these 2 dates, begin & end :

 
20100430235830
20100501000200

Is there anyway, awk, ksh, perl to calculate the difference in sec and get for our example : 210

Noting that the script should work for all dates and time of the year ...
It means leap years, months with 30 or 31 days, ... because begin & end dates could be more then several dates.

I'm working on a :
SunOS 5.10 Generic_118833-36 sun4u sparc SUNW,Sun-Fire-V240

As usual, thx a lot in advance ...

Can you tell me in what format it is there ?

20100430235830

yyyymmdd??????

touch -t 201004302358.30 file1
touch -t 201005010002.00 file2
time_elap=$(echo `stat -c%Y file2` - `stat -c%Y file1` | bc)
echo $time_elap

Note that I had to adjust the time going in with a decimal point before seconds. Depending on how/where this number is coming from, you might need to do something with strings.
file1 & file2 can be deleted when done; other filenames can be used.

perl -MTime::Local -le'
  /(....)(..)(..)(..)(..)(..)/g and
    push @dt, timelocal $6, $5, $4, $3, $2 - 1, $1 - 1900 
      for @ARGV;
  print $dt[1] - $dt[0]; 
  ' dt1 dt2

With your data:

% perl -MTime::Local -le'
  /(....)(..)(..)(..)(..)(..)/g and
    push @dt, timelocal $6, $5, $4, $3, $2 - 1, $1 - 1900
      for @ARGV;
  print $dt[1] - $dt[0];
  ' 20100430235830 20100501000200
210

Hi,

The format is : YYYYMMDDHHMMSS

Unfortunately, I don't have stat command on my system.

Is there any solution else ...

Regs,

Yep,
check my post above.

Hi Radu,

I executed your perl shell but got the following errors. Do you have an idea why ??

$ cat temp.sh
perl -MTime::Local -le'
/(....)(..)(..)(..)(..)(..)/g and
push @dt, timelocal $6, $5, $4, $3, $2 - 1, $1 - 1900
for @ARGV;
print $dt[1] - $dt[0] if eof;
' 20100430235830 20100501000200

$ perl temp.sh
Number found where operator expected at temp.sh line 7, near "' 20100430235830"
  (Might be a runaway multi-line '' string starting on line 2)
        (Missing operator before  20100430235830?)
Number found where operator expected at temp.sh line 7, near "20100430235830 20100501000200"
        (Missing operator before  20100501000200?)
syntax error at temp.sh line 2, near "-le"
Execution of temp.sh aborted due to compilation errors.

Regs,

---------- Post updated at 10:11 AM ---------- Previous update was at 10:06 AM ----------

Oh, Sorry , I got it ....

I'm typing perl twice ... I don't know well this language yet ....

Thx a lot for all ....