rev 1875 - trunk/buscador/lib/Buscador

[prev] [thread] [next] [lurker] [Date index for 2005/02/14]

From: simon
Subject: rev 1875 - trunk/buscador/lib/Buscador
Date: 10:39 on 14 Feb 2005
Author: simon
Date: 2005-02-14 10:39:30 +0000 (Mon, 14 Feb 2005)
New Revision: 1875

Modified:
   trunk/buscador/lib/Buscador/Date.pm
Log:
Make dates more 'intuitive' i.e try and DTRT wherever possible


Modified: trunk/buscador/lib/Buscador/Date.pm
===================================================================
--- trunk/buscador/lib/Buscador/Date.pm	2005-02-14 10:39:11 UTC (rev 1874)
+++ trunk/buscador/lib/Buscador/Date.pm	2005-02-14 10:39:30 UTC (rev 1875)
@@ -71,7 +71,11 @@
     my $deftime    = localtime;
     my $time;
 
-    my $s = sprintf "%.4d-%.2d-%.2d", $objects[0], $objects[1], $objects[2];
+    my $year  = fix_year($objects[0]); 
+    my $month = fix_month($objects[1]);
+    my $day   = $objects[2]; 
+
+    my $s = sprintf "%.4d-%.2d-%.2d", $year, $month, $day;
     eval {
             $time      = Time::Piece->strptime($s, "%Y-%m-%d");
     };
@@ -92,8 +96,11 @@
 sub month {
     my ($self,$r, @objects) = @_;
 
-    my $year  = $objects[0];
-    my $month = $objects[1];
+    my $year  = fix_year($objects[0]);  return 0 unless defined $year;
+    my $month = fix_month($objects[1]); return 0 unless defined $month;
+
+
+    
     my $s     = sprintf "%.4d-%.2d-%.2d", $year, $month, 15;
     my $date  = Time::Piece->strptime($s, "%Y-%m-%d");
 
@@ -122,9 +129,13 @@
 
 
     my @months;
-    my $year = $objects[0];
+    my $year = fix_year($objects[0]);
+    return 0 unless defined $year;
+        
 
+    
 
+
     for my $m (1..12) {
         $months[ $m - 1 ] = scalar Email::Store::Date->search( year => $year, month => $m );
     }
@@ -139,6 +150,35 @@
     return 1;
 }
 
+sub fix_year {
+    my $year = shift; return unless $year =~ /^\d+$/;
+    $year   += 0; # force numerical    
+    my @lt   = localtime();
+    my $cur  = sprintf("%02d", $lt[5] % 100) + 0;
+    my $millenia = $lt[5] + 1900; $millenia -= $millenia % 1000;
+
+    return $millenia           + $year if ($year <= $cur);
+    return ($millenia - 1000)  + $year if ($year < 100);
+    return $year;
+}
+
+
+sub fix_month {
+    my $month = shift;
+    if ($month =~ /^\d+/) {
+        return undef if $month > 12 || $month < 1;
+        return $month;
+    }    
+    $month = lc(substr $month,0,3);
+    my %months; my $counter = 1;
+    for (qw(jan feb mar apr may jun jul aug sep oct nov dec)) {
+        $months{$_} = $counter++;
+    }
+    
+    return $months{$month};
+
+}
+
 use Class::DBI::Pager;
 sub do_pager {
     my ($self, $r) = @_;

Generated at 11:00 on 14 Feb 2005 by mariachi 0.52