Author: dylan
Date: 2004-08-22 00:19:11 -0400 (Sun, 22 Aug 2004)
New Revision: 356

Added:
   trunk/main/common/lib/Haver/Formats.pm
Modified:
   trunk/main/common/lib/Haver/Util/Misc.pm
   trunk/main/common/lib/POE/Filter/Haver.pm
Log:
Moved formatting stuff over to
Haver::Formats, removed from Haver::Util::Misc.
Made the filter more forgiving, temporarily.


Added: trunk/main/common/lib/Haver/Formats.pm
===================================================================
--- trunk/main/common/lib/Haver/Formats.pm      2004-08-22 04:18:11 UTC (rev 
355)
+++ trunk/main/common/lib/Haver/Formats.pm      2004-08-22 04:19:11 UTC (rev 
356)
@@ -0,0 +1,99 @@
+# Haver::Formats - Parsing and Formatting routines.
+# 
+# Copyright (C) 2004 Dylan William Hardison
+#
+# This module is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This module is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this module; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+package Haver::Formats;
+use strict;
+#use warnings;
+use Haver::Preprocessor;
+
+use POSIX          qw( strftime );
+use Time::Duration qw( duration_exact concise );
+use Date::Parse    qw( str2time );
+
+use Carp;
+use Exporter;
+use base 'Exporter';
+
+our @EXPORT_OK = qw( format_datetime parse_datetime parse_duration 
format_duration );
+our %EXPORT_TAGS = (
+       'datetime' => [qw( format_datetime parse_datetime )],
+       'duration' => [qw( format_duration parse_duration )],
+);
+our $VERSION = 0.03;
+our $RELOAD = 1;
+
+
+# Author: dylan
+sub format_datetime {
+       # dylan: Because bd_ thought it should work this way...
+       # ASSERT: @_ <= 1;
+       my $now = @_ ? shift : time;
+
+       strftime('%Y-%m-%d %H:%M:%S %z', localtime($now));
+}
+
+
+sub parse_datetime {
+       # ASSERT: @_ == 1;
+       my $str = shift;
+       # patterns
+       my $date = qr/(\d{4})-(\d\d)-(\d\d)/;
+       my $time = qr/(\d\d):(\d\d):(\d\d)/;
+       my $tz   = qr/([+-])(\d{4})/;
+
+       if ($str =~ /^$date $time $tz$/) {
+               return str2time($str);
+       } else {
+               croak "Datetime format is invalid!";
+       }
+}
+
+# TODO
+#  * Do this ourself, don't rely on Time::Duration.
+sub format_duration {
+       my ($secs) = shift;
+       
+       return concise(duration_exact($secs));
+}
+
+sub parse_duration {
+       my $dur   = shift;
+       my $secs  = 0;
+
+       my %table = (
+               y => 31556930, # a year
+               w => 604800,   # a week
+               d => 86400,    # a day
+               h => 3600,     # an hour
+               m => 60,       # a minute
+               s => 1,        # a second
+       );
+
+       # If there is no s at the end,
+       # add one.
+       # Thus 5h23 is the same as 5h23s.
+       if ($dur =~ s/(\d+)$//) {
+               $dur .= "${1}s";
+       }
+       
+       while ( $dur =~ s/^(\d+)([ywdhms])// ) {
+               $secs += $1 * $table{$2};
+       }
+       return $secs;
+}
+
+1;

Modified: trunk/main/common/lib/Haver/Util/Misc.pm
===================================================================
--- trunk/main/common/lib/Haver/Util/Misc.pm    2004-08-22 04:18:11 UTC (rev 
355)
+++ trunk/main/common/lib/Haver/Util/Misc.pm    2004-08-22 04:19:11 UTC (rev 
356)
@@ -19,43 +19,17 @@
 use strict;
 #use warnings;
 use Haver::Preprocessor;
-use POSIX qw( strftime );
+use Scalar::Util qw(reftype);
 use Carp;
+
 use Exporter;
-use Scalar::Util qw(reftype);
 use base 'Exporter';
 
-our @EXPORT_OK = qw( format_datetime merge_struct parse_datetime );
+our @EXPORT_OK = qw( merge_struct);
 our $VERSION = 0.02;
 our $RELOAD = 1;
 
 
-# Author: dylan
-sub format_datetime {
-       # dylan: Because bd_ thought it should work this way...
-       # ASSERT: @_ <= 1;
-       my $now = @_ ? shift : time;
-
-       strftime('%Y-%m-%d %H:%M:%S %z', localtime($now));
-}
-
-
-sub parse_datetime {
-       # ASSERT: @_ == 1;
-       my $str = shift;
-       require Date::Parse;
-       # patterns
-       my $date = qr/(\d{4})-(\d\d)-(\d\d)/;
-       my $time = qr/(\d\d):(\d\d):(\d\d)/;
-       my $tz   = qr/([+-])(\d{4})/;
-
-       if ($str =~ /^$date $time $tz$/) {
-               return Date::Parse::str2time($str);
-       } else {
-               croak "Datetime format is invalid!";
-       }
-}
-
 # Author: bdonlan
 sub merge_struct {
        # ASSERT: @_ == 2;

Modified: trunk/main/common/lib/POE/Filter/Haver.pm
===================================================================
--- trunk/main/common/lib/POE/Filter/Haver.pm   2004-08-22 04:18:11 UTC (rev 
355)
+++ trunk/main/common/lib/POE/Filter/Haver.pm   2004-08-22 04:19:11 UTC (rev 
356)
@@ -32,7 +32,10 @@
 sub new {
        my ($this) = @_;
        
-       return $this->SUPER::new(Literal => $CRLF);
+       return $this->SUPER::new(
+               OutputLiteral => $CRLF,
+               InputRegexp => qr/\r?\n/,
+       );
 }
 
 sub get {


Reply via email to