/usr/share/perl5/DateTime/Incomplete.pm is in libdatetime-incomplete-perl 0.08-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 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 | package DateTime::Incomplete;
use strict;
use DateTime::Set 0.0901;
use DateTime::Event::Recurrence;
use Params::Validate qw( validate );
use vars qw( $VERSION );
my $UNDEF_CHAR;
my ( @FIELDS, %FIELD_LENGTH, @TIME_FIELDS, @FIELDS_SORTED );
BEGIN
{
$VERSION = '0.08';
$UNDEF_CHAR = 'x';
@FIELDS = ( year => 0, month => 1, day => 1,
hour => 0, minute => 0, second => 0, nanosecond => 0 );
%FIELD_LENGTH = (
year => 4, month => 2, day => 2,
hour => 2, minute => 2, second => 2, nanosecond => 9,
time_zone => 0, locale => 0 );
@TIME_FIELDS = qw( hour minute second nanosecond );
@FIELDS_SORTED = qw( year month day
hour minute second nanosecond
time_zone locale );
# Generate named accessors
for my $field ( @FIELDS_SORTED )
{
no strict 'refs';
*{$field} = sub { $_[0]->_get($field) };
*{"has_$field"} = sub { $_[0]->_has($field) };
next if $field eq 'nanosecond';
my $length = $FIELD_LENGTH{$field};
next unless $length;
*{"_$field"} = sub { defined $_[0]->$field() ?
sprintf( "%0.${length}d", $_[0]->$field() ) :
$UNDEF_CHAR x $length };
}
# Generate DateTime read-only functions
for my $meth ( qw/
epoch
hires_epoch
is_dst
utc_rd_values
utc_rd_as_seconds
/ )
{
no strict 'refs';
*{$meth} = sub
{
# to_datetime() dies if there is no "base"
# we get 'undef' if this happens
eval { (shift)->to_datetime( @_ )->$meth() };
};
}
for my $meth ( qw/
week week_year week_number week_of_month
day_name day_abbr
day_of_week wday dow
day_of_year doy
quarter day_of_quarter doq
weekday_of_month
jd mjd
/ )
{
no strict 'refs';
*{$meth} = sub { $_[0]->_datetime_method( $meth, 'year', 'month', 'day' ) };
}
for my $meth ( qw/
is_leap_year ce_year era year_with_era
/ )
{
no strict 'refs';
*{$meth} = sub { $_[0]->_datetime_method( $meth, 'year' ) };
}
for my $meth ( qw/
month_name month_abbr
/ )
{
no strict 'refs';
*{$meth} = sub { $_[0]->_datetime_method( $meth, 'month' ) };
}
for my $meth ( qw/
hour_1 hour_12 hour_12_0
/ )
{
no strict 'refs';
*{$meth} = sub { $_[0]->_datetime_method( $meth, 'hour' ) };
}
for my $meth ( qw/
millisecond microsecond
/ )
{
no strict 'refs';
*{$meth} = sub { $_[0]->_datetime_method( $meth, 'nanosecond' ) };
}
}
*_nanosecond = \&_format_nanosecs;
*mon = \&month;
*day_of_month = \&day;
*mday = \&day;
*min = \&minute;
*sec = \&second;
# Internal sub to call "DateTime" methods
sub _datetime_method
{
my ( $self, $method ) = ( shift, shift );
my @fields = @_; # list of required fields
my $date;
for ( @fields )
{
return undef unless ( $self->_has($_) )
}
my %param;
# if we don't need 'year', then we can safely set it to whatever.
$param{year} = 1970 if ! @fields || $fields[0] ne 'year';
$param{locale} = $self->locale if $self->has_locale;
$param{time_zone} = $self->time_zone if $self->has_time_zone;
$param{$_} = $self->$_() for @fields;
$date = DateTime->new( %param );
return $date->$method();
}
# DATETIME-LIKE METHODS
sub fractional_second {
$_[0]->_datetime_method( 'fractional_second', 'second', 'nanosecond' );
}
sub offset {
$_[0]->_datetime_method( 'offset' );
}
sub time_zone_short_name {
$_[0]->_datetime_method( 'time_zone_short_name' );
}
sub time_zone_long_name {
$_[0]->_datetime_method( 'time_zone_long_name' );
}
sub _from_datetime
{
my $class = shift;
my $dt = shift;
my %param;
$param{$_} = $dt->$_() for @FIELDS_SORTED;
return $class->new( %param );
}
sub last_day_of_month {
my $self = shift;
my %param = @_;
my $result = $self->_from_datetime( DateTime->last_day_of_month( @_ ) );
for ( @TIME_FIELDS ) {
$result->set( $_, undef ) unless defined $param{$_};
}
return $result;
}
sub from_epoch {
return (shift)->_from_datetime( DateTime->from_epoch( @_ ) );
}
sub now {
return (shift)->_from_datetime( DateTime->now( @_ ) );
}
sub from_object {
return (shift)->_from_datetime( DateTime->from_object( @_ ) );
}
sub from_day_of_year {
my $self = shift;
my %param = @_;
my $result = $self->_from_datetime( DateTime->from_day_of_year( @_ ) );
for ( @TIME_FIELDS ) {
$result->set( $_, undef ) unless defined $param{$_};
}
return $result;
}
sub today
{
my $class = shift;
my $now = DateTime->now( @_ );
my %param;
my %fields = ( %FIELD_LENGTH );
delete $fields{$_} for ( qw/ hour minute second nanosecond / );
$param{$_} = $now->$_() for ( keys %fields );
return $class->new( %param );
}
sub new
{
# parameter checking is done in "set" method.
my $class = shift;
my %param = @_;
my $base = delete $param{base};
die "base must be a datetime" if defined $base &&
! UNIVERSAL::can( $base, 'utc_rd_values' );
my $self = bless {
has => \%param,
}, $class;
$self->set_base( $base );
$self->set( locale => $self->{has}{locale} ) if $self->{has}{locale};
$self->set_time_zone( $self->{has}{time_zone} ) if $self->{has}{time_zone};
return $self;
}
sub set_base
{
my $self = shift;
$self->{base} = shift;
if ( defined $self->{base} )
{
my ($key, $value);
while (($key, $value) = each %{$self->{has}} ) {
next unless defined $value;
if ( $key eq 'time_zone' )
{
$self->{base}->set_time_zone( $value );
next;
}
$self->{base}->set( $key => $value );
}
}
}
sub base
{
return undef unless defined $_[0]->{base};
$_[0]->{base}->clone;
}
sub has_base
{
return defined $_[0]->{base} ? 1 : 0;
}
sub set
{
my $self = shift;
my %p = @_;
while ( my ( $k, $v ) = each %p )
{
if ( $k eq 'locale' )
{
$self->_set_locale($v);
next;
}
$self->{base}->set( $k => $v ) if $self->{base} && defined $v;
$self->{has}{ $k } = $v;
}
}
sub _get
{
$_[0]->{has}{$_[1]};
}
sub _has
{
defined $_[0]->{has}{$_[1]} ? 1 : 0;
}
sub has {
# returns true or false
my $self = shift;
foreach (@_) {
return 0 unless $self->_has( $_ )
}
return 1
}
sub has_date {
$_[0]->has_year && $_[0]->has_month && $_[0]->has_day
}
sub has_time {
$_[0]->has_hour && $_[0]->has_minute && $_[0]->has_second
}
sub defined_fields {
# no params, returns a list of fields
my $self = shift;
my @has = ();
for ( @FIELDS_SORTED )
{
push @has, $_ if $self->_has( $_ );
}
return @has;
}
sub can_be_datetime {
my $self = shift;
return 0 if ! $self->has_year;
my $can = 1;
for ( qw( month day hour minute second nanosecond ) )
{
return 0 if ! $can && $self->_has( $_ );
$can = 0 if $can && ! $self->_has( $_ );
}
return 1;
}
#sub become_datetime {
# my $self = shift;
# return undef unless $self->has_year;
# # warn "param = @{[ %{$self->{has}} ]} ";
# # return DateTime->new( %{$self->{has}} );
# my @parm = map { ( $_, $self->$_() ) } $self->defined_fields;
# # warn "param = @parm";
# return DateTime->new( @parm );
#}
sub set_time_zone
{
die "set_time_zone() requires a time_zone value" unless $#_ == 1;
my $time_zone = $_[1];
if ( defined $time_zone )
{
$time_zone = DateTime::TimeZone->new( name => $time_zone ) unless ref $time_zone;
$_[0]->{base}->set_time_zone( $time_zone ) if defined $_[0]->{base};
}
$_[0]->{has}{time_zone} = $time_zone;
}
sub _set_locale
{
die "set_locale() requires a locale value" unless $#_ == 1;
my $locale = $_[1];
if ( defined $locale )
{
$locale = DateTime::Locale->load( $locale ) unless ref $locale;
$_[0]->{base}->set( locale => $locale ) if defined $_[0]->{base};
}
$_[0]->{has}{locale} = $locale;
}
sub clone
{
my $base;
$base = $_[0]->{base}->clone if defined $_[0]->{base};
bless {
has => { %{ $_[0]->{has} } },
base => $base,
},
ref $_[0];
}
sub is_finite { 1 }
sub is_infinite { 0 }
sub truncate
{
my $self = shift;
my %p = validate( @_,
{ to =>
{ regex => qr/^(?:year|month|day|hour|minute|second)$/ },
},
);
my @fields = @FIELDS;
my $field;
my $value;
my $set = 0;
while ( @fields )
{
( $field, $value ) = ( shift @fields, shift @fields );
$self->set( $field => $value ) if $set;
$set = 1 if $p{to} eq $field;
}
return $self;
}
# Stringification methods
sub ymd
{
my ( $self, $sep ) = ( @_, '-' );
return $self->_year . $sep. $self->_month . $sep . $self->_day;
}
*date = \&ymd;
sub mdy
{
my ( $self, $sep ) = ( @_, '-' );
return $self->_month . $sep. $self->_day . $sep . $self->_year;
}
sub dmy
{
my ( $self, $sep ) = ( @_, '-' );
return $self->_day . $sep. $self->_month . $sep . $self->_year;
}
sub hms
{
my ( $self, $sep ) = ( @_, ':' );
return $self->_hour . $sep. $self->_minute . $sep . $self->_second;
}
# don't want to override CORE::time()
*DateTime::Incomplete::time = \&hms;
sub iso8601 { join 'T', $_[0]->ymd('-'), $_[0]->hms(':') }
*datetime = \&iso8601;
# "strftime"
# Modified from DateTime::strftime %formats; many changes.
my %formats =
( 'a' => sub { $_[0]->has_day ?
$_[0]->day_abbr :
$UNDEF_CHAR x 3 },
'A' => sub { $_[0]->has_day ?
$_[0]->day_name :
$UNDEF_CHAR x 5 },
'b' => sub { $_[0]->has_month ?
$_[0]->month_abbr :
$UNDEF_CHAR x 3 },
'B' => sub { $_[0]->has_month ?
$_[0]->month_name :
$UNDEF_CHAR x 5 },
'c' => sub { $_[0]->has_locale ?
$_[0]->strftime( $_[0]->locale->default_datetime_format ) :
$_[0]->datetime },
'C' => sub { $_[0]->has_year ?
int( $_[0]->year / 100 ) :
$UNDEF_CHAR x 2},
'd' => sub { $_[0]->_day },
'D' => sub { $_[0]->strftime( '%m/%d/%y' ) },
'e' => sub { $_[0]->has_month ?
sprintf( '%2d', $_[0]->day_of_month ) :
" $UNDEF_CHAR" },
'F' => sub { $_[0]->ymd('-') },
'g' => sub { substr( $_[0]->week_year, -2 ) },
'G' => sub { $_[0]->week_year },
'H' => sub { $_[0]->_hour },
'I' => sub { $_[0]->has_hour ?
sprintf( '%02d', $_[0]->hour_12 ) :
$UNDEF_CHAR x 2 },
'j' => sub { defined $_[0]->day_of_year ?
$_[0]->day_of_year :
$UNDEF_CHAR x 3 },
'k' => sub { $_[0]->_hour },
'l' => sub { $_[0]->has_hour ?
sprintf( '%2d', $_[0]->hour_12 ) :
" $UNDEF_CHAR" },
'm' => sub { $_[0]->_month },
'M' => sub { $_[0]->_minute },
'n' => sub { "\n" }, # should this be OS-sensitive?
'N' => sub { (shift)->_format_nanosecs( @_ ) },
'p' => sub { $_[0]->_format_am_pm },
'P' => sub { lc $_[0]->_format_am_pm },
'r' => sub { $_[0]->strftime( '%I:%M:%S %p' ) },
'R' => sub { $_[0]->strftime( '%H:%M' ) },
's' => sub { $_[0]->_format_epoch },
'S' => sub { $_[0]->_second },
't' => sub { "\t" },
'T' => sub { $_[0]->strftime( '%H:%M:%S' ) },
'u' => sub { $_[0]->day_of_week },
# algorithm from Date::Format::wkyr
'U' => sub { my $dow = $_[0]->day_of_week;
return $UNDEF_CHAR x 2 unless defined $dow;
$dow = 0 if $dow == 7; # convert to 0-6, Sun-Sat
my $doy = $_[0]->day_of_year - 1;
return int( ( $doy - $dow + 13 ) / 7 - 1 )
},
'w' => sub { my $dow = $_[0]->day_of_week;
return $UNDEF_CHAR unless defined $dow;
return $dow % 7;
},
'W' => sub { my $dow = $_[0]->day_of_week;
return $UNDEF_CHAR x 2 unless defined $dow;
my $doy = $_[0]->day_of_year - 1;
return int( ( $doy - $dow + 13 ) / 7 - 1 )
},
'x' => sub { $_[0]->has_locale ?
$_[0]->strftime( $_[0]->locale->default_date_format ) :
$_[0]->date },
'X' => sub { $_[0]->has_locale ?
$_[0]->strftime( $_[0]->locale->default_time_format ) :
$_[0]->time },
'y' => sub { $_[0]->has_year ?
sprintf( '%02d', substr( $_[0]->year, -2 ) ) :
$UNDEF_CHAR x 2 },
'Y' => sub { $_[0]->_year },
'z' => sub { defined $_[0]->time_zone ?
DateTime::TimeZone::offset_as_string( $_[0]->offset ) :
$UNDEF_CHAR x 5 },
'Z' => sub { defined $_[0]->time_zone ?
$_[0]->time_zone_short_name :
$UNDEF_CHAR x 5 },
'%' => sub { '%' },
);
$formats{h} = $formats{b};
sub _format_epoch {
my $epoch;
$epoch = $_[0]->epoch;
return $UNDEF_CHAR x 6 unless defined $epoch;
return $epoch;
}
sub _format_am_pm {
defined $_[0]->locale ?
$_[0]->locale->am_pm( $_[0] ) :
$UNDEF_CHAR x 2
}
sub _format_nanosecs
{
my $self = shift;
my $precision = shift || 9;
return $UNDEF_CHAR x $precision unless defined $self->nanosecond;
# rd_nanosecs can have a fractional separator
my ( $ret, $frac ) = split /[.,]/, $self->nanosecond;
$ret = sprintf "09d" => $ret; # unless length( $ret ) == 9;
$ret .= $frac if $frac;
return substr( $ret, 0, $precision );
}
sub strftime
{
my $self = shift;
# make a copy or caller's scalars get munged
my @formats = @_;
my @r;
foreach my $f (@formats)
{
$f =~ s/
%\{(\w+)\}
/
if ( $self->can($1) )
{
my $tmp = $self->$1();
defined $tmp ?
$tmp :
( exists $FIELD_LENGTH{$1} ?
$UNDEF_CHAR x $FIELD_LENGTH{$1} :
$UNDEF_CHAR x 2 );
}
/sgex;
# regex from Date::Format - thanks Graham!
$f =~ s/
%([%a-zA-Z])
/
$formats{$1} ? $formats{$1}->($self) : $1
/sgex;
# %3N
$f =~ s/
%(\d+)N
/
$formats{N}->($self, $1)
/sgex;
return $f unless wantarray;
push @r, $f;
}
return @r;
}
# DATETIME::INCOMPLETE METHODS
sub is_undef
{
for ( values %{$_[0]->{has}} )
{
return 0 if defined $_;
}
return 1;
}
sub to_datetime
{
my $self = shift;
my %param = @_;
$param{base} = $self->{base} if defined $self->{base} &&
! exists $param{base};
my $result;
if ( defined $param{base} &&
UNIVERSAL::can( $param{base}, 'utc_rd_values' ) )
{
$result = $param{base}->clone;
}
else
{
$result = DateTime->today;
}
my @params;
for my $key ( @FIELDS_SORTED )
{
my $value = $self->{has}{$key};
next unless defined $value;
if ( $key eq 'time_zone' )
{
$result->set_time_zone( $value );
next;
}
push @params, ( $key => $value );
}
$result->set( @params );
return $result;
}
sub contains {
my $self = shift;
my $dt = shift;
die "no datetime" unless defined $dt &&
UNIVERSAL::can( $dt, 'utc_rd_values' );
if ( $self->has_time_zone )
{
$dt = $dt->clone;
$dt->set_time_zone( $self->time_zone );
}
my ($key, $value);
while (($key, $value) = each %{$self->{has}} ) {
next unless defined $value;
if ( $key eq 'time_zone' ||
$key eq 'locale' )
{
# time_zone and locale are ignored.
next;
}
return 0 unless $dt->$key() == $value;
}
return 1;
}
# _fix_time_zone
# internal method used by next, previous
#
sub _fix_time_zone {
my ($self, $base, $code) = @_;
$base = $self->{base} if defined $self->{base} &&
! defined $base;
die "no base datetime" unless defined $base &&
UNIVERSAL::can( $base, 'utc_rd_values' );
my $base_tz = $base->time_zone;
my $result = $base->clone;
$result->set_time_zone( $self->time_zone )
if $self->has_time_zone;
$result = $code->($self, $result);
return undef
unless defined $result;
$result->set_time_zone( $self->time_zone )
if $self->has_time_zone;
$result->set_time_zone( $base_tz );
return $result;
}
sub next
{
# returns 'next or equal'
my $self = shift;
my $base = shift;
return $self->_fix_time_zone( $base,
sub {
my ($self, $result) = @_;
REDO: for (1..10) {
# warn "next: self ".$self->datetime." base ".$result->datetime;
my @fields = @FIELDS;
my ( $field, $overflow, $bigger_field );
while ( @fields )
{
( $field, undef ) = ( shift @fields, shift @fields );
if ( defined $self->$field() )
{
$overflow = ( $self->$field() < $result->$field() );
return undef if $overflow && $field eq $FIELDS[0];
if ( $self->$field() != $result->$field() )
{
eval { $result->set( $field => $self->$field() ) };
if ( $@ )
{
$result->set( @fields );
eval { $result->set( $field => $self->$field() ) };
if ( $@ )
{
$overflow = 1;
}
}
if ( $overflow )
{
$result->add( $bigger_field . 's' => 1 );
next REDO;
}
else
{
$result->set( @fields );
}
}
}
$bigger_field = $field;
}
return $result;
}
return undef;
} );
}
sub previous
{
# returns 'previous or equal'
my $self = shift;
my $base = shift;
return $self->_fix_time_zone( $base,
sub {
my ($self, $result) = @_;
# warn "# previous: self ".$self->datetime." base ".$result->datetime." ".$result->time_zone->name;
my ( $field, $value, $overflow, $bigger_field );
REDO: for (1..10) {
my @fields = @FIELDS;
while ( @fields )
{
( $field, $value ) = ( shift @fields, shift @fields );
if ( defined $self->$field() )
{
$overflow = ( $self->$field() > $result->$field() );
return undef if $overflow && $field eq $FIELDS[0];
if ( $self->$field() != $result->$field() )
{
if ( $overflow )
{
$result->set( $field => $value, @fields );
$result->subtract( nanoseconds => 1 );
next REDO;
}
my $diff = $result->$field() - $self->$field() ;
$diff--;
$result->subtract( $field . 's' => $diff );
$result->set( @fields );
$result->subtract( nanoseconds => 1 );
if ( $result->$field() != $self->$field() )
{
$result->set( @fields );
$result->subtract( nanoseconds => 1 );
}
}
}
$bigger_field = $field;
}
return $result;
}
return undef;
} );
}
sub closest
{
# returns 'closest datetime'
my $self = shift;
my $base = shift;
$base = $self->{base} if defined $self->{base} &&
! defined $base;
die "no base datetime" unless defined $base &&
UNIVERSAL::can( $base, 'utc_rd_values' );
my $dt1 = $self->previous( $base );
my $dt2 = $self->next( $base );
return $dt1 unless defined $dt2;
return $dt2 unless defined $dt1;
my $delta = $base - $dt1;
return $dt1 if ( $dt2 - $delta ) >= $base;
return $dt2;
}
sub start
{
my $self = shift;
return undef unless $self->has_year;
my $dt = $self->to_datetime;
$dt->subtract( years => 1 );
return $self->next( $dt );
}
sub end
{
my $self = shift;
return undef unless $self->has_year;
my $dt = $self->to_datetime;
$dt->add( years => 1 );
my $end = $self->previous( $dt );
$end->add( nanoseconds => 1 ) unless $self->has_nanosecond;
return $end;
}
sub to_span
{
my $self = shift;
my $start = $self->start;
my $end = $self->end;
return DateTime::Set->empty_set->complement->span
if ! $start && ! $end;
my @start;
@start = ( 'start', $start ) if $start;
my @end;
if ( $end )
{
if ( $self->has_nanosecond )
{
@end = ( 'end', $end );
}
else
{
@end = ( 'before', $end );
}
}
return DateTime::Span->from_datetimes( @start, @end );
}
sub to_recurrence
{
my $self = shift;
my %param;
my $freq = '';
my $year;
for ( qw( second minute hour day month year ) )
{
my $by = $_ . 's'; # months, hours
if ( exists $self->{has}{$_} && defined $self->{has}{$_} )
{
if ( $_ eq 'year' )
{
$year = $self->$_();
next;
}
$param{$by} = [ $self->$_() ];
next;
}
$freq = $_ unless $freq;
# TODO: use a hash
$param{$by} = [ 1 .. 12 ] if $_ eq 'month';
$param{$by} = [ 1 .. 31 ] if $_ eq 'day';
$param{$by} = [ 0 .. 23 ] if $_ eq 'hour';
$param{$by} = [ 0 .. 59 ] if $_ eq 'minute';
$param{$by} = [ 0 .. 59 ] if $_ eq 'second';
}
if ( $freq eq '' )
{
# it is a single date
my $dt = DateTime->new( %{$self->{has}} );
return DateTime::Set->from_datetimes( dates => [ $dt ] );
}
# for ( keys %param ) { print STDERR " param $_ = @{$param{$_}} \n"; }
my $r = DateTime::Event::Recurrence->yearly( %param );
if ( defined $year ) {
my $span = DateTime::Span->from_datetimes(
start => DateTime->new( year => $year ),
before => DateTime->new( year => $year + 1 ) );
$r = $r->intersection( $span );
}
return $r;
}
sub to_spanset
{
my $self = shift;
my @reset;
for ( qw( second minute hour day month year ) )
{
if ( $self->has( $_ ) )
{
my %fields = @FIELDS;
@reset = map { $_ => $fields{$_} } @reset;
my $dti = $self->clone;
$dti->set( @reset ) if @reset;
return DateTime::SpanSet->from_set_and_duration (
set => $dti->to_recurrence,
$_ . 's' => 1,
);
}
push @reset, $_;
}
return $self->to_span;
}
sub STORABLE_freeze
{
my ( $self, $cloning ) = @_;
return if $cloning;
my @data;
for my $key ( @FIELDS_SORTED )
{
next unless defined $self->{has}{$key};
if ( $key eq 'locale' )
{
push @data, "locale:" . $self->{has}{locale}->id;
}
elsif ( $key eq 'time_zone' )
{
push @data, "tz:" . $self->{has}{time_zone}->name;
}
else
{
push @data, "$key:" . $self->{has}{$key};
}
}
return join( '|', @data ), [$self->base];
}
sub STORABLE_thaw
{
my ( $self, $cloning, $data, $base ) = @_;
my %data = map { split /:/ } split /\|/, $data;
my $locale = delete $data{locale};
my $tz = delete $data{tz};
$self->{has} = \%data;
$self->set_time_zone( $tz );
$self->set( locale => $locale );
$self->{base} = $base->[0];
return $self;
}
1;
__END__
=head1 NAME
DateTime::Incomplete - An incomplete datetime, like January 5
=head1 SYNOPSIS
my $dti = DateTime::Incomplete->new( year => 2003 );
# 2003-xx-xx
$dti->set( month => 12 );
# 2003-12-xx
$dt = $dti->to_datetime( base => DateTime->now );
# 2003-12-19T16:54:33
=head1 DESCRIPTION
DateTime::Incomplete is a class for representing partial dates and
times.
These are actually encountered relatively frequently. For example, a
birthday is commonly given as a month and day, without a year.
=head1 ERROR HANDLING
Constructor and mutator methods (such as C<new> and C<set>) will die
if there is an attempt to set the datetime to an invalid value.
Invalid values are detected by setting the appropriate fields of a
"base" datetime object. See the C<set_base> method.
Accessor methods (such as C<day()>) will return either a value or
C<undef>, but will never die.
=head1 THE "BASE" DATETIME
A C<DateTime::Incomplete> object can have a "base" C<DateTime.pm>
object. This object is used as a default datetime in the
C<to_datetime()> method, and it also used to validate inputs to the
C<set()> method.
The base object must use the year/month/day system. Most calendars
use this system including Gregorian (C<DateTime>) and Julian. Note
that this module has not been well tested with base objects from
classes other than C<DateTime.pm> class.
By default, newly created C<DateTime::Incomplete> objects have no
base.
=head1 DATETIME-LIKE METHODS
Most methods provided by this class are designed to emulate the
behavior of C<DateTime.pm> whenever possible.
=over
=item * new()
Creates a new incomplete date:
my $dti = DateTime::Incomplete->new( year => 2003 );
# 2003-xx-xx
This class method accepts parameters for each date and time component:
"year", "month", "day", "hour", "minute", "second", "nanosecond".
Additionally, it accepts "time_zone", "locale", and "base" parameters.
Any parameters not given default to C<undef>.
Calling the C<new()> method without parameters creates a completely
undefined datetime:
my $dti = DateTime::Incomplete->new();
=item * from_day_of_year( ... )
This constructor takes the same arguments as can be given to the
C<new()> method, except that it does not accept a "month" or "day"
argument. Instead, it requires both "year" and "day_of_year". The
day of year must be between 1 and 366, and 366 is only allowed for
leap years.
It creates a C<DateTime::Incomplete> object with all date fields
defined, but with the time fields (hour, minute, etc.) set to undef.
=item * from_object( object => $object, ... )
This class method can be used to construct a new
C<DateTime::Incomplete> object from any object that implements the
C<utc_rd_values()> method. All C<DateTime::Calendar> modules must
implement this method in order to provide cross-calendar
compatibility. This method accepts a "locale" parameter.
If the object passed to this method has a C<time_zone()> method, that
is used to set the time zone. Otherwise UTC is used.
It creates a C<DateTime::Incomplete> object with all fields defined.
=item * from_epoch( ... )
This class method can be used to construct a new
C<DateTime::Incomplete> object from an epoch time instead of
components. Just as with the C<new()> method, it accepts "time_zone"
and "locale" parameters.
If the epoch value is not an integer, the part after the decimal will
be converted to nanoseconds. This is done in order to be compatible
with C<Time::HiRes>.
It creates a C<DateTime::Incomplete> object with all fields defined.
=item * now( ... )
This class method is equivalent to C<< DateTime->now >>.
It creates a new C<DateTime::Incomplete> object with all fields
defined.
=item * today( ... )
This class method is equivalent to C<now()>, but it leaves hour,
minute, second and nanosecond undefined.
=item * clone
Creates a new object with the same information as the object this
method is called on.
=back
=head2 "Get" Methods
=over 4
=item * year
=item * month
=item * day
=item * hour
=item * minute
=item * second
=item * nanosecond
=item * time_zone
=item * locale
These methods returns the field value for the object, or C<undef>.
These values can also be accessed using the same alias methods
available in C<DateTime.pm>, such as C<mon()>, C<mday()>, etc.
=item * has_year
=item * has_month
=item * has_day
=item * has_hour
=item * has_minute
=item * has_second
=item * has_nanosecond
=item * has_time_zone
=item * has_locale
=item * has_date
=item * has_time
Returns a boolean value indicating whether the corresponding component is
defined.
C<has_date> tests for year, month, and day.
C<has_time> tests for hour, minute, and second.
=item * has
$has_date = $dti->has( 'year', 'month', 'day' );
Returns a boolean value indicating whether all fields in the argument list are defined.
=item * defined_fields
@fields = $dti->defined_fields; # list of field names
Returns a list containing the names of the fields that are defined.
The list order is: year, month, day, hour, minute, second, nanosecond,
time_zone, locale.
=item * datetime, ymd, date, hms, time, iso8601, mdy, dmy
These are equivalent to DateTime stringification methods with the same
name, except that the undefined fields are replaced by 'xx' or 'xxxx'
as appropriate.
=item * epoch
=item * hires_epoch
=item * is_dst
=item * utc_rd_values
=item * utc_rd_as_seconds
my $epoch = $dti->epoch( base => $dt );
These methods are equivalent to the C<DateTime> methods with the same
name.
They all accept a "base" argument to use in order to calculate the
method's return values.
If no "base" argument is given, then C<today> is used.
=item * is_finite, is_infinite
Incomplete dates are always "finite".
=item * strftime( $format, ... )
This method implements functionality similar to the C<strftime()>
method in C. However, if given multiple format strings, then it will
return multiple scalars, one for each format string.
See the "strftime Specifiers" section in the C<DateTime.pm>
documentation for a list of all possible format specifiers.
Undefined fields are replaced by 'xx' or 'xxxx' as appropriate.
The specification C<%s> (epoch) is calculated using C<today> as the base date,
unless the object has a base datetime set.
=back
=head3 Computed Values
All other accessors, such as C<day_of_week()>, or C<week_year()> are
computed from the base values for a datetime. When these methods are
called, they return the requested information if there is enough data
to compute them, otherwise they return C<undef>
=head3 Unimplemented Methods
The following C<DateTime.pm> methods are not implemented in
C<DateTime::Incomplete>, though some of them may be implemented in
future versions:
=over 4
=item * add_duration
=item * add
=item * subtract_duration
=item * subtract
=item * subtract_datetime
=item * subtract_datetime_absolute
=item * delta_md
=item * delta_days
=item * delta_ms
=item * compare
=item * compare_ignore_floating
=item * DefaultLanguage
=back
=head2 "Set" Methods
=over 4
=item * set
Use this to set or undefine a datetime field:
$dti->set( month => 12 );
$dti->set( day => 24 );
$dti->set( day => undef );
This method takes the same arguments as the C<set()> method in
C<DateTime.pm>, but it can accept C<undef> for any value.
=item * set_time_zone
This method accepts either a time zone object or a string that can be
passed as the "name" parameter to C<< DateTime::TimeZone->new() >>.
Unlike with C<DateTime.pm>, if the new time zone's offset is different
from the previous time zone, no local time adjustment is made.
You can remove time zone information by calling this method with the
value C<undef>.
=item * truncate( to => ... )
This method allows you to reset some of the local time components in
the object to their "zero" values. The "to" parameter is used to
specify which values to truncate, and it may be one of "year",
"month", "day", "hour", "minute", or "second". For example, if
"month" is specified, then the local day becomes 1, and the hour,
minute, and second all become 0.
Note that the "to" parameter B<cannot be "week">.
=back
=head1 "DATETIME::INCOMPLETE" METHODS
C<DateTime::Incomplete> objects also have a number of methods unique
to this class.
=over 4
=item * base
Returns the base datetime value, or C<undef> if the object has none.
=item * has_base
Returns a boolean value indicating whether or not the object has a
base datetime set.
=item * is_undef
Returns true if the datetime is completely undefined.
=item * can_be_datetime
Returns true if the datetime has enough information to be converted to
a proper DateTime object.
The year field must be valid, followed by a sequence of valid fields.
Examples:
Can be datetime:
2003-xx-xxTxx:xx:xx
2003-10-xxTxx:xx:xx
2003-10-13Txx:xx:xx
Can not be datetime:
2003-10-13Txx:xx:30
xxxx-10-13Txx:xx:30
=cut
#=item * become_datetime
#
#Returns a C<DateTime> object.
#
#Returns C<undef> if the year value is not set.
#
#This method may C<die> if the parameters are not valid
#in the call to C<DateTime->new>.
=item * set_base
Sets the base datetime object for the C<DateTime::Incomplete> object.
The default value for "base" is C<undef>, which means no validation is
made on input.
=item * to_datetime
This method takes an optional "base" parameter and returns a
"complete" datetime.
$dt = $dti->to_datetime( base => DateTime->now );
$dti->set_base( DateTime->now );
$dt = $dti->to_datetime;
The resulting datetime can be either before of after the given base
datetime. No adjustments are made, besides setting the missing fields.
This method will use C<today> if the object has no base datetime set and none
is given as an argument.
This method may die if it results in a datetime that doesn't
actually exist, such as February 30, for example.
The fields in the resulting datetime are set in this order: locale,
time_zone, nanosecond, second, minute, hour, day, month, year.
=item * to_recurrence
This method generates the set of all possible datetimes that fit into
an incomplete datetime definition.
$dti = DateTime::Incomplete->new( month => 12, day => 24 );
$dtset1 = $dti->to_recurrence;
# Christmas recurrence, with _seconds_ resolution
$dti->truncate( to => 'day' );
$dtset2 = $dti->to_recurrence;
# Christmas recurrence, with days resolution (hour/min/sec = 00:00:00)
Those recurrences are C<DateTime::Set> objects:
$dt_next_xmas = $dti->to_recurrence->next( DateTime->today );
Incomplete dates that have the year defined will generate finite sets.
This kind of set can take a lot of resources (RAM and CPU). The
following incomplete datetime would generate the set of I<all seconds>
in 2003:
2003-xx-xxTxx:xx:xx
Recurrences are generated with up to 1 second resolution. The
C<nanosecond> value is ignored.
=item * to_spanset
This method generates the set of all possible spans that fit into
an incomplete datetime definition.
$dti = DateTime::Incomplete->new( month => 12, day => 24 );
$dtset1 = $dti->to_spanset;
# Christmas recurrence, from xxxx-12-24T00:00:00
# to xxxx-12-25T00:00:00
=item * start
=item * end
=item * to_span
These methods view an incomplete datetime as a "time span".
For example, the incomplete datetime C<2003-xx-xxTxx:xx:xx> starts
in C<2003-01-01T00:00:00> and ends in C<2004-01-01T00:00:00>.
The C<to_span> method returns a C<DateTime::Span> object.
An incomplete datetime without an year spans "forever".
Start and end datetimes are C<undef>.
=item * contains
Returns a true value if the incomplete datetime range I<contains> a
given datetime value.
For example:
2003-xx-xx contains 2003-12-24
2003-xx-xx does not contain 1999-12-14
=item * previous / next / closest
$dt2 = $dti->next( $dt );
The C<next()> returns the first complete date I<after or equal> to the
given datetime.
The C<previous()> returns the first complete date I<before or equal>
to the given datetime.
The C<closest()> returns the closest complete date (previous or next)
to the given datetime.
All of these methods return C<undef> if there is no matching complete
datetime.
If no datetime is given, these methods use the "base" datetime.
Note: The definition of C<previous()> and C<next()> is different from
the methods of the same name in the C<DateTime::Set> class.
The datetimes are generated with 1 nanosecond precision. The last
"time" value of a given day is 23:59:59.999999999 (for non leapsecond
days).
=back
=head1 SUPPORT
Support for this module is provided via the datetime@perl.org email
list. See http://lists.perl.org/ for more details.
=head1 AUTHORS
Flavio S. Glock <fglock[at]cpan.org>
With
Ben Bennett <fiji[at]ayup.limey.net>,
Claus Farber <claus[at]xn--frber-gra.muc.de>,
Dave Rolsky <autarch[at]urth.org>,
Eugene Van Der Pijll <pijll[at]gmx.net>,
Rick Measham <rick[at]isite.net.au>,
and the DateTime team.
=head1 COPYRIGHT
Copyright (c) 2003 Flavio S. Glock. All rights reserved. This
program is free software; you can redistribute 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
datetime@perl.org mailing list
http://datetime.perl.org/
=cut
|