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
|
package DateTime::Format::Pg;
use strict;
use vars qw ($VERSION);
use Carp;
use DateTime 0.13;
use DateTime::Duration;
use DateTime::Format::Builder 0.72;
use DateTime::TimeZone 0.06;
use DateTime::TimeZone::UTC;
use DateTime::TimeZone::Floating;
$VERSION = '0.16012';
$VERSION = eval $VERSION;
our @ISA = ('DateTime::Format::Builder');
=head1 NAME
DateTime::Format::Pg - Parse and format PostgreSQL dates and times
=head1 SYNOPSIS
use DateTime::Format::Pg;
my $dt = DateTime::Format::Pg->parse_datetime( '2003-01-16 23:12:01' );
# 2003-01-16 23:12:01
DateTime::Format::Pg->format_datetime($dt);
=head1 DESCRIPTION
This module understands the formats used by PostgreSQL for its DATE, TIME,
TIMESTAMP, and INTERVAL data types. It can be used to parse these formats in
order to create C<DateTime> or C<DateTime::Duration> objects, and it can take a
C<DateTime> or C<DateTime::Duration> object and produce a string representing
it in a format accepted by PostgreSQL.
=head1 CONSTRUCTORS
The following methods can be used to create C<DateTime::Format::Pg> objects.
=over 4
=item * new( name => value, ... )
Creates a new C<DateTime::Format::Pg> instance. This is generally not
required for simple operations. If you wish to use a different parsing
style from the default then it is more comfortable to create an object.
my $parser = DateTime::Format::Pg->new()
my $copy = $parser->new( 'european' => 1 );
This method accepts the following options:
=over 8
=item * european
If european is set to non-zero, dates are assumed to be in european
dd/mm/yyyy format. The default is to assume US mm/dd/yyyy format
(because this is the default for PostgreSQL).
This option only has an effect if PostgreSQL is set to output dates in
the 'PostgreSQL' (DATE only) and 'SQL' (DATE and TIMESTAMP) styles.
Note that you don't have to set this option if the PostgreSQL server has
been set to use the 'ISO' format, which is the default.
=item * server_tz
This option can be set to a C<DateTime::TimeZone> object or a string
that contains a time zone name.
This value must be set to the same value as the PostgreSQL server's time
zone in order to parse TIMESTAMP WITH TIMEZONE values in the
'PostgreSQL', 'SQL', and 'German' formats correctly.
Note that you don't have to set this option if the PostgreSQL server has
been set to use the 'ISO' format, which is the default.
=back
=cut
sub _add_param
{
my ($to,%param) = @_;
foreach(keys %param)
{
if($_ eq 'european') {
$$to{'_european'} = $param{$_};
} elsif($_ eq 'server_tz') {
$$to{'_server_tz'} = $param{$_};
} else {
croak("Unknown option $_." );
}
}
}
sub european {
my ($self,%param) = @_;
return $param{'european'} if exists $param{'european'};
return $self->{'_european'} if ref $self;
}
sub server_tz {
my ($self,%param) = @_;
return $param{''} if (ref($param{'server_tz'})) =~ /TimeZone/;
return DateTime::TimeZone->new('name' => $param{''}) if exists $param{'server_tz'};
return ((ref $self) && $self->{'_server_tz'});
}
sub new
{
my $class = shift;
my $self = bless {}, ref($class)||$class;
if (ref $class)
{
$self->{'_european'} = ( scalar $class->{'_european'} );
}
_add_param($self,@_);
return $self;
}
=item * clone()
This method is provided for those who prefer to explicitly clone via a
method called C<clone()>.
my $clone = $original->clone();
If called as a class method it will die.
=back
=cut
sub clone
{
my $self = shift;
croak('Calling object method as class method!') unless ref $self;
return $self->new();
}
sub _create_infinity
{
my $self = shift;
my %p = @_;
if ($p{sign}) {
return DateTime::Infinite::Past->new;
} else {
return DateTime::Infinite::Future->new;
}
}
# infinite datetimes
my $pg_infinity =
{
regex => qr/^(-)?infinity$/,
params => [ qw(sign) ],
constructor => \&_create_infinity,
};
# Dates (without time zone)
#
# see EncodeDateOnly() in
# pgsql-server/src/backend/utils/adt/datetime.c
#
# 2003-04-18 (USE_ISO_DATES)
#
my $pg_dateonly_iso =
{
regex => qr/^(\d{4,})-(\d{2,})-(\d{2,})( BC)?$/,
params => [ qw( year month day era ) ],
postprocess => \&_fix_era,
};
# 18/04/2003 (USE_SQL_DATES, EuroDates)
# 18-04-2003 (USE_POSTGRES_DATES, EuroDates)
# 04/18/2003 (USE_SQL_DATES, !EuroDates)
# 04-18-2003 (USE_POSTGRES_DATES, !EuroDates)
#
my $pg_dateonly_sql =
{
regex => qr/^(\d{2,})[\/-](\d{2,})[\/-](\d{4,})( BC)?$/,
params => [ qw( month day year era) ],
postprocess => [ \&_fix_era, \&_fix_eu ],
};
# 18.04.2003 (USE_GERMAN_DATES)
#
my $pg_dateonly_german =
{
regex => qr/^(\d{2,})\.(\d{2,})\.(\d{4,})( BC)?$/,
params => [ qw( day month year era ) ],
postprocess => \&_fix_era
};
# Times (with/without time zone)
#
# see EncodeTimeOnly() in
# pgsql-server/src/backend/utils/adt/datetime.c
#
# 17:20:24.373942+02
# (NB: always uses numerical tz)
#
my $pg_timeonly =
{
regex => qr/^T?(\d{2,}):(\d{2,}):(\d{2,})(\.\d+)? *([-\+][\d:]+)?$/,
params => [ qw( hour minute second nanosecond time_zone) ],
extra => { year => '1970' },
postprocess => [ \&_fix_timezone, \&_fix_nanosecond ],
};
# Timestamps (with/without time zone)
#
# see EncodeDateTime() in
# pgsql-server/src/backend/utils/adt/datetime.c
#
# 2003-04-18 17:20:24.373942+02 (USE_ISO_DATES)
# (NB: always uses numerical tz)
#
my $pg_datetime_iso =
{
regex => qr/^(\d{4,})-(\d{2,})-(\d{2,})[ T](\d{2,}):(\d{2,}):(\d{2,})(\.\d+)? *([-\+][\d:]+)?( BC)?$/,
params => [ qw( year month day hour minute second nanosecond time_zone era) ],
postprocess => [ \&_fix_era, \&_fix_timezone, \&_fix_nanosecond ],
};
# Fri 18 Apr 17:20:24.373942 2003 CEST (USE_POSTGRES_DATES, EuroDates)
#
my $pg_datetime_pg_eu =
{
regex => qr/^\S{3,} (\d{2,}) (\S{3,}) (\d{2,}):(\d{2,}):(\d{2,})(\.\d+)? (\d{4,}) *((?:[-\+][\d:]+)|(?:\S+))?( BC)?$/,
params => [ qw( day month hour minute second nanosecond year time_zone era ) ],
postprocess => [ \&_fix_era, \&_fix_timezone, \&_fix_nanosecond ],
};
# Fri Apr 18 17:20:24.373942 2003 CEST (USE_POSTGRES_DATES, !EuroDates)
#
my $pg_datetime_pg_us =
{
regex => qr/^\S{3,} (\S{3,}) (\s{2,}) (\d{2,}):(\d{2,}):(\d{2,})(\.\d+)? (\d{4,}) *((?:[-\+][\d:]+)|(?:\S+))?( BC)?$/,
params => [ qw( month day hour minute second nanosecond year time_zone era ) ],
postprocess => [ \&_fix_era, \&_fix_month_names, \&_fix_timezone, \&_fix_nanosecond ],
};
# 18/04/2003 17:20:24.373942 CEST (USE_SQL_DATES, EuroDates)
# 04/18/2003 17:20:24.373942 CEST (USE_SQL_DATES, !EuroDates)
#
my $pg_datetime_sql =
{
regex => qr/^(\d{2,})\/(\d{2,})\/(\d{4,}) (\d{2,}):(\d{2,}):(\d{2,})(\.\d+)? *((?:[-\+][\d:]+)|(?:\S+))?( BC)?$/,
params => [ qw( month day year hour minute second nanosecond time_zone era ) ],
postprocess => [ \&_fix_era, \&_fix_eu, \&_fix_timezone, \&_fix_nanosecond ],
};
# 18.04.2003 17:20:24.373942 CEST (USE_GERMAN_DATES)
#
my $pg_datetime_german =
{
regex => qr/^(\d{2,})\.(\d{2,})\.(\d{4,}) (\d{2,}):(\d{2,}):(\d{2,})(\.\d+)? *((?:[-\+][\d:]+)|(?:\S+))?( BC)?$/,
params => [ qw( day month year hour minute second nanosecond time_zone era ) ],
postprocess => [ \&_fix_era, \&_fix_timezone, \&_fix_nanosecond ],
};
# Helper functions
#
# Fix BC dates (1 BC => year 0, 2 BC => year -1)
#
sub _fix_era {
my %args = @_;
my $era = (delete $args{'parsed'}->{'era'}) || '';
if ($era =~ m/BC/) {
$args{'parsed'}->{'year'} = 1-$args{'parsed'}->{'year'}
}
return 1;
}
# Fix European dates (swap month and day)
#
sub _fix_eu {
my %args = @_;
if($args{'self'}->european(@{$args{'args'}}) ) {
my $save = $args{'parsed'}->{'month'};
$args{'parsed'}->{'month'} = $args{'parsed'}->{'day'};
$args{'parsed'}->{'day'} = $save;
}
return 1;
}
# Fix month names (name => numeric)
#
my %months = (
'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12, );
sub _fix_month_names {
my %args = @_;
$args{'parsed'}->{'month'} = $months{lc( $args{'parsed'}->{'month'} )};
return $args{'parsed'}->{'month'} ? 1 : undef;
}
# Fix time zones
#
sub _fix_timezone {
my %args = @_;
my %param = $args{'args'} ? (@{$args{'args'}}) : ();
if($param{'_force_tz'}) {
$args{'parsed'}->{'time_zone'} = $param{'_force_tz'};
}
elsif(!defined($args{'parsed'}->{'time_zone'})) {
# For very early and late dates, PostgreSQL always returns times in
# UTC and does not tell us that it did so.
#
if ( $args{'parsed'}->{'year'} < 1901
|| ( $args{'parsed'}->{'year'} == 1901 && ($args{'parsed'}->{'month'} < 12 || $args{'parsed'}->{'day'} < 14) )
|| $args{'parsed'}->{'year'} > 2038
|| ( $args{'parsed'}->{'year'} == 2038 && ($args{'parsed'}->{'month'} > 01 || $args{'parsed'}->{'day'} > 18) )
) {
$args{'parsed'}->{'time_zone'} = DateTime::TimeZone::UTC->new();
}
# DT->new() does not like undef time_zone params, which are generated
# by the regexps
#
else {
delete $args{'parsed'}->{'time_zone'};
}
}
# Numerical time zone
#
elsif($args{'parsed'}->{'time_zone'} =~ m/^([-\+])(\d+)(?::(\d+))?(?::(\d+))?$/) {
my $tz;
if (length($2) == 2) {
# regular hour notation
my ($min, $sec) = ($3 || '00', $4 || '00');
$tz = sprintf "%s%02d:%02d:%02d", $1, $2, $min, $sec;
} else {
$tz = "$1$2";
}
$args{'parsed'}->{'time_zone'} = $tz;
}
# Non-numerical time zone returned, which can be ambiguous :(
#
else
{
# XXX This barfs because 'self' may not necessarily be initialized
# Need to fix it
my $stz = $args{'self'}->_server_tz($args{'args'} ? @{$args{'args'}} : ());
$args{'parsed'}->{'time_zone'} = $stz || 'floating';
}
return 1;
}
# Fix fractional seconds
#
sub _fix_nanosecond {
my %args = @_;
if(defined $args{'parsed'}->{'nanosecond'}) {
$args{'parsed'}->{'nanosecond'} *= 1.0E9;
} else {
delete $args{'parsed'}->{'nanosecond'}
};
return 1;
}
# Parser generation
#
DateTime::Format::Builder->create_class
(
parsers =>
{
parse_date => [ $pg_dateonly_iso, $pg_dateonly_sql,
$pg_dateonly_german, $pg_infinity ],
parse_timetz => [ $pg_timeonly, ],
parse_timestamptz => [ $pg_datetime_iso, $pg_datetime_pg_eu,
$pg_datetime_pg_us, $pg_datetime_sql,
$pg_datetime_german, $pg_infinity ],
parse_datetime => [ $pg_datetime_iso, $pg_datetime_pg_eu,
$pg_datetime_pg_us, $pg_datetime_sql,
$pg_datetime_german,
$pg_dateonly_iso, $pg_dateonly_german,
$pg_dateonly_sql, $pg_timeonly, $pg_infinity],
}
);
=head1 METHODS
This class provides the following methods. The parse_datetime, parse_duration,
format_datetime, and format_duration methods are general-purpose methods
provided for compatibility with other C<DateTime::Format> modules.
The other methods are specific to the corresponding PostgreSQL date/time data
types. The names of these methods are derived from the name of the PostgreSQL
data type. (Note: Prior to PostgreSQL 7.3, the TIMESTAMP type was equivalent
to the TIMESTAMP WITH TIME ZONE type. This data type corresponds to the
format/parse_timestamp_with_time_zone method but not to the
format/parse_timestamp method.)
=head2 PARSING METHODS
This class provides the following parsing methods.
As a general rule, the parsing methods accept input in any format that the
PostgreSQL server can produce. However, if PostgreSQL's DateStyle is set to
'SQL' or 'PostgreSQL', dates can only be parsed correctly if the 'european'
option is set correctly (i.e. same as the PostgreSQL server). The same is true
for time zones and the 'australian_timezones' option in all modes but 'ISO'.
The default DateStyle, 'ISO', will always produce unambiguous results
and is also parsed most efficiently by this parser class. I strongly
recommend using this setting unless you have a good reason not to.
=over 4
=item * parse_datetime($string,...)
Given a string containing a date and/or time representation, this method
will return a new C<DateTime> object.
If the input string does not contain a date, it is set to 1970-01-01.
If the input string does not contain a time, it is set to 00:00:00.
If the input string does not contain a time zone, it is set to the
floating time zone.
If given an improperly formatted string, this method may die.
=cut
# sub parse_datetime {
# *** created automatically ***
# }
=item * parse_timestamptz($string,...)
=item * parse_timestamp_with_time_zone($string,...)
Given a string containing a timestamp (date and time) representation,
this method will return a new C<DateTime> object. This method is
suitable for the TIMESTAMPTZ (or TIMESTAMP WITH TIME ZONE) type.
If the input string does not contain a time zone, it is set to the
floating time zone.
Please note that PostgreSQL does not actually store a time zone along
with the TIMESTAMP WITH TIME ZONE (or TIMESTAMPTZ) type but will just
return a time stamp converted for the server's local time zone.
If given an improperly formatted string, this method may die.
=cut
# sub parse_timestamptz {
# *** created automatically ***
# }
*parse_timestamp_with_time_zone = \&parse_timestamptz;
=item * parse_timestamp($string,...)
=item * parse_timestamp_without_time_zone($string,...)
Similar to the functions above, but always returns a C<DateTime> object
with a floating time zone. This method is suitable for the TIMESTAMP (or
TIMESTAMP WITHOUT TIME ZONE) type.
If the server does return a time zone, it is ignored.
If given an improperly formatted string, this method may die.
=cut
sub parse_timestamp {
parse_timestamptz(@_,'_force_tz' => DateTime::TimeZone::Floating->new());
}
*parse_timestamp_without_time_zone = \&parse_timestamp;
=item * parse_timetz($string,...)
=item * parse_time_with_time_zone($string,...)
Given a string containing a time representation, this method will return
a new C<DateTime> object. The date is set to 1970-01-01. This method is
suitable for the TIMETZ (or TIME WITH TIME ZONE) type.
If the input string does not contain a time zone, it is set to the
floating time zone.
Please note that PostgreSQL stores a numerical offset with its TIME WITH
TIME ZONE (or TIMETZ) type. It does not store a time zone name (such as
'Europe/Rome').
If given an improperly formatted string, this method may die.
=cut
# sub parse_timetz {
# *** created automatically ***
# }
*parse_time_with_time_zone = \&parse_timetz;
=item * parse_time($string,...)
=item * parse_time_without_time_zone($string,...)
Similar to the functions above, but always returns an C<DateTime> object
with a floating time zone. If the server returns a time zone, it is
ignored. This method is suitable for use with the TIME (or TIME WITHOUT
TIME ZONE) type.
This ensures that the resulting C<DateTime> object will always have the
time zone expected by your application.
If given an improperly formatted string, this method may die.
=cut
sub parse_time {
parse_timetz(@_,'_force_tz' => 'floating');
}
*parse_time_without_time_zone = \&parse_time;
=item * parse_date($string,...)
Given a string containing a date representation, this method will return
a new C<DateTime> object. The time is set to 00:00:00 (floating time
zone). This method is suitable for the DATE type.
If given an improperly formatted string, this method may die.
=cut
# sub parse_date {
# *** generated automatically ***
# }
=item * parse_duration($string)
=item * parse_interval($string)
Given a string containing a duration (SQL type INTERVAL) representation,
this method will return a new C<DateTime::Duration> object.
If given an improperly formatted string, this method may die.
=cut
sub parse_duration {
my ($self, $string_to_parse) = @_;
# NB: We can't just pass our values to new() because it treats all
# arguments as negative if we have a single negative component.
# PostgreSQL might return mixed signs, e.g. '1 mon -1day'.
my $du = DateTime::Duration->new;
my %units = ( map(($_, ["seconds", 1]), qw(s second seconds sec secs)),
map(($_, ["minutes", 1]), qw(m minute minutes min mins)),
map(($_, ["hours", 1]), qw(h hr hour hours)),
map(($_, ["days", 1]), qw(d day days)),
map(($_, ["weeks", 1]), qw(w week weeks)),
map(($_, ["months", 1]), qw(M mon mons month months)),
map(($_, ["years", 1]), qw(y yr yrs year years)),
map(($_, ["years", 10]), qw(decade decades dec decs)),
map(($_, ["years", 100]), qw(c cent century centuries)),
map(($_, ["years", 1000]), qw(millennium millennia millenniums mil mils)) );
(my $string = $string_to_parse) =~ s/^@\s*//;
$string =~ s/\+(\d+)/$1/g;
my $subtract = 0;
if ( $string =~ s/ago// ) {
$subtract = 1;
}
my $sign = 0;
my %done;
# $timespec =~ s/\b(\d+):(\d\d):((\d\d)|(\d\d.\d+))\b/$1h $2m $3s/g;
$string =~ s/\b(\d+):(\d\d):(\d\d)\b/$1h $2m $3s/g;
$string =~ s/\b(\d+):(\d\d)\b/$1h $2m/g;
$string =~ s/(-\d+h)\s+(\d+m)\s+(\d+s)\s*/$1 -$2 -$3 /;
$string =~ s/(-\d+h)\s+(\d+m)\s*/$1 -$2 /;
while ($string =~ s/^\s*(-?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i) {
my($amount, $unit) = ($1, $2);
$unit = lc($unit) unless length($unit) == 1;
my ($base_unit, $num);
if ( defined( $units{$unit} ) ) {
($base_unit, $num) = @{$units{$unit}};
my $key = $base_unit . "-" . $num;
Carp::croak "Unknown timespec: $string_to_parse" if defined($done{$key});
$done{$key} = 1;
$amount =~ s/,/./;
if ( $subtract ) {
$du->subtract( $base_unit => $amount * $num );
} else {
$du->add( $base_unit => $amount * $num );
}
} else {
Carp::croak "Unknown timespec: $string_to_parse";
}
}
if ($string =~ /\S/) { # OK to have extra spaces, but nothing else
Carp::croak "Unknown timespec: $string_to_parse";
}
return $du;
}
*parse_interval = \&parse_duration;
=back
=head2 FORMATTING METHODS
This class provides the following formatting methods.
The output is always in the format mandated by the SQL standard (derived
from ISO 8601), which is parsed by PostgreSQL unambiguously in all
DateStyle modes.
=over 4
=item * format_datetime($datetime,...)
Given a C<DateTime> object, this method returns a string appropriate as
input for all date and date/time types of PostgreSQL. It will contain
date and time.
If the time zone of the C<DateTime> part is floating, the resulting
string will contain no time zone, which will result in the server's time
zone being used. Otherwise, the numerical offset of the time zone is
used.
=cut
*format_datetime = \&format_timestamptz;
=item * format_time($datetime,...)
=item * format_time_without_time_zone($datetime,...)
Given a C<DateTime> object, this method returns a string appropriate as
input for the TIME type (also known as TIME WITHOUT TIME ZONE), which
will contain the local time of the C<DateTime> object and no time zone.
=cut
sub _format_fractional
{
my $ns = shift->nanosecond;
return $ns ? sprintf(".%09d", "$ns") : ''
}
sub format_time
{
my ($self,$dt,%param) = @_;
return $dt->hms(':')._format_fractional($dt);
}
*format_time_without_time_zone = \&format_time;
=item * format_timetz($datetime)
=item * format_time_with_time_zone($datetime)
Given a C<DateTime> object, this method returns a string appropriate as
input for the TIME WITH TIME ZONE type (also known as TIMETZ), which
will contain the local part of the C<DateTime> object and a numerical
time zone.
You should not use the TIME WITH TIME ZONE type to store dates with
floating time zones. If the time zone of the C<DateTime> part is
floating, the resulting string will contain no time zone, which will
result in the server's time zone being used.
=cut
sub _format_time_zone
{
my $dt = shift;
return '' if $dt->time_zone->is_floating;
return &DateTime::TimeZone::offset_as_string($dt->offset);
}
sub format_timetz
{
my ($self,$dt) = @_;
return $dt->hms(':')._format_fractional($dt)._format_time_zone($dt);
}
*format_time_with_time_zone = \&format_timetz;
=item * format_date($datetime)
Given a C<DateTime> object, this method returns a string appropriate as
input for the DATE type, which will contain the date part of the
C<DateTime> object.
=cut
sub format_date
{
my ($self,$dt) = @_;
if($dt->is_infinite) {
return $dt->isa('DateTime::Infinite::Future') ? 'infinity' : '-infinity';
} elsif($dt->year()<=0) {
return sprintf('%04d-%02d-%02d BC',
1-$dt->year(),
$dt->month(),
$dt->day());
} else {
return $dt->ymd('-');
}
}
=item * format_timestamp($datetime)
=item * format_timestamp_without_time_zone($datetime)
Given a C<DateTime> object, this method returns a string appropriate as
input for the TIMESTAMP type (also known as TIMESTAMP WITHOUT TIME
ZONE), which will contain the local time of the C<DateTime> object and
no time zone.
=cut
sub format_timestamp
{
my ($self,$dt,%param) = @_;
if($dt->is_infinite) {
return $dt->isa('DateTime::Infinite::Future') ? 'infinity' : '-infinity';
} elsif($dt->year()<=0) {
return sprintf('%04d-%02d-%02d %s BC',
1-$dt->year(),
$dt->month(),
$dt->day(),
$dt->hms(':')._format_fractional($dt));
} else {
return $dt->ymd('-').' '.$dt->hms(':')._format_fractional($dt);
}
}
*format_timestamp_without_time_zone = \&format_timestamp;
=item * format_timestamptz($datetime)
=item * format_timestamp_with_time_zone($datetime)
Given a C<DateTime> object, this method returns a string appropriate as
input for the TIMESTAMP WITH TIME ZONE type, which will contain the
local part of the C<DateTime> object and a numerical time zone.
You should not use the TIMESTAMP WITH TIME ZONE type to store dates with
floating time zones. If the time zone of the C<DateTime> part is
floating, the resulting string will contain no time zone, which will
result in the server's time zone being used.
=cut
sub format_timestamptz
{
my ($self,$dt,%param) = @_;
if($dt->is_infinite) {
return $dt->isa('DateTime::Infinite::Future') ? 'infinity' : '-infinity';
} elsif($dt->year()<=0) {
return sprintf('%04d-%02d-%02d',
1-$dt->year(),
$dt->month(),
$dt->day()).
' '.
$dt->hms(':').
_format_fractional($dt).
_format_time_zone($dt).
' BC';
} else {
return $dt->ymd('-').' '.$dt->hms(':').
_format_fractional($dt).
_format_time_zone($dt);
}
}
*format_timestamp_with_time_zone = \&format_timestamptz;
=item * format_duration($du)
=item * format_interval($du)
Given a C<DateTime::Duration> object, this method returns a string appropriate
as input for the INTERVAL type.
=cut
sub format_duration {
shift if UNIVERSAL::isa($_[0], __PACKAGE__) || $_[0] eq __PACKAGE__;
my($du,%param) = @_;
croak 'DateTime::Duration object expected' unless UNIVERSAL::isa($du,'DateTime::Duration');
my %deltas = $du->deltas();
my $output = '@';
if($deltas{'nanoseconds'}) {
$deltas{'seconds'} =
sprintf('%f', $deltas{'seconds'} + $deltas{'nanoseconds'} /
DateTime::Duration::MAX_NANOSECONDS);
}
foreach(qw(months days minutes seconds)) {
$output .= ' '.$deltas{$_}.' '.$_ if $deltas{$_};
}
$output .= ' 0' if(length($output)<=2);
return $output;
}
*format_interval = \&format_duration;
=back
=cut
1;
__END__
=head1 LIMITATIONS
Some output formats of PostgreSQL have limitations that can only be passed on
by this class.
As a general rules, none of these limitations apply to the 'ISO' output
format. It is strongly recommended to use this format (and to use
PostgreSQL's to_char function when another output format that's not
supposed to be handled by a parser of this class is desired). 'ISO' is
the default but you are advised to explicitly set it at the beginning of
the session by issuing a SET DATESTYLE TO 'ISO'; command in case the
server administrator changes that setting.
When formatting DateTime objects, this class always uses a format that's
handled unambiguously by PostgreSQL.
=head2 TIME ZONES
If DateStyle is set to 'PostgreSQL', 'SQL', or 'German', PostgreSQL does
not send numerical time zones for the TIMESTAMPTZ (or TIMESTAMP WITH
TIME ZONE) type. Unfortunately, the time zone names used instead can be
ambiguous: For example, 'EST' can mean -0500, +1000, or +1100.
You must set the 'server_tz' variable to a time zone that is identical to that
of the PostgreSQL server. If the server is set to a different time zone (or the
underlying operating system interprets the time zone differently), the parser
will return wrong times.
You can avoid such problems by setting the server's time zone to UTC
using the SET TIME ZONE 'UTC' command and setting 'server_tz' parameter
to 'UTC' (or by using the ISO output format, of course).
=head2 EUROPEAN DATES
For the SQL (for DATE and TIMSTAMP[TZ]) and the PostgreSQL (for DATE)
output format, the server can send dates in both European-style
'dd/mm/yyyy' and in US-style 'mm/dd/yyyy' format. In order to parse
these dates correctly, you have to pass the 'european' option to the
constructor or to the C<parse_xxx> routines.
This problem does not occur when using the ISO or German output format
(and for PostgreSQL with TIMESTAMP[TZ] as month names are used then).
=head2 INTERVAL ELEMENTS
C<DateTime::Duration> stores months, days, minutes and seconds
separately. PostgreSQL only stores months and seconds and disregards the
irregular length of days due to DST switching and the irregular length
of minutes due to leap seconds. Therefore, it is not possible to store
C<DateTime::Duration> objects as SQL INTERVALs without the loss of some
information.
=head2 NEGATIVE INTERVALS
In the SQL and German output formats, the server does not send an
indication of the sign with intervals. This means that '1 month ago' and
'1 month' are both returned as '1 mon'.
This problem can only be avoided by using the 'ISO' or 'PostgreSQL'
output format.
=head1 SUPPORT
Support for this module is provided via the datetime@perl.org email
list. See http://lists.perl.org/ for more details.
=head1 AUTHOR
Daisuke Maki E<lt>daisuke@endeworks.jpE<gt>
=head1 AUTHOR EMERITUS
Claus A. Faerber <perl@faerber.muc.de>
=head1 COPYRIGHT
Copyright (c) 2003 Claus A. Faerber. Copyright (c) 2005-2007 Daisuke Maki
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
|