File Coverage

blib/lib/Convert/NLS_DATE_FORMAT.pm
Criterion Covered Total %
statement 39 39 100.0
branch 12 14 85.7
condition n/a
subroutine 7 7 100.0
pod 2 3 66.6
total 60 63 95.2


line stmt bran cond sub pod time code
1             package Convert::NLS_DATE_FORMAT;
2              
3 2     2   67544 use 5.006;
  2         12  
  2         2492  
4 2     2   15 use strict;
  2         3  
  2         131  
5 2     2   9 use warnings;
  2         11  
  2         1972  
6              
7             require Exporter;
8              
9             our @ISA = qw(Exporter);
10             our %EXPORT_TAGS = ( 'all' => [ qw(oracle2posix posix2oracle) ] );
11             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
12             our @EXPORT = qw();
13              
14             our $VERSION = '0.05';
15              
16             our @formats = (
17             [ Q => '%{quarter}' ], # quarter number
18             [ WW => '%U' ], # week number
19             [ IW => '%V' ], # ISO week number
20             [ W => '' ], # week in month
21             [ J => '' ], # Julian days since 31 Dec 4713BC
22             [ YEAR => '' ], # year spelled out
23             [ SYYYY => '%Y' ], # signed year (BC is negative)
24             [ YYYY => '%Y' ], # four digit year
25             [ IYYY => '%G' ], # ISO four digit year
26             [ YYY => '' ], # last three digits of year
27             [ IYY => '' ], # ISO last three digits of year
28             [ YY => '%y' ], # last two digits of year
29             [ IY => '%g' ], # ISO last two digits of year
30             [ RR => '%y' ], # last two digits of year relative to current date
31             [ Month => '%B' ], # month spelled out
32             [ Mon => '%b' ], # three-letter abbreviation month
33             [ MM => '%m' ], # month number
34             [ RM => '' ], # roman numeral month XXII
35             [ DDD => '%j' ], # day of year
36             [ DD => '%d' ], # day of month
37             [ Day => '%A' ], # day of week spelled out
38             [ Dy => '%a' ], # three-letter abbreviation day of week
39             [ D => '%u' ], # day of week
40             [ HH24 => '%H' ], # hours (24)
41             [ HH12 => '%I' ], # hours (12)
42             [ HH => '%I' ], # hours (12)
43             [ MI => '%M' ], # minutes
44             [ SSSSS => '' ], # seconds since midnight
45             [ SS => '%S' ], # seconds
46             [ AM => '%p' ], # displays AM or PM
47             [ PM => '%p' ],
48             [ 'A.M.'=> '' ], # displays A.M. or P.M.
49             [ 'P.M.'=> '' ],
50             [ am => '%P' ], # displays am or pm
51             [ pm => '%P' ],
52             [ 'a.m.'=> '' ], # displays a.m. or p.m.
53             [ 'p.m.'=> '' ],
54             [ BC => '' ], # displays BC or AD
55             [ AD => '' ],
56             [ 'B.C.'=> '' ], # displays B.C. or A.D.
57             [ 'A.D.'=> '' ],
58             [ XFF9 => '.%9N' ], # special case until X can translate to %{decimal}
59             [ XFF6 => '.%6N' ], # special case until X can translate to %{decimal}
60             [ XFF3 => '.%3N' ], # special case until X can translate to %{decimal}
61             [ XFF => '.%6N' ], # special case until X can translate to %{decimal}
62             [ FF => '%6N' ],
63             [ TZHTZM=> '%z' ], # time zone hour offset from UTC
64             [ TZH => '%z' ],
65             [ TZR => '%Z' ], # time zone name
66             [ TH => '' ], # appends 'st', 'nd', 'rd', 'th'
67             [ Y => '' ], # last digit of year
68             [ I => '' ], # ISO last digit of year
69             );
70              
71             my %formats = generate_formats();
72              
73             sub oracle2posix {
74 18     18 1 7365 my ($oracle_format) = @_;
75             # quoted strings require separate processing
76 20         44 return join(
77             '',
78 18         72 map { _convert_oracle2posix($_) }
79             split(/(".*?")/, $oracle_format)
80             );
81             }
82              
83             sub _convert_oracle2posix {
84 20     20   30 my ($oracle_format) = @_;
85              
86             # return quoted strings as-is, with the quotes removed
87 20 100       59 return $1 if $oracle_format =~ m/^"(.*?)"$/;
88              
89 19         25 my $string = $oracle_format;
90 19         32 foreach my $pair (@formats) {
91 988         1823 my ($key, $value) = @$pair;
92              
93             # all are case insensitive except am/pm
94 988 100       9844 $key = qr/$key/i unless $key =~ m/^[ap]m$/i;
95              
96             # translate formats found in $oracle_format
97 988 100       21139 if ($string =~ /(?
98 106 100       180 if ($value) {
99 104         1575 $string =~ s/(?
100             } else {
101 2         30 my ($format) = $string =~ /(?
102 2         80 warn "Oracle format '$format' has no POSIX equivalent.\n";
103             }
104             }
105             }
106 19         172 return $string;
107             }
108              
109             sub posix2oracle {
110 11     11 1 4480 my ($format) = @_;
111             # regex from DateTime
112 11         29 $format =~ s/
113             (%{\w+})
114             /
115 1 50       9 $formats{$1} ? $formats{$1} : "\%$1"
116             /sgex;
117             # special case for XFF until X can translate to %{decimal}
118 11         26 $format =~ s/
119             (\.%\d?N)
120             /
121 2         8 "XFF"
122             /sgex;
123             # regex from Date::Format
124 11         46 $format =~ s/
125             (%[%a-zA-Z])
126             /
127 60 50       282 $formats{$1} ? $formats{$1} : "\%$1"
128             /sgex;
129 11         54 return $format;
130             }
131              
132             sub generate_formats {
133 2     2 0 5 my %f = ();
134 2         6 foreach my $pair (@formats) {
135 104         118 my ($nls, $posix_format) = @$pair;
136 104 100       344 if ($posix_format) {
137 68         131 $f{$posix_format} = $nls;
138             }
139             }
140 2         6 $f{'%z'} = 'TZHTZM'; # special case
141 2         40 return %f;
142             }
143              
144             1;
145             __END__