| 1 | #!perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Date::Interval - handling of temporal intervals based on Date::Manip | ||
| 6 | |||
| 7 | =head1 COPYRIGHT | ||
| 8 | |||
| 9 | Copyright (C) 1997 by Kristian Torp, <F<torp@cs.auc.dk>> | ||
| 10 | |||
| 11 | This program is free software. You can redistribute it and/or modify | ||
| 12 | it under the terms of the GNU General Public License as published by | ||
| 13 | the Free Software Foundation; either version 2 of the License, or (at | ||
| 14 | your option) any later version. | ||
| 15 | |||
| 16 | This program is distributed AS IS in the hope that it will be useful, | ||
| 17 | but WITHOUT ANY WARRANTY. See the GNU General Public License for more | ||
| 18 | details. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | package Date::Interval; | ||
| 23 | |||
| 24 | 11 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 11 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 11 | ▶ | use vars qw (@ISA @EXPORT @EXPORT_OK $VERSION |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | $FALSE $TRUE $ABSOLUTE $RELATIVE | ||
| 28 | $OPEN $CLOSED $LEFT_CLOSED $RIGHT_CLOSED $LEFT_OPEN $RIGHT_OPEN | ||
| 29 | $CLOSED_INT $RIGHT_OPEN_INT $LEFT_OPEN_INT $OPEN_INT | ||
| 30 | $BEFORE $MEETS $LEFT_OVERLAPS $RIGHT_OVERLAPS | ||
| 31 | $TOTALLY_OVERLAPS $DURING $EXTENDS $AFTER | ||
| 32 | $ALLEN_BEFORE $ALLEN_MEETS $ALLEN_LEFT_OVERLAPS $ALLEN_LEFT_COVERS | ||
| 33 | $ALLEN_COVERS $ALLEN_STARTS $ALLEN_EQUALS $ALLEN_RIGHT_COVERS | ||
| 34 | $ALLEN_DURING $ALLEN_FINISHES $ALLEN_RIGHT_OVERLAPS | ||
| 35 | $ALLEN_EXTENDS $ALLEN_AFTER | ||
| 36 | 11 | $DisplayFormat $DefaultType $Now); | |
| 37 | @ISA = qw (Exporter); | ||
| 38 | @EXPORT = qw (); | ||
| 39 | @EXPORT_OK = qw ($CLOSED_INT $RIGHT_OPEN_INT $LEFT_OPEN_INT $OPEN_INT | ||
| 40 | $BEFORE $MEETS $LEFT_OVERLAPS $RIGHT_OVERLAPS | ||
| 41 | $TOTALLY_OVERLAPS $DURING $EXTENDS $AFTER | ||
| 42 | $ALLEN_BEFORE $ALLEN_MEETS $ALLEN_LEFT_OVERLAPS | ||
| 43 | $ALLEN_LEFT_COVERS $ALLEN_COVERS $ALLEN_STARTS | ||
| 44 | $ALLEN_EQUALS $ALLEN_RIGHT_COVERS | ||
| 45 | $ALLEN_DURING $ALLEN_FINISHES $ALLEN_RIGHT_OVERLAPS | ||
| 46 | $ALLEN_EXTENDS $ALLEN_AFTER); | ||
| 47 | $VERSION = 0.03; | ||
| 48 | |||
| 49 | 0 | ▶ | use Date::Manip; # data types of the end points in the interval |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 50 | use Carp; | ||
| 51 | |||
| 52 | use overload | ||
| 53 | '+' => \&_plus, | ||
| 54 | '-' => \&_minus, | ||
| 55 | '<' => \&_smaller_than, | ||
| 56 | '>' => \&_greater_than, | ||
| 57 | '==' => \&_equal, | ||
| 58 | '!=' => \&_not_equal, | ||
| 59 | '<=>' => \&_spaceship, | ||
| 60 | qw("" _stringify); | ||
| 61 | |||
| 62 | # Must set the time zone to use Date::Manip | ||
| 63 | BEGIN {$Date::Manip::TZ = "CET";} # Default Central European Time | ||
| 64 | |||
| 65 | ############################################################################## | ||
| 66 | # Constants | ||
| 67 | ############################################################################## | ||
| 68 | |||
| 69 | # Boolean values | ||
| 70 | $FALSE = 0; | ||
| 71 | $TRUE = 1; | ||
| 72 | |||
| 73 | # <value type> | ||
| 74 | $ABSOLUTE = 0; | ||
| 75 | $RELATIVE = 1; | ||
| 76 | |||
| 77 | # For output | ||
| 78 | $LEFT_CLOSED = '['; | ||
| 79 | $RIGHT_CLOSED = ']'; | ||
| 80 | $LEFT_OPEN = '('; | ||
| 81 | $RIGHT_OPEN = ')'; | ||
| 82 | |||
| 83 | # <interval type> | ||
| 84 | $CLOSED_INT = 1; | ||
| 85 | $RIGHT_OPEN_INT = 2; | ||
| 86 | $LEFT_OPEN_INT = 3; | ||
| 87 | $OPEN_INT = 4; | ||
| 88 | |||
| 89 | # <interval end> | ||
| 90 | $OPEN = 1; | ||
| 91 | $CLOSED = 2; | ||
| 92 | |||
| 93 | # <overlap type> | ||
| 94 | $BEFORE = 1; | ||
| 95 | $MEETS = 2; | ||
| 96 | $LEFT_OVERLAPS = 3; | ||
| 97 | $RIGHT_OVERLAPS = 4; | ||
| 98 | $TOTALLY_OVERLAPS = 5; | ||
| 99 | $DURING = 6; | ||
| 100 | $EXTENDS = 7; | ||
| 101 | $AFTER = 8; | ||
| 102 | |||
| 103 | # <Allen overlap type> | ||
| 104 | $ALLEN_BEFORE = 1; | ||
| 105 | $ALLEN_MEETS = 2; | ||
| 106 | $ALLEN_LEFT_OVERLAPS = 3; | ||
| 107 | $ALLEN_LEFT_COVERS = 4; | ||
| 108 | $ALLEN_COVERS = 5; | ||
| 109 | $ALLEN_STARTS = 6; | ||
| 110 | $ALLEN_EQUALS = 7; | ||
| 111 | $ALLEN_RIGHT_COVERS = 8; | ||
| 112 | $ALLEN_DURING = 9; | ||
| 113 | $ALLEN_FINISHES = 10; | ||
| 114 | $ALLEN_RIGHT_OVERLAPS = 11; | ||
| 115 | $ALLEN_EXTENDS = 12; | ||
| 116 | $ALLEN_AFTER = 13; | ||
| 117 | |||
| 118 | ############################################################################## | ||
| 119 | # Class variables | ||
| 120 | ############################################################################## | ||
| 121 | |||
| 122 | $DisplayFormat = "%d/%m/%Y"; # Default <display format> | ||
| 123 | $DefaultType = $RIGHT_OPEN_INT; # Default <interval type> | ||
| 124 | $Now = &_getCurrentTime ($FALSE); # Big brother time, see POD | ||
| 125 | |||
| 126 | ################################################################################ | ||
| 127 | # Class Methods | ||
| 128 | ################################################################################ | ||
| 129 | |||
| 130 | =head1 Class Methods | ||
| 131 | |||
| 132 | =head2 setDefaultIntervalType | ||
| 133 | |||
| 134 | Description: Sets the default <interval type> | ||
| 135 | Input: <interval type> | ||
| 136 | Output: none | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub setDefaultIntervalType | ||
| 141 | { | ||
| 142 | my ($class, $nType) = @_; | ||
| 143 | |||
| 144 | if (ref ($class)) | ||
| 145 | { | ||
| 146 | confess "Class method called as object method"; | ||
| 147 | } | ||
| 148 | if ($nType < 1 || $nType > 4) | ||
| 149 | { | ||
| 150 | confess "Unknown <interval type> $nType"; | ||
| 151 | } | ||
| 152 | $DefaultType = $nType; | ||
| 153 | } | ||
| 154 | |||
| 155 | =head2 getDefaultIntervalType | ||
| 156 | |||
| 157 | Description: Gets the default <interval type> | ||
| 158 | Input: none | ||
| 159 | Output: <interval type> | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | sub getDefaultIntervalType | ||
| 164 | { | ||
| 165 | my $class = shift; | ||
| 166 | if (ref ($class)) { confess "Class method called as object method"; } | ||
| 167 | return $DefaultType; | ||
| 168 | } | ||
| 169 | |||
| 170 | =head2 setDisplayFormat | ||
| 171 | |||
| 172 | Description: Sets the default <display format> | ||
| 173 | Input: <display format> | ||
| 174 | Output: none | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub setDisplayFormat | ||
| 179 | { | ||
| 180 | my ($class) = shift; | ||
| 181 | if (ref ($class)) { confess "Class method called as object method"; } | ||
| 182 | unless (@_ == 1) { confess "usage: Interval->setDisplayFormat(<string>)"; } | ||
| 183 | $DisplayFormat = shift; | ||
| 184 | } | ||
| 185 | |||
| 186 | =head2 getDisplayFormat | ||
| 187 | |||
| 188 | Description: Gets the default <display format> | ||
| 189 | Input: none | ||
| 190 | Output: <display format> | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub getDisplayFormat | ||
| 195 | { | ||
| 196 | my $class = shift; | ||
| 197 | if (ref ($class)) { confess "Class method called as object method"; } | ||
| 198 | return $DisplayFormat; | ||
| 199 | } | ||
| 200 | |||
| 201 | #------------------------------------------------------------------------------- | ||
| 202 | # Instance Variables | ||
| 203 | #------------------------------------------------------------------------------- | ||
| 204 | |||
| 205 | my %fields = (m_nStart => undef, # Start value | ||
| 206 | m_nStop => undef, # Stop value | ||
| 207 | m_bStart => undef, # Start absolute or relative | ||
| 208 | m_bStop => undef, # Stop absolute or relative | ||
| 209 | m_nLeft => undef, # Left open or closed <interval end> | ||
| 210 | m_nRight => undef, # Right open or closed <interval end> | ||
| 211 | m_bEmpty => undef); # is the interval empty | ||
| 212 | |||
| 213 | #------------------------------------------------------------------------------- | ||
| 214 | # Public Methods | ||
| 215 | #------------------------------------------------------------------------------- | ||
| 216 | |||
| 217 | =head1 Public Methods | ||
| 218 | |||
| 219 | =head2 new | ||
| 220 | |||
| 221 | Description: Constructor | ||
| 222 | Input: <start value> <stop value> [<interval type>] | ||
| 223 | Output: reference | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | sub new | ||
| 228 | { | ||
| 229 | my ($proto) = shift; | ||
| 230 | |||
| 231 | my ($class) = ref ($proto) || $proto; | ||
| 232 | my ($self) = {}; | ||
| 233 | bless ($self, $class); | ||
| 234 | |||
| 235 | $self->_initialize (@_); | ||
| 236 | return $self; | ||
| 237 | } | ||
| 238 | |||
| 239 | =head2 length | ||
| 240 | |||
| 241 | Description: Returns the Length of an interval in Date::Manip format | ||
| 242 | Input: none | ||
| 243 | Output: <delta> | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | sub length | ||
| 248 | { | ||
| 249 | my ($self) = shift; | ||
| 250 | |||
| 251 | # Return 0 length | ||
| 252 | if ($self->{m_bEmpty}) | ||
| 253 | { | ||
| 254 | return &DateCalc (&_getCurrentTime($TRUE), &_getCurrentTime($TRUE)); | ||
| 255 | } | ||
| 256 | |||
| 257 | my ($startDate) = _to_date ($FALSE, $self->{m_bStart}, $self->{m_nStart}); | ||
| 258 | my ($stopDate) = _to_date ($TRUE, $self->{m_bStop}, $self->{m_nStop}, | ||
| 259 | $self->{m_bStart}, $startDate); | ||
| 260 | |||
| 261 | if ($startDate lt $stopDate) | ||
| 262 | { | ||
| 263 | return &DateCalc ($startDate, $stopDate); | ||
| 264 | } | ||
| 265 | else | ||
| 266 | { | ||
| 267 | return &DateCalc (&_getCurrentTime($TRUE), &_getCurrentTime($TRUE)); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | =head2 length | ||
| 272 | |||
| 273 | Description: Returns the length of an interval in string format | ||
| 274 | Input: none | ||
| 275 | Output: string | ||
| 276 | |||
| 277 | =cut | ||
| 278 | |||
| 279 | sub lengthString | ||
| 280 | { | ||
| 281 | my ($self) = shift; | ||
| 282 | if ($self->{m_bEmpty}) { return ''; } | ||
| 283 | my $delta = $self->length; | ||
| 284 | my ($nYears, $nMonths, $nDays, $nHours, $nMinuts, $nSeconds) = | ||
| 285 | split (':', $delta); | ||
| 286 | $nYears =~ s/^[+|-]//; | ||
| 287 | return "$nYears Years $nMonths Months $nDays Days" . | ||
| 288 | " $nHours Hours $nMinuts Minuts $nSeconds Seconds"; | ||
| 289 | } | ||
| 290 | |||
| 291 | =head2 get | ||
| 292 | |||
| 293 | Description: Returns the interval in string format | ||
| 294 | Input: none | ||
| 295 | Output: string | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | sub get | ||
| 300 | { | ||
| 301 | my ($self) = shift; | ||
| 302 | |||
| 303 | my ($szResult) = ''; | ||
| 304 | my ($sep) = defined ($,) ? $, : ','; # Which separtor | ||
| 305 | if ($self->{m_bEmpty}) { return '<empty>'; } | ||
| 306 | |||
| 307 | if ($self->{m_nLeft} == $CLOSED) { $szResult .= $LEFT_CLOSED; } | ||
| 308 | else { $szResult .= $LEFT_OPEN; } | ||
| 309 | |||
| 310 | $szResult .= _to_string ($self->{m_bStart}, $self->{m_nStart}); | ||
| 311 | $szResult .= "$sep "; | ||
| 312 | $szResult .= _to_string ($self->{m_bStop}, $self->{m_nStop}); | ||
| 313 | |||
| 314 | if ($self->{m_nRight} == $CLOSED) { $szResult .= $RIGHT_CLOSED; } | ||
| 315 | else { $szResult .= $RIGHT_OPEN; } | ||
| 316 | |||
| 317 | return $szResult; | ||
| 318 | } | ||
| 319 | |||
| 320 | =head2 getStart | ||
| 321 | |||
| 322 | Description: Returns the <start value> | ||
| 323 | Input: none | ||
| 324 | Output: <date> | ||
| 325 | |||
| 326 | =cut | ||
| 327 | |||
| 328 | sub getStart | ||
| 329 | { | ||
| 330 | my ($self) = shift; | ||
| 331 | return _to_string ($self->{m_bStart}, $self->{m_nStart}); | ||
| 332 | } | ||
| 333 | |||
| 334 | =head2 getStop | ||
| 335 | |||
| 336 | Description: Returns the <stop value> | ||
| 337 | Input: none | ||
| 338 | Output: <date> | ||
| 339 | |||
| 340 | =cut | ||
| 341 | |||
| 342 | sub getStop | ||
| 343 | { | ||
| 344 | my ($self) = shift; | ||
| 345 | return _to_string ($self->{m_bStop}, $self->{m_nStop}); | ||
| 346 | } | ||
| 347 | |||
| 348 | =head2 overlaps | ||
| 349 | |||
| 350 | Description: Checks if two intervals overlap | ||
| 351 | Input: <interval> | ||
| 352 | Output: <boolean> | ||
| 353 | |||
| 354 | =cut | ||
| 355 | |||
| 356 | sub overlaps | ||
| 357 | { | ||
| 358 | my ($self, $other) = @_; | ||
| 359 | |||
| 360 | if ($self->{m_bEmpty} || $other->{m_bEmpty}) { return $FALSE; } | ||
| 361 | |||
| 362 | if ($self->_overlaps ($other)) { return $TRUE; } | ||
| 363 | else { return $FALSE; } | ||
| 364 | } | ||
| 365 | |||
| 366 | =head2 getOverlap | ||
| 367 | |||
| 368 | Description: Return the overlap of two intervals | ||
| 369 | Input: <interval> | ||
| 370 | Output: <interval> | undef | ||
| 371 | |||
| 372 | =cut | ||
| 373 | |||
| 374 | sub getOverlap | ||
| 375 | { | ||
| 376 | my ($self, $other) = @_; | ||
| 377 | |||
| 378 | my ($nStart, $nStop, $nLeft, $nRight); | ||
| 379 | |||
| 380 | if ($self->{m_bEmpty} || $other->{m_bEmpty}) | ||
| 381 | { | ||
| 382 | return _new_empty Date::Interval; | ||
| 383 | } | ||
| 384 | |||
| 385 | if ($self->{m_bStart} == $RELATIVE || $self->{m_bStop} == $RELATIVE || | ||
| 386 | $other->{m_bStart} == $RELATIVE || $other->{m_bStop} == $RELATIVE) | ||
| 387 | { | ||
| 388 | print STDERR "Sorry, overlap of relative intervals not implemented yet\n"; | ||
| 389 | return _new_empty Date::Interval; | ||
| 390 | } | ||
| 391 | |||
| 392 | # Meets | ||
| 393 | if ($self->{m_nStop} eq $other->{m_nStart} && | ||
| 394 | ($self->{m_nRight} == $CLOSED || $other->{m_nLeft} == $CLOSED)) | ||
| 395 | { | ||
| 396 | $nStart = $nStop = $self->{m_nStop}; | ||
| 397 | $nLeft = $self->{m_nRight}; | ||
| 398 | $nRight = $other->{m_nLeft}; | ||
| 399 | } | ||
| 400 | # Extends | ||
| 401 | elsif ($self->{m_nStart} eq $other->{m_nStop} && | ||
| 402 | ($self->{m_nLeft} == $CLOSED || $other->{m_nRight} == $CLOSED)) | ||
| 403 | { | ||
| 404 | $nStart = $nStop = $self->{m_nStart}; | ||
| 405 | $nLeft = $self->{m_nLeft}; | ||
| 406 | $nRight = $other->{m_nRight}; | ||
| 407 | } | ||
| 408 | # Overlaps | ||
| 409 | elsif ($self->{m_nStart} le $other->{m_nStop} && | ||
| 410 | $other->{m_nStart} le $self->{m_nStop}) | ||
| 411 | { | ||
| 412 | # Max start time | ||
| 413 | if ($other->{m_nStart} lt $self->{m_nStart}) | ||
| 414 | { | ||
| 415 | $nStart = $self->{m_nStart}; | ||
| 416 | } | ||
| 417 | else | ||
| 418 | { | ||
| 419 | $nStart = $other->{m_nStart}; | ||
| 420 | } | ||
| 421 | # left bracket | ||
| 422 | if ($self->{m_nLeft} == $OPEN || $other->{m_nLeft} == $OPEN) | ||
| 423 | { | ||
| 424 | $nLeft = $OPEN; | ||
| 425 | } | ||
| 426 | else | ||
| 427 | { | ||
| 428 | $nLeft = $CLOSED; | ||
| 429 | } | ||
| 430 | |||
| 431 | # Min stop time | ||
| 432 | if ($other->{m_nStop} lt $self->{m_nStop}) | ||
| 433 | { | ||
| 434 | $nStop = $other->{m_nStop}; | ||
| 435 | } | ||
| 436 | else | ||
| 437 | { | ||
| 438 | $nStop = $self->{m_nStop}; | ||
| 439 | } | ||
| 440 | |||
| 441 | # right bracket | ||
| 442 | if ($self->{m_nRight} == $OPEN || $other->{m_nRight} == $OPEN) | ||
| 443 | { | ||
| 444 | $nRight = $OPEN; | ||
| 445 | } | ||
| 446 | else | ||
| 447 | { | ||
| 448 | $nRight = $CLOSED; | ||
| 449 | } | ||
| 450 | |||
| 451 | my $int = new Date::Interval ($nStart, $nStop); | ||
| 452 | $int->_setBrackets ($nLeft, $nRight); | ||
| 453 | |||
| 454 | return $int; | ||
| 455 | } | ||
| 456 | else | ||
| 457 | { | ||
| 458 | return _new_empty Date::Interval; | ||
| 459 | } | ||
| 460 | } | ||
| 461 | |||
| 462 | =head2 before | ||
| 463 | |||
| 464 | Description: Examines if interval is before | ||
| 465 | Input: <interval> | ||
| 466 | Output: <boolean> | ||
| 467 | |||
| 468 | =cut | ||
| 469 | |||
| 470 | sub before | ||
| 471 | { | ||
| 472 | my ($self, $other) = @_; | ||
| 473 | $self->_overlaps ($other) == $BEFORE ? return $TRUE : return $FALSE; | ||
| 474 | } | ||
| 475 | |||
| 476 | =head2 meets | ||
| 477 | |||
| 478 | Description: Examines if intervals meets | ||
| 479 | Input: <interval> | ||
| 480 | Output: <boolean> | ||
| 481 | |||
| 482 | =cut | ||
| 483 | |||
| 484 | sub meets | ||
| 485 | { | ||
| 486 | my $self = shift; my $other = shift; | ||
| 487 | $self->_overlaps ($other) == $MEETS ? return $TRUE : return $FALSE; | ||
| 488 | } | ||
| 489 | |||
| 490 | =head2 leftOverlaps | ||
| 491 | |||
| 492 | Description: Examines if two intervals left overlap | ||
| 493 | Input: <interval> | ||
| 494 | Output: <boolean> | ||
| 495 | |||
| 496 | =cut | ||
| 497 | |||
| 498 | sub leftOverlaps | ||
| 499 | { | ||
| 500 | my ($self, $other) = @_; | ||
| 501 | $self->_overlaps ($other) == $LEFT_OVERLAPS ? return $TRUE : return $FALSE; | ||
| 502 | } | ||
| 503 | |||
| 504 | =head2 rightOverlaps | ||
| 505 | |||
| 506 | Description: Examines if two intervals right overlap | ||
| 507 | Input: <interval> | ||
| 508 | Output: <boolean> | ||
| 509 | |||
| 510 | =cut | ||
| 511 | |||
| 512 | sub rightOverlaps | ||
| 513 | { | ||
| 514 | my ($self, $other) = @_; | ||
| 515 | $self->_overlaps ($other) == $RIGHT_OVERLAPS ? return $TRUE : return $FALSE; | ||
| 516 | } | ||
| 517 | |||
| 518 | =head2 during | ||
| 519 | |||
| 520 | Description: Examines if two intervals during overlap | ||
| 521 | Input: <interval> | ||
| 522 | Output: <boolean> | ||
| 523 | |||
| 524 | =cut | ||
| 525 | |||
| 526 | sub during | ||
| 527 | { | ||
| 528 | my ($self, $other) = @_; | ||
| 529 | $self->_overlaps ($other) == $DURING ? return $TRUE : return $FALSE; | ||
| 530 | } | ||
| 531 | |||
| 532 | =head2 totallyOverlaps | ||
| 533 | |||
| 534 | Description: Examines if two intervals totally overlap | ||
| 535 | Input: <interval> | ||
| 536 | Output: <boolean> | ||
| 537 | |||
| 538 | =cut | ||
| 539 | |||
| 540 | sub totallyOverlaps | ||
| 541 | { | ||
| 542 | my ($self, $other) = @_; | ||
| 543 | $self->_overlaps ($other) == $TOTALLY_OVERLAPS ? | ||
| 544 | return $TRUE : return $FALSE; | ||
| 545 | } | ||
| 546 | |||
| 547 | =head2 extends | ||
| 548 | |||
| 549 | Description: Examines if two intervals extends | ||
| 550 | Input: <interval> | ||
| 551 | Output: <boolean> | ||
| 552 | |||
| 553 | =cut | ||
| 554 | |||
| 555 | sub extends | ||
| 556 | { | ||
| 557 | my $self = shift; my $other = shift; | ||
| 558 | $self->_overlaps ($other) == $EXTENDS ? return $TRUE : return $FALSE; | ||
| 559 | } | ||
| 560 | |||
| 561 | =head2 after | ||
| 562 | |||
| 563 | Description: Examines if two intervals after | ||
| 564 | Input: <interval> | ||
| 565 | Output: <boolean> | ||
| 566 | |||
| 567 | =cut | ||
| 568 | |||
| 569 | sub after | ||
| 570 | { | ||
| 571 | my ($self, $other) = @_; | ||
| 572 | $self->_overlaps ($other) == $EXTENDS ? return $TRUE : return $FALSE; | ||
| 573 | } | ||
| 574 | |||
| 575 | =head2 howOverlaps | ||
| 576 | |||
| 577 | Description: Describes in text how intervals overlaps | ||
| 578 | Input: <interval> | ||
| 579 | Output: to screen | ||
| 580 | |||
| 581 | =cut | ||
| 582 | |||
| 583 | sub howOverlaps | ||
| 584 | { | ||
| 585 | my ($self, $other) = @_; | ||
| 586 | |||
| 587 | my ($szOverlap) = ' does not overlap '; | ||
| 588 | if ($self->{m_bEmpty} || $other->{m_bEmpty}) | ||
| 589 | { | ||
| 590 | print $self->get, $szOverlap, $other->get, "\n"; | ||
| 591 | } | ||
| 592 | else | ||
| 593 | { | ||
| 594 | my ($bOverlaps) = $self->_overlaps($other); | ||
| 595 | |||
| 596 | if ($bOverlaps == $MEETS) | ||
| 597 | { | ||
| 598 | $szOverlap = ' meets '; | ||
| 599 | } | ||
| 600 | elsif ($bOverlaps == $LEFT_OVERLAPS) | ||
| 601 | { | ||
| 602 | $szOverlap = ' left overlaps '; | ||
| 603 | } | ||
| 604 | elsif ($bOverlaps == $RIGHT_OVERLAPS) | ||
| 605 | { | ||
| 606 | $szOverlap = ' right overlaps '; | ||
| 607 | } | ||
| 608 | elsif ($bOverlaps == $TOTALLY_OVERLAPS) | ||
| 609 | { | ||
| 610 | $szOverlap = ' totally overlaps '; | ||
| 611 | } | ||
| 612 | elsif ($bOverlaps == $DURING) | ||
| 613 | { | ||
| 614 | $szOverlap = ' inclusion overlaps '; | ||
| 615 | } | ||
| 616 | elsif ($bOverlaps == $EXTENDS) | ||
| 617 | { | ||
| 618 | $szOverlap = ' extends '; | ||
| 619 | } | ||
| 620 | else | ||
| 621 | { | ||
| 622 | # do nothing | ||
| 623 | } | ||
| 624 | print $self->get, $szOverlap, $other->get, "\n"; | ||
| 625 | } | ||
| 626 | } | ||
| 627 | |||
| 628 | =head2 AllenHowOverlaps | ||
| 629 | |||
| 630 | Description: Return how intervals overlap in Allen´s terminology | ||
| 631 | Input: <interval> | ||
| 632 | Output: to screen | ||
| 633 | |||
| 634 | =cut | ||
| 635 | |||
| 636 | sub AllenHowOverlaps | ||
| 637 | { | ||
| 638 | my ($self, $other) = @_; | ||
| 639 | |||
| 640 | my ($szOverlap) = ' does not overlap '; | ||
| 641 | |||
| 642 | # If one of the intervals are empty AllenOverlap is undefined | ||
| 643 | if ($self->{m_bEmpty} || $other->{m_bEmpty}) | ||
| 644 | { | ||
| 645 | print $self->get, $szOverlap, $other->get, "\n"; | ||
| 646 | return $FALSE; | ||
| 647 | } | ||
| 648 | |||
| 649 | # Non-empty intervals | ||
| 650 | my ($bOverlaps) = $self->_AllenOverlaps($other); | ||
| 651 | |||
| 652 | if ($bOverlaps == $ALLEN_BEFORE) | ||
| 653 | { | ||
| 654 | $szOverlap = ' before '; | ||
| 655 | } | ||
| 656 | elsif ($bOverlaps == $ALLEN_MEETS) | ||
| 657 | { | ||
| 658 | $szOverlap = ' meets '; | ||
| 659 | } | ||
| 660 | elsif ($bOverlaps == $ALLEN_LEFT_OVERLAPS) | ||
| 661 | { | ||
| 662 | $szOverlap = ' left overlaps '; | ||
| 663 | } | ||
| 664 | elsif ($bOverlaps == $ALLEN_LEFT_COVERS) | ||
| 665 | { | ||
| 666 | $szOverlap = ' left covers '; | ||
| 667 | } | ||
| 668 | elsif ($bOverlaps == $ALLEN_COVERS) | ||
| 669 | { | ||
| 670 | $szOverlap = ' covers '; | ||
| 671 | } | ||
| 672 | elsif ($bOverlaps == $ALLEN_STARTS) | ||
| 673 | { | ||
| 674 | $szOverlap = ' starts '; | ||
| 675 | } | ||
| 676 | elsif ($bOverlaps == $ALLEN_EQUALS) | ||
| 677 | { | ||
| 678 | $szOverlap = ' equals '; | ||
| 679 | } | ||
| 680 | elsif ($bOverlaps == $ALLEN_RIGHT_COVERS) | ||
| 681 | { | ||
| 682 | $szOverlap = ' right covers '; | ||
| 683 | } | ||
| 684 | elsif ($bOverlaps == $ALLEN_DURING) | ||
| 685 | { | ||
| 686 | $szOverlap = ' during '; | ||
| 687 | } | ||
| 688 | elsif ($bOverlaps == $ALLEN_FINISHES) | ||
| 689 | { | ||
| 690 | $szOverlap = ' finishes '; | ||
| 691 | } | ||
| 692 | elsif ($bOverlaps == $ALLEN_RIGHT_OVERLAPS) | ||
| 693 | { | ||
| 694 | $szOverlap = ' right overlaps '; | ||
| 695 | } | ||
| 696 | elsif ($bOverlaps == $ALLEN_EXTENDS) | ||
| 697 | { | ||
| 698 | $szOverlap = ' extends '; | ||
| 699 | } | ||
| 700 | elsif ($bOverlaps == $ALLEN_AFTER) | ||
| 701 | { | ||
| 702 | $szOverlap = ' after '; | ||
| 703 | } | ||
| 704 | print $self->get, $szOverlap, $other->get, "\n"; | ||
| 705 | } | ||
| 706 | |||
| 707 | =head2 AllenBefore | ||
| 708 | |||
| 709 | Description: Examines if intervals Allen before | ||
| 710 | Input: <interval> | ||
| 711 | Output: <boolean> | ||
| 712 | |||
| 713 | =cut | ||
| 714 | |||
| 715 | sub AllenBefore | ||
| 716 | { | ||
| 717 | my ($self, $other) = @_; | ||
| 718 | $self->_AllenOverlaps ($other) == $ALLEN_BEFORE ? | ||
| 719 | return $TRUE : return $FALSE; | ||
| 720 | } | ||
| 721 | |||
| 722 | =head2 AllenMeets | ||
| 723 | |||
| 724 | Description: Examines if intervals Allen meets | ||
| 725 | Input: <interval> | ||
| 726 | Output: <boolean> | ||
| 727 | |||
| 728 | =cut | ||
| 729 | |||
| 730 | sub AllenMeets | ||
| 731 | { | ||
| 732 | my ($self, $other) = @_; | ||
| 733 | $self->_AllenOverlaps ($other) == $ALLEN_MEETS ? | ||
| 734 | return $TRUE : return $FALSE; | ||
| 735 | } | ||
| 736 | |||
| 737 | =head2 AllenLeftOverlaps | ||
| 738 | |||
| 739 | Description: Examines if intervals Allen left overlaps | ||
| 740 | Input: <interval> | ||
| 741 | Output: <boolean> | ||
| 742 | |||
| 743 | =cut | ||
| 744 | |||
| 745 | sub AllenLeftOverlaps | ||
| 746 | { | ||
| 747 | my ($self, $other) = @_; | ||
| 748 | $self->_AllenOverlaps ($other) == $ALLEN_LEFT_OVERLAPS ? | ||
| 749 | return $TRUE : return $FALSE; | ||
| 750 | } | ||
| 751 | |||
| 752 | =head2 AllenLeftCovers | ||
| 753 | |||
| 754 | Description: Examines if intervals Allen left covers | ||
| 755 | Input: <interval> | ||
| 756 | Output: <boolean> | ||
| 757 | |||
| 758 | =cut | ||
| 759 | |||
| 760 | sub AllenLeftCovers | ||
| 761 | { | ||
| 762 | my ($self, $other) = @_; | ||
| 763 | $self->_AllenOverlaps ($other) == $ALLEN_LEFT_COVERS ? return $TRUE : return $FALSE; | ||
| 764 | } | ||
| 765 | |||
| 766 | =head2 AllenCovers | ||
| 767 | |||
| 768 | Description: Examines if intervals Allen covers | ||
| 769 | Input: <interval> | ||
| 770 | Output: <boolean> | ||
| 771 | |||
| 772 | =cut | ||
| 773 | |||
| 774 | sub AllenCovers | ||
| 775 | { | ||
| 776 | my ($self, $other) = @_; | ||
| 777 | $self->_AllenOverlaps ($other) == $ALLEN_COVERS ? | ||
| 778 | return $TRUE : return $FALSE; | ||
| 779 | } | ||
| 780 | |||
| 781 | =head2 AllenStarts | ||
| 782 | |||
| 783 | Description: Examines if intervals Allen starts | ||
| 784 | Input: <interval> | ||
| 785 | Output: <boolean> | ||
| 786 | |||
| 787 | =cut | ||
| 788 | |||
| 789 | sub AllenStarts | ||
| 790 | { | ||
| 791 | my ($self, $other) = @_; | ||
| 792 | $self->_AllenOverlaps ($other) == $ALLEN_STARTS ? | ||
| 793 | return $TRUE : return $FALSE; | ||
| 794 | } | ||
| 795 | |||
| 796 | =head2 AllenEquals | ||
| 797 | |||
| 798 | Description: Examines if intervals Allen equals | ||
| 799 | Input: <interval> | ||
| 800 | Output: <boolean> | ||
| 801 | |||
| 802 | =cut | ||
| 803 | |||
| 804 | sub AllenEquals | ||
| 805 | { | ||
| 806 | my ($self, $other) = @_; | ||
| 807 | $self->_AllenOverlaps ($other) == $ALLEN_EQUALS ? | ||
| 808 | return $TRUE : return $FALSE; | ||
| 809 | } | ||
| 810 | |||
| 811 | =head2 AllenRightCovers | ||
| 812 | |||
| 813 | Description: Examines if intervals Allen right covers | ||
| 814 | Input: <interval> | ||
| 815 | Output: <boolean> | ||
| 816 | |||
| 817 | =cut | ||
| 818 | |||
| 819 | sub AllenRightCovers | ||
| 820 | { | ||
| 821 | my ($self, $other) = @_; | ||
| 822 | $self->_AllenOverlaps ($other) == $ALLEN_RIGHT_COVERS ? | ||
| 823 | return $TRUE : return $FALSE; | ||
| 824 | } | ||
| 825 | |||
| 826 | =head2 AllenDuring | ||
| 827 | |||
| 828 | Description: Examines if intervals Allen during | ||
| 829 | Input: <interval> | ||
| 830 | Output: <boolean> | ||
| 831 | |||
| 832 | =cut | ||
| 833 | |||
| 834 | sub AllenDuring | ||
| 835 | { | ||
| 836 | my ($self, $other) = @_; | ||
| 837 | $self->_AllenOverlaps ($other) == $ALLEN_DURING ? | ||
| 838 | return $TRUE : return $FALSE; | ||
| 839 | } | ||
| 840 | |||
| 841 | =head2 AllenFinishes | ||
| 842 | |||
| 843 | Description: Examines if intervals Allen finishes | ||
| 844 | Input: <interval> | ||
| 845 | Output: <boolean> | ||
| 846 | |||
| 847 | =cut | ||
| 848 | |||
| 849 | sub AllenFinishes | ||
| 850 | { | ||
| 851 | my ($self, $other) = @_; | ||
| 852 | $self->_AllenOverlaps ($other) == $ALLEN_FINISHES ? | ||
| 853 | return $TRUE : return $FALSE; | ||
| 854 | } | ||
| 855 | |||
| 856 | =head2 AllenRightOverlaps | ||
| 857 | |||
| 858 | Description: Examines if intervals Allen right overlaps | ||
| 859 | Input: <interval> | ||
| 860 | Output: <boolean> | ||
| 861 | |||
| 862 | =cut | ||
| 863 | |||
| 864 | sub AllenRightOverlaps | ||
| 865 | { | ||
| 866 | my ($self, $other) = @_; | ||
| 867 | $self->_AllenOverlaps ($other) == $ALLEN_RIGHT_OVERLAPS ? | ||
| 868 | return $TRUE : return $FALSE; | ||
| 869 | } | ||
| 870 | |||
| 871 | =head2 AllenExtends | ||
| 872 | |||
| 873 | Description: Examines if intervals Allen extends | ||
| 874 | Input: <interval> | ||
| 875 | Output: <boolean> | ||
| 876 | |||
| 877 | =cut | ||
| 878 | |||
| 879 | sub AllenExtends | ||
| 880 | { | ||
| 881 | my ($self, $other) = @_; | ||
| 882 | $self->_AllenOverlaps ($other) == $ALLEN_EXTENDS ? | ||
| 883 | return $TRUE : return $FALSE; | ||
| 884 | } | ||
| 885 | |||
| 886 | =head2 AllenAfter | ||
| 887 | |||
| 888 | Description: Examines if intervals Allen after | ||
| 889 | Input: <interval> | ||
| 890 | Output: <boolean> | ||
| 891 | |||
| 892 | =cut | ||
| 893 | |||
| 894 | sub AllenAfter | ||
| 895 | { | ||
| 896 | my ($self, $other) = @_; | ||
| 897 | $self->_AllenOverlaps ($other) == $ALLEN_AFTER ? | ||
| 898 | return $TRUE : return $FALSE; | ||
| 899 | } | ||
| 900 | |||
| 901 | #------------------------------------------------------------------------------- | ||
| 902 | # Private Methods | ||
| 903 | #------------------------------------------------------------------------------- | ||
| 904 | |||
| 905 | =head1 Private Methods | ||
| 906 | |||
| 907 | =head2 _initialize | ||
| 908 | |||
| 909 | Description: Initization of new instances | ||
| 910 | Input: <start value> <stop value> [<interval type>] | ||
| 911 | Output: none | ||
| 912 | |||
| 913 | =cut | ||
| 914 | |||
| 915 | sub _initialize | ||
| 916 | { | ||
| 917 | my ($self, $szStart, $szStop, $nType) = @_; | ||
| 918 | |||
| 919 | $self->{m_bEmpty} = $FALSE; | ||
| 920 | |||
| 921 | # Parse the interval end points | ||
| 922 | ($self->{m_nStart}, $self->{m_bStart}) = _getEndPoint($szStart); | ||
| 923 | if (!defined ($self->{m_nStart})) | ||
| 924 | { | ||
| 925 | print STDERR "Problems using $szStart as start value\n"; | ||
| 926 | $self->{m_bEmpty} = $TRUE; | ||
| 927 | } | ||
| 928 | |||
| 929 | ($self->{m_nStop}, $self->{m_bStop}) = _getEndPoint($szStop); | ||
| 930 | if (!defined ($self->{m_nStop})) | ||
| 931 | { | ||
| 932 | print STDERR "Problems using $szStop as stop value\n"; | ||
| 933 | $self->{m_bEmpty} = $TRUE; | ||
| 934 | } | ||
| 935 | |||
| 936 | # Check the end point values | ||
| 937 | my $start = _to_date ($FALSE, $self->{m_bStart}, $self->{m_nStart}); | ||
| 938 | my $stop = _to_date ($TRUE, $self->{m_bStop}, | ||
| 939 | $self->{m_nStop}, $self->{m_bStart}, $start); | ||
| 940 | |||
| 941 | if ($self->{m_bStop} == $ABSOLUTE && $start gt $stop) | ||
| 942 | { | ||
| 943 | die "Start date larger than stop date\n"; | ||
| 944 | } | ||
| 945 | |||
| 946 | # Use the default <interval type>? | ||
| 947 | if (!defined($nType)) { $nType = $DefaultType; } | ||
| 948 | if (!$self->_setIntervalType ($nType)) | ||
| 949 | { | ||
| 950 | print STDERR "Problems setting the <interval type> $nType\n"; | ||
| 951 | } | ||
| 952 | } | ||
| 953 | |||
| 954 | =head2 _new_empty | ||
| 955 | |||
| 956 | Description: Initization of new empty instance | ||
| 957 | Input: none | ||
| 958 | Output: empty <interval> | ||
| 959 | |||
| 960 | =cut | ||
| 961 | |||
| 962 | sub _new_empty | ||
| 963 | { | ||
| 964 | my ($proto) = shift; | ||
| 965 | |||
| 966 | my ($class) = ref ($proto) || $proto; | ||
| 967 | my ($self) = {}; | ||
| 968 | bless ($self, $class); | ||
| 969 | |||
| 970 | $self->{m_bEmpty} = $TRUE; | ||
| 971 | return $self; | ||
| 972 | } | ||
| 973 | |||
| 974 | =head2 _setIntervalType | ||
| 975 | |||
| 976 | Description: Sets the <interval type> | ||
| 977 | Input: <interval type> | ||
| 978 | Output: <boolean> | ||
| 979 | |||
| 980 | =cut | ||
| 981 | |||
| 982 | sub _setIntervalType | ||
| 983 | { | ||
| 984 | my ($self, $nType) = @_; | ||
| 985 | |||
| 986 | if ($nType == $CLOSED_INT) | ||
| 987 | { | ||
| 988 | $self->{m_nLeft} = $CLOSED; | ||
| 989 | $self->{m_nRight} = $CLOSED; | ||
| 990 | } | ||
| 991 | elsif ($nType == $RIGHT_OPEN_INT) | ||
| 992 | { | ||
| 993 | $self->{m_nLeft} = $CLOSED; | ||
| 994 | $self->{m_nRight} = $OPEN; | ||
| 995 | } | ||
| 996 | elsif ($nType == $LEFT_OPEN_INT) | ||
| 997 | { | ||
| 998 | $self->{m_nLeft} = $OPEN; | ||
| 999 | $self->{m_nRight} = $CLOSED; | ||
| 1000 | } | ||
| 1001 | elsif ($nType == $OPEN_INT) | ||
| 1002 | { | ||
| 1003 | $self->{m_nLeft} = $OPEN; | ||
| 1004 | $self->{m_nRight} = $OPEN; | ||
| 1005 | } | ||
| 1006 | else | ||
| 1007 | { | ||
| 1008 | return $FALSE; | ||
| 1009 | } | ||
| 1010 | return $TRUE; | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | =head2 _setBrackets | ||
| 1014 | |||
| 1015 | Description: Sets the interval brackets | ||
| 1016 | Input: <interval end> <interval end> | ||
| 1017 | Output: <boolean> | ||
| 1018 | |||
| 1019 | =cut | ||
| 1020 | |||
| 1021 | sub _setBrackets | ||
| 1022 | { | ||
| 1023 | my ($self, $nLeft, $nRight) = @_; | ||
| 1024 | $self->{m_nLeft} = $nLeft; | ||
| 1025 | $self->{m_nRight} = $nRight; | ||
| 1026 | return $TRUE; | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | =head2 _overlaps | ||
| 1030 | |||
| 1031 | Description: Examines how intervals overlaps | ||
| 1032 | Input: <interval> | ||
| 1033 | Output: <overlap type> || FALSE | ||
| 1034 | |||
| 1035 | =cut | ||
| 1036 | |||
| 1037 | sub _overlaps | ||
| 1038 | { | ||
| 1039 | my ($self, $other) = @_; | ||
| 1040 | |||
| 1041 | my ($bHowOverlaps, $bLeft); | ||
| 1042 | $bHowOverlaps = $bLeft = $FALSE; | ||
| 1043 | |||
| 1044 | if ($self->{m_bEmpty} || $other->{m_bEmpty}) { return $FALSE; } | ||
| 1045 | |||
| 1046 | my $start1 = _to_date ($FALSE, $self->{m_bStart}, $self->{m_nStart}); | ||
| 1047 | my $stop1 = _to_date ($TRUE, $self->{m_bStop}, | ||
| 1048 | $self->{m_nStop}, $self->{m_bStart}, $start1); | ||
| 1049 | my $start2 = _to_date ($TRUE, $other->{m_bStart}, $other->{m_nStart}); | ||
| 1050 | my $stop2 = _to_date ($TRUE, $other->{m_bStop}, | ||
| 1051 | $other->{m_nStop}, $other->{m_bStart}, $start2); | ||
| 1052 | |||
| 1053 | # Meets | ||
| 1054 | if ($stop1 eq $start2 && | ||
| 1055 | ($self->{m_nRight} == $CLOSED || $other->{m_nLeft} == $CLOSED)) | ||
| 1056 | { | ||
| 1057 | $bHowOverlaps = $MEETS; | ||
| 1058 | } | ||
| 1059 | # Extends | ||
| 1060 | elsif ($start1 eq $stop2 && | ||
| 1061 | ($self->{m_nLeft} == $CLOSED || $other->{m_nRight} == $CLOSED)) | ||
| 1062 | { | ||
| 1063 | $bHowOverlaps = $EXTENDS; | ||
| 1064 | } | ||
| 1065 | # Overlaps | ||
| 1066 | elsif ($start1 le $stop2 && $start2 le $stop1) | ||
| 1067 | { | ||
| 1068 | $bHowOverlaps = $TOTALLY_OVERLAPS; # A guess | ||
| 1069 | |||
| 1070 | # Left overlap or inclusion | ||
| 1071 | if ($start2 le $stop1 && $stop1 le $stop2) | ||
| 1072 | { | ||
| 1073 | $bHowOverlaps = $LEFT_OVERLAPS; # A guess | ||
| 1074 | $bLeft = $TRUE; # Saved for inclusion | ||
| 1075 | } | ||
| 1076 | # Right overlap or inclusion | ||
| 1077 | if ($start2 le $start1 && $start1 le $stop2) | ||
| 1078 | { | ||
| 1079 | $bHowOverlaps = $bLeft ? $DURING : $RIGHT_OVERLAPS; | ||
| 1080 | } | ||
| 1081 | } | ||
| 1082 | return $bHowOverlaps; | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | =head2 _AllenOverlaps | ||
| 1086 | |||
| 1087 | Description: Finds how intervals overlap in Allen terminology | ||
| 1088 | Input: <interval> | ||
| 1089 | Output: <Allen overlap type> | ||
| 1090 | |||
| 1091 | =cut | ||
| 1092 | |||
| 1093 | sub _AllenOverlaps | ||
| 1094 | { | ||
| 1095 | my ($self, $other) = @_; | ||
| 1096 | |||
| 1097 | my ($bHowOverlaps) = $FALSE; | ||
| 1098 | |||
| 1099 | if ($self->{m_bEmpty} || $other->{m_bEmpty}) { return $FALSE; } | ||
| 1100 | |||
| 1101 | my $start1 = _to_date ($FALSE, $self->{m_bStart}, $self->{m_nStart}); | ||
| 1102 | my $stop1 = _to_date ($TRUE, $self->{m_bStop}, $self->{m_nStop}, | ||
| 1103 | $self->{m_bStart}, $start1); | ||
| 1104 | my $start2 = _to_date ($TRUE, $other->{m_bStart}, $other->{m_nStart}); | ||
| 1105 | my $stop2 = _to_date ($TRUE, $other->{m_bStop}, $other->{m_nStop}, | ||
| 1106 | $other->{m_bStart}, $start2); | ||
| 1107 | |||
| 1108 | # before/meets/left overlaps/left covers/covers | ||
| 1109 | # (note the order is important) | ||
| 1110 | if ($start1 lt $start2) | ||
| 1111 | { | ||
| 1112 | if ($stop1 lt $start2) { $bHowOverlaps = $ALLEN_BEFORE; } | ||
| 1113 | elsif ($stop1 eq $start2) { $bHowOverlaps = $ALLEN_MEETS; } | ||
| 1114 | elsif ($stop1 lt $stop2) { $bHowOverlaps = $ALLEN_LEFT_OVERLAPS; } | ||
| 1115 | elsif ($stop1 eq $stop2) { $bHowOverlaps = $ALLEN_LEFT_COVERS; } | ||
| 1116 | elsif ($stop1 gt $stop2) { $bHowOverlaps = $ALLEN_COVERS; } | ||
| 1117 | else {} # do nothing | ||
| 1118 | } | ||
| 1119 | # starts/equals/right covers | ||
| 1120 | elsif ($start1 eq $start2) | ||
| 1121 | { | ||
| 1122 | if ($stop1 lt $stop2) { $bHowOverlaps = $ALLEN_STARTS; } | ||
| 1123 | elsif ($stop1 eq $stop2) { $bHowOverlaps = $ALLEN_EQUALS; } | ||
| 1124 | elsif ($stop1 gt $stop2) { $bHowOverlaps = $ALLEN_RIGHT_COVERS; } | ||
| 1125 | else {} # do nothing | ||
| 1126 | } | ||
| 1127 | # extends/after/during/finishes/right overlaps (note the order is important) | ||
| 1128 | elsif ($start1 gt $start2) | ||
| 1129 | { | ||
| 1130 | if ($start1 eq $stop2) { $bHowOverlaps = $ALLEN_EXTENDS; } | ||
| 1131 | elsif ($start1 gt $stop2) { $bHowOverlaps = $ALLEN_AFTER; } | ||
| 1132 | elsif ($stop1 lt $stop2) { $bHowOverlaps = $ALLEN_DURING; } | ||
| 1133 | elsif ($stop1 eq $stop2) { $bHowOverlaps = $ALLEN_FINISHES; } | ||
| 1134 | elsif ($stop1 gt $stop2) { $bHowOverlaps = $ALLEN_RIGHT_OVERLAPS; } | ||
| 1135 | else {} # do nothing | ||
| 1136 | } | ||
| 1137 | return $bHowOverlaps; | ||
| 1138 | } | ||
| 1139 | |||
| 1140 | ################################################################################ | ||
| 1141 | # Overloaded Operators | ||
| 1142 | ################################################################################ | ||
| 1143 | |||
| 1144 | =head1 Overloaded Operators | ||
| 1145 | |||
| 1146 | =head2 _plus | ||
| 1147 | |||
| 1148 | Description: + operator. If two intervals overlaps the union is returned | ||
| 1149 | Input: <interval> <interval> | ||
| 1150 | Output: <interval> || undefined | ||
| 1151 | |||
| 1152 | =cut | ||
| 1153 | |||
| 1154 | sub _plus | ||
| 1155 | { | ||
| 1156 | my ($i1, $i2, $regular) = @_; | ||
| 1157 | my ($nMin, $nMax); | ||
| 1158 | $nMin = $nMax = 0; | ||
| 1159 | if ($i2->{m_bEmpty}) | ||
| 1160 | { | ||
| 1161 | return (ref $i1)->new ($i1->{m_nStart}, $i1->{m_nStop}); | ||
| 1162 | } | ||
| 1163 | if ($i1->{m_bEmpty}) | ||
| 1164 | { | ||
| 1165 | return (ref $i2)->new ($i2->{m_nStart}, $i2->{m_nStop}); | ||
| 1166 | } | ||
| 1167 | |||
| 1168 | if ($i1->{m_bStart} == $RELATIVE || $i1->{m_bStop} == $RELATIVE || | ||
| 1169 | $i2->{m_bStart} == $RELATIVE || $i2->{m_bStop} == $RELATIVE) | ||
| 1170 | { | ||
| 1171 | print STDERR "Sorry, + of relative intervals not implemented yet\n"; | ||
| 1172 | return _new_empty Date::Interval; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | if ($i1->overlaps ($i2)) | ||
| 1176 | { | ||
| 1177 | $nMin = $i1->{m_nStart} lt $i2->{m_nStart} ? | ||
| 1178 | $i1->{m_nStart} : $i2->{m_nStart}; | ||
| 1179 | $nMax = $i1->{m_nStop} gt $i2->{m_nStop} ? | ||
| 1180 | $i1->{m_nStop} : $i2->{m_nStop}; | ||
| 1181 | } | ||
| 1182 | return (ref $i1)->new ($nMin, $nMax); | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | =head2 _minus | ||
| 1186 | |||
| 1187 | Description: - operator. | ||
| 1188 | If two intervals overlaps the intersection is returned | ||
| 1189 | Input: <interval> <interval> | ||
| 1190 | Output: <interval> [ <interval> ] || undefined | ||
| 1191 | |||
| 1192 | =cut | ||
| 1193 | |||
| 1194 | sub _minus | ||
| 1195 | { | ||
| 1196 | my ($i1, $i2, $regular) = @_; | ||
| 1197 | my ($nStart1, $nStop1, $nLeft1, $nRight1); | ||
| 1198 | my ($nStart2, $nStop2, $nLeft2, $nRight2); | ||
| 1199 | |||
| 1200 | # | ||
| 1201 | if ($i2->{m_bEmpty}) | ||
| 1202 | { | ||
| 1203 | return (ref $i1)->new ($i1->{m_nStart}, $i1->{m_nStop}); | ||
| 1204 | } | ||
| 1205 | if ($i1->{m_bEmpty}) { return _new_empty Date::Interval; } | ||
| 1206 | |||
| 1207 | # Handle relative intervals | ||
| 1208 | if ($i1->{m_bStart} == $RELATIVE || $i1->{m_bStop} == $RELATIVE || | ||
| 1209 | $i2->{m_bStart} == $RELATIVE || $i2->{m_bStop} == $RELATIVE) | ||
| 1210 | { | ||
| 1211 | print STDERR "Sorry, + of relative intervals not implemented yet\n"; | ||
| 1212 | return _new_empty Date::Interval; | ||
| 1213 | } | ||
| 1214 | |||
| 1215 | my ($nOverlap) = $i1->_overlaps ($i2); | ||
| 1216 | $nStart1 = $nStop1 = 0; | ||
| 1217 | my ($bDefined) = $TRUE; # Used if temporal element should be returned | ||
| 1218 | |||
| 1219 | |||
| 1220 | if ($nOverlap == $MEETS) | ||
| 1221 | { | ||
| 1222 | $nStart1 = $i1->{m_nStart}; | ||
| 1223 | $nStop1 = $i1->{m_nStop}; | ||
| 1224 | $nLeft1 = $i1->{m_nLeft}; | ||
| 1225 | if ($i2->{m_nLeft} == $CLOSED) { $nRight1 = $OPEN; } | ||
| 1226 | else { $nRight1 = $i1->{m_nRight}; } | ||
| 1227 | } | ||
| 1228 | elsif ($nOverlap == $LEFT_OVERLAPS) | ||
| 1229 | { | ||
| 1230 | $nStart1 = $i1->{m_nStart}; | ||
| 1231 | $nStop1 = $i2->{m_nStart}; | ||
| 1232 | $nLeft1 = $i1->{m_nLeft}; | ||
| 1233 | if ($i2->{m_nLeft} == $CLOSED) { $nRight1 = $OPEN; } | ||
| 1234 | else { $nRight1 = $CLOSED; } | ||
| 1235 | } | ||
| 1236 | elsif ($nOverlap == $RIGHT_OVERLAPS) | ||
| 1237 | { | ||
| 1238 | $nStart1 = $i2->{m_nStop}; | ||
| 1239 | $nStop1 = $i1->{m_nStop}; | ||
| 1240 | if ($i2->{m_nRight} == $CLOSED) { $nLeft1 = $OPEN; } | ||
| 1241 | else { $nLeft1 = $CLOSED; } | ||
| 1242 | $nRight1 = $i1->{m_nRight}; | ||
| 1243 | } | ||
| 1244 | elsif ($nOverlap == $TOTALLY_OVERLAPS) | ||
| 1245 | { | ||
| 1246 | # First <interval> | ||
| 1247 | $nStart1 = $i1->{m_nStart}; | ||
| 1248 | $nStop1 = $i2->{m_nStart}; | ||
| 1249 | $nLeft1 = $i1->{m_nLeft}; | ||
| 1250 | if ($i2->{m_nLeft} == $CLOSED) { $nRight1 = $OPEN; } | ||
| 1251 | else { $nRight1 = $CLOSED; } | ||
| 1252 | # Second <interval> | ||
| 1253 | $nStart2 = $i2->{m_nStop}; | ||
| 1254 | $nStop2 = $i1->{m_nStop}; | ||
| 1255 | if ($i2->{m_nRight} == $CLOSED) { $nLeft2 = $OPEN; } | ||
| 1256 | else { $nLeft2 = $CLOSED; } | ||
| 1257 | $nRight2 = $i1->{m_nRight}; | ||
| 1258 | } | ||
| 1259 | elsif ($nOverlap == $DURING) | ||
| 1260 | { | ||
| 1261 | $bDefined = $FALSE; | ||
| 1262 | } | ||
| 1263 | elsif ($nOverlap == $EXTENDS) | ||
| 1264 | { | ||
| 1265 | $nStart1 = $i1->{m_nStart}; | ||
| 1266 | $nStop1 = $i1->{m_nStop}; | ||
| 1267 | if ($i2->{m_nRight} == $CLOSED) { $nLeft1 = $OPEN; } | ||
| 1268 | else { $nLeft1 = $i1->{m_nLeft}; } | ||
| 1269 | $nRight1 = $i1->{m_nRight}; | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | else | ||
| 1273 | { | ||
| 1274 | # The intervals do not overlap | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | # Build the returned intervals | ||
| 1278 | if ($bDefined) | ||
| 1279 | { | ||
| 1280 | my $int1 = new Date::Interval ($nStart1, $nStop1); | ||
| 1281 | $int1->_setBrackets ($nLeft1, $nRight1); | ||
| 1282 | if ($nOverlap == $TOTALLY_OVERLAPS) | ||
| 1283 | { | ||
| 1284 | my $int2 = new Date::Interval ($nStart2, $nStop2); | ||
| 1285 | $int2->_setBrackets ($nLeft2, $nRight2); | ||
| 1286 | return wantarray ? ($int1, $int2) : $int1; | ||
| 1287 | } | ||
| 1288 | else | ||
| 1289 | { | ||
| 1290 | return $int1; | ||
| 1291 | } | ||
| 1292 | } | ||
| 1293 | else | ||
| 1294 | { | ||
| 1295 | return _new_empty Date::Interval; | ||
| 1296 | } | ||
| 1297 | } | ||
| 1298 | |||
| 1299 | =head2 _smaller_than | ||
| 1300 | |||
| 1301 | Description: < operator. | ||
| 1302 | Input: <interval> <interval> | ||
| 1303 | Output: <boolean> | ||
| 1304 | |||
| 1305 | =cut | ||
| 1306 | |||
| 1307 | sub _smaller_than | ||
| 1308 | { | ||
| 1309 | my ($i1, $i2) = @_; | ||
| 1310 | if ($i1->{m_bEmpty} || $i2->{m_bEmpty}) { return $FALSE; } | ||
| 1311 | |||
| 1312 | my $start1 = _to_date ($FALSE, $i1->{m_bStart}, $i1->{m_nStart}); | ||
| 1313 | my $stop1 = _to_date ($TRUE, $i1->{m_bStop}, $i1->{m_nStop}, | ||
| 1314 | $i1->{m_bStart}, $start1); | ||
| 1315 | my $start2 = _to_date ($TRUE, $i2->{m_bStart}, $i2->{m_nStart}); | ||
| 1316 | my $stop2 = _to_date ($TRUE, $i2->{m_bStop}, $i2->{m_nStop}, | ||
| 1317 | $i2->{m_bStart}, $start2); | ||
| 1318 | |||
| 1319 | if ($stop1 lt $start2) { return $TRUE; } | ||
| 1320 | else { return $FALSE; } | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | =head2 _greater_than | ||
| 1324 | |||
| 1325 | Description: > operator. | ||
| 1326 | Input: <interval> <interval> | ||
| 1327 | Output: <boolean> | ||
| 1328 | |||
| 1329 | =cut | ||
| 1330 | |||
| 1331 | sub _greater_than | ||
| 1332 | { | ||
| 1333 | my ($i1, $i2) = @_; | ||
| 1334 | if ($i1->{m_bEmpty} || $i2->{m_bEmpty}) { return $FALSE; } | ||
| 1335 | |||
| 1336 | my $start1 = _to_date ($FALSE, $i1->{m_bStart}, $i1->{m_nStart}); | ||
| 1337 | my $stop1 = _to_date ($TRUE, $i1->{m_bStop}, $i1->{m_nStop}, | ||
| 1338 | $i1->{m_bStart}, $start1); | ||
| 1339 | my $start2 = _to_date ($TRUE, $i2->{m_bStart}, $i2->{m_nStart}); | ||
| 1340 | my $stop2 = _to_date ($TRUE, $i2->{m_bStop}, $i2->{m_nStop}, | ||
| 1341 | $i2->{m_bStart}, $start2); | ||
| 1342 | |||
| 1343 | if ($start1 gt $stop2) { return $TRUE; } | ||
| 1344 | else { return $FALSE; } | ||
| 1345 | } | ||
| 1346 | |||
| 1347 | =head2 _equal | ||
| 1348 | |||
| 1349 | Description: == operator. | ||
| 1350 | Input: <interval> <interval> | ||
| 1351 | Output: <boolean> | ||
| 1352 | |||
| 1353 | =cut | ||
| 1354 | |||
| 1355 | sub _equal | ||
| 1356 | { | ||
| 1357 | my ($i1, $i2) = @_; | ||
| 1358 | if ($i1->{m_bEmpty} || $i2->{m_bEmpty}) { return $FALSE; } | ||
| 1359 | |||
| 1360 | my $start1 = _to_date ($FALSE, $i1->{m_bStart}, $i1->{m_nStart}); | ||
| 1361 | my $stop1 = _to_date ($TRUE, $i1->{m_bStop}, $i1->{m_nStop}, | ||
| 1362 | $i1->{m_bStart}, $start1); | ||
| 1363 | my $start2 = _to_date ($TRUE, $i2->{m_bStart}, $i2->{m_nStart}); | ||
| 1364 | my $stop2 = _to_date ($TRUE, $i2->{m_bStop}, $i2->{m_nStop}, | ||
| 1365 | $i2->{m_bStart}, $start2); | ||
| 1366 | |||
| 1367 | if ($start1 eq $start2 && $stop1 eq $stop2) { return $TRUE; } | ||
| 1368 | else { return $FALSE; } | ||
| 1369 | } | ||
| 1370 | |||
| 1371 | =head2 _equal | ||
| 1372 | |||
| 1373 | Description: != operator. | ||
| 1374 | Input: <interval> <interval> | ||
| 1375 | Output: <boolean> | ||
| 1376 | |||
| 1377 | =cut | ||
| 1378 | |||
| 1379 | sub _not_equal | ||
| 1380 | { | ||
| 1381 | my ($i1, $i2) = @_; | ||
| 1382 | if ($i1->{m_bEmpty} || $i2->{m_bEmpty}) { return $FALSE; } | ||
| 1383 | |||
| 1384 | my $start1 = _to_date ($FALSE, $i1->{m_bStart}, $i1->{m_nStart}); | ||
| 1385 | my $stop1 = _to_date ($TRUE, $i1->{m_bStop}, $i1->{m_nStop}, | ||
| 1386 | $i1->{m_bStart}, $start1); | ||
| 1387 | my $start2 = _to_date ($TRUE, $i2->{m_bStart}, $i2->{m_nStart}); | ||
| 1388 | my $stop2 = _to_date ($TRUE, $i2->{m_bStop}, $i2->{m_nStop}, | ||
| 1389 | $i2->{m_bStart}, $start2); | ||
| 1390 | |||
| 1391 | if ($i1 == $i2) { return $FALSE; } | ||
| 1392 | else { return $TRUE; } | ||
| 1393 | } | ||
| 1394 | |||
| 1395 | =head2 _spaceship | ||
| 1396 | |||
| 1397 | Description: <=> operator. | ||
| 1398 | Use ONLY for sorting because based on the start value | ||
| 1399 | Input: <interval> <interval> | ||
| 1400 | Output: -1 || 0 || 1 | ||
| 1401 | |||
| 1402 | =cut | ||
| 1403 | |||
| 1404 | sub _spaceship | ||
| 1405 | { | ||
| 1406 | my ($i1, $i2) = @_; | ||
| 1407 | |||
| 1408 | my $start1 = _to_date ($FALSE, $i1->{m_bStart}, $i1->{m_nStart}); | ||
| 1409 | my $stop1 = _to_date ($TRUE, $i1->{m_bStop}, $i1->{m_nStop}, | ||
| 1410 | $i1->{m_bStart}, $start1); | ||
| 1411 | my $start2 = _to_date ($TRUE, $i2->{m_bStart}, $i2->{m_nStart}); | ||
| 1412 | my $stop2 = _to_date ($TRUE, $i2->{m_bStop}, $i2->{m_nStop}, | ||
| 1413 | $i2->{m_bStart}, $start2); | ||
| 1414 | |||
| 1415 | if ($i1->{m_bEmpty}) { return -1; } # per definition :-) | ||
| 1416 | elsif ($i2->{m_bEmpty}) { return 1; } # ditto | ||
| 1417 | elsif ($start1 eq $start2) { return 0; } | ||
| 1418 | elsif ($start1 lt $start2) { return -1; } | ||
| 1419 | elsif ($start1 gt $start2) { return 1; } | ||
| 1420 | else { print STDERR "Error in spaceship\n"; } | ||
| 1421 | } | ||
| 1422 | |||
| 1423 | =head2 _stringify | ||
| 1424 | |||
| 1425 | Description: For strinifying an interval | ||
| 1426 | Input: <interval> | ||
| 1427 | Output: string | ||
| 1428 | |||
| 1429 | =cut | ||
| 1430 | |||
| 1431 | sub _stringify | ||
| 1432 | { | ||
| 1433 | my $self = shift; | ||
| 1434 | return $self->get; | ||
| 1435 | } | ||
| 1436 | |||
| 1437 | ############################################################################## | ||
| 1438 | # Various help functions | ||
| 1439 | ############################################################################## | ||
| 1440 | |||
| 1441 | =head2 _getEndPoint | ||
| 1442 | |||
| 1443 | Description: Converts a string to an <end point> | ||
| 1444 | Input: string | ||
| 1445 | Output: <end point> <value type> | ||
| 1446 | |||
| 1447 | =cut | ||
| 1448 | |||
| 1449 | sub _getEndPoint | ||
| 1450 | { | ||
| 1451 | my ($szEndPoint) = @_; | ||
| 1452 | |||
| 1453 | # Is it a reference to a date? | ||
| 1454 | if (ref $szEndPoint) | ||
| 1455 | { | ||
| 1456 | return ($szEndPoint, $ABSOLUTE); | ||
| 1457 | } | ||
| 1458 | # Is it a NOBIND and a NOW or a delta | ||
| 1459 | elsif ($szEndPoint =~ /^(\s*)NOBIND(\s*)(.*)/) | ||
| 1460 | { | ||
| 1461 | my $szRest = $3; | ||
| 1462 | if ($szRest =~ /NOW/) | ||
| 1463 | { | ||
| 1464 | return ('NOBIND NOW', $RELATIVE); | ||
| 1465 | } | ||
| 1466 | else | ||
| 1467 | { | ||
| 1468 | # Can we parse up the delta? | ||
| 1469 | my $delta = &ParseDateDelta ($szRest); | ||
| 1470 | if (defined ($delta)) | ||
| 1471 | { | ||
| 1472 | return ($szRest, $RELATIVE); | ||
| 1473 | } | ||
| 1474 | } | ||
| 1475 | } | ||
| 1476 | # Is it a string specifying a date | ||
| 1477 | else | ||
| 1478 | { | ||
| 1479 | my $date = &ParseDate ($szEndPoint); | ||
| 1480 | if (defined ($date)) | ||
| 1481 | { | ||
| 1482 | return ($date, $ABSOLUTE); | ||
| 1483 | } | ||
| 1484 | } | ||
| 1485 | return ('', ''); # An error | ||
| 1486 | } | ||
| 1487 | |||
| 1488 | =head2 _to_date | ||
| 1489 | |||
| 1490 | Description: Converts <end point> to a date | ||
| 1491 | Input: <fix clock> <value type> <end point> [<start date value type> | ||
| 1492 | <start date>] | ||
| 1493 | Output: <date> | ||
| 1494 | |||
| 1495 | =cut | ||
| 1496 | |||
| 1497 | sub _to_date | ||
| 1498 | { | ||
| 1499 | my ($bFixClock, $nValueType, $szEndPoint, $nStartValueType, $dStartDate) = @_; | ||
| 1500 | my ($dDate); | ||
| 1501 | |||
| 1502 | if($nValueType == $RELATIVE) | ||
| 1503 | { | ||
| 1504 | if ($szEndPoint eq 'NOBIND NOW') # NOW | ||
| 1505 | { | ||
| 1506 | $dDate = &_getCurrentTime ($bFixClock); | ||
| 1507 | } | ||
| 1508 | else # a delta | ||
| 1509 | { | ||
| 1510 | # use dStartDate as outset for delta | ||
| 1511 | if (defined($dStartDate) && $nStartValueType == $ABSOLUTE) | ||
| 1512 | { | ||
| 1513 | $dDate = &DateCalc ($dStartDate, $szEndPoint); | ||
| 1514 | } | ||
| 1515 | else # use NOW as outset for delta | ||
| 1516 | { | ||
| 1517 | $dDate = &DateCalc (&_getCurrentTime ($bFixClock), $szEndPoint); | ||
| 1518 | } | ||
| 1519 | } | ||
| 1520 | } | ||
| 1521 | elsif ($nValueType == $ABSOLUTE) | ||
| 1522 | { | ||
| 1523 | $dDate = $szEndPoint; | ||
| 1524 | } | ||
| 1525 | else | ||
| 1526 | { | ||
| 1527 | print STDERR "ERROR: Wrong <value type> $nValueType\n"; | ||
| 1528 | } | ||
| 1529 | return $dDate; | ||
| 1530 | } | ||
| 1531 | |||
| 1532 | =head2 _getCurrentTime | ||
| 1533 | |||
| 1534 | Description: Converts an <end point> to a string | ||
| 1535 | Input: <boolean> | ||
| 1536 | Output: string | ||
| 1537 | |||
| 1538 | =cut | ||
| 1539 | |||
| 1540 | sub _getCurrentTime | ||
| 1541 | { | ||
| 1542 | my($bFixClock) = @_; | ||
| 1543 | if (!$bFixClock) { $Now = &ParseDate ('today'); } | ||
| 1544 | return $Now; | ||
| 1545 | } | ||
| 1546 | |||
| 1547 | =head2 _to_string | ||
| 1548 | |||
| 1549 | Description: Converts an <end point> to a string | ||
| 1550 | Input: <value type> <end point> | ||
| 1551 | Output: string | ||
| 1552 | |||
| 1553 | =cut | ||
| 1554 | |||
| 1555 | sub _to_string | ||
| 1556 | { | ||
| 1557 | my ($nValueType, $szEndPoint) = @_; | ||
| 1558 | my ($szResult); | ||
| 1559 | |||
| 1560 | if ($nValueType == $ABSOLUTE) | ||
| 1561 | { | ||
| 1562 | $szResult = &UnixDate ($szEndPoint, $DisplayFormat); | ||
| 1563 | } | ||
| 1564 | elsif ($nValueType == $RELATIVE) | ||
| 1565 | { | ||
| 1566 | if ($szEndPoint eq 'NOBIND NOW') { $szResult = 'NOBIND NOW'; } | ||
| 1567 | else { $szResult = $szEndPoint; } | ||
| 1568 | } | ||
| 1569 | else | ||
| 1570 | { | ||
| 1571 | print STDERR "ERROR wrong <value type> $nValueType in _to_string\n"; | ||
| 1572 | } | ||
| 1573 | return $szResult; | ||
| 1574 | } | ||
| 1575 | |||
| 1576 | 1; | ||
| 1577 | |||
| 1578 | __END__ | ||