File Coverage

blib/lib/DateTime/Format/DB2.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 32 32 100.0


line stmt bran cond sub pod time code
1             package DateTime::Format::DB2;
2              
3 3     3   559506 use strict;
  3         5  
  3         142  
4              
5 3     3   16 use vars qw ($VERSION);
  3         7  
  3         246  
6              
7             $VERSION = '0.06';
8              
9 3     3   2893 use DateTime;
  3         1899191  
  3         1535  
10             use DateTime::Format::Builder
11 3         256 ( parsers =>
12             # 12/13/2005
13             { parse_date =>
14             { params => [ qw( year month day ) ],
15             regex => qr!^(\d\d\d\d)-(\d\d)-(\d\d)$!,
16             },
17             # 12:17:46
18             parse_time =>
19             { params => [ qw( hour minute second ) ],
20             regex => qr!^(\d\d):(\d\d):(\d\d)$!,
21             extra => { time_zone => 'floating' },
22             },
23             # 2005-12-13-12.19.07.276270
24             parse_timestamp =>
25             [ { params => [ qw( year month day hour minute second nanosecond) ],
26             regex => qr/^(\d\d\d\d)-(\d\d)-(\d\d)-(\d\d)\.(\d\d)\.(\d\d)(\.\d+)?$/,
27             extra => { time_zone => 'floating' },
28             postprocess => \&_fix_nano
29             },
30             { params => [ qw( year month day hour minute second nanosecond) ],
31             regex => qr/^(\d\d\d\d)-(\d\d)-(\d\d)\s(\d\d):(\d\d):(\d\d)(\.\d+)?$/,
32             extra => { time_zone => 'floating' },
33             postprocess => \&_fix_nano
34             },
35             ],
36             parse_datetime =>
37             [ { params => [ qw( year month day hour minute second nanosecond) ],
38             regex => qr/^(\d\d\d\d)-(\d\d)-(\d\d)-(\d\d)\.(\d\d)\.(\d\d)(\.\d+)?$/,
39             extra => { time_zone => 'floating' },
40             postprocess => \&_fix_nano
41             },
42             { params => [ qw( year month day hour minute second nanosecond) ],
43             regex => qr/^(\d\d\d\d)-(\d\d)-(\d\d)\s(\d\d):(\d\d):(\d\d)(\.\d+)?$/,
44             extra => { time_zone => 'floating' },
45             postprocess => \&_fix_nano
46             },
47             ],
48             },
49 3     3   2309 );
  3         245009  
50              
51             sub _fix_nano
52             {
53 3     3   258650 my %p = @_;
54              
55 3         28 $p{parsed}{nanosecond} = int($p{parsed}{nanosecond} * 10**9);
56              
57 3         12 return 1;
58             }
59              
60             sub format_date
61             {
62 2     2 1 276886 my ( $self, $dt ) = @_;
63              
64 2         11 return $dt->ymd('-');
65             }
66              
67             sub format_time
68             {
69 1     1 1 929 my ( $self, $dt ) = @_;
70              
71 1         6 return $dt->hms(':');
72             }
73              
74             sub format_timestamp
75             {
76 1     1 1 1154 my ( $self, $dt ) = @_;
77              
78 1         6 return $self->format_date($dt) . '-' . $dt->hms('.');
79             }
80              
81             *format_datetime = *format_timestamp;
82              
83             1;
84              
85             __END__
86              
87             =head1 NAME
88              
89             DateTime::Format::DB2 - Parse and format DB2 dates and times
90              
91             =head1 SYNOPSIS
92              
93             use DateTime::Format::DB2;
94              
95             my $dt = DateTime::Format::DB2->parse_timestamp( '2003-01-16-23.12.01.300000' );
96              
97             # 2003-01-16-23.12.01.300000
98             DateTime::Format::DB2->format_timestamp($dt);
99              
100             =head1 DESCRIPTION
101              
102             This module understands the formats used by DB2 for its DATE,
103             TIME, and TIMESTAMP data types. It can be used to parse
104             these formats in order to create DateTime objects, and it can take a
105             DateTime object and produce a string representing it in the DB2
106             format.
107              
108             =head1 METHODS
109              
110             This class offers the following methods. All of the parsing methods
111             set the returned DateTime object's time zone to the floating time
112             zone, because DB2 does not provide time zone information.
113              
114             =over 4
115              
116             =item * parse_time($string)
117              
118             =item * parse_date($string)
119              
120             =item * parse_timestamp($string)
121              
122             Given a value of the appropriate type, this method will return a new
123             C<DateTime> object. The time zone for this object will always be the
124             floating time zone, because by DB2 stores the local datetime, not
125             UTC.
126              
127             If given an improperly formatted string, this method may die.
128              
129             =item * format_date($datetime)
130              
131             =item * format_time($datetime)
132              
133             =item * format_timestamp($datetime)
134              
135             Given a C<DateTime> object, this methods returns an appropriately
136             formatted string.
137              
138             =back
139              
140             =head1 SUPPORT
141              
142             Support for this module is provided via the datetime@perl.org email
143             list. See http://lists.perl.org/ for more details.
144              
145             =head1 AUTHOR
146              
147             Jess Robinson <castaway@desert-island.demon.co.uk>
148              
149             This module was shamelessly cloned from Dave Rolsky's L<DateTime::Format::MySQL> module.
150              
151             =head1 COPYRIGHT
152              
153             Copyright (c) 2005 Jess Robinson. All rights reserved. This program
154             is free software; you can redistribute it and/or modify it under the
155             same terms as Perl itself.
156              
157             =head1 SEE ALSO
158              
159             datetime@perl.org mailing list
160              
161             http://datetime.perl.org/
162              
163             =cut