"Charitha Dias" schreef:

> The format of date and time is something like YYYYMMDDhhmmssmm (for
> example 2001030400060400). Actually I need to get time difference.
> For example I want to get difference between 2001030400053200 and
> 2001030400060400.

What precision do you need?

#!/usr/bin/perl
# delta-time guesser

  use strict ;
  use warnings ;

  sub delta_time
  {
    my $t ;

    my $RE =
       qr/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(?:(\d{2}))?/ ;
         #   ccyy   mm     dd     HH     MM     SS        CC

    sub calc { ( ( ( ( ( $1*365.25 + $2*365.25/12 + $3
                       ) * 24 + $4
                     ) * 60 + $5
                   ) * 60 + $6
                 ) + ($7 or 0)/100
               )
             }

    $_[0] =~ $RE  and  $t -= calc()  and
    $_[1] =~ $RE  and  $t += calc()  and  return $t ;
    return
  }


  if ( my $t = delta_time '2001030400053240',
                          '20010304000604' ) {
    printf "%.2f seconds\n", $t ;
  } else {
    print "ERROR\n" ;
  }

-- 
Affijn, Ruud

"Gewoon is een tijger."




Reply via email to