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);

DESCRIPTION

This module may be used to convert Oracle date and timestamp values into \*(C`DateTime\*(C' objects. It also can take a \*(C`DateTime\*(C' object and produce a date string matching the \*(C`NLS_DATE_FORMAT\*(C'.

Oracle has flexible date formatting via its \*(C`NLS_DATE_FORMAT\*(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`NLS_DATE_FORMAT\*(C'.

Timestamp values will match either the \*(C`NLS_TIMESTAMP_FORMAT\*(C' or \*(C`NLS_TIMESTAMP_TZ_FORMAT\*(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 %ENV hash must also be updated.

METHODS

This class offers the following methods.

  • nls_date_format This method is used to determine the current value of Oracle's \*(C`NLS_DATE_FORMAT\*(C'. It currently just reads the value from $ENV{'NLS_DATE_FORMAT'} or if that is not set, from the package variable $nls_date_format, which has a default value of \*(C`YYYY-MM-DD HH24:MI:SS\*(C'. This is a good default to have, but is not Oracle's default. Dates will fail to parse if Oracle's \s-1NLS_DATE_FORMAT\s0 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 . "'" );

  • nls_timestamp_format This method is used to determine the current value of Oracle's \*(C`NLS_TIMESTAMP_FORMAT\*(C'. It currently just reads the value from $ENV{'NLS_TIMESTAMP_FORMAT'} or if that is not set, from the package variable $nls_timestamp_format, which has a default value of \*(C`YYYY-MM-DD HH24:MI:SS\*(C'. This is a good default to have, but is not Oracle's default. Dates will fail to parse if Oracle's \s-1NLS_TIMESTAMP_FORMAT\s0 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 . "'" );

  • nls_timestamp_tz_format This method is used to determine the current value of Oracle's \*(C`NLS_TIMESTAMP_TZ_FORMAT\*(C'. It currently just reads the value from $ENV{'NLS_TIMESTAMP_TZ_FORMAT'} or if that is not set, from the package variable $nls_timestamp_tz_format, which has a default value of \*(C`YYYY-MM-DD HH24:MI:SS TZHTZM\*(C'. This is a good default to have, but is not Oracle's default. Dates will fail to parse if Oracle's \s-1NLS_TIMESTAMP_TZ_FORMAT\s0 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 . "'" );

  • parse_datetime Given a string containing a date and/or time representation matching \*(C`NLS_DATE_FORMAT\*(C', this method will return a new \*(C`DateTime\*(C' object. If given an improperly formatted string, this method may die.

  • parse_date Alias to \*(C`parse_datetime\*(C'. Oracle's date datatype also holds time information.

  • parse_timestamp Given a string containing a date and/or time representation matching \*(C`NLS_TIMESTAMP_FORMAT\*(C', this method will return a new \*(C`DateTime\*(C' object. If given an improperly formatted string, this method may die.

  • parse_timestamptz =item * parse_timestamp_with_time_zone Given a string containing a date and/or time representation matching \*(C`NLS_TIMESTAMP_TZ_FORMAT\*(C', this method will return a new \*(C`DateTime\*(C' object. If given an improperly formatted string, this method may die.

  • current_date_parser The current \*(C`DateTime::Format::Builder\*(C' generated parsing method used by \*(C`parse_datetime\*(C' and \*(C`parse_date\*(C'.

  • current_timestamp_parser The current \*(C`DateTime::Format::Builder\*(C' generated parsing method used by \*(C`parse_timestamp\*(C'.

  • current_timestamptz_parser The current \*(C`DateTime::Format::Builder\*(C' generated parsing method used by \*(C`parse_timestamptz\*(C'.

  • format_datetime Given a \*(C`DateTime\*(C' object, this method returns a string matching the current value of \*(C`NLS_DATE_FORMAT\*(C'. It is important to keep the value of $ENV{'NLS_DATE_FORMAT'} the same as the value of the Oracle session variable \*(C`NLS_DATE_FORMAT\*(C'. To determine the current value of Oracle's \*(C`NLS_DATE_FORMAT\*(C': select NLS_DATE_FORMAT from NLS_SESSION_PARAMETERS To reset Oracle's \*(C`NLS_DATE_FORMAT\*(C': alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS' It is generally a good idea to set \*(C`NLS_DATE_FORMAT\*(C' to an unambiguos value, with four-digit year, and hour, minute, and second.

  • format_date Alias to \*(C`format_datetime\*(C'.

  • format_timestamp Given a \*(C`DateTime\*(C' object, this method returns a string matching the current value of \*(C`NLS_TIMESTAMP_FORMAT\*(C'. It is important to keep the value of $ENV{'NLS_TIMESTAMP_FORMAT'} the same as the value of the Oracle session variable \*(C`NLS_TIMESTAMP_FORMAT\*(C'. To determine the current value of Oracle's \*(C`NLS_TIMESTAMP_FORMAT\*(C': select NLS_TIMESTAMP_FORMAT from NLS_SESSION_PARAMETERS To reset Oracle's \*(C`NLS_TIMESTAMP_FORMAT\*(C': alter session set NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS' It is generally a good idea to set \*(C`NLS_TIMESTAMP_FORMAT\*(C' to an unambiguos value, with four-digit year, and hour, minute, and second.

  • format_timestamptz =item * format_timestamp_with_time_zone Given a \*(C`DateTime\*(C' object, this method returns a string matching the current value of \*(C`NLS_TIMESTAMP_TZ_FORMAT\*(C'. It is important to keep the value of $ENV{'NLS_TIMESTAMP_TZ_FORMAT'} the same as the value of the Oracle session variable \*(C`NLS_TIMESTAMP_TZ_FORMAT\*(C'. To determine the current value of Oracle's \*(C`NLS_TIMESTAMP_TZ_FORMAT\*(C': select NLS_TIMESTAMP_TZ_FORMAT from NLS_SESSION_PARAMETERS To reset Oracle's \*(C`NLS_TIMESTAMP_TZ_FORMAT\*(C': 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\*(C' to an unambiguos value, with four-digit year, and hour, minute, and second.

  • current_date_format The current generated method used by \*(C`format_datetime\*(C', \*(C`format_date\*(C', and \*(C`current_date_parser\*(C' to keep track of the \*(C`strptime\*(C' translation of \*(C`NLS_DATE_FORMAT\*(C'.

  • current_timestamp_format The current generated method used by \*(C`format_timestamp\*(C', \*(C`format_timestamp_with_time_zone\*(C', and \*(C`current_timestamp_parser\*(C' to keep track of the \*(C`strptime\*(C' translation of \*(C`NLS_TIMESTAMP_FORMAT\*(C'.

  • current_timestamptz_format The current generated method used by \*(C`format_timestamptz\*(C', \*(C`format_timestamp_with_time_zone\*(C', and \*(C`current_timestamp_parser\*(C' to keep track of the \*(C`strptime\*(C' translation of \*(C`NLS_TIMESTAMP_FORMAT\*(C'.

  • oracle_to_posix Given an \*(C`NLS_DATE_FORMAT\*(C', \*(C`NLS_TIMESTAMP_FORMAT\*(C', or \*(C`NLS_TIMESTAMP_TZ_FORMAT\*(C' value, this method returns a \*(C`DateTime\*(C'-compatible \*(C`strptime\*(C' format value. Translation is currently handled by \*(C`Convert::NLS_DATE_FORMAT\*(C'.

LIMITATIONS

Oracle is more flexible with the case of names, such as the month, whereas \*(C`DateTime\*(C' generally returns names in \*(C`ucfirst\*(C' format.

MONTH -> FEBRUARY Month -> February month -> february

All translate to:

%B -> February

\s-1TIME\s0 \s-1ZONES\s0

Oracle returns all dates and timestamps in a time zone similar to the \*(C`DateTime\*(C' floating time zone, except for 'timestamp with time zone' columns.

\s-1INTERVAL\s0 \s-1ELEMENTS\s0

I have not implemented \*(C`parse_duration\*(C', \*(C`format_duration\*(C', \*(C`parse_interval\*(C', nor \*(C`format_interval\*(C', and have no plans to do so.

If you need these features, unit tests, method implementations, and pointers to documentation are all welcome.

SUPPORT

Support for this module is provided via the [email protected] email list. See http://lists.perl.org/ for more details.

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\*(C'.

AUTHOR

Nathan Gray, <[email protected]>

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 \s-1RFC\s0 draft of this module.

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.

RELATED TO DateTime::Format::Oracle…

Convert::NLS_DATE_FORMAT

[email protected] mailing list

http://datetime.perl.org/