DateTime-Format-Oracle-0.06/0000755000175000017500000000000011645166136015757 5ustar kolibriekolibrieDateTime-Format-Oracle-0.06/META.yml0000644000175000017500000000124411645166136017231 0ustar kolibriekolibrie--- #YAML:1.0 name: DateTime-Format-Oracle version: 0.06 abstract: Parse and format Oracle dates and timestamps author: - Nathan Gray license: perl distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: Convert::NLS_DATE_FORMAT: 0.03 DateTime: 0 DateTime::Format::Builder: 0 Test::More: 0 no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 DateTime-Format-Oracle-0.06/Changes0000644000175000017500000000142111645165366017254 0ustar kolibriekolibrieRevision history for DateTime-Format-Oracle 0.06 Tue Oct 11 21:18:12 2011 - added a test that specifies subsecond granularity - now requires Convert::NLS_DATE_FORMAT 0.03 (Thank you Jon Bjornstad and Zefram) 0.05 Tue Jun 10 12:55:55 2008 - implemented parse_timestamp_with_timezone and format_timestamp_with_timezone - patch provided by Jozef Kutej 0.04 Wed Feb 27 11:09:41 2008 - implemented parse_timestamp and format_timestamp - patch provided by Aldo Calpini 0.03 Thu Aug 23 10:41:48 2007 - set default value for 'nls_date_format' to use when %ENV variable not set - thanks to jaybuffington@gmail.com for sample patch 0.02 Fri Nov 3 23:40:32 2006 - CPAN release 0.01 Sun Jul 23 10:26:58 2006 - RFC version posted to datetime@perl.org DateTime-Format-Oracle-0.06/MANIFEST0000644000175000017500000000031111305111026017060 0ustar kolibriekolibrieChanges MANIFEST META.yml # Will be created by "make dist" Makefile.PL README lib/DateTime/Format/Oracle.pm t/defaults.t t/format_datetime.t t/format_timestamp.t t/parse_datetime.t t/parse_timestamp.t DateTime-Format-Oracle-0.06/README0000644000175000017500000000270111645164523016635 0ustar kolibriekolibrieDateTime-Format-Oracle The README is used to introduce the module and provide instructions on how to install the module, any machine dependencies it may have (for example C compilers and installed libraries) and any other information that should be provided before the module is installed. A README file is required for CPAN modules since CPAN extracts the README file from a module distribution so that people browsing the archive can use it get an idea of the modules uses. It is usually a good idea to provide version information here so that people can decide whether fixes for the module are worth downloading. INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install SUPPORT AND DOCUMENTATION After installing, you can find documentation for this module with the perldoc command. perldoc DateTime::Format::Oracle You can also look for information at: Search CPAN http://search.cpan.org/dist/DateTime-Format-Oracle CPAN Request Tracker: http://rt.cpan.org/NoAuth/Bugs.html?Dist=DateTime-Format-Oracle AnnoCPAN, annotated CPAN documentation: http://annocpan.org/dist/DateTime-Format-Oracle CPAN Ratings: http://cpanratings.perl.org/d/DateTime-Format-Oracle COPYRIGHT AND LICENCE Copyright (C) 2006, 2008, 2011 Nathan Gray This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. DateTime-Format-Oracle-0.06/lib/0000755000175000017500000000000011645166136016525 5ustar kolibriekolibrieDateTime-Format-Oracle-0.06/lib/DateTime/0000755000175000017500000000000011645166136020221 5ustar kolibriekolibrieDateTime-Format-Oracle-0.06/lib/DateTime/Format/0000755000175000017500000000000011645166136021451 5ustar kolibriekolibrieDateTime-Format-Oracle-0.06/lib/DateTime/Format/Oracle.pm0000644000175000017500000003050011645165341023207 0ustar kolibriekolibriepackage 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 objects. It also can take a C object and produce a date string matching the C. Oracle has flexible date formatting via its C 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. Timestamp values will match either the C or C 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. 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. 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. 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. 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. 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. 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, this method will return a new C 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. 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, this method will return a new C 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, this method will return a new C 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 generated parsing method used by C and C. =cut *current_date_parser = _parser_generator('current_date_format'); =item * current_timestamp_parser The current C generated parsing method used by C. =cut *current_timestamp_parser = _parser_generator('current_timestamp_format'); =item * current_timestamptz_parser The current C generated parsing method used by C. =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 object, this method returns a string matching the current value of C. It is important to keep the value of C<$ENV{'NLS_DATE_FORMAT'}> the same as the value of the Oracle session variable C. To determine the current value of Oracle's C: select NLS_DATE_FORMAT from NLS_SESSION_PARAMETERS To reset Oracle's C: alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS' It is generally a good idea to set C 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. =cut *format_date = \&format_datetime; =item * format_timestamp Given a C object, this method returns a string matching the current value of C. It is important to keep the value of C<$ENV{'NLS_TIMESTAMP_FORMAT'}> the same as the value of the Oracle session variable C. To determine the current value of Oracle's C: select NLS_TIMESTAMP_FORMAT from NLS_SESSION_PARAMETERS To reset Oracle's C: alter session set NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS' It is generally a good idea to set C 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 object, this method returns a string matching the current value of C. 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. To determine the current value of Oracle's C: select NLS_TIMESTAMP_TZ_FORMAT from NLS_SESSION_PARAMETERS To reset Oracle's C: alter session set NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS TZHTZM' It is generally a good idea to set C 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, C, and C to keep track of the C translation of C. =cut *current_date_format = _format_generator('nls_date_format'); =item * current_timestamp_format The current generated method used by C, C, and C to keep track of the C translation of C. =cut *current_timestamp_format = _format_generator('nls_timestamp_format'); =item * current_timestamptz_format The current generated method used by C, C, and C to keep track of the C translation of C. =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, C, or C value, this method returns a C-compatible C format value. Translation is currently handled by C. =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 generally returns names in C 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 floating time zone, except for 'timestamp with time zone' columns. =head2 INTERVAL ELEMENTS I have not implemented C, C, C, nor C, 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. =head1 AUTHOR Nathan Gray, Ekolibrie@cpan.orgE =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 DateTime-Format-Oracle-0.06/t/0000755000175000017500000000000011645166136016222 5ustar kolibriekolibrieDateTime-Format-Oracle-0.06/t/format_datetime.t0000644000175000017500000000166110522762143021550 0ustar kolibriekolibrieuse strict; # Shamelessly stolen from DateTime::Format::MySQL - Thanks Dave use Test::More tests => 6; use DateTime::Format::Oracle; my $class = 'DateTime::Format::Oracle'; my $dt = DateTime->new( year => 2000, month => 5, day => 6, hour => 15, minute => 23, second => 44, time_zone => 'UTC', ); my %tests = ( '2000-05-06 15:23:44' => 'YYYY-MM-DD HH24:MI:SS', '2000-05-06 15:23:44 UTC' => 'YYYY-MM-DD HH24:MI:SS TZR', '06-May-00' => 'DD-Mon-RR', '06-May-00 03.23.44.000000 PM' => 'DD-Mon-RR HH.MI.SSXFF AM', '06-May-00 03.23.44.000000 PM UTC' => 'DD-Mon-RR HH.MI.SSXFF AM TZR', 'Saturday, 06 May 2000' => 'DAY, DD Mon YYYY', ); foreach my $result (keys %tests) { my $nls_format = $tests{$result}; local $ENV{NLS_DATE_FORMAT} = $nls_format; is($class->format_date($dt), $result, "format_date $nls_format"); } DateTime-Format-Oracle-0.06/t/parse_datetime.t0000644000175000017500000000472510522762143021376 0ustar kolibriekolibrieuse strict; use Test::More tests => 4 * 7; # strings * methods use DateTime::Format::Oracle; my $class = 'DateTime::Format::Oracle'; my %tests = ( '2003-02-15' => { format => 'YYYY-MM-DD', year => 2003, month => 2, day => 15, hour => 0, minute => 0, second => 0, time_zone => 'floating', }, '15-FEB-03' => { format => 'DD-MON-RR', year => 2003, month => 2, day => 15, hour => 0, minute => 0, second => 0, time_zone => 'floating', }, '2003-02-15 14:39:06' => { format => 'YYYY-MM-DD HH24:MI:SS', year => 2003, month => 2, day => 15, hour => 14, minute => 39, second => 6, time_zone => 'floating', }, '15-FEB-03 02.39.06.167901 PM' => { format => 'DD-MON-RR HH.MI.SSXFF AM', year => 2003, month => 2, day => 15, hour => 14, minute => 39, second => 6, time_zone => 'floating', }, # time zone parsing seems to not work in DateTime::Format::Builder #'2003-02-15 14:39:06 AMERICA/NEW_YORK' => { # format => 'YYYY-MM-DD HH24:MI:SS TZR', # year => 2003, # month => 2, # day => 15, # hour => 14, # minute => 39, # second => 6, # time_zone => 'America/New_York', #}, #'15-FEB-03 02.39.06.167901 PM AMERICA/NEW_YORK' => { # format => 'DD-MON-RR HH.MI.SSXFF AM TZR', # year => 2003, # month => 2, # day => 15, # hour => 14, # minute => 39, # second => 6, # time_zone => 'America/New_York', #}, ); foreach my $string (keys %tests) { my $params = $tests{$string}; my $nls_format = delete $params->{format}; local $ENV{NLS_DATE_FORMAT} = $nls_format; my $dt; eval { $dt = $class->parse_date($string) }; if ($@) { warn "failed to parse date '$string' via format '$nls_format' (" . $class->current_date_format . ")"; next; } foreach my $method (keys %$params) { if ($method eq 'time_zone') { is($dt->$method->name, $params->{$method}, "$nls_format $method"); } else { is($dt->$method, $params->{$method}, "$nls_format $method"); } } } DateTime-Format-Oracle-0.06/t/defaults.t0000644000175000017500000000045411305111026020176 0ustar kolibriekolibrieuse strict; use Test::More tests => 1; use DateTime::Format::Oracle; my %tests = ( nls_date_format => 'YYYY-MM-DD HH24:MI:SS', ); foreach my $method (keys %tests) { local $ENV{uc($method)} = ''; is(DateTime::Format::Oracle->$method(), $tests{$method}, "default value for $method"); } DateTime-Format-Oracle-0.06/t/parse_timestamp.t0000644000175000017500000000537711645154735021622 0ustar kolibriekolibrieuse strict; use Test::More tests => 5 * 7; # strings * methods use DateTime::Format::Oracle; my $class = 'DateTime::Format::Oracle'; my %tests = ( '2003-02-15' => { format => 'YYYY-MM-DD', year => 2003, month => 2, day => 15, hour => 0, minute => 0, second => 0, time_zone => 'floating', }, '15-FEB-03' => { format => 'DD-MON-RR', year => 2003, month => 2, day => 15, hour => 0, minute => 0, second => 0, time_zone => 'floating', }, '2003-02-15 14:39:06' => { format => 'YYYY-MM-DD HH24:MI:SS', year => 2003, month => 2, day => 15, hour => 14, minute => 39, second => 6, time_zone => 'floating', }, '15-FEB-03 02.39.06.167901 PM' => { format => 'DD-MON-RR HH.MI.SSXFF AM', year => 2003, month => 2, day => 15, hour => 14, minute => 39, second => 6, time_zone => 'floating', }, '15-FEB-03 02.39.06.167901987 PM' => { format => 'DD-MON-RR HH.MI.SSXFF9 AM', year => 2003, month => 2, day => 15, hour => 14, minute => 39, second => 6, time_zone => 'floating', }, # time zone parsing seems to not work in DateTime::Format::Builder #'2003-02-15 14:39:06 AMERICA/NEW_YORK' => { # format => 'YYYY-MM-DD HH24:MI:SS TZR', # year => 2003, # month => 2, # day => 15, # hour => 14, # minute => 39, # second => 6, # time_zone => 'America/New_York', #}, #'15-FEB-03 02.39.06.167901 PM AMERICA/NEW_YORK' => { # format => 'DD-MON-RR HH.MI.SSXFF AM TZR', # year => 2003, # month => 2, # day => 15, # hour => 14, # minute => 39, # second => 6, # time_zone => 'America/New_York', #}, ); foreach my $string (keys %tests) { my $params = $tests{$string}; my $nls_format = delete $params->{format}; local $ENV{NLS_TIMESTAMP_FORMAT} = $nls_format; my $dt; eval { $dt = $class->parse_timestamp($string) }; if ($@) { warn "failed to parse date '$string' via format '$nls_format' (" . $class->current_timestamp_format . ")"; next; } foreach my $method (keys %$params) { if ($method eq 'time_zone') { is($dt->$method->name, $params->{$method}, "$nls_format $method"); } else { is($dt->$method, $params->{$method}, "$nls_format $method"); } } } DateTime-Format-Oracle-0.06/t/format_timestamp.t0000644000175000017500000000170011305111026021735 0ustar kolibriekolibrieuse strict; # Shamelessly stolen from DateTime::Format::MySQL - Thanks Dave use Test::More tests => 6; use DateTime::Format::Oracle; my $class = 'DateTime::Format::Oracle'; my $dt = DateTime->new( year => 2000, month => 5, day => 6, hour => 15, minute => 23, second => 44, time_zone => 'UTC', ); my %tests = ( '2000-05-06 15:23:44' => 'YYYY-MM-DD HH24:MI:SS', '2000-05-06 15:23:44 UTC' => 'YYYY-MM-DD HH24:MI:SS TZR', '06-May-00' => 'DD-Mon-RR', '06-May-00 03.23.44.000000 PM' => 'DD-Mon-RR HH.MI.SSXFF AM', '06-May-00 03.23.44.000000 PM UTC' => 'DD-Mon-RR HH.MI.SSXFF AM TZR', 'Saturday, 06 May 2000' => 'DAY, DD Mon YYYY', ); foreach my $result (keys %tests) { my $nls_format = $tests{$result}; local $ENV{NLS_TIMESTAMP_FORMAT} = $nls_format; is($class->format_timestamp($dt), $result, "format_timestamp $nls_format"); } DateTime-Format-Oracle-0.06/Makefile.PL0000644000175000017500000000102311645164605017724 0ustar kolibriekolibrieuse strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'DateTime::Format::Oracle', AUTHOR => 'Nathan Gray ', VERSION_FROM => 'lib/DateTime/Format/Oracle.pm', ABSTRACT_FROM => 'lib/DateTime/Format/Oracle.pm', LICENSE => 'perl', PL_FILES => {}, PREREQ_PM => { 'Test::More' => 0, 'DateTime' => 0, 'DateTime::Format::Builder' => 0, 'Convert::NLS_DATE_FORMAT' => 0.03, }, );