root/DateTime-Span-Birthdate/trunk/lib/DateTime/Span/Birthdate.pm

Revision 2053 (checked in by miyagawa, 14 years ago)

this be 0.02. rename the modules

Line 
1 package DateTime::Span::Birthdate;
2
3 use strict;
4 use base qw( DateTime::Span );
5 our $VERSION = '0.02';
6
7 use Carp;
8 use DateTime;
9 use DateTime::Span;
10
11 sub new {
12     my $class = shift;
13     my %p     = @_;
14
15     $p{on} ||= DateTime->now;
16
17     my @dates;
18     if ($p{from} && $p{to}) {
19         @dates = $class->_from_array($p{from}, $p{to}, $p{on});
20     } elsif ($p{age}) {
21         @dates = $class->_from_age($p{age}, $p{on});
22     } else {
23         croak "DateTime::Span::Birthdate->new() requires from, to or age parameter";
24     }
25
26     return $class->from_datetimes( start => $dates[0], end => $dates[1] );
27 }
28
29 sub _from_array {
30     my($class, $from, $to, $dt) = @_;
31
32     my $start = $dt->clone->subtract(years => $to + 1, days => -1);
33     my $end   = $dt->clone->subtract(years => $from);
34
35     return ($start, $end);
36 }
37
38 sub _from_age {
39     my($class, $age, $dt) = @_;
40
41     my $start = $dt->clone->subtract(years => $age + 1, days => -1);
42     my $end   = $dt->clone->subtract(years => $age);
43
44     return ($start, $end);
45 }
46
47 1;
48 __END__
49
50 =for stopwords SQL datetime
51
52 =head1 NAME
53
54 DateTime::Span::Birthdate - Date span of birthdays for an age
55
56 =head1 SYNOPSIS
57
58   use DateTime::Span::Birthdate;
59
60   # birthday span for people who are 28 years old today
61   my $span = DateTime::Span::Birthdate->new(age => 28);
62
63   # birthday span for 20 years old in $dt
64   my $span = DateTime::Span::Birthdate->new(age => 28, on => $dt);
65
66   # birthday span for teenagers
67   my $span = DateTime::Span::Birthdate->new(from => 13, to => 19);
68
69 =head1 DESCRIPTION
70
71 DateTime::Span::Birthdate is a port of Date::Range::Birth module and works
72 with DateTime::Span object. This module allows you to say "who is now
73 28 years old, based on their birthday dates?"
74
75 This would be particularly useful when you build an SQL query to, say,
76 select teenagers from your customers database which has 'birthday'
77 datetime column.
78
79 =head1 AUTHOR
80
81 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
82
83 =head1 LICENSE
84
85 This library is free software; you can redistribute it and/or modify
86 it under the same terms as Perl itself.
87
88 =head1 SEE ALSO
89
90 L<DateTime::Span>, L<Date::Range::Birthdate>
91
92 =cut
Note: See TracBrowser for help on using the browser.