Convert::NLS_DATE_FORMAT - Convert Oracle NLS_DATE_FORMAT <-> strftime Format Strings


Convert-NLS_DATE_FORMAT documentation Contained in the Convert-NLS_DATE_FORMAT distribution.

Index


Code Index:

NAME

Top

Convert::NLS_DATE_FORMAT - Convert Oracle NLS_DATE_FORMAT <-> strftime Format Strings

SYNOPSIS

Top

  use Convert::NLS_DATE_FORMAT qw(oracle2posix posix2oracle);
  my $strptime = oracle2posix($NLS_DATE_FORMAT);
  $NLS_DATE_FORMAT = posix2oracle($strftime);

DESCRIPTION

Top

Convert Oracle's NLS_DATE_FORMAT string into a strptime format string, or the reverse.

Functions

oracle2posix

Takes an Oracle NLS_DATE_FORMAT string and converts it into formatting string compatible with strftime or strptime.

  my $format = oracle2posix('YYYY-MM-DD HH24:MI:SS'); # '%Y-%m-%d %H:%M:%S'

posix2oracle

Takes a strftime or strptime formatting string and converts it into an Oracle NLS_DATE_FORMAT string. It is possible to create strings which Oracle will not accept as valid NLS_DATE_FORMAT strings.

  my $format = posix2oracle('%Y-%m-%d %H:%M:%S'); # 'YYYY-MM-DD HH24:MI:SS'

EXPORT

None by default. oracle2posix and posix2oracle when asked.

SEE ALSO

Top

DateTime::Format::Oracle.

AUTHOR

Top

Nathan Gray, <kolibrie@cpan.org>

COPYRIGHT AND LICENSE

Top


Convert-NLS_DATE_FORMAT documentation Contained in the Convert-NLS_DATE_FORMAT distribution.

package Convert::NLS_DATE_FORMAT;

use 5.006;
use strict;
use warnings;

require Exporter;

our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw(oracle2posix posix2oracle) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw();

our $VERSION = '0.02';

our @formats = (
    Q     => '%{quarter}', # quarter number
    WW    => '%U', # week number
    IW    => '%V', # ISO week number
    W     => '', # week in month
    J     => '', # Julian days since 31 Dec 4713BC
    YEAR  => '', # year spelled out
    SYYYY => '%Y', # signed year (BC is negative)
    YYYY  => '%Y', # four digit year
    IYYY  => '%G', # ISO four digit year
    YYY   => '', # last three digits of year
    IYY   => '', # ISO last three digits of year
    YY    => '%y', # last two digits of year
    IY    => '%g', # ISO last two digits of year
    RR    => '%y', # last two digits of year relative to current date
    Month => '%B', # month spelled out
    Mon   => '%b', # three-letter abbreviation month
    MM    => '%m', # month number
    RM    => '', # roman numeral month XXII
    DDD   => '%j', # day of year
    DD    => '%d', # day of month
    Day   => '%A', # day of week spelled out
    Dy    => '%a', # three-letter abbreviation day of week
    D     => '%u', # day of week
    HH24  => '%H', # hours (24)
    HH12  => '%I', # hours (12)
    HH    => '%I', # hours (12)
    MI    => '%M', # minutes
    SSSSS => '', # seconds since midnight
    SS    => '%S', # seconds
    AM    => '%p', # displays AM or PM
    PM    => '%p',
    'A.M.'=> '', # displays A.M. or P.M.
    'P.M.'=> '',
    am    => '%P', # displays am or pm
    pm    => '%P',
    'a.m.'=> '', # displays a.m. or p.m.
    'p.m.'=> '',
    BC    => '', # displays BC or AD
    AD    => '',
    'B.C.'=> '', # displays B.C. or A.D.
    'A.D.'=> '',
    XFF   => '.%6N', # special case until X can translate to %{decimal}
    FF    => '%6N',
    TZHTZM=> '%z', # time zone hour offset from UTC
    TZH   => '%z',
    TZR   => '%Z', # time zone name
    TH    => '', # appends 'st', 'nd', 'rd', 'th'
    Y     => '', # last digit of year
    I     => '', # ISO last digit of year
);

my %formats = generate_formats();

sub oracle2posix {
    my $oracle_format = shift;
    my $posix_format = $oracle_format;
    my $c = 0;
    while (my $key = $formats[$c++]) {
        my $value = $formats[$c++];
	if ($value) {
	    if ($key =~ m/^[ap]m$/i) {
		$posix_format =~ s/(?<!%)$key/$value/g;
	    } else {
		$posix_format =~ s/(?<!%)$key/$value/gi;
	    }
	}
    }
    return $posix_format;
}

sub posix2oracle {
    my $format = shift;
    # regex from DateTime
    $format =~ s/
		    		    (%{\w+})
	        	        /
		    		    $formats{$1} ? $formats{$1} : "\%$1"
	        	        /sgex;
    # special case for XFF until X can translate to %{decimal}
    $format =~ s/
                                        (\.%\d?N)
                                /
                                        "XFF"
                                /sgex;
    # regex from Date::Format
    $format =~ s/
		    		    (%[%a-zA-Z])
	        	        /
		    		    $formats{$1} ? $formats{$1} : "\%$1"
	        	        /sgex;
    return $format;
}

sub generate_formats {
    my $c = 0;
    my %f = ();
    while (my $nls = $formats[$c++]) {
        my $posix_format = $formats[$c++];
	if ($posix_format) {
	    $f{$posix_format} = $nls;
	}
    }
    $f{'%z'} = 'TZHTZM'; # special case
    return %f;
}

1;
__END__