| 1 | package Date::Manip::Obj; |
| 2 | # Copyright (c) 2008-2026 Sullivan Beck. All rights reserved. |
| 3 | # This program is free software; you can redistribute it and/or modify it |
| 4 | # under the same terms as Perl itself. |
| 5 | |
| 6 | ######################################################################## |
| 7 | ######################################################################## |
| 8 | |
| 9 | require 5.010000; |
| 10 | use warnings; |
| 11 | use strict; |
| 12 | use IO::File; |
| 13 | use Storable qw(dclone); |
| 14 | use Carp; |
| 15 | |
| 16 | our ($VERSION); |
| 17 | $VERSION='6.99'; |
| 18 | END { undef $VERSION; } |
| 19 | |
| 20 | ######################################################################## |
| 21 | # METHODS |
| 22 | ######################################################################## |
| 23 | |
| 24 | my %classes = ( 'Date::Manip::Base' => 1, |
| 25 | 'Date::Manip::TZ' => 1, |
| 26 | 'Date::Manip::Date' => 1, |
| 27 | 'Date::Manip::Delta' => 1, |
| 28 | 'Date::Manip::Recur' => 1, |
| 29 | ); |
| 30 | |
| 31 | sub new { |
| 32 | my(@args) = @_; |
| 33 | my(@allargs) = @args; |
| 34 | |
| 35 | # $old is the object (if any) being used to create a new object |
| 36 | # $new is the new object |
| 37 | # $class is the class of the new object |
| 38 | # $tz is a Date::Manip::TZ object to base the new object on |
| 39 | # (only for Date, Delta, Recur objects) |
| 40 | # $base is the Date::Manip::Base object to base the new object on |
| 41 | # @opts options to pass to config method |
| 42 | |
| 43 | my($old,$new,$class,$tz,$base,@opts); |
| 44 | |
| 45 | # Get the class of the new object |
| 46 | |
| 47 | if (exists $classes{ $args[0] }) { |
| 48 | |
| 49 | # $obj = new CLASS |
| 50 | $class = shift(@args); |
| 51 | |
| 52 | } else { |
| 53 | |
| 54 | # $obj->new |
| 55 | $class = ref($args[0]); |
| 56 | } |
| 57 | |
| 58 | # Find out if there are any config options (which will be the |
| 59 | # final argument). |
| 60 | |
| 61 | if (@args && ref($args[$#args]) eq 'ARRAY') { |
| 62 | @opts = @{ pop(@args) }; |
| 63 | } |
| 64 | |
| 65 | # Get an old object |
| 66 | |
| 67 | if (ref($args[0]) =~ /^Date::Manip/) { |
| 68 | # $old->new |
| 69 | # new CLASS $old |
| 70 | $old = shift(@args); |
| 71 | } |
| 72 | |
| 73 | # Additional arguments will be passed to parse. |
| 74 | |
| 75 | ######################## |
| 76 | |
| 77 | # Get Base/TZ objects from an existing object |
| 78 | |
| 79 | if ($old) { |
| 80 | if (ref($old) eq 'Date::Manip::Base') { |
| 81 | $base = $old; |
| 82 | } elsif (ref($old) eq 'Date::Manip::TZ') { |
| 83 | $tz = $old; |
| 84 | $base = $$tz{'base'}; |
| 85 | |
| 86 | # *** I think this is useless code, deprecate |
| 87 | # } elsif (ref($old) eq 'ARRAY') { |
| 88 | # my %old = @$old; |
| 89 | # $tz = $old{'tz'}; |
| 90 | # $base = $$tz{'base'}; |
| 91 | |
| 92 | } else { |
| 93 | $tz = $$old{'tz'}; |
| 94 | $base = $$tz{'base'}; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | # Create a new empty object. |
| 99 | |
| 100 | $new = { |
| 101 | 'data' => {}, |
| 102 | 'err' => '', |
| 103 | }; |
| 104 | |
| 105 | # Create Base/TZ objects if necessary |
| 106 | |
| 107 | if ($base && @opts) { |
| 108 | $base = _clone($base); |
| 109 | $tz = new Date::Manip::TZ $base if ($tz); |
| 110 | } |
| 111 | |
| 112 | my $init = 1; |
| 113 | if ($class eq 'Date::Manip::Base') { |
| 114 | if ($base) { |
| 115 | # new Date::Manip::Base $base |
| 116 | # |
| 117 | # We have to clone it (which we already did if @opts was given) |
| 118 | # |
| 119 | if (@opts) { |
| 120 | $new = $base; |
| 121 | } else { |
| 122 | $new = _clone($base); |
| 123 | } |
| 124 | $init = 0; |
| 125 | } |
| 126 | |
| 127 | } elsif ($class eq 'Date::Manip::TZ') { |
| 128 | if ($tz) { |
| 129 | # new Date::Manip::TZ $tz |
| 130 | if (@opts) { |
| 131 | $new = $tz; |
| 132 | } else { |
| 133 | $new = _clone($tz); |
| 134 | } |
| 135 | $init = 0; |
| 136 | } elsif (! $base) { |
| 137 | $base = new Date::Manip::Base; |
| 138 | } |
| 139 | $$new{'base'} = $base; |
| 140 | |
| 141 | } else { |
| 142 | if (! $tz) { |
| 143 | if ($base) { |
| 144 | $tz = new Date::Manip::TZ $base; |
| 145 | } else { |
| 146 | $tz = new Date::Manip::TZ; |
| 147 | } |
| 148 | } |
| 149 | $$new{'tz'} = $tz; |
| 150 | } |
| 151 | |
| 152 | $$new{'args'} = [ @args ]; |
| 153 | bless $new,$class; |
| 154 | |
| 155 | $new->_init() if ($init); |
| 156 | $new->config(@opts) if (@opts); |
| 157 | $new->_init_args() if (@args); |
| 158 | $new->_init_final(); |
| 159 | return $new; |
| 160 | } |
| 161 | |
| 162 | # This clones an object. Currently, it only clones a Base or TZ |
| 163 | # object, but dclone can't handle stored regexps so we have to copy |
| 164 | # them manually. |
| 165 | # |
| 166 | sub _clone { |
| 167 | my($obj) = @_; |
| 168 | |
| 169 | if (ref($obj) eq 'Date::Manip::Base') { |
| 170 | |
| 171 | my $tmp = $$obj{'data'}{'rx'}; |
| 172 | delete $$obj{'data'}{'rx'}; |
| 173 | my $new = dclone($obj); |
| 174 | $$obj{'data'}{'rx'} = $tmp; |
| 175 | $$new{'data'}{'rx'} = $tmp; |
| 176 | return $new; |
| 177 | |
| 178 | } else { |
| 179 | |
| 180 | my $base = $$obj{'base'}; |
| 181 | delete $$obj{'base'}; |
| 182 | |
| 183 | my @rx = qw(namerx zonerx abbrx offrx zrx offabbrx orrparrx); |
| 184 | my @tmp; |
| 185 | foreach my $rx (@rx) { |
| 186 | push(@tmp,$$obj{'data'}{$rx}); |
| 187 | delete $$obj{'data'}{$rx}; |
| 188 | } |
| 189 | |
| 190 | my $new = dclone($obj); |
| 191 | |
| 192 | foreach my $rx (@rx) { |
| 193 | my $r = shift(@tmp); |
| 194 | $$obj{'data'}{$rx} = $r; |
| 195 | $$new{'data'}{$rx} = $r; |
| 196 | } |
| 197 | |
| 198 | $$obj{'base'} = $base; |
| 199 | $$new{'base'} = $base; |
| 200 | return $new; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | # Only called if extra @args exist |
| 205 | sub _init_args { |
| 206 | my($self) = @_; |
| 207 | |
| 208 | my @args = @{ $$self{'args'} }; |
| 209 | carp "WARNING: [new] invalid arguments: @args"; |
| 210 | |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | sub _init_final { |
| 215 | my($self) = @_; |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | sub new_config { |
| 220 | my(@args) = @_; |
| 221 | |
| 222 | # Make sure that @opts is passed in as the final argument. |
| 223 | |
| 224 | if (! @args || |
| 225 | ! (ref($args[$#args]) eq 'ARRAY')) { |
| 226 | push(@args,['ignore','ignore']); |
| 227 | } |
| 228 | |
| 229 | return new(@args); |
| 230 | } |
| 231 | |
| 232 | sub new_date { |
| 233 | my(@args) = @_; |
| 234 | require Date::Manip::Date; |
| 235 | return new Date::Manip::Date @args; |
| 236 | } |
| 237 | sub new_delta { |
| 238 | my(@args) = @_; |
| 239 | require Date::Manip::Delta; |
| 240 | return new Date::Manip::Delta @args; |
| 241 | } |
| 242 | sub new_recur { |
| 243 | my(@args) = @_; |
| 244 | require Date::Manip::Recur; |
| 245 | return new Date::Manip::Recur @args; |
| 246 | } |
| 247 | |
| 248 | sub base { |
| 249 | my($self) = @_; |
| 250 | my $t = ref($self); |
| 251 | if ($t eq 'Date::Manip::Base') { |
| 252 | return undef; |
| 253 | } elsif ($t eq 'Date::Manip::TZ') { |
| 254 | return $$self{'base'}; |
| 255 | } else { |
| 256 | my $dmt = $$self{'tz'}; |
| 257 | return $$dmt{'base'}; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | sub tz { |
| 262 | my($self) = @_; |
| 263 | my $t = ref($self); |
| 264 | |
| 265 | if ($t eq 'Date::Manip::Base' || |
| 266 | $t eq 'Date::Manip::TZ') { |
| 267 | return undef; |
| 268 | } |
| 269 | |
| 270 | return $$self{'tz'}; |
| 271 | } |
| 272 | |
| 273 | sub config { |
| 274 | my($self,@opts) = @_; |
| 275 | my $obj; |
| 276 | if (ref($self) eq 'Date::Manip::Base' || |
| 277 | ref($self) eq 'Date::Manip::TZ') { |
| 278 | $obj = $self; |
| 279 | } else { |
| 280 | $obj = $$self{'tz'}; |
| 281 | } |
| 282 | |
| 283 | while (@opts) { |
| 284 | my $var = shift(@opts); |
| 285 | my $val = shift(@opts); |
| 286 | $obj->_config_var($var,$val); |
| 287 | } |
| 288 | |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | sub get_config { |
| 293 | my($self,@args) = @_; |
| 294 | |
| 295 | my $base; |
| 296 | my $t = ref($self); |
| 297 | if ($t eq 'Date::Manip::Base') { |
| 298 | $base = $self; |
| 299 | } elsif ($t eq 'Date::Manip::TZ') { |
| 300 | $base = $$self{'base'}; |
| 301 | } else { |
| 302 | my $dmt = $$self{'tz'}; |
| 303 | $base = $$dmt{'base'}; |
| 304 | } |
| 305 | |
| 306 | if (@args) { |
| 307 | my @ret; |
| 308 | foreach my $var (@args) { |
| 309 | # uncoverable branch false |
| 310 | if (exists $$base{'data'}{'sections'}{'conf'}{lc($var)}) { |
| 311 | push @ret,$$base{'data'}{'sections'}{'conf'}{lc($var)}; |
| 312 | } else { |
| 313 | # uncoverable statement |
| 314 | carp "ERROR: [config] invalid config variable: $var"; |
| 315 | # uncoverable statement |
| 316 | return ''; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (@ret == 1) { |
| 321 | return $ret[0]; |
| 322 | } else { |
| 323 | return @ret; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | my @ret = sort keys %{ $$base{'data'}{'sections'}{'conf'} }; |
| 328 | return @ret; |
| 329 | } |
| 330 | |
| 331 | sub err { |
| 332 | my($self,$arg) = @_; |
| 333 | if ($arg) { |
| 334 | $$self{'err'} = ''; |
| 335 | return; |
| 336 | } else { |
| 337 | return $$self{'err'}; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | sub is_date { |
| 342 | return 0; |
| 343 | } |
| 344 | sub is_delta { |
| 345 | return 0; |
| 346 | } |
| 347 | sub is_recur { |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | sub version { |
| 352 | my($self,$flag) = @_; |
| 353 | if ($flag && ref($self) ne 'Date::Manip::Base') { |
| 354 | my $dmt; |
| 355 | if (ref($self) eq 'Date::Manip::TZ') { |
| 356 | $dmt = $self; |
| 357 | } else { |
| 358 | $dmt = $$self{'tz'}; |
| 359 | } |
| 360 | my $tz = $dmt->_now('systz'); |
| 361 | return "$VERSION [$tz]"; |
| 362 | } else { |
| 363 | return $VERSION; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | 1; |
| 368 | # Local Variables: |
| 369 | # mode: cperl |
| 370 | # indent-tabs-mode: nil |
| 371 | # cperl-indent-level: 3 |
| 372 | # cperl-continued-statement-offset: 2 |
| 373 | # cperl-continued-brace-offset: 0 |
| 374 | # cperl-brace-offset: 0 |
| 375 | # cperl-brace-imaginary-offset: 0 |
| 376 | # cperl-label-offset: 0 |
| 377 | # End: |