| 1 | package Badger::Period; | ||
| 2 | |||
| 3 | use Badger::Class | ||
| 4 | version => 0.01, | ||
| 5 | debug => 0, | ||
| 6 | base => 'Badger::Comparable', | ||
| 7 | utils => 'numlike is_object', | ||
| 8 | accessors => 'uri', | ||
| 9 | as_text => 'uri', | ||
| 10 | is_true => 1, | ||
| 11 | constants => 'HASH ARRAY DELIMITER', | ||
| 12 | constant => { | ||
| 13 | FIELD_NAMES => undef, | ||
| 14 | TYPE_NAME => 'period', | ||
| 15 | }, | ||
| 16 | messages => { | ||
| 17 | bad_type => 'Invalid %s: %s', | ||
| 18 | bad_duration => 'Invalid duration: %s', | ||
| 19 | }; | ||
| 20 | |||
| 21 | use Time::Local; | ||
| 22 | use POSIX 'strftime'; | ||
| 23 | |||
| 24 | our @YMD = qw( year month day ); | ||
| 25 | our @HMS = qw( hour minute second ); | ||
| 26 | our @SMHD = qw( second minute hour day ); | ||
| 27 | our @YMDHMS = (@YMD, @HMS); | ||
| 28 | our $SECONDS = { | ||
| 29 | s => 1, | ||
| 30 | m => 60, | ||
| 31 | h => 60*60, | ||
| 32 | d => 60*60*24, | ||
| 33 | M => 60*60*24*30, | ||
| 34 | y => 60*60*24*365, | ||
| 35 | }; | ||
| 36 | |||
| 37 | |||
| 38 | sub split_regex { | ||
| 39 | shift->not_implemented; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | sub join_format { | ||
| 44 | shift->not_implemented; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub text_format { | ||
| 49 | shift->join_format; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub field_names { | ||
| 54 | my $class = shift; | ||
| 55 | my $names = $class->FIELD_NAMES | ||
| 56 | || return $class->not_implemented; | ||
| 57 | |||
| 58 | $names = [ split(DELIMITER, $names) ] | ||
| 59 | unless ref $names eq ARRAY; | ||
| 60 | |||
| 61 | return wantarray | ||
| 62 | ? @$names | ||
| 63 | : \@$names | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | #----------------------------------------------------------------------- | ||
| 68 | # Methods | ||
| 69 | #----------------------------------------------------------------------- | ||
| 70 | |||
| 71 | sub new { | ||
| 72 | my $class = shift; | ||
| 73 | my @fields = $class->field_names; | ||
| 74 | my $self = bless { map { ($_, 0) } @fields }, ref $class || $class; | ||
| 75 | my ($config, $time); | ||
| 76 | |||
| 77 | if (@_ > 1) { | ||
| 78 | # multiple arguments are named params | ||
| 79 | $config = { @_ }; | ||
| 80 | } | ||
| 81 | elsif (@_ == 1 && defined $_[0]) { | ||
| 82 | # single argument is a hash of named params, a timestamp or time in | ||
| 83 | # seconds since the epoch | ||
| 84 | $config = ref $_[0] eq HASH ? shift : { time => shift }; | ||
| 85 | } | ||
| 86 | # otherwise we default to now | ||
| 87 | else { | ||
| 88 | $config = { time => time() }; | ||
| 89 | } | ||
| 90 | |||
| 91 | if ($time = $config->{ time }) { | ||
| 92 | if (numlike $time) { | ||
| 93 | # $time is seconds since epoch | ||
| 94 | (@$config{ @YMDHMS }) = reverse( ( localtime($time) )[0..5] ); | ||
| 95 | $config->{ year }+= 1900; | ||
| 96 | $config->{ month }++; | ||
| 97 | $config->{ etime } = $time; | ||
| 98 | } | ||
| 99 | elsif (is_object(ref $class || $class, $time)) { | ||
| 100 | $config->{ uri } = $time->uri; | ||
| 101 | $config->{ etime } = $time->epoch_time; | ||
| 102 | $self->split_uri($config->{ uri }, $config); | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | # $time is a timestamp so split and rejoin into canonical form | ||
| 106 | $config->{ uri } = $time; | ||
| 107 | $self->split_uri($config); | ||
| 108 | } | ||
| 109 | $self->join_uri($config); | ||
| 110 | } | ||
| 111 | |||
| 112 | # set any fields defined in config, allowing singular (second,month, | ||
| 113 | # etc) and plural (seconds, months, etc) | ||
| 114 | foreach my $field (@fields) { | ||
| 115 | $self->{ $field } = $config->{ $field } || $config->{"${field}s"} || 0; | ||
| 116 | } | ||
| 117 | |||
| 118 | $self->join_uri; | ||
| 119 | |||
| 120 | return $self; | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | sub copy { | ||
| 125 | my $self = shift; | ||
| 126 | $self->new( $self->{ uri } ); | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | sub split_uri { | ||
| 131 | my $self = shift; | ||
| 132 | my $target = shift || $self; | ||
| 133 | my $regex = $self->split_regex; | ||
| 134 | my @fields = $self->field_names; | ||
| 135 | |||
| 136 | $target->{ uri } = '' unless defined $target->{ uri }; | ||
| 137 | |||
| 138 | (@$target{ @fields } = map { 0+$_ } $target->{ uri } =~ m/$regex/o) | ||
| 139 | || return $self->error_msg( bad_type => $self->TYPE_NAME, $target->{ uri } ); | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | sub join_uri { | ||
| 144 | my $self = shift; | ||
| 145 | my $target = shift || $self; | ||
| 146 | my @fields = $self->field_names; | ||
| 147 | |||
| 148 | return ($target->{ uri } = sprintf( | ||
| 149 | $self->join_format, | ||
| 150 | map { defined $_ ? $_ : 0 } | ||
| 151 | @$target{ @fields } | ||
| 152 | )); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | sub epoch_time { | ||
| 157 | my $self = shift; | ||
| 158 | |||
| 159 | return $self->{ etime } ||= timelocal( | ||
| 160 | $self->posix_args | ||
| 161 | ); | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | sub posix_args { | ||
| 166 | my $self = shift; | ||
| 167 | return ( | ||
| 168 | ( | ||
| 169 | map { $self->{ $_ } || 0 } | ||
| 170 | @SMHD | ||
| 171 | ), | ||
| 172 | ($self->{ month } || 1) - 1, | ||
| 173 | ($self->{ year } || 1900) - 1900 | ||
| 174 | ); | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | sub format { | ||
| 179 | my $self = shift; | ||
| 180 | my $fmt = shift; | ||
| 181 | return strftime( | ||
| 182 | $fmt, | ||
| 183 | $self->posix_args | ||
| 184 | ); | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | sub text { | ||
| 189 | my $self = shift; | ||
| 190 | my @fields = $self->field_names; | ||
| 191 | return $self->{ text } | ||
| 192 | ||= sprintf( $self->text_format, @$self{ @fields } ); | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | sub duration { | ||
| 198 | my ($self, $duration) = @_; | ||
| 199 | |||
| 200 | # $duration can be a number, assumed to be seconds | ||
| 201 | return $duration | ||
| 202 | if numlike($duration); | ||
| 203 | |||
| 204 | # Otherwise the $duration should be of the form "3 minutes". We only | ||
| 205 | # look at the first character of the word (e.g. "3 m"), which creates a | ||
| 206 | # potential conflict between "m(inute) and m(onth)". So we use a capital | ||
| 207 | # 'M' for month. This is based on code by Mark Fisher in CGI.pm. | ||
| 208 | |||
| 209 | $duration =~ s/month/Month/i; | ||
| 210 | |||
| 211 | # TODO: make this parser a bit smarter so we can support multiple | ||
| 212 | # items (e.g. "2 hours 30 minutes") as per adjust() | ||
| 213 | if ($duration =~ /^ ( -? (?: \d+ | \d*\.\d+ ) ) \s* ([smhdMy]?) /x) { | ||
| 214 | return ($SECONDS->{ $2 } || 1) * $1; | ||
| 215 | } | ||
| 216 | else { | ||
| 217 | return $self->error_msg( bad_duration => $duration ); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | sub compare { | ||
| 223 | my $self = shift; | ||
| 224 | my @fields = $self->field_names; | ||
| 225 | |||
| 226 | # optimisation: if the $self object has an epoch time and a single | ||
| 227 | # numerical argument is passed (also an epoch time) then we can do a | ||
| 228 | # simple comparison | ||
| 229 | return $self->{ etime } <=> $_[0] | ||
| 230 | if $self->{ etime } | ||
| 231 | && @_ == 1 | ||
| 232 | && numlike $_[0]; | ||
| 233 | |||
| 234 | # otherwise we upgrade any argument(s) to another timestamp and comare | ||
| 235 | # them piecewise | ||
| 236 | my $comp = @_ && is_object(ref $self || $self, $_[0]) | ||
| 237 | ? shift | ||
| 238 | : $self->new(@_); | ||
| 239 | |||
| 240 | foreach my $item (@fields) { | ||
| 241 | if ($self->{ $item } < $comp->{ $item }) { | ||
| 242 | return -1; # -1 - self earlier than comparison timestamp | ||
| 243 | } | ||
| 244 | elsif ($self->{ $item } > $comp->{ $item }) { | ||
| 245 | return 1; # 1 - self later than comparison timestamp | ||
| 246 | } | ||
| 247 | } | ||
| 248 | return 0; # 0 - same time | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | sub days_in_month { | ||
| 253 | my $self = shift; | ||
| 254 | my $month = shift || $self->{ month }; | ||
| 255 | |||
| 256 | if ($month == 4 || $month == 6 || $month == 9 || $month == 11) { | ||
| 257 | return 30; | ||
| 258 | } | ||
| 259 | elsif ($month == 2) { | ||
| 260 | return $self->leap_year(@_) ? 29 : 28; | ||
| 261 | } | ||
| 262 | else { | ||
| 263 | return 31; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | sub leap_year { | ||
| 269 | my $self = shift; | ||
| 270 | my $year = shift || $self->{ year }; | ||
| 271 | |||
| 272 | if ($year % 4) { | ||
| 273 | return 0; | ||
| 274 | } | ||
| 275 | elsif ($year % 400 == 0) { | ||
| 276 | return 1; | ||
| 277 | } | ||
| 278 | elsif ($year % 100 == 0) { | ||
| 279 | return 0; | ||
| 280 | } | ||
| 281 | else { | ||
| 282 | return 1; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | |||
| 287 | 1; | ||
| 288 | __END__ |