blib/lib/DateTime/Span/Common.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
↑ Summary
1 package DateTime::Span::Common;
2  
3 our $VERSION = '0.03';
4  
5 0 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use Moose::Util::TypeConstraints;
7  
8 subtype 'DayName'
9 => as Str
10 => where {
11 /Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday/ ;
12 };
13  
14  
15 has 'week_start_day' => (isa => 'DayName', is => 'rw', default => 'Sunday') ;
16  
17 use DateTime;
18 use DateTime::Span;
19  
20 sub set_eod {
21 my($self, $dt)=@_;
22  
23 $dt->set(hour => 23, minute => 59, second => 59);
24 }
25  
26 # now, but with HH:MM::SS = 23:59:59
27 sub DateTime::nowe {
28  
29 my $now = DateTime->now;
30 set_eod(undef, $now);
31  
32 }
33  
34 sub DateTime::Span::datetimes {
35 my ($datetime_span)=@_;
36  
37 ($datetime_span->start, $datetime_span->end);
38 }
39  
40 sub DateTime::Span::from_array {
41 my ($self, $start,$end)=@_;
42  
43 #warn "$start , $end";
44  
45 DateTime::Span->from_datetimes( start => $start, end => $end ) ;
46 }
47  
48  
49 sub today {
50  
51 my $now = DateTime->nowe;
52 my $sod = DateTime->now->truncate(to => 'day');
53  
54  
55 DateTime::Span->from_datetimes( start => $sod, end => $now ) ;
56 }
57  
58 sub yesterday {
59 my $t = today;
60  
61 my @a = map { $_->subtract(days => 1) } ($t->datetimes) ;
62
63 DateTime::Span->from_array(@a);
64 }
65  
66 sub this_week {
67 my($self)=@_;
68  
69 my $now = DateTime->nowe;
70 my $sow = DateTime->now->truncate(to => 'day');
71  
72 return today if $sow->day_name eq $self->week_start_day ;
73  
74 while ($sow->day_name ne $self->week_start_day) {
75  
76 $sow->subtract(days => 1) ;
77  
78 }
79  
80 DateTime::Span->from_datetimes( start => $sow, end => $now ) ;
81 }
82  
83 sub last_week {
84 my ($self)=@_;
85  
86 my ($sow, undef) = $self->this_week->datetimes;
87
88 my $eow = $sow->clone;
89  
90 $sow->subtract(days => 7) ;
91  
92 $eow->subtract(days => 1) ;
93 $eow->set(hour => 23, minute => 59, second => 59);
94  
95  
96 DateTime::Span->from_datetimes( start => $sow, end => $eow ) ;
97 }
98  
99 sub this_month {
100  
101 my ($self)=@_;
102  
103 my ($som, $nowe) = $self->today->datetimes;
104
105 $som->set(day => 1);
106  
107 DateTime::Span->from_datetimes( start => $som, end => $nowe ) ;
108 }
109  
110 sub this_year {
111 my($self)=@_;
112  
113 my ($soy, $nowe) = $self->this_month->datetimes;
114
115 $soy->set(day => 1, month => 1);
116  
117 DateTime::Span->from_datetimes( start => $soy, end => $nowe ) ;
118 }
119  
120 sub last_year {
121 my($self)=@_;
122  
123 my ($soy, $eoy) = $self->this_year->datetimes;
124
125 $soy->subtract(years => 1);
126  
127 $eoy->subtract(years => 1);
128 $eoy->set(month => 12, day => 31);
129  
130 DateTime::Span->from_datetimes( start => $soy, end => $eoy ) ;
131 }
132  
133  
134 1; # End of DateTime::Span::Common
↑ Summary