Changeset 1880

Show
Ignore:
Timestamp:
07/18/06 17:00:14
Author:
miyagawa
Message:

0.02: lc() if timespec is not 1-char long

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Time-Duration-Parse/trunk/Changes

    r1879 r1880  
    11Revision history for Perl extension Time::Duration::Parse 
     2 
     30.02  Tue Jul 18 16:58:53 JST 2006 
     4        - Accept upper-case timespec like '3 Seconds' as well. 
     5        - Unified exception format 
    26 
    370.01  Tue Jul 18 02:29:01 2006 
  • Time-Duration-Parse/trunk/lib/Time/Duration/Parse.pm

    r1879 r1880  
    22 
    33use strict; 
    4 our $VERSION = '0.01'; 
     4our $VERSION = '0.02'; 
    55 
    66use Carp; 
     
    2424    my $duration = 0; 
    2525    while ($timespec =~ s/^\s*(\d+)\s+(\w+)(?:\s*(?:,|and)\s*)*//i) { 
    26         if (my $unit = $_Expiration_Units{$2}) { 
    27             $duration += $1 * $unit; 
     26        my($amount, $unit) = ($1, $2); 
     27        $unit = lc($unit) unless length($unit) == 1; 
     28 
     29        if (my $value = $_Expiration_Units{$unit}) { 
     30            $duration += $amount * $value; 
    2831        } else { 
    2932            Carp::croak "Unknown timespec: $1 $2"; 
     
    3235 
    3336    if ($timespec) { 
    34         Carp::croak "timespec with cruft leftover: $timespec"; 
     37        Carp::croak "Unknown timespec: $timespec"; 
    3538    } 
    3639 
    3740    return $duration; 
    3841} 
    39  
    4042 
    41431; 
     
    7577 
    7678Parses duration string and returns seconds. When it encounters an 
    77 error in a given string, it dies an exception saying: 
    78  
    79 =over 8 
    80  
    81 =item Unknown timespec: %d %s 
    82  
    83 =item timespec with cruft leftover: %s 
    84  
    85 =back 
     79error in a given string, it dies an exception saying "Unknown 
     80timespec: blah blah blah". This function is exported by default. 
    8681 
    8782=head1 AUTHOR 
  • Time-Duration-Parse/trunk/t/01_parse.t

    r1879 r1880  
    11use strict; 
    2 use Test::More tests => 9
     2use Test::More tests => 11
    33 
    44use Time::Duration::Parse; 
     
    1616 
    1717ok_duration '3 seconds', 3; 
     18ok_duration '3 Seconds', 3; 
    1819ok_duration '3 s', 3; 
    1920ok_duration '6 minutes', 360; 
    2021ok_duration '6 minutes and 3 seconds', 363; 
     22ok_duration '6 Minutes and 3 seconds', 363; 
    2123ok_duration '1 day', 86400; 
    2224ok_duration '1 day, and 3 seconds', 86403;