/usr/share/perl5/DateTime/Format/Oracle.pm is in libdatetime-format-oracle-perl 0.06-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 | package DateTime::Format::Oracle;
use strict;
use Carp;
use DateTime;
use DateTime::Format::Builder;
use Convert::NLS_DATE_FORMAT;
our $VERSION = '0.06';
our $nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
our $nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS';
our $nls_timestamp_tz_format = 'YYYY-MM-DD HH24:MI:SS TZHTZM';
=head1 NAME
DateTime::Format::Oracle - Parse and format Oracle dates and timestamps
=head1 SYNOPSIS
use DateTime::Format::Oracle;
$ENV{'NLS_DATE_FORMAT'} = 'YYYY-MM-DD HH24:MI:SS';
my $dt = DateTime::Format::Oracle->parse_datetime('2003-01-16 23:12:01');
my $string = DateTime::Format::Oracle->format_datetime($dt);
=head1 DESCRIPTION
This module may be used to convert Oracle date and timestamp values
into C<DateTime> objects. It also can take a C<DateTime> object and
produce a date string matching the C<NLS_DATE_FORMAT>.
Oracle has flexible date formatting via its C<NLS_DATE_FORMAT> session
variable. Date values will be returned from Oracle according to the
current value of that variable. Date values going into Oracle must also
match the current setting of C<NLS_DATE_FORMAT>.
Timestamp values will match either the C<NLS_TIMESTAMP_FORMAT> or
C<NLS_TIMESTAMP_TZ_FORMAT> session variables.
This module keeps track of these Oracle session variable values by
examining environment variables of the same name. Each time one of
Oracle's formatting session variables is updated, the C<%ENV> hash
must also be updated.
=head1 METHODS
This class offers the following methods.
=over 4
=item * nls_date_format
This method is used to determine the current value of Oracle's
C<NLS_DATE_FORMAT>. It currently just reads the value from
$ENV{'NLS_DATE_FORMAT'}
or if that is not set, from the package variable C<$nls_date_format>,
which has a default value of C<YYYY-MM-DD HH24:MI:SS>. This is
a good default to have, but is not Oracle's default. Dates will fail
to parse if Oracle's NLS_DATE_FORMAT and the value from this method
are not the same.
If you want to use the default from this module, you can do something
like this after you connect to Oracle:
$dbh->do(
"alter session set nls_date_format = '" .
DateTime::Format::Oracle->nls_date_format .
"'"
);
=cut
sub nls_date_format { $ENV{NLS_DATE_FORMAT} || $nls_date_format }
=item * nls_timestamp_format
This method is used to determine the current value of Oracle's
C<NLS_TIMESTAMP_FORMAT>. It currently just reads the value from
$ENV{'NLS_TIMESTAMP_FORMAT'}
or if that is not set, from the package variable C<$nls_timestamp_format>,
which has a default value of C<YYYY-MM-DD HH24:MI:SS>. This is
a good default to have, but is not Oracle's default. Dates will fail
to parse if Oracle's NLS_TIMESTAMP_FORMAT and the value from this method
are not the same.
If you want to use the default from this module, you can do something
like this after you connect to Oracle:
$dbh->do(
"alter session set nls_timestamp_format = '" .
DateTime::Format::Oracle->nls_timestamp_format .
"'"
);
=cut
sub nls_timestamp_format { $ENV{NLS_TIMESTAMP_FORMAT} || $nls_timestamp_format }
=item * nls_timestamp_tz_format
This method is used to determine the current value of Oracle's
C<NLS_TIMESTAMP_TZ_FORMAT>. It currently just reads the value from
$ENV{'NLS_TIMESTAMP_TZ_FORMAT'}
or if that is not set, from the package variable C<$nls_timestamp_tz_format>,
which has a default value of C<YYYY-MM-DD HH24:MI:SS TZHTZM>. This is
a good default to have, but is not Oracle's default. Dates will fail
to parse if Oracle's NLS_TIMESTAMP_TZ_FORMAT and the value from this method
are not the same.
If you want to use the default from this module, you can do something
like this after you connect to Oracle:
$dbh->do(
"alter session set nls_timestamp_tz_format = '" .
DateTime::Format::Oracle->nls_timestamp_tz_format .
"'"
);
=cut
sub nls_timestamp_tz_format { $ENV{NLS_TIMESTAMP_TZ_FORMAT} || $nls_timestamp_tz_format }
=item * parse_datetime
Given a string containing a date and/or time representation
matching C<NLS_DATE_FORMAT>, this method will return a new
C<DateTime> object.
If given an improperly formatted string, this method may die.
=cut
sub parse_datetime { $_[0]->current_date_parser->(@_); }
=item * parse_date
Alias to C<parse_datetime>. Oracle's date datatype also holds
time information.
=cut
*parse_date = \&parse_datetime;
=item * parse_timestamp
Given a string containing a date and/or time representation
matching C<NLS_TIMESTAMP_FORMAT>, this method will return a new
C<DateTime> object.
If given an improperly formatted string, this method may die.
=cut
sub parse_timestamp { $_[0]->current_timestamp_parser->(@_); }
=item * parse_timestamptz
=item * parse_timestamp_with_time_zone
Given a string containing a date and/or time representation
matching C<NLS_TIMESTAMP_TZ_FORMAT>, this method will return a new
C<DateTime> object.
If given an improperly formatted string, this method may die.
=cut
sub parse_timestamptz { $_[0]->current_timestamptz_parser->(@_); }
*parse_timestamp_with_time_zone = \&parse_timestamptz;
=item * current_date_parser
The current C<DateTime::Format::Builder> generated parsing method
used by C<parse_datetime> and C<parse_date>.
=cut
*current_date_parser = _parser_generator('current_date_format');
=item * current_timestamp_parser
The current C<DateTime::Format::Builder> generated parsing method
used by C<parse_timestamp>.
=cut
*current_timestamp_parser = _parser_generator('current_timestamp_format');
=item * current_timestamptz_parser
The current C<DateTime::Format::Builder> generated parsing method
used by C<parse_timestamptz>.
=cut
*current_timestamptz_parser = _parser_generator('current_timestamptz_format');
sub _parser_generator {
# takes a method name for getting the current POSIX format
# returns a parser generator code ref
my $previous_format = '';
my $previous_parser = '';
my $method = shift;
sub {
my ( $self ) = @_;
unless ($previous_format eq (my $current_format = $self->$method())) {
$previous_format = $current_format;
$previous_parser = $self->_create_parser_method($current_format);
}
return $previous_parser;
}
}
sub _create_parser_method {
# takes a strptime format, returns a parser method code ref
my ( $self, $date_format ) = @_;
my %parse_date = ( strptime => { pattern => $date_format } );
my $parser = DateTime::Format::Builder->create_parser(\%parse_date);
return DateTime::Format::Builder->create_method($parser);
}
=item * format_datetime
Given a C<DateTime> object, this method returns a string matching
the current value of C<NLS_DATE_FORMAT>.
It is important to keep the value of C<$ENV{'NLS_DATE_FORMAT'}> the
same as the value of the Oracle session variable C<NLS_DATE_FORMAT>.
To determine the current value of Oracle's C<NLS_DATE_FORMAT>:
select NLS_DATE_FORMAT from NLS_SESSION_PARAMETERS
To reset Oracle's C<NLS_DATE_FORMAT>:
alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
It is generally a good idea to set C<NLS_DATE_FORMAT> to an
unambiguos value, with four-digit year, and hour, minute, and second.
=cut
sub format_datetime {
my ($self, $dt) = @_;
return $dt->strftime($self->current_date_format);
}
=item * format_date
Alias to C<format_datetime>.
=cut
*format_date = \&format_datetime;
=item * format_timestamp
Given a C<DateTime> object, this method returns a string matching
the current value of C<NLS_TIMESTAMP_FORMAT>.
It is important to keep the value of C<$ENV{'NLS_TIMESTAMP_FORMAT'}> the
same as the value of the Oracle session variable C<NLS_TIMESTAMP_FORMAT>.
To determine the current value of Oracle's C<NLS_TIMESTAMP_FORMAT>:
select NLS_TIMESTAMP_FORMAT from NLS_SESSION_PARAMETERS
To reset Oracle's C<NLS_TIMESTAMP_FORMAT>:
alter session set NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS'
It is generally a good idea to set C<NLS_TIMESTAMP_FORMAT> to an
unambiguos value, with four-digit year, and hour, minute, and second.
=cut
sub format_timestamp {
my ($self, $dt) = @_;
return $dt->strftime($self->current_timestamp_format);
}
=item * format_timestamptz
=item * format_timestamp_with_time_zone
Given a C<DateTime> object, this method returns a string matching
the current value of C<NLS_TIMESTAMP_TZ_FORMAT>.
It is important to keep the value of C<$ENV{'NLS_TIMESTAMP_TZ_FORMAT'}> the
same as the value of the Oracle session variable C<NLS_TIMESTAMP_TZ_FORMAT>.
To determine the current value of Oracle's C<NLS_TIMESTAMP_TZ_FORMAT>:
select NLS_TIMESTAMP_TZ_FORMAT from NLS_SESSION_PARAMETERS
To reset Oracle's C<NLS_TIMESTAMP_TZ_FORMAT>:
alter session set NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS TZHTZM'
It is generally a good idea to set C<NLS_TIMESTAMP_TZ_FORMAT> to an
unambiguos value, with four-digit year, and hour, minute, and second.
=cut
sub format_timestamptz {
my ($self, $dt) = @_;
return $dt->strftime($self->current_timestamptz_format);
}
*format_timestamp_with_time_zone = \&format_timestamptz;
=item * current_date_format
The current generated method used by C<format_datetime>,
C<format_date>, and C<current_date_parser> to keep track of
the C<strptime> translation of C<NLS_DATE_FORMAT>.
=cut
*current_date_format = _format_generator('nls_date_format');
=item * current_timestamp_format
The current generated method used by C<format_timestamp>,
C<format_timestamp_with_time_zone>, and C<current_timestamp_parser> to keep track of
the C<strptime> translation of C<NLS_TIMESTAMP_FORMAT>.
=cut
*current_timestamp_format = _format_generator('nls_timestamp_format');
=item * current_timestamptz_format
The current generated method used by C<format_timestamptz>,
C<format_timestamp_with_time_zone>, and C<current_timestamp_parser> to keep track of
the C<strptime> translation of C<NLS_TIMESTAMP_FORMAT>.
=cut
*current_timestamptz_format = _format_generator('nls_timestamp_tz_format');
sub _format_generator {
# takes a method name for getting the current Oracle format
# returns a format generator code ref
my $previous_nls_format = '';
my $previous_format = '';
my $method = shift;
sub {
my ( $self ) = @_;
unless ($previous_nls_format eq (my $current_nls_format = $self->$method())) {
$previous_nls_format = $current_nls_format;
$previous_format = $self->oracle_to_posix($current_nls_format);
}
return $previous_format;
}
}
=item * oracle_to_posix
Given an C<NLS_DATE_FORMAT>, C<NLS_TIMESTAMP_FORMAT>, or
C<NLS_TIMESTAMP_TZ_FORMAT> value, this method returns a
C<DateTime>-compatible C<strptime> format value.
Translation is currently handled by C<Convert::NLS_DATE_FORMAT>.
=cut
sub oracle_to_posix {
my ( $self, $nls_format ) = @_;
Convert::NLS_DATE_FORMAT::oracle2posix($nls_format);
}
=back
=cut
1;
__END__
=head1 LIMITATIONS
Oracle is more flexible with the case of names, such as the month,
whereas C<DateTime> generally returns names in C<ucfirst> format.
MONTH -> FEBRUARY
Month -> February
month -> february
All translate to:
%B -> February
=head2 TIME ZONES
Oracle returns all dates and timestamps in a time zone similar to
the C<DateTime> floating time zone, except for 'timestamp with time zone'
columns.
=head2 INTERVAL ELEMENTS
I have not implemented C<parse_duration>, C<format_duration>,
C<parse_interval>, nor C<format_interval>, and have no plans to do so.
If you need these features, unit tests, method implementations, and
pointers to documentation are all welcome.
=head1 SUPPORT
Support for this module is provided via the datetime@perl.org email
list. See http://lists.perl.org/ for more details.
=head1 TODO
Possibly read an environment variable to determine a time zone to
use instead of 'floating'.
Test and document creating an instance via C<new>.
=head1 AUTHOR
Nathan Gray, E<lt>kolibrie@cpan.orgE<gt>
=head1 ACKNOWLEDGEMENTS
I might have put this module off for another couple years
without the lure of Jifty, Catalyst, and DBIx::Class pulling
at me.
Thanks to Dan Horne for his RFC draft of this module.
=head1 COPYRIGHT & LICENSE
Copyright (C) 2006, 2008, 2011 Nathan Gray.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.4 or,
at your option, any later version of Perl 5 you may have available.
=head1 SEE ALSO
Convert::NLS_DATE_FORMAT
datetime@perl.org mailing list
http://datetime.perl.org/
=cut
|