0

How can i compare two dates in the format using Perl:

"dd mon yyyy hh:mm:ss GMT"

e.g.: 12 May 2013 10:10:20 GMT

I cannot install any external Perl modules. Please advice.

david
  • 122
  • 3
  • 11
  • 4
    "I cannot any install external Perl modules" — That [isn't very likely](http://www.perlmonks.org/?node=693828) – Quentin Jun 11 '13 at 09:38
  • 3
    "I cannot install any external Perl modules" - Then that is the first problem you should solve. If you can't install modules from CPAN then you're not using most of the power of Perl. Fixing that should be your highest priority. – Dave Cross Jun 11 '13 at 11:08
  • If you can have your own Perl code on a system, you can install a Perl module. Either through [local::lib](http://p3rl.org/local::lib) or just copying them with your code. – Brad Gilbert Aug 24 '13 at 01:58

3 Answers3

5

If you have Perl v5.9.5 or newer, you can use Time::Piece core module. Here's a simple demonstration of relevant operations

  • Convert the dates from string to Time::Piece object

    my $date = "12 May 2013 10:10:20 GMT";
    my $tp1 = Time::Piece->strptime($date, "%d %B %Y %T %Z");
    my $tp2 = ...
    
  • Find the difference between the 2 time

    my $diff = $tp2 - $tp1;
    

    which gives you a Time::Seconds object.

  • Finally, display the difference in units of seconds (or something else).

    print $diff->seconds;
    
  • Or you could just compare the two directly (thanks stevenl)

    $tp2 > $tp1
    

References:

  1. Time::Seconds
  2. Time::Piece
  3. man strftime for format string to use with Time::Piece->strptime
  4. man strptime for format string to use with $tp->strftime
    • Note that only format characters aAbBcdHIjmMpSUwWxXyYZ% are safe if you're using non-Unix system (for example, Window's Perl doesn't support the %e specifier).
Community
  • 1
  • 1
doubleDown
  • 8,048
  • 1
  • 32
  • 48
2

Convert the dates to the format yyyymmddhhmmss (e.g. 20130512101020) and compare as strings. Handling the time zones might get tricky without modules, though.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • hmm looks good. Don't have to anything with timezones here. yyyymmddhhmmss works in all cases? – david Jun 11 '13 at 09:42
  • @david: Try to find a case when it does not. – choroba Jun 11 '13 at 09:44
  • yep , i'm trying to but i don't think ill find one :) – david Jun 11 '13 at 09:46
  • Why did this answer get a -1. Are you kidding me? – david Jun 11 '13 at 09:51
  • 1
    @david Because any answer about dates that treats dates as simple strings or numbers is a bad answer. – darch Jun 11 '13 at 15:43
  • @darch: Can you elaborate? – choroba Jun 11 '13 at 23:42
  • @choroba Dates are hard. Shockingly so. They involve more tradition than science and more politics than either of those. Solutions that don't take these complexities into account contain the most expensive kinds of bugs: those that appear to be working correctly during development only to fail hilariously (and possibly invisibly) after deployment. – darch Jun 12 '13 at 08:09
1

One of the most popular date modules is DateTime. It has a FAQ which may help you get started.

sub to_comparable {

       my ($date) = @_;
       my ($H,$M,$S,$d,$m,$Y) = $date =~ m{^([0-9]{2}):([0-9]{2}):([0-9]{2}), ([0-9]{2})/([0-9]{2})/([0-9]{4})\z}
          or die;
       return "$Y$m$d$H$M$S";
    }

    if (to_comparable($date2) < to_comparable($date1)) {
       ...
    } else {
       ...
    }
Jayram
  • 18,820
  • 6
  • 51
  • 68