| 1 | # Copyright 2007, 2008, 2009 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 | |||
| 18 | package App::Chart::Timebase::Weeks; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Date::Calc; | ||
| 22 | use Locale::TextDomain ('App-Chart'); | ||
| 23 | use POSIX (); | ||
| 24 | |||
| 25 | use base 'App::Chart::Timebase'; | ||
| 26 | |||
| 27 | sub new_from_ymd { | ||
| 28 | my ($class, $year, $month, $day) = @_; | ||
| 29 | return $class->SUPER::new | ||
| 30 | (base => ymd_to_wdate_floor ($year, $month, $day)); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub to_ymd { | ||
| 34 | my ($self, $t) = @_; | ||
| 35 | return Date::Calc::Add_Delta_Days (1970,1,5, 7 * ($t + $self->{'base'})); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub from_ymd_floor { | ||
| 39 | my ($self, $year, $month, $day) = @_; | ||
| 40 | return ymd_to_wdate_floor ($year, $month, $day) - $self->{'base'}; | ||
| 41 | } | ||
| 42 | sub from_ymd_ceil { | ||
| 43 | my ($self, $year, $month, $day) = @_; | ||
| 44 | return ymd_to_wdate_ceil ($year, $month, $day) - $self->{'base'}; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub adjective { return __('Weekly'); } | ||
| 48 | |||
| 49 | |||
| 50 | #------------------------------------------------------------------------------ | ||
| 51 | |||
| 52 | my $CDAYS_5Jan1970 = Date::Calc::Date_to_Days (1970, 1, 5); | ||
| 53 | |||
| 54 | sub ymd_to_wdate_floor { | ||
| 55 | my ($year, $month, $day) = @_; | ||
| 56 | return POSIX::floor | ||
| 57 | ((Date::Calc::Date_to_Days ($year, $month, $day) - $CDAYS_5Jan1970) / 7); | ||
| 58 | } | ||
| 59 | sub ymd_to_wdate_ceil { | ||
| 60 | my ($year, $month, $day) = @_; | ||
| 61 | return POSIX::ceil | ||
| 62 | ((Date::Calc::Date_to_Days ($year, $month, $day) - $CDAYS_5Jan1970) / 7); | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | 1; |