| 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::Quarters; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Locale::TextDomain ('App-Chart'); | ||
| 22 | use POSIX (); | ||
| 23 | |||
| 24 | use base 'App::Chart::Timebase'; | ||
| 25 | use App::Chart::Timebase::Months; | ||
| 26 | |||
| 27 | use constant MONTHS_PER_QUARTER => 3; | ||
| 28 | |||
| 29 | sub new_from_ymd { | ||
| 30 | my ($class, $year, $month, $day) = @_; | ||
| 31 | return $class->SUPER::new | ||
| 32 | (base => ymd_to_qdate ($year, $month, $day)); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub to_ymd { | ||
| 36 | my ($self, $t) = @_; | ||
| 37 | return Date::Calc::Add_Delta_YM | ||
| 38 | (1970,1,1, 0, MONTHS_PER_QUARTER * ($t + $self->{'base'})); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub from_ymd_floor { | ||
| 42 | my ($self, $year, $month, $day) = @_; | ||
| 43 | return ymd_to_qdate ($year, $month, $day) - $self->{'base'}; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub adjective { return __('Quarterly'); } | ||
| 47 | |||
| 48 | |||
| 49 | #------------------------------------------------------------------------------ | ||
| 50 | sub ymd_to_qdate { | ||
| 51 | my ($year, $month, $day) = @_; | ||
| 52 | return | ||
| 53 | POSIX::floor (App::Chart::Timebase::Months::ymd_to_mdate($year,$month,$day) | ||
| 54 | / MONTHS_PER_QUARTER); | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | 1; |