App/Chart/Timebase.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran n/a Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 102 CC 1 SCAR 6.9
CC
1
Cov
0%
new
2.0
new_from_iso
2.0
new_from_timebase
2.0
CRAP 2.0
1 # Copyright 2007, 2008, 2009, 2010 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17 package App::Chart::Timebase;
18 use 5.004;
19 use strict;
20 use warnings;
21 use Date::Calc;
22 use POSIX (); # no exports avoids clash with new strftime below
23 use POSIX::Wide;
24 # use Locale::TextDomain ('App-Chart');
25  
26 use App::Chart;
27 use App::Chart::TZ;
28  
29 sub new {
30 my $class = shift;
31 return bless { @_ }, $class;
32 }
33  
34 sub new_from_iso {
35 my ($class, $iso) = @_;
36 my ($year, $month, $day) = App::Chart::iso_to_ymd ($iso);
37 return $class->new_from_ymd ($year, $month, $day);
38 }
39  
40 sub new_from_timebase {
41 my ($class, $timebase) = @_;
42 my ($year, $month, $day) = $timebase->to_ymd (0);
43 return $class->new_from_ymd ($year, $month, $day);
44 }
45  
46 sub to_iso {
47 my ($self, $t) = @_;
48 my ($year, $month, $day) = $self->to_ymd ($t);
49 return App::Chart::ymd_to_iso ($year, $month, $day);
50 }
51  
52 sub from_iso_floor {
53 my ($self, $str) = @_;
54 my ($year, $month, $day) = App::Chart::iso_to_ymd ($str);
55 return $self->from_ymd_floor ($year, $month, $day);
56 }
57  
58 sub from_iso_ceil {
59 my ($self, $str) = @_;
60 my ($year, $month, $day) = App::Chart::iso_to_ymd ($str);
61 return $self->from_ymd_ceil ($year, $month, $day);
62 }
63  
64 sub convert_from_floor {
65 my ($self, $from_timebase, $from_t) = @_;
66 my ($year, $month, $day) = $from_timebase->to_ymd ($from_t);
67 return $self->from_ymd_floor ($year, $month, $day);
68 }
69  
70 sub convert_from_ceil {
71 my ($self, $from_timebase, $from_t) = @_;
72 return $self->convert_from_floor ($from_timebase, $from_t + 1) - 1;
73 }
74  
75 sub strftime {
76 my ($self, $format, $date) = @_;
77 return strftime_ymd ($format, $self->to_ymd ($date));
78 }
79  
80 sub strftime_ymd {
81 my ($format, $year, $month, $day) = @_;
82 return POSIX::Wide::strftime
83 ($format, 0,0,0,
84 $day, $month-1, $year-1900,
85 Date::Calc::Day_of_Week ($year,$month,$day) % 7,
86 Date::Calc::Day_of_Year ($year,$month,$day),
87 -1); # isdst
88 }
89  
90 sub today {
91 my ($self, $timezone) = @_;
92 $timezone ||= App::Chart::TZ->loco;
93 my ($year, $month, $day) = $timezone->ymd;
94 return $self->from_ymd_floor ($year, $month, $day);
95 }
96  
97 sub today_ceil {
98 my ($self, $timezone) = @_;
99 $timezone ||= App::Chart::TZ->loco;
100 my ($year, $month, $day) = $timezone->ymd;
101 return $self->from_ymd_ceil ($year, $month, $day);
102 }
103  
104 1;
105 __END__