| 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__ | ||
| 106 | |||
| 107 | =for stopwords timebases ie internationalizations | ||
| 108 | |||
| 109 | =head1 NAME | ||
| 110 | |||
| 111 | App::Chart::Timebase -- timebases | ||
| 112 | |||
| 113 | =head1 SYNOPSIS | ||
| 114 | |||
| 115 | use App::Chart::Timebase; | ||
| 116 | |||
| 117 | =head1 DESCRIPTION | ||
| 118 | |||
| 119 | A C<App::Chart::Timebase> object represents a date/time period and a starting | ||
| 120 | point. | ||
| 121 | |||
| 122 | Dates in a timebase are integers starting from 0 for the starting point. | ||
| 123 | For example a timebase might be weeks starting from 19 Nov 2007, in which | ||
| 124 | case that week is 0, the following week is 1, etc. Methods on the timebase | ||
| 125 | objects allow conversion of year/month/day dates to or from such an index | ||
| 126 | number. | ||
| 127 | |||
| 128 | =head1 FUNCTIONS | ||
| 129 | |||
| 130 | =over 4 | ||
| 131 | |||
| 132 | =item C<< App::Chart::Timebase::Days->new_from_iso ($start) >> | ||
| 133 | |||
| 134 | =item C<< App::Chart::Timebase::Weeks->new_from_iso ($start) >> | ||
| 135 | |||
| 136 | =item C<< App::Chart::Timebase::Months->new_from_iso ($start) >> | ||
| 137 | |||
| 138 | =item C<< App::Chart::Timebase::Quarters->new_from_iso ($start) >> | ||
| 139 | |||
| 140 | =item C<< App::Chart::Timebase::Years->new_from_iso ($start) >> | ||
| 141 | |||
| 142 | =item C<< App::Chart::Timebase::Decades->new_from_iso ($start) >> | ||
| 143 | |||
| 144 | Create and return a new timebase object representing the given | ||
| 145 | days/weeks/etc type of period, and with a 0 at the given C<$start> date. | ||
| 146 | C<$start> is an ISO format string like "2007-12-31". | ||
| 147 | |||
| 148 | Days means weekdays, ie. trading days. Weeks is calendar weeks starting | ||
| 149 | from each Monday, through to the following Sunday. Months is calendar | ||
| 150 | months. Quarters are calendar quarters like Jan/Feb/Mar then Apr/May/Jun, | ||
| 151 | etc. | ||
| 152 | |||
| 153 | =item C<< $timebase->to_iso ($t) >> | ||
| 154 | |||
| 155 | Return an ISO date string like "2007-12-31" for the given C<$t> timebase | ||
| 156 | index (an integer). For example, | ||
| 157 | |||
| 158 | my $timebase = App::Chart::Timebase::Days->new_from_iso ('2008-05-01'); | ||
| 159 | my $iso = $timebase->to_iso (5); | ||
| 160 | # $iso is '2008-05-08' (weekday 5 counting from 0 at 1 May) | ||
| 161 | |||
| 162 | =item C<< $timebase->from_ymd_floor ($year, $month, $day) >> | ||
| 163 | |||
| 164 | =item C<< $timebase->from_iso_floor ($str) >> | ||
| 165 | |||
| 166 | =item C<< $timebase->from_iso_ceil ($str) >> | ||
| 167 | |||
| 168 | Return a time value (an integer) in C<$timebase> which corresponds to the | ||
| 169 | given date, either as values C<$year>, C<$month> and C<$day>, or an ISO date | ||
| 170 | string C<$str> like "2007-12-31". | ||
| 171 | |||
| 172 | If the date is not representable in C<$timebase>, then for C<floor> the | ||
| 173 | return is the next earlier timebase value or for C<ceil> the next later. | ||
| 174 | This only arises on a C<Days> timebase when the date requested is a Saturday | ||
| 175 | or Sunday. In that case C<floor> gives the preceding Friday or C<ceil> the | ||
| 176 | following Monday. | ||
| 177 | |||
| 178 | =item C<< $timebase->convert_from_floor ($from_timebase, $from_t) >> | ||
| 179 | |||
| 180 | =item C<< $timebase->convert_from_ceil ($from_timebase, $from_t) >> | ||
| 181 | |||
| 182 | Convert an time value in C<$from_timebase> to a value in C<$timebase>. The | ||
| 183 | two timebases can have different starting points and different units, such | ||
| 184 | as converting a day number into a week number. | ||
| 185 | |||
| 186 | When the destination C<$timebase> is a higher resolution than | ||
| 187 | C<$from_timebase> the C<convert_from_floor> version gives the start of the | ||
| 188 | C<$from_t> period and the C<convert_from_ceil> version gives the end. For | ||
| 189 | example if C<$from_timebase> is years but the destination C<$timebase> is | ||
| 190 | months then C<floor> gives the first month (ie. January) in the C<$from_t> | ||
| 191 | year and C<ceil> gives the last month (ie. December). | ||
| 192 | |||
| 193 | =item $timebase->strftime ($format, $t) | ||
| 194 | |||
| 195 | Return an C<strftime> formatted string which is timebase value C<$t> (an | ||
| 196 | integer) under C<$format>. For example, | ||
| 197 | |||
| 198 | $timebase->strftime ('%d %b %Y', $t) | ||
| 199 | # gives say "31 December 2007" | ||
| 200 | |||
| 201 | =item $timebase->today () | ||
| 202 | |||
| 203 | =item $timebase->today ($timezone) | ||
| 204 | |||
| 205 | Return today's date as an integer in C<$timebase>. The optional | ||
| 206 | C<$timezone> is a C<App::Chart::TZ> object to use, or the default is | ||
| 207 | local time. | ||
| 208 | |||
| 209 | =item C<< $timebase->adjective() >> | ||
| 210 | |||
| 211 | Return a string which is an adjective for the C<$timebase>. For example on | ||
| 212 | a years timebase the return would be C<"Yearly">. The string is translated | ||
| 213 | through the usual Chart internationalizations if possible. | ||
| 214 | |||
| 215 | =back | ||
| 216 | |||
| 217 | =cut |