|
|
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 |
|
There is no answer at this time. |
|
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-------- |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |