/usr/share/perl5/DateTime/Span.pm is in libdatetime-set-perl 0.31-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | # Copyright (c) 2003 Flavio Soibelmann Glock. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
package DateTime::Span;
use strict;
use DateTime::Set;
use DateTime::SpanSet;
use Params::Validate qw( validate SCALAR BOOLEAN OBJECT CODEREF ARRAYREF );
use vars qw( $VERSION );
use constant INFINITY => DateTime::INFINITY;
use constant NEG_INFINITY => DateTime::NEG_INFINITY;
$VERSION = $DateTime::Set::VERSION;
sub set_time_zone {
my ( $self, $tz ) = @_;
$self->{set} = $self->{set}->iterate(
sub {
my %tmp = %{ $_[0]->{list}[0] };
$tmp{a} = $tmp{a}->clone->set_time_zone( $tz ) if ref $tmp{a};
$tmp{b} = $tmp{b}->clone->set_time_zone( $tz ) if ref $tmp{b};
\%tmp;
}
);
return $self;
}
# note: the constructor must clone its DateTime parameters, such that
# the set elements become immutable
sub from_datetimes {
my $class = shift;
my %args = validate( @_,
{ start =>
{ type => OBJECT,
optional => 1,
},
end =>
{ type => OBJECT,
optional => 1,
},
after =>
{ type => OBJECT,
optional => 1,
},
before =>
{ type => OBJECT,
optional => 1,
},
}
);
my $self = {};
my $set;
die "No arguments given to DateTime::Span->from_datetimes\n"
unless keys %args;
if ( exists $args{start} && exists $args{after} ) {
die "Cannot give both start and after arguments to DateTime::Span->from_datetimes\n";
}
if ( exists $args{end} && exists $args{before} ) {
die "Cannot give both end and before arguments to DateTime::Span->from_datetimes\n";
}
my ( $start, $open_start, $end, $open_end );
( $start, $open_start ) = ( NEG_INFINITY, 0 );
( $start, $open_start ) = ( $args{start}, 0 ) if exists $args{start};
( $start, $open_start ) = ( $args{after}, 1 ) if exists $args{after};
( $end, $open_end ) = ( INFINITY, 0 );
( $end, $open_end ) = ( $args{end}, 0 ) if exists $args{end};
( $end, $open_end ) = ( $args{before}, 1 ) if exists $args{before};
if ( $start > $end ) {
die "Span cannot start after the end in DateTime::Span->from_datetimes\n";
}
$set = Set::Infinite::_recurrence->new( $start, $end );
if ( $start != $end ) {
# remove start, such that we have ">" instead of ">="
$set = $set->complement( $start ) if $open_start;
# remove end, such that we have "<" instead of "<="
$set = $set->complement( $end ) if $open_end;
}
$self->{set} = $set;
bless $self, $class;
return $self;
}
sub from_datetime_and_duration {
my $class = shift;
my %args = @_;
my $key;
my $dt;
# extract datetime parameters
for ( qw( start end before after ) ) {
if ( exists $args{$_} ) {
$key = $_;
$dt = delete $args{$_};
}
}
# extract duration parameters
my $dt_duration;
if ( exists $args{duration} ) {
$dt_duration = $args{duration};
}
else {
$dt_duration = DateTime::Duration->new( %args );
}
# warn "Creating span from $key => ".$dt->datetime." and $dt_duration";
my $other_date = $dt->clone->add_duration( $dt_duration );
# warn "Creating span from $key => ".$dt->datetime." and ".$other_date->datetime;
my $other_key;
if ( $dt_duration->is_positive ) {
# check if have to invert keys
$key = 'after' if $key eq 'end';
$key = 'start' if $key eq 'before';
$other_key = 'before';
}
else {
# check if have to invert keys
$other_key = 'end' if $key eq 'after';
$other_key = 'before' if $key eq 'start';
$key = 'start';
}
return $class->new( $key => $dt, $other_key => $other_date );
}
# This method is intentionally not documented. It's really only for
# use by ::Set and ::SpanSet's as_list() and iterator() methods.
sub new {
my $class = shift;
my %args = @_;
# If we find anything _not_ appropriate for from_datetimes, we
# assume it must be for durations, and call this constructor.
# This way, we don't need to hardcode the DateTime::Duration
# parameters.
foreach ( keys %args )
{
return $class->from_datetime_and_duration(%args)
unless /^(?:before|after|start|end)$/;
}
return $class->from_datetimes(%args);
}
sub is_empty_set {
my $set = $_[0];
$set->{set}->is_null;
}
sub clone {
bless {
set => $_[0]->{set}->copy,
}, ref $_[0];
}
# Set::Infinite methods
sub intersection {
my ($set1, $set2) = @_;
my $class = ref($set1);
my $tmp = {}; # $class->new();
$set2 = $set2->as_spanset
if $set2->can( 'as_spanset' );
$set2 = $set2->as_set
if $set2->can( 'as_set' );
$set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
unless $set2->can( 'union' );
$tmp->{set} = $set1->{set}->intersection( $set2->{set} );
# intersection() can generate something more complex than a span.
bless $tmp, 'DateTime::SpanSet';
return $tmp;
}
sub intersects {
my ($set1, $set2) = @_;
my $class = ref($set1);
$set2 = $set2->as_spanset
if $set2->can( 'as_spanset' );
$set2 = $set2->as_set
if $set2->can( 'as_set' );
$set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
unless $set2->can( 'union' );
return $set1->{set}->intersects( $set2->{set} );
}
sub contains {
my ($set1, $set2) = @_;
my $class = ref($set1);
$set2 = $set2->as_spanset
if $set2->can( 'as_spanset' );
$set2 = $set2->as_set
if $set2->can( 'as_set' );
$set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
unless $set2->can( 'union' );
return $set1->{set}->contains( $set2->{set} );
}
sub union {
my ($set1, $set2) = @_;
my $class = ref($set1);
my $tmp = {}; # $class->new();
$set2 = $set2->as_spanset
if $set2->can( 'as_spanset' );
$set2 = $set2->as_set
if $set2->can( 'as_set' );
$set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
unless $set2->can( 'union' );
$tmp->{set} = $set1->{set}->union( $set2->{set} );
# union() can generate something more complex than a span.
bless $tmp, 'DateTime::SpanSet';
# # We have to check it's internal structure to find out.
# if ( $#{ $tmp->{set}->{list} } != 0 ) {
# bless $tmp, 'Date::SpanSet';
# }
return $tmp;
}
sub complement {
my ($set1, $set2) = @_;
my $class = ref($set1);
my $tmp = {}; # $class->new;
if (defined $set2) {
$set2 = $set2->as_spanset
if $set2->can( 'as_spanset' );
$set2 = $set2->as_set
if $set2->can( 'as_set' );
$set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
unless $set2->can( 'union' );
$tmp->{set} = $set1->{set}->complement( $set2->{set} );
}
else {
$tmp->{set} = $set1->{set}->complement;
}
# complement() can generate something more complex than a span.
bless $tmp, 'DateTime::SpanSet';
# # We have to check it's internal structure to find out.
# if ( $#{ $tmp->{set}->{list} } != 0 ) {
# bless $tmp, 'Date::SpanSet';
# }
return $tmp;
}
sub start {
return DateTime::Set::_fix_datetime( $_[0]->{set}->min );
}
*min = \&start;
sub end {
return DateTime::Set::_fix_datetime( $_[0]->{set}->max );
}
*max = \&end;
sub start_is_open {
# min_a returns info about the set boundary
my ($min, $open) = $_[0]->{set}->min_a;
return $open;
}
sub start_is_closed { $_[0]->start_is_open ? 0 : 1 }
sub end_is_open {
# max_a returns info about the set boundary
my ($max, $open) = $_[0]->{set}->max_a;
return $open;
}
sub end_is_closed { $_[0]->end_is_open ? 0 : 1 }
# span == $self
sub span { @_ }
sub duration {
my $dur;
local $@;
eval {
local $SIG{__DIE__}; # don't want to trap this (rt ticket 5434)
$dur = $_[0]->end->subtract_datetime_absolute( $_[0]->start )
};
return $dur if defined $dur;
return DateTime::Infinite::Future->new -
DateTime::Infinite::Past->new;
}
*size = \&duration;
1;
__END__
=head1 NAME
DateTime::Span - Datetime spans
=head1 SYNOPSIS
use DateTime;
use DateTime::Span;
$date1 = DateTime->new( year => 2002, month => 3, day => 11 );
$date2 = DateTime->new( year => 2003, month => 4, day => 12 );
$set2 = DateTime::Span->from_datetimes( start => $date1, end => $date2 );
# set2 = 2002-03-11 until 2003-04-12
$set = $set1->union( $set2 ); # like "OR", "insert", "both"
$set = $set1->complement( $set2 ); # like "delete", "remove"
$set = $set1->intersection( $set2 ); # like "AND", "while"
$set = $set1->complement; # like "NOT", "negate", "invert"
if ( $set1->intersects( $set2 ) ) { ... # like "touches", "interferes"
if ( $set1->contains( $set2 ) ) { ... # like "is-fully-inside"
# data extraction
$date = $set1->start; # first date of the span
$date = $set1->end; # last date of the span
=head1 DESCRIPTION
C<DateTime::Span> is a module for handling datetime spans, otherwise
known as ranges or periods ("from X to Y, inclusive of all datetimes
in between").
This is different from a C<DateTime::Set>, which is made of individual
datetime points as opposed to a range. There is also a module
C<DateTime::SpanSet> to handle sets of spans.
=head1 METHODS
=over 4
=item * from_datetimes
Creates a new span based on a starting and ending datetime.
A 'closed' span includes its end-dates:
$span = DateTime::Span->from_datetimes( start => $dt1, end => $dt2 );
An 'open' span does not include its end-dates:
$span = DateTime::Span->from_datetimes( after => $dt1, before => $dt2 );
A 'semi-open' span includes one of its end-dates:
$span = DateTime::Span->from_datetimes( start => $dt1, before => $dt2 );
$span = DateTime::Span->from_datetimes( after => $dt1, end => $dt2 );
A span might have just a beginning date, or just an ending date.
These spans end, or start, in an imaginary 'forever' date:
$span = DateTime::Span->from_datetimes( start => $dt1 );
$span = DateTime::Span->from_datetimes( end => $dt2 );
$span = DateTime::Span->from_datetimes( after => $dt1 );
$span = DateTime::Span->from_datetimes( before => $dt2 );
You cannot give both a "start" and "after" argument, nor can you give
both an "end" and "before" argument. Either of these conditions will
cause the C<from_datetimes()> method to die.
To summarize, a datetime passed as either "start" or "end" is included
in the span. A datetime passed as either "after" or "before" is
excluded from the span.
=item * from_datetime_and_duration
Creates a new span.
$span = DateTime::Span->from_datetime_and_duration(
start => $dt1, duration => $dt_dur1 );
$span = DateTime::Span->from_datetime_and_duration(
after => $dt1, hours => 12 );
The new "end of the set" is I<open> by default.
=item * clone
This object method returns a replica of the given object.
=item * set_time_zone( $tz )
This method accepts either a time zone object or a string that can be
passed as the "name" parameter to C<< DateTime::TimeZone->new() >>.
If the new time zone's offset is different from the old time zone,
then the I<local> time is adjusted accordingly.
If the old time zone was a floating time zone, then no adjustments to
the local time are made, except to account for leap seconds. If the
new time zone is floating, then the I<UTC> time is adjusted in order
to leave the local time untouched.
=item * duration
The total size of the set, as a C<DateTime::Duration> object, or as a
scalar containing infinity.
Also available as C<size()>.
=item * start
=item * end
First or last dates in the span.
It is possible that the return value from these methods may be a
C<DateTime::Infinite::Future> or a C<DateTime::Infinite::Past>xs object.
If the set ends C<before> a date C<$dt>, it returns C<$dt>. Note that
in this case C<$dt> is not a set element - but it is a set boundary.
=cut
# scalar containing either negative infinity
# or positive infinity.
=item * start_is_closed
=item * end_is_closed
Returns true if the first or last dates belong to the span ( begin <= x <= end ).
=item * start_is_open
=item * end_is_open
Returns true if the first or last dates are excluded from the span ( begin < x < end ).
=item * union
=item * intersection
=item * complement
Set operations may be performed not only with C<DateTime::Span>
objects, but also with C<DateTime::Set> and C<DateTime::SpanSet>
objects. These set operations always return a C<DateTime::SpanSet>
object.
$set = $span->union( $set2 ); # like "OR", "insert", "both"
$set = $span->complement( $set2 ); # like "delete", "remove"
$set = $span->intersection( $set2 ); # like "AND", "while"
$set = $span->complement; # like "NOT", "negate", "invert"
=item * intersects
=item * contains
These set functions return a boolean value.
if ( $span->intersects( $set2 ) ) { ... # like "touches", "interferes"
if ( $span->contains( $dt ) ) { ... # like "is-fully-inside"
These methods can accept a C<DateTime>, C<DateTime::Set>,
C<DateTime::Span>, or C<DateTime::SpanSet> object as an argument.
=back
=head1 SUPPORT
Support is offered through the C<datetime@perl.org> mailing list.
Please report bugs using rt.cpan.org
=head1 AUTHOR
Flavio Soibelmann Glock <fglock@gmail.com>
The API was developed together with Dave Rolsky and the DateTime Community.
=head1 COPYRIGHT
Copyright (c) 2003-2006 Flavio Soibelmann Glock. All rights reserved.
This program is free software; you can distribute it and/or modify it
under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file
included with this module.
=head1 SEE ALSO
Set::Infinite
For details on the Perl DateTime Suite project please see
L<http://datetime.perl.org>.
=cut
|