Google Answers Logo
View Question
 
Q: Perl regular expression code ( No Answer,   3 Comments )
Question  
Subject: Perl regular expression code
Category: Computers > Programming
Asked by: mattbert-ga
List Price: $10.00
Posted: 25 Aug 2006 06:26 PDT
Expires: 24 Sep 2006 06:26 PDT
Question ID: 759414
I need a regular expression I can use in perl that will look at text
strings of dates and times and tell me the difference, in minutes,
between the two.

Here are examples of the strings with timestamps:

08/18 10:29PM
08/18 8:44AM
08/06 3:18PM

I'd like perl code that could compare any two of these timestamps,
like the first two, and return the right answer (825 is the diff.
between the first two above, and 17674 is the diff. between the second
two timestamps above).

Note that the hour field is sometimes 1 and sometimes 2 digits, and
*not* in military format.

Clarification of Question by mattbert-ga on 27 Aug 2006 07:56 PDT
The program always runs within the same year (in this case, 2006).  It
never runs at end of February, so there's no concern for leap year or
end-of-year.

I'd use the Date::Manip suggestion, but I don't control the server
where the code runs so I cannot add modules.  I checked to see if
Date::Manip is installed, and it is not - which is too bad, as it
appears to be an elegant solution.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Perl regular expression code
From: rhinst-ga on 26 Aug 2006 21:48 PDT
 
Are you assuming dates are always in the year 2006? For example, how
many minutes are between 02/27 12:00AM and 03/01 12:00AM?  In a leap
year, there are 4,320 minutes in between those 2 times, but every
other year, there's just 2,880 minutes. If the year is not specified,
and it isn't always assumed to be a particular year, then this problem
is not possible to solve.
Subject: Re: Perl regular expression code
From: jlavold-ga on 26 Aug 2006 22:12 PDT
 
This isn't really what regular expressions are used for.

But here's a solution to your problem:



use Date::Manip;

my $date1 = "08/18 10:29PM";
my $date2 = "08/18 8:44AM";

my $diff = (UnixDate($date1,"%s") -  UnixDate($date2,"%s"))/60;




It requires you to install the Date::Manip module, which is available
on cpan, run "perl -MCPAN -e 'install Date::Manip'".  The beauty of
using this module is that it understands hundreds of different date
formats.  You can pretty much just throw any date string in there and
it will work.
Subject: Re: Perl regular expression code
From: errpt-ga on 03 Sep 2006 19:35 PDT
 
I write a piece code for your specific problem. u can just minus the
results u get from seconds_since_epoch to get the difference in
seconds between any two timestamps.

Rgds

Kai 
--------cut_begin-------
#!/usr/bin/perl
use Time::Local;

sub seconds_since_epoch {
    my $ts = shift;

    $ts =~ /(\d+)\/(\d+)\s+(\d+):(\d+)(AM|PM)/;
    my $mon = $1 - 1; 
    my $mday = $2;    
    my $min = $4;     
    my $hour = $3;    

    # we assume year is 1970
    my $year = 100;

    my $time;
    if ($5 eq 'AM') {
        $time = timelocal(0,$min,$hour,$mday,$mon,$year);
    } else {
        $hour += 12;
        $time = timelocal(0,$min,$hour,$mday,$mon,$year);
    }

    $time;
}


$ts_a = '08/18 10:29PM';
$ts_b = '08/18 8:44AM';
$ts_c = '08/06 3:18PM';

print "ts in seconds is " . &seconds_since_epoch($ts_a) . "\n";
print "ts in seconds is " . &seconds_since_epoch($ts_b) . "\n";
print "ts in seconds is " . &seconds_since_epoch($ts_c) . "\n";
--------cut_end--------

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy