Changeset 368

Show
Ignore:
Timestamp:
12/08/01 19:53:43
Author:
miyagawa
Message:

0.02

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Date-Range-Birth/trunk/Changes

    r365 r368  
    11Revision history for Perl extension Date::Range::Birth. 
     2 
     30.02  Sat Dec  8 19:52:42 JST 2001 
     4        - Added more tests 
     5        - Added EXAMPLE in pod 
     6        - Workaround for broken base.pm 
     7          (Thanks to William R. Ward <bill@wards.net>) 
    28 
    390.01  Sat Dec  8 00:24:49 2001 
  • Date-Range-Birth/trunk/MANIFEST

    r365 r368  
    66t/00_compile.t 
    77t/01_range.t 
     8t/02_fail.t 
  • Date-Range-Birth/trunk/README

    r365 r368  
    3131        If the age is provided as array reference (like `[ $young, $old ]'), 
    3232        returns range of birthday for those who are between `$young' - 
    33         `$old' years old. 
     33        `$old' years old. It may be handy for searching teenagers, etc. 
    3434 
    3535    Other methods are inherited from Date::Range. See the Date::Range 
    3636    manpage for details. 
     37 
     38EXAMPLE 
     39    Your customer database schema: 
     40 
     41      CREATE TABLE customer ( 
     42          name     varchar(64) NOT NULL, 
     43          birthday date NOT NULL 
     44      ); 
     45 
     46    What you should do is to select name and birthday of the customers who 
     47    are 2X years old (between 20 and 29). 
     48 
     49      use DBI; 
     50      use Date::Range::Birth; 
     51 
     52      my $dbh = DBI->connect( ... ); 
     53      my $range = Date::Range::Birth->new([ 20, 29 ]); 
     54 
     55      my $sth = $dbh->prepare(<<'SQL') 
     56      SELECT name, birthday FROM customer WHERE birthday >= ? AND birthday <= ? 
     57      SQL 
     58 
     59      # Date::Simple overloads to 'yyyy-mm-dd'! 
     60      $sth->execute($range->start, $range->end); 
     61 
     62      while (my $data = $sth->fetchrow_arrayref) { 
     63          print "name: $data->[0] birthday: $data->[1]\n"; 
     64      } 
     65      $dbh->disconnect; 
    3766 
    3867AUTHOR 
  • Date-Range-Birth/trunk/lib/Date/Range/Birth.pm

    r365 r368  
    33use strict; 
    44use vars qw($VERSION); 
    5 $VERSION = '0.01'; 
     5$VERSION = '0.02'; 
    66 
     7require Date::Range; 
    78use base qw(Date::Range); 
    89 
     
    3435    my($class, $age, $date) = @_; 
    3536 
    36     my @start = Date::Calc::Add_Delta_YMD(_ymd($date),  - $age - 1, 0, 1); 
    37     my @end   = Date::Calc::Add_Delta_YMD(_ymd($date),  - $age , 0, 0); 
     37    my @start = Date::Calc::Add_Delta_YMD(_ymd($date),  -$age - 1, 0, 1); 
     38    my @end   = Date::Calc::Add_Delta_YMD(_ymd($date),  -$age, 0, 0); 
    3839 
    3940    return Date::Simple->new(@start), Date::Simple->new(@end); 
     
    4647 
    4748    # old's start to young's end 
    48     my @start = Date::Calc::Add_Delta_YMD(_ymd($date),  - $ages[1] - 1, 0, 1); 
    49     my @end   = Date::Calc::Add_Delta_YMD(_ymd($date),  - $ages[0] , 0, 0); 
     49    my @start = Date::Calc::Add_Delta_YMD(_ymd($date),  -$ages[1] - 1, 0, 1); 
     50    my @end   = Date::Calc::Add_Delta_YMD(_ymd($date),  -$ages[0], 0, 0); 
    5051 
    5152    return Date::Simple->new(@start), Date::Simple->new(@end); 
     
    100101If the age is provided as array reference (like C<[ $young, $old ]>), 
    101102returns range of birthday for those who are between C<$young> - 
    102 C<$old> years old. 
     103C<$old> years old. It may be handy for searching teenagers, etc. 
    103104 
    104105=back 
     
    106107Other methods are inherited from Date::Range. See L<Date::Range> for 
    107108details. 
     109 
     110=head1 EXAMPLE 
     111 
     112Your customer database schema: 
     113 
     114  CREATE TABLE customer ( 
     115      name     varchar(64) NOT NULL, 
     116      birthday date NOT NULL 
     117  ); 
     118 
     119What you should do is to select name and birthday of the customers who are 
     1202X years old (between 20 and 29). 
     121 
     122  use DBI; 
     123  use Date::Range::Birth; 
     124 
     125  my $dbh = DBI->connect( ... ); 
     126  my $range = Date::Range::Birth->new([ 20, 29 ]); 
     127 
     128  my $sth = $dbh->prepare(<<'SQL') 
     129  SELECT name, birthday FROM customer WHERE birthday >= ? AND birthday <= ? 
     130  SQL 
     131 
     132  # Date::Simple overloads to 'yyyy-mm-dd'! 
     133  $sth->execute($range->start, $range->end); 
     134 
     135  while (my $data = $sth->fetchrow_arrayref) { 
     136      print "name: $data->[0] birthday: $data->[1]\n"; 
     137  } 
     138  $dbh->disconnect; 
    108139 
    109140=head1 AUTHOR 
  • Date-Range-Birth/trunk/t/01_range.t

    r365 r368  
    11use strict; 
    2 use Test::More tests => 8; 
     2use Test::More tests => 28; 
    33 
    44use Date::Simple; 
    55use Date::Range::Birth; 
    66 
    7 
    8     my $date  = Date::Simple->new(2000, 11, 11); 
    9     my $range = Date::Range::Birth->new(20, $date); 
    10     is $range->start->format("%Y %m %d"), "1979 11 12", 'start'; 
    11     is $range->end->format("%Y %m %d"), "1980 11 11", 'start'; 
     7my @tests = ( 
     8    [ 2000, 11, 11 ], 20, [ "1979 11 12", "1980 11 11" ], 
     9    [ 2001, 12, 8 ], [ 24, 25 ], [ "1975 12 09", "1977 12 08" ], 
     10    [ 2001, 12, 8 ], 50, [ "1950 12 09", "1951 12 08" ], 
     11    [ 2001, 12, 8 ], [ 50, 50 ], [ "1950 12 09", "1951 12 08" ], 
     12    [ 2001, 12, 8 ], [ 50, 60 ], [ "1940 12 09", "1951 12 08" ], 
     13    [ 2001, 12, 8 ], [ 60, 50 ], [ "1940 12 09", "1951 12 08" ], 
     14    [ 2001, 12, 31 ], 20, [ "1981 01 01", "1981 12 31" ], 
     15); 
     16 
     17while (my($date, $age, $test) = splice(@tests, 0, 3)) { 
     18    my $range = Date::Range::Birth->new($age, Date::Simple->new(@$date)); 
     19    isa_ok $range, 'Date::Range'; 
     20    isa_ok $range, 'Date::Range::Birth'; 
     21    is $range->start->format("%Y %m %d"), $test->[0], "format: $test->[0]"; 
     22    is $range->end->format("%Y %m %d"), $test->[1], "format: $test->[1]"; 
    1223} 
    1324 
    14 { 
    15     my $date  = Date::Simple->new(2001, 12, 8); 
    16     my $range = Date::Range::Birth->new([24, 25], $date); 
    17     is $range->start->format("%Y %m %d"), "1975 12 09"; 
    18     is $range->end->format("%Y %m %d"), "1977 12 08"; 
    19 } 
    20  
    21 { 
    22     my $date  = Date::Simple->new(2001, 12, 8); 
    23     my $range = Date::Range::Birth->new(50, $date); 
    24     is $range->start->format("%Y %m %d"), "1950 12 09"; 
    25     is $range->end->format("%Y %m %d"), "1951 12 08"; 
    26 } 
    27  
    28 { 
    29     my $date  = Date::Simple->new(2001, 12, 8); 
    30     my $range = Date::Range::Birth->new([50, 60], $date); 
    31     is $range->start->format("%Y %m %d"), "1940 12 09"; 
    32     is $range->end->format("%Y %m %d"), "1951 12 08"; 
    33 } 
    34