File Coverage

blib/lib/DateTime/Format/Oracle.pm
Criterion Covered Total %
statement 47 51 92.1
branch 5 10 50.0
condition n/a
subroutine 17 20 85.0
pod 10 10 100.0
total 79 91 86.8


line stmt bran cond sub pod time code
1             package DateTime::Format::Oracle;
2              
3 5     5   161707 use strict;
  5         13  
  5         198  
4              
5 5     5   27 use Carp;
  5         10  
  5         487  
6 5     5   7521 use DateTime;
  5         1221175  
  5         274  
7 5     5   6520 use DateTime::Format::Builder;
  5         163592  
  5         124  
8 5     5   5732 use Convert::NLS_DATE_FORMAT;
  5         18974  
  5         5324  
9              
10             our $VERSION = '0.06';
11             our $nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
12             our $nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS';
13             our $nls_timestamp_tz_format = 'YYYY-MM-DD HH24:MI:SS TZHTZM';
14              
15             =head1 NAME
16              
17             DateTime::Format::Oracle - Parse and format Oracle dates and timestamps
18              
19             =head1 SYNOPSIS
20              
21             use DateTime::Format::Oracle;
22              
23             $ENV{'NLS_DATE_FORMAT'} = 'YYYY-MM-DD HH24:MI:SS';
24             my $dt = DateTime::Format::Oracle->parse_datetime('2003-01-16 23:12:01');
25             my $string = DateTime::Format::Oracle->format_datetime($dt);
26              
27             =head1 DESCRIPTION
28              
29             This module may be used to convert Oracle date and timestamp values
30             into C objects. It also can take a C object and
31             produce a date string matching the C.
32              
33             Oracle has flexible date formatting via its C session
34             variable. Date values will be returned from Oracle according to the
35             current value of that variable. Date values going into Oracle must also
36             match the current setting of C.
37              
38             Timestamp values will match either the C or
39             C session variables.
40              
41             This module keeps track of these Oracle session variable values by
42             examining environment variables of the same name. Each time one of
43             Oracle's formatting session variables is updated, the C<%ENV> hash
44             must also be updated.
45              
46             =head1 METHODS
47              
48             This class offers the following methods.
49              
50             =over 4
51              
52             =item * nls_date_format
53              
54             This method is used to determine the current value of Oracle's
55             C. It currently just reads the value from
56              
57             $ENV{'NLS_DATE_FORMAT'}
58              
59             or if that is not set, from the package variable C<$nls_date_format>,
60             which has a default value of C. This is
61             a good default to have, but is not Oracle's default. Dates will fail
62             to parse if Oracle's NLS_DATE_FORMAT and the value from this method
63             are not the same.
64              
65             If you want to use the default from this module, you can do something
66             like this after you connect to Oracle:
67              
68             $dbh->do(
69             "alter session set nls_date_format = '" .
70             DateTime::Format::Oracle->nls_date_format .
71             "'"
72             );
73              
74             =cut
75              
76 11 100   11 1 135 sub nls_date_format { $ENV{NLS_DATE_FORMAT} || $nls_date_format }
77              
78             =item * nls_timestamp_format
79              
80             This method is used to determine the current value of Oracle's
81             C. It currently just reads the value from
82              
83             $ENV{'NLS_TIMESTAMP_FORMAT'}
84              
85             or if that is not set, from the package variable C<$nls_timestamp_format>,
86             which has a default value of C. This is
87             a good default to have, but is not Oracle's default. Dates will fail
88             to parse if Oracle's NLS_TIMESTAMP_FORMAT and the value from this method
89             are not the same.
90              
91             If you want to use the default from this module, you can do something
92             like this after you connect to Oracle:
93              
94             $dbh->do(
95             "alter session set nls_timestamp_format = '" .
96             DateTime::Format::Oracle->nls_timestamp_format .
97             "'"
98             );
99              
100             =cut
101              
102 11 50   11 1 70 sub nls_timestamp_format { $ENV{NLS_TIMESTAMP_FORMAT} || $nls_timestamp_format }
103              
104             =item * nls_timestamp_tz_format
105              
106             This method is used to determine the current value of Oracle's
107             C. It currently just reads the value from
108              
109             $ENV{'NLS_TIMESTAMP_TZ_FORMAT'}
110              
111             or if that is not set, from the package variable C<$nls_timestamp_tz_format>,
112             which has a default value of C. This is
113             a good default to have, but is not Oracle's default. Dates will fail
114             to parse if Oracle's NLS_TIMESTAMP_TZ_FORMAT and the value from this method
115             are not the same.
116              
117             If you want to use the default from this module, you can do something
118             like this after you connect to Oracle:
119              
120             $dbh->do(
121             "alter session set nls_timestamp_tz_format = '" .
122             DateTime::Format::Oracle->nls_timestamp_tz_format .
123             "'"
124             );
125              
126             =cut
127              
128 0 0   0 1 0 sub nls_timestamp_tz_format { $ENV{NLS_TIMESTAMP_TZ_FORMAT} || $nls_timestamp_tz_format }
129              
130             =item * parse_datetime
131              
132             Given a string containing a date and/or time representation
133             matching C, this method will return a new
134             C object.
135              
136             If given an improperly formatted string, this method may die.
137              
138             =cut
139              
140 4     4 1 15338 sub parse_datetime { $_[0]->current_date_parser->(@_); }
141              
142             =item * parse_date
143              
144             Alias to C. Oracle's date datatype also holds
145             time information.
146              
147             =cut
148              
149             *parse_date = \&parse_datetime;
150              
151             =item * parse_timestamp
152              
153             Given a string containing a date and/or time representation
154             matching C, this method will return a new
155             C object.
156              
157             If given an improperly formatted string, this method may die.
158              
159             =cut
160              
161 5     5 1 20838 sub parse_timestamp { $_[0]->current_timestamp_parser->(@_); }
162              
163             =item * parse_timestamptz
164             =item * parse_timestamp_with_time_zone
165              
166             Given a string containing a date and/or time representation
167             matching C, this method will return a new
168             C object.
169              
170             If given an improperly formatted string, this method may die.
171              
172             =cut
173              
174 0     0 1 0 sub parse_timestamptz { $_[0]->current_timestamptz_parser->(@_); }
175             *parse_timestamp_with_time_zone = \&parse_timestamptz;
176              
177             =item * current_date_parser
178              
179             The current C generated parsing method
180             used by C and C.
181              
182             =cut
183              
184             *current_date_parser = _parser_generator('current_date_format');
185              
186             =item * current_timestamp_parser
187              
188             The current C generated parsing method
189             used by C.
190              
191             =cut
192              
193             *current_timestamp_parser = _parser_generator('current_timestamp_format');
194              
195             =item * current_timestamptz_parser
196              
197             The current C generated parsing method
198             used by C.
199              
200             =cut
201              
202             *current_timestamptz_parser = _parser_generator('current_timestamptz_format');
203              
204             sub _parser_generator {
205             # takes a method name for getting the current POSIX format
206             # returns a parser generator code ref
207 15     15   28 my $previous_format = '';
208 15         18 my $previous_parser = '';
209 15         25 my $method = shift;
210             sub {
211 9     9   27 my ( $self ) = @_;
212 9 50       44 unless ($previous_format eq (my $current_format = $self->$method())) {
213 9         19 $previous_format = $current_format;
214 9         45 $previous_parser = $self->_create_parser_method($current_format);
215             }
216 9         374 return $previous_parser;
217             }
218 15         78 }
219              
220             sub _create_parser_method {
221             # takes a strptime format, returns a parser method code ref
222 9     9   20 my ( $self, $date_format ) = @_;
223 9         63 my %parse_date = ( strptime => { pattern => $date_format } );
224 9         74 my $parser = DateTime::Format::Builder->create_parser(\%parse_date);
225 9         14286 return DateTime::Format::Builder->create_method($parser);
226             }
227              
228             =item * format_datetime
229              
230             Given a C object, this method returns a string matching
231             the current value of C.
232              
233             It is important to keep the value of C<$ENV{'NLS_DATE_FORMAT'}> the
234             same as the value of the Oracle session variable C.
235              
236             To determine the current value of Oracle's C:
237              
238             select NLS_DATE_FORMAT from NLS_SESSION_PARAMETERS
239              
240             To reset Oracle's C:
241              
242             alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
243              
244             It is generally a good idea to set C to an
245             unambiguos value, with four-digit year, and hour, minute, and second.
246              
247             =cut
248              
249             sub format_datetime {
250 6     6 1 13513 my ($self, $dt) = @_;
251 6         27 return $dt->strftime($self->current_date_format);
252             }
253              
254             =item * format_date
255              
256             Alias to C.
257              
258             =cut
259              
260             *format_date = \&format_datetime;
261              
262             =item * format_timestamp
263              
264             Given a C object, this method returns a string matching
265             the current value of C.
266              
267             It is important to keep the value of C<$ENV{'NLS_TIMESTAMP_FORMAT'}> the
268             same as the value of the Oracle session variable C.
269              
270             To determine the current value of Oracle's C:
271              
272             select NLS_TIMESTAMP_FORMAT from NLS_SESSION_PARAMETERS
273              
274             To reset Oracle's C:
275              
276             alter session set NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS'
277              
278             It is generally a good idea to set C to an
279             unambiguos value, with four-digit year, and hour, minute, and second.
280              
281             =cut
282              
283             sub format_timestamp {
284 6     6 1 3340 my ($self, $dt) = @_;
285 6         19 return $dt->strftime($self->current_timestamp_format);
286             }
287              
288             =item * format_timestamptz
289             =item * format_timestamp_with_time_zone
290              
291             Given a C object, this method returns a string matching
292             the current value of C.
293              
294             It is important to keep the value of C<$ENV{'NLS_TIMESTAMP_TZ_FORMAT'}> the
295             same as the value of the Oracle session variable C.
296              
297             To determine the current value of Oracle's C:
298              
299             select NLS_TIMESTAMP_TZ_FORMAT from NLS_SESSION_PARAMETERS
300              
301             To reset Oracle's C:
302              
303             alter session set NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS TZHTZM'
304              
305             It is generally a good idea to set C to an
306             unambiguos value, with four-digit year, and hour, minute, and second.
307              
308             =cut
309              
310             sub format_timestamptz {
311 0     0 1 0 my ($self, $dt) = @_;
312 0         0 return $dt->strftime($self->current_timestamptz_format);
313             }
314              
315             *format_timestamp_with_time_zone = \&format_timestamptz;
316              
317             =item * current_date_format
318              
319             The current generated method used by C,
320             C, and C to keep track of
321             the C translation of C.
322              
323             =cut
324              
325             *current_date_format = _format_generator('nls_date_format');
326              
327             =item * current_timestamp_format
328              
329             The current generated method used by C,
330             C, and C to keep track of
331             the C translation of C.
332              
333             =cut
334              
335             *current_timestamp_format = _format_generator('nls_timestamp_format');
336              
337             =item * current_timestamptz_format
338              
339             The current generated method used by C,
340             C, and C to keep track of
341             the C translation of C.
342              
343             =cut
344              
345             *current_timestamptz_format = _format_generator('nls_timestamp_tz_format');
346              
347             sub _format_generator {
348             # takes a method name for getting the current Oracle format
349             # returns a format generator code ref
350 15     15   21 my $previous_nls_format = '';
351 15         16 my $previous_format = '';
352 15         18 my $method = shift;
353             sub {
354 21     21   46 my ( $self ) = @_;
355 21 50       89 unless ($previous_nls_format eq (my $current_nls_format = $self->$method())) {
356 21         43 $previous_nls_format = $current_nls_format;
357 21         78 $previous_format = $self->oracle_to_posix($current_nls_format);
358             }
359 21         26276 return $previous_format;
360             }
361 15         57 }
362              
363             =item * oracle_to_posix
364              
365             Given an C, C, or
366             C value, this method returns a
367             C-compatible C format value.
368              
369             Translation is currently handled by C.
370              
371             =cut
372              
373             sub oracle_to_posix {
374 21     21 1 40 my ( $self, $nls_format ) = @_;
375 21         103 Convert::NLS_DATE_FORMAT::oracle2posix($nls_format);
376             }
377              
378             =back
379              
380             =cut
381              
382             1;
383              
384             __END__