line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Date::Easy::Units; |
2
|
|
|
|
|
|
|
|
3
|
15
|
|
|
15
|
|
87
|
use strict; |
|
15
|
|
|
|
|
21
|
|
|
15
|
|
|
|
|
376
|
|
4
|
15
|
|
|
15
|
|
63
|
use warnings; |
|
15
|
|
|
|
|
22
|
|
|
15
|
|
|
|
|
367
|
|
5
|
15
|
|
|
15
|
|
62
|
use autodie; |
|
15
|
|
|
|
|
23
|
|
|
15
|
|
|
|
|
68
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.09_01'; # TRIAL VERSION |
8
|
|
|
|
|
|
|
|
9
|
15
|
|
|
15
|
|
64307
|
use Carp; |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
896
|
|
10
|
|
|
|
|
|
|
|
11
|
15
|
|
|
15
|
|
83
|
use Exporter; |
|
15
|
|
|
|
|
26
|
|
|
15
|
|
|
|
|
480
|
|
12
|
15
|
|
|
15
|
|
83
|
use parent 'Exporter'; |
|
15
|
|
|
|
|
30
|
|
|
15
|
|
|
|
|
68
|
|
13
|
|
|
|
|
|
|
my @date_units = qw< days weeks months years >; |
14
|
|
|
|
|
|
|
my @datetime_units = (qw< seconds minutes hours >, @date_units); |
15
|
|
|
|
|
|
|
our @EXPORT_OK = @datetime_units; |
16
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( date => \@date_units, datetime => \@datetime_units, all => \@EXPORT_OK ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
############################## |
20
|
|
|
|
|
|
|
# FUNCTIONS (*NOT* METHODS!) # |
21
|
|
|
|
|
|
|
############################## |
22
|
|
|
|
|
|
|
|
23
|
6
|
|
|
6
|
1
|
1618
|
sub seconds () { __PACKAGE__->new("seconds") } |
24
|
6
|
|
|
6
|
1
|
561
|
sub minutes () { __PACKAGE__->new("minutes") } |
25
|
4
|
|
|
4
|
1
|
519
|
sub hours () { __PACKAGE__->new("hours") } |
26
|
5
|
|
|
5
|
1
|
1705
|
sub days () { __PACKAGE__->new("days") } |
27
|
4
|
|
|
4
|
1
|
1061
|
sub weeks () { __PACKAGE__->new("weeks") } |
28
|
4
|
|
|
4
|
1
|
1071
|
sub months () { __PACKAGE__->new("months") } |
29
|
5
|
|
|
5
|
1
|
1089
|
sub years () { __PACKAGE__->new("years") } |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
####################### |
33
|
|
|
|
|
|
|
# REGULAR CLASS STUFF # |
34
|
|
|
|
|
|
|
####################### |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub new |
37
|
|
|
|
|
|
|
{ |
38
|
53
|
|
|
53
|
1
|
98
|
my ($class, $unit, $qty) = @_; |
39
|
53
|
100
|
|
|
|
88
|
if (defined $qty) |
40
|
|
|
|
|
|
|
{ |
41
|
19
|
100
|
|
|
|
106
|
croak("can only do integer math") unless $qty == int($qty); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else |
44
|
|
|
|
|
|
|
{ |
45
|
34
|
|
|
|
|
42
|
$qty = 1; |
46
|
|
|
|
|
|
|
} |
47
|
46
|
|
|
|
|
161
|
return bless { qty => $qty, unit => $unit }, $class; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub to_string |
52
|
|
|
|
|
|
|
{ |
53
|
14
|
|
|
14
|
1
|
20
|
my $self = shift; |
54
|
|
|
|
|
|
|
|
55
|
14
|
|
|
|
|
24
|
my $qty = $self->{qty}; |
56
|
14
|
|
|
|
|
23
|
my $unit = $self->{unit}; |
57
|
14
|
100
|
|
|
|
49
|
$unit =~ s/s$// if $qty == 1; |
58
|
14
|
|
|
|
|
141
|
return "$qty $unit"; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
######################## |
63
|
|
|
|
|
|
|
# OVERLOADED OPERATORS # |
64
|
|
|
|
|
|
|
######################## |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _add_to |
67
|
|
|
|
|
|
|
{ |
68
|
29
|
|
|
29
|
|
49
|
my ($self, $operand) = @_; |
69
|
29
|
|
|
|
|
51
|
my $unit = $self->{unit}; |
70
|
29
|
|
|
|
|
45
|
my $qty = $self->{qty}; |
71
|
29
|
|
|
|
|
41
|
my $method = "add_$unit"; |
72
|
29
|
|
|
|
|
114
|
return $operand->$method($qty); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _subtract_from |
76
|
|
|
|
|
|
|
{ |
77
|
16
|
|
|
16
|
|
26
|
my ($self, $operand) = @_; |
78
|
16
|
|
|
|
|
26
|
my $unit = $self->{unit}; |
79
|
16
|
|
|
|
|
24
|
my $qty = $self->{qty}; |
80
|
16
|
|
|
|
|
20
|
my $method = "subtract_$unit"; |
81
|
16
|
|
|
|
|
66
|
return $operand->$method($qty); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
use overload |
85
|
0
|
|
|
0
|
|
0
|
'""' => sub { shift->to_string }, |
86
|
14
|
|
|
14
|
|
4478
|
'cmp' => sub { $_[0]->to_string cmp $_[1] }, |
87
|
|
|
|
|
|
|
|
88
|
12
|
|
|
12
|
|
1262
|
'+' => sub { shift->_add_to( @_ ) }, |
89
|
12
|
50
|
|
12
|
|
1704
|
'-' => sub { $_[2] ? shift->_subtract_from( @_ ) : croak("can't subtract from a unit") }, |
90
|
19
|
|
|
19
|
|
4295
|
'*' => sub { __PACKAGE__->new( $_[0]->{unit}, $_[0]->{qty} * $_[1] ) }, |
91
|
15
|
|
|
15
|
|
9862
|
; |
|
15
|
|
|
|
|
29
|
|
|
15
|
|
|
|
|
264
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# ABSTRACT: units objects for easy date/datetime math |
100
|
|
|
|
|
|
|
# COPYRIGHT |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
__END__ |