root/lang/perl/autobox-DateTime-Duration/trunk/lib/autobox/DateTime/Duration.pm @ 4663

Revision 4663, 2.0 kB (checked in by miyagawa, 6 years ago)

Checking in changes prior to tagging of version 0.02. Changelog diff is:

=== Changes
==================================================================
--- Changes (revision 9568)
+++ Changes (local)
@@ -1,4 +1,7 @@

Revision history for Perl extension autobox::DateTime::Duration


+0.02 Tue Jan 15 17:53:53 PST 2008
+ - Add dependencies
+

0.01 Tue Jan 15 17:39:40 2008

  • original version
Line 
1package autobox::DateTime::Duration;
2
3use strict;
4use 5.8.1;
5our $VERSION = '0.02';
6
7use autobox;
8use DateTime;
9use DateTime::Duration;
10
11for my $accessor (qw( day hour minute month second week year )) {
12    no strict 'refs';
13    my $plural = $accessor . "s";
14    *{"SCALAR::$accessor"} = *{"SCALAR::$plural"} = sub {
15        DateTime::Duration->new($plural => $_[0]);
16    };
17}
18
19sub SCALAR::fortnight {
20    DateTime::Duration->new(weeks => 2 * $_[0]);
21}
22
23*SCALAR::fortnights = \&SCALAR::fortnight;
24
25sub DateTime::Duration::ago {
26    my $duration = shift;
27    my $dt = $_[0] ? $_[0]->clone : DateTime->now;
28    $dt->subtract_duration($duration);
29}
30
31*DateTime::Duration::until = \&DateTime::Duration::ago;
32
33sub DateTime::Duration::from_now {
34    my $duration = shift;
35    my $dt = $_[0] ? $_[0]->clone : DateTime->now;
36    $dt->add_duration($duration);
37}
38
39*DateTime::Duration::since = \&DateTime::Duration::from_now;
40
411;
42__END__
43
44=for stopwords DateTime ActiveSupport autobox
45
46=head1 NAME
47
48autobox::DateTime::Duration - ActiveSupport equivalent to Perl numeric variables
49
50=head1 SYNOPSIS
51
52  use autobox;
53  use autobox::DateTime::Duration;
54
55  # equivalent to DateTime::Duration->new(months => 1, days => 5);
56  $duration = 1->month + 2->days;
57
58  # equivalent to DateTime->now->add(years => 2);
59  $datetime = 2->years->from_now;
60
61  # equivalent to DateTime->now->add(months => 4, years => 5);
62  $datetime = (4->months + 5->years)->from_now;
63
64  # equivalent to DateTime->now->subtract(days => 3);
65  $datetime = 3->days->ago;
66
67=head1 DESCRIPTION
68
69autobox::DateTime::Duration is an autobox module to add Time-related
70methods to core integer values by using constant overloading. Inspired
71by ActiveSupport (Rails) Core extensions to Numeric values.
72
73=head1 AUTHOR
74
75Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
76
77=head1 LICENSE
78
79This library is free software; you can redistribute it and/or modify
80it under the same terms as Perl itself.
81
82=head1 SEE ALSO
83
84L<http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Numeric/Time.html>
85
86L<DateTime::Duration>, L<bigint>, L<overload>
87
88=cut
Note: See TracBrowser for help on using the browser.