| 1 | # BingoX::Time | ||
| 2 | # ----------------- | ||
| 3 | # $Revision: 1.10 $ | ||
| 4 | # $Date: 2000/12/12 18:54:34 $ | ||
| 5 | # --------------------------------------------------------- | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | BingoX::Time - Time display parent class containing generic methods | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use BingoX::Time; | ||
| 14 | |||
| 15 | CONSTRUCTORS | ||
| 16 | |||
| 17 | $BR = $proto->new( [ $time ] [, $data_object ] ); | ||
| 18 | |||
| 19 | OBJECT METHODS | ||
| 20 | |||
| 21 | $date->month_full; | ||
| 22 | $date->month_full2abrv; | ||
| 23 | |||
| 24 | $date->months_list; | ||
| 25 | $date->months_full_list; | ||
| 26 | |||
| 27 | $date->months; | ||
| 28 | $date->months_pad; | ||
| 29 | $date->months_full; | ||
| 30 | $date->months_full_pad; | ||
| 31 | $date->month_abrv; | ||
| 32 | |||
| 33 | $date->int_months; | ||
| 34 | $date->int_months_full; | ||
| 35 | |||
| 36 | $date->last_day; | ||
| 37 | $date->last_days; | ||
| 38 | |||
| 39 | $date->is_leap_year; | ||
| 40 | |||
| 41 | $date->hours24; | ||
| 42 | $date->hours; | ||
| 43 | $date->minutes; | ||
| 44 | |||
| 45 | =head1 REQUIRES | ||
| 46 | |||
| 47 | strict | ||
| 48 | Date::Parse | ||
| 49 | Date::Language | ||
| 50 | Date::Language::English | ||
| 51 | Time::Object | ||
| 52 | |||
| 53 | =head1 EXPORTS | ||
| 54 | |||
| 55 | localtime, gmtime | ||
| 56 | |||
| 57 | =head1 DESCRIPTION | ||
| 58 | |||
| 59 | Time provides an OO interface to Time/Dates, ala Time::Object. | ||
| 60 | |||
| 61 | =head1 METHODS | ||
| 62 | |||
| 63 | =over 4 | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | package BingoX::Time; | ||
| 68 | |||
| 69 | use vars qw(@ISA @EXPORT $debug); | ||
| 70 | use strict; | ||
| 71 | |||
| 72 | use Date::Parse; | ||
| 73 | use Date::Language; | ||
| 74 | use Date::Language::English; | ||
| 75 | |||
| 76 | |||
| 77 | BEGIN { | ||
| 78 | $BingoX::Time::REVISION = (qw$Revision: 1.10 $)[-1]; | ||
| 79 | $BingoX::Time::VERSION = '1.92'; | ||
| 80 | |||
| 81 | @ISA = qw(Time::Object); | ||
| 82 | @EXPORT = qw(localtime gmtime); | ||
| 83 | $debug = undef; | ||
| 84 | |||
| 85 | if ($debug) { | ||
| 86 | require Data::Dumper; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | use Time::Object qw(); | ||
| 91 | |||
| 92 | |||
| 93 | =item C<new> ( [ $time ] [, $islocal ] ) | ||
| 94 | |||
| 95 | This is a Time::Object overloaded constructor that can take as values | ||
| 96 | the time in seconds, or defaults to 'time'. It can also take the $islocal | ||
| 97 | flag, which tells _mktime() to use localtime (1) or gmtime (0). It always | ||
| 98 | returns a BingoX Time Object. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | sub new { | ||
| 102 | my $proto = shift; | ||
| 103 | my $class = ref($proto) || $proto; | ||
| 104 | my $time = shift; | ||
| 105 | my $islocal = shift; | ||
| 106 | |||
| 107 | my $self; | ||
| 108 | |||
| 109 | if (defined $time) { | ||
| 110 | $self = _mktime( $time, (defined $islocal ? $islocal : 1), $class ); | ||
| 111 | } elsif (ref($proto) && $proto->isa('Time::Object')) { | ||
| 112 | $self = _mktime($proto->[9], $proto->[10], $class); | ||
| 113 | } else { | ||
| 114 | $self = _mktime( time, (defined $islocal ? $islocal : 1), $class ); | ||
| 115 | } | ||
| 116 | |||
| 117 | return bless $self, $class; | ||
| 118 | } # END sub new | ||
| 119 | |||
| 120 | |||
| 121 | =begin comment | ||
| 122 | |||
| 123 | _mktime( ) | ||
| 124 | |||
| 125 | This is a Time::Object overloaded constructor that take as values | ||
| 126 | the time in seconds, and the $islocal flag, which tells it to use | ||
| 127 | localtime (1) or gmtime (0). It returns a BingoX Time Object | ||
| 128 | or Time::Object depending on whether it was called from a sub class. | ||
| 129 | If it was called as a 'wantarray' it will return the contents returned | ||
| 130 | localtime, or gmtime depending on the $islocal flag. | ||
| 131 | |||
| 132 | =end comment | ||
| 133 | |||
| 134 | =cut | ||
| 135 | sub _mktime { | ||
| 136 | my ($time, $islocal, $class) = @_; | ||
| 137 | my @time = $islocal | ||
| 138 | ? CORE::localtime($time) | ||
| 139 | : CORE::gmtime($time); | ||
| 140 | wantarray ? @time : bless [@time, $time, $islocal], ($class->isa('Time::Object') | ||
| 141 | ? $class | ||
| 142 | : 'Time::Object'); | ||
| 143 | } # END sub _mktime | ||
| 144 | |||
| 145 | |||
| 146 | =item C<str2time> ( $string ) | ||
| 147 | |||
| 148 | This is method is used to parse your default date format into a format | ||
| 149 | that str2time understands, such as: | ||
| 150 | |||
| 151 | Date: 961221 (yymmdd) | ||
| 152 | Date: 12-21-96 (mm-dd-yy) ( '-', '.' or '/' ) | ||
| 153 | Date: 12-June-96 (dd-month-yy) ( '-', '.' or '/' ) | ||
| 154 | Date: June 12 96 00:00PM (month dd yy hh:mmPM) | ||
| 155 | Date: June 12 96 00::00:00 (month dd yy hh:mm::ss) | ||
| 156 | |||
| 157 | If time is not passed then time defaults to 00:00:00. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | sub str2time { | ||
| 161 | my $self = shift; | ||
| 162 | my $string = shift; | ||
| 163 | |||
| 164 | Date::Parse::str2time( $string ); | ||
| 165 | } # END sub str2time | ||
| 166 | |||
| 167 | |||
| 168 | =item C<time2str> ( $format ) | ||
| 169 | |||
| 170 | For compatibility for the older DateTime::Date modules. Passes the | ||
| 171 | $format to strftime. | ||
| 172 | |||
| 173 | =cut | ||
| 174 | sub time2str { | ||
| 175 | my $self = shift; | ||
| 176 | my $f = shift; | ||
| 177 | $self->strftime( $f ); | ||
| 178 | } # END of time2str | ||
| 179 | |||
| 180 | |||
| 181 | =item C<time_local> ( ) | ||
| 182 | |||
| 183 | For compatibility for the older DateTime::Date modules. Returns $self->epoch. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | sub time_local { $_[0]->epoch } | ||
| 187 | |||
| 188 | |||
| 189 | =item C<month_full> ( ) | ||
| 190 | |||
| 191 | Returns the full month string based on the integer month. | ||
| 192 | |||
| 193 | =item C<month_full2abrv> ( ) | ||
| 194 | |||
| 195 | Returns the abbreviated (3 char) month string based on the full month name. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | sub month_full { return $_[0]->months_full_list->[ $_[0]->_mon ] } | ||
| 199 | sub month_full2abrv { return $_[0]->month_abrv->{ $_[0]->month_full } } | ||
| 200 | |||
| 201 | |||
| 202 | =item C<months_list> ( ) | ||
| 203 | |||
| 204 | Returns an array ref of abbreviated months. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | sub months_list { | ||
| 208 | return [ @Date::Language::English::MoYs ]; | ||
| 209 | } # END of months_list | ||
| 210 | |||
| 211 | |||
| 212 | =item C<months_full_list> ( ) | ||
| 213 | |||
| 214 | Returns an array ref of months. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | sub months_full_list { | ||
| 218 | return [ @Date::Language::English::MoY ]; | ||
| 219 | } # END of months_full_list | ||
| 220 | |||
| 221 | |||
| 222 | =item C<months> ( ) | ||
| 223 | |||
| 224 | Returns a hash ref of abbreviated months. With the keys being the true month | ||
| 225 | value (1 .. 12). | ||
| 226 | |||
| 227 | =item C<months_full> ( ) | ||
| 228 | |||
| 229 | Returns a hash ref of full month names. With the keys being the true month | ||
| 230 | value (1 .. 12). | ||
| 231 | |||
| 232 | =item C<month_abrv> ( ) | ||
| 233 | |||
| 234 | Returns a hash ref of abbreviated months. With the keys being the full month name. | ||
| 235 | |||
| 236 | =cut | ||
| 237 | sub months { return { map { $_ => $_[0]->months_list->[$_ - 1] } (1 .. @{ $_[0]->months_list }) } } | ||
| 238 | sub months_pad { return { map { sprintf("%02d", $_) => $_[0]->months_list->[$_ - 1] } (1 .. @{ $_[0]->months_list }) } } | ||
| 239 | sub months_full { return { map { $_ => $_[0]->months_full_list->[$_ - 1] } (1 .. @{ $_[0]->months_full_list }) } } | ||
| 240 | sub months_full_pad { return { map { sprintf("%02d", $_) => $_[0]->months_full_list->[$_ - 1] } (1 .. @{ $_[0]->months_full_list }) } } | ||
| 241 | sub month_abrv { return { map { $_[0]->months_full_list->[$_] => $_[0]->months_list->[$_] } (0 .. 11) } } | ||
| 242 | |||
| 243 | |||
| 244 | =item C<int_months> ( ) | ||
| 245 | |||
| 246 | Returns a hash ref of abbreviated months. With the keys being the integer month | ||
| 247 | value (0 .. 12). | ||
| 248 | |||
| 249 | =item C<int_months_full> ( ) | ||
| 250 | |||
| 251 | Returns a hash ref of full month names. With the keys being the integer month | ||
| 252 | value (0 .. 12). | ||
| 253 | |||
| 254 | =cut | ||
| 255 | sub int_months { return { map { $_ => $_[0]->months_list->[$_] } (0 .. $#{ $_[0]->months_list } ) } } | ||
| 256 | sub int_months_full { return { map { $_ => $_[0]->months_full_list->[$_] } (0 .. $#{ $_[0]->months_full_list } ) } } | ||
| 257 | |||
| 258 | |||
| 259 | =item C<last_day> ( [ $mon ] [, $year ] ) | ||
| 260 | |||
| 261 | Returns the last day of the of the month and year passed. If nothing is passed | ||
| 262 | then it uses the objects month and year. Month is the integer month 0 .. 11, and | ||
| 263 | year is the 4 digit year. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | sub last_day { | ||
| 267 | my $self = shift; | ||
| 268 | my $mon = shift; | ||
| 269 | my $year = shift; | ||
| 270 | |||
| 271 | $mon = $self->mon unless (defined $mon); | ||
| 272 | $year = $self->year unless (defined $year); | ||
| 273 | |||
| 274 | return $self->last_days->{ $mon } unless ($mon == 1); | ||
| 275 | return 28 unless $self->is_leap_year( $year ); | ||
| 276 | return 29; | ||
| 277 | } # END of last_day | ||
| 278 | |||
| 279 | |||
| 280 | =item C<last_days> ( ) | ||
| 281 | |||
| 282 | Returns a hash ref of the last day for each month. The keys are | ||
| 283 | the integer months (0 .. 11). | ||
| 284 | |||
| 285 | =cut | ||
| 286 | sub last_days { return { 0 => '31', 1 => '28', 2 => '31', 3 => '30', 4 => '31', 5 => '30', 6 => '31', 7 => '31', 8 => '30', 9 => '31', 10 => '30', 11 => '31' } } | ||
| 287 | |||
| 288 | |||
| 289 | =item C<is_leap_year> ( ) | ||
| 290 | |||
| 291 | Returns true if the year is a leap year. | ||
| 292 | |||
| 293 | =cut | ||
| 294 | sub is_leap_year { | ||
| 295 | my $self = shift; | ||
| 296 | my $year = shift || $self->year; | ||
| 297 | |||
| 298 | (($year % 4 == 0) && (($year % 100 != 0) || ($year % 400 == 0))) ? 1 : 0; | ||
| 299 | } # END of is_leap_year | ||
| 300 | |||
| 301 | |||
| 302 | =item C<hours24> ( ) | ||
| 303 | |||
| 304 | Returns an array ref of hours in 24hr time. | ||
| 305 | |||
| 306 | =item C<hours> ( ) | ||
| 307 | |||
| 308 | Returns an array ref of hours in 12hr time. Padded with zeros. | ||
| 309 | |||
| 310 | =item C<minutes> ( ) | ||
| 311 | |||
| 312 | Returns an array ref of minutes (0 .. 60). Padded with zeros. | ||
| 313 | |||
| 314 | =cut | ||
| 315 | sub hours24 { return [ '00','01','02','03','04','05','06','07','08','09', (10 .. 23) ] } | ||
| 316 | sub hours { return [ '01','02','03','04','05','06','07','08','09','10','11','12' ] } | ||
| 317 | sub minutes { return [ '00','01','02','03','04','05','06','07','08','09', (10 .. 59) ] } | ||
| 318 | |||
| 319 | |||
| 320 | # This is where we muck in someone else's namespace. Not for the faint of heart mind you! | ||
| 321 | package Time::Object; | ||
| 322 | |||
| 323 | use overload 'fallback' => 1; | ||
| 324 | |||
| 325 | 1; | ||
| 326 | |||
| 327 | __END__ |