File Coverage

blib/lib/DateTime/Format/MySQL.pm
Criterion Covered Total %
statement 25 25 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 41 41 100.0


line stmt bran cond sub pod time code
1             package DateTime::Format::MySQL;
2              
3 3     3   211151 use strict;
  3         25  
  3         104  
4              
5 3     3   28 use vars qw ($VERSION);
  3         8  
  3         162  
6              
7             $VERSION = '0.0701';
8              
9 3     3   2919 use DateTime;
  3         1554960  
  3         1961  
10             use DateTime::Format::Builder
11 3         330 ( parsers =>
12             { parse_date =>
13             { params => [ qw( year month day ) ],
14             regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})$/,
15             },
16              
17             parse_datetime =>
18             [ { params => [ qw( year month day hour minute second ) ],
19             regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})[\sT](\d{1,2})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})$/,
20             extra => { time_zone => 'floating' },
21             },
22             { params => [ qw( year month day hour minute second microsecond ) ],
23             regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})[\sT](\d{1,2})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})\.(\d{1,6})/,
24             extra => { time_zone => 'floating' },
25             postprocess => \&_convert_micro_to_nanosecs,
26             },
27             ],
28              
29             parse_timestamp =>
30             [ { params => [ qw( year month day hour minute second microsecond ) ],
31             regex => qr/^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)\.(\d{1,6})$/,
32             extra => { time_zone => 'floating' },
33             postprocess => \&_convert_micro_to_nanosecs,
34             },
35             { params => [ qw( year month day hour minute second microsecond ) ],
36             regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})[\sT](\d{1,2})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})\.(\d{1,6})/,
37             extra => { time_zone => 'floating' },
38             postprocess => \&_convert_micro_to_nanosecs,
39             },
40             { length => 14,
41             params => [ qw( year month day hour minute second ) ],
42             regex => qr/^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
43             extra => { time_zone => 'floating' },
44             },
45             {
46             params => [ qw( year month day hour minute second ) ],
47             regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})[\sT](\d{1,2})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})$/,
48             extra => { time_zone => 'floating'},
49             },
50             { length => 12,
51             params => [ qw( year month day hour minute second ) ],
52             regex => qr/^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
53             extra => { time_zone => 'floating' },
54             postprocess => \&_fix_2_digit_year,
55             },
56             { length => 10,
57             params => [ qw( year month day hour minute ) ],
58             regex => qr/^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
59             extra => { time_zone => 'floating' },
60             postprocess => \&_fix_2_digit_year,
61             },
62             { length => 8,
63             params => [ qw( year month day ) ],
64             regex => qr/^(\d\d\d\d)(\d\d)(\d\d)$/,
65             extra => { time_zone => 'floating' },
66             },
67             { length => 6,
68             params => [ qw( year month day ) ],
69             regex => qr/^(\d\d)(\d\d)(\d\d)$/,
70             extra => { time_zone => 'floating' },
71             postprocess => \&_fix_2_digit_year,
72             },
73             { length => 4,
74             params => [ qw( year month ) ],
75             regex => qr/^(\d\d)(\d\d)$/,
76             extra => { time_zone => 'floating' },
77             postprocess => \&_fix_2_digit_year,
78             },
79             { length => 2,
80             params => [ qw( year ) ],
81             regex => qr/^(\d\d)$/,
82             extra => { time_zone => 'floating' },
83             postprocess => \&_fix_2_digit_year,
84             },
85             ],
86             },
87 3     3   2284 );
  3         216823  
88              
89             sub _fix_2_digit_year
90             {
91 6     6   17891 my %p = @_;
92              
93 6 100       40 $p{parsed}{year} += $p{parsed}{year} <= 69 ? 2000 : 1900;
94             }
95              
96             sub format_date
97             {
98 2     2 1 800 my ( $self, $dt ) = @_;
99              
100 2         7 return $dt->ymd('-');
101             }
102              
103             sub format_time
104             {
105 6     6 1 5184 my ( $self, $dt ) = @_;
106              
107 6 100       19 return $dt->microsecond ? join('.', $dt->hms(':'), $dt->microsecond) : $dt->hms(':');
108             }
109              
110             sub format_datetime
111             {
112 1     1 1 873 my ( $self, $dt ) = @_;
113              
114 1         4 return $self->format_date($dt) . ' ' . $self->format_time($dt);
115             }
116              
117             # DateTime constructor only has nanosecond. MySQL provides micro
118             sub _convert_micro_to_nanosecs
119             {
120 5     5   35349 my %p = @_;
121 5         15 my $micro_secs = delete $p{parsed}{microsecond};
122              
123             # right pad with zeros
124 5         15 $micro_secs .= '0' x (6 - length($micro_secs));
125 5         17 $p{parsed}{nanosecond} = $micro_secs * 1000;
126 5         17 return 1; # parse successful
127             }
128              
129              
130              
131             1;
132              
133             __END__
134              
135             =head1 NAME
136              
137             DateTime::Format::MySQL - Parse and format MySQL dates and times
138              
139             =head1 SYNOPSIS
140              
141             use DateTime::Format::MySQL;
142              
143             my $dt = DateTime::Format::MySQL->parse_datetime( '2003-01-16 23:12:01' );
144              
145             # 2003-01-16 23:12:01
146             DateTime::Format::MySQL->format_datetime($dt);
147              
148             =head1 DESCRIPTION
149              
150             This module understands the formats used by MySQL for its DATE,
151             DATETIME, TIME, and TIMESTAMP data types. It can be used to parse
152             these formats in order to create DateTime objects, and it can take a
153             DateTime object and produce a string representing it in the MySQL
154             format.
155              
156             =head1 METHODS
157              
158             This class offers the following methods. All of the parsing methods
159             set the returned DateTime object's time zone to the floating time
160             zone, because MySQL does not provide time zone information.
161              
162             =over 4
163              
164             =item * parse_datetime($string)
165              
166             =item * parse_date($string)
167              
168             =item * parse_timestamp($string)
169              
170             Given a value of the appropriate type, this method will return a new
171             C<DateTime> object. The time zone for this object will always be the
172             floating time zone, because by MySQL stores the local datetime, not
173             UTC.
174              
175             If given an improperly formatted string, this method may die.
176              
177             =item * format_date($datetime)
178              
179             =item * format_time($datetime)
180              
181             =item * format_datetime($datetime)
182              
183             Given a C<DateTime> object, this methods returns an appropriately
184             formatted string.
185              
186             =back
187              
188             =head1 SUPPORT
189              
190             Support for this module is provided via the datetime@perl.org email
191             list. See http://lists.perl.org/ for more details.
192              
193             =head1 AUTHOR
194              
195             Dave Rolsky <autarch@urth.org>
196              
197             =head1 COPYRIGHT
198              
199             Copyright (c) 2003-2014 David Rolsky. All rights reserved. This program
200             is free software; you can redistribute it and/or modify it under the
201             same terms as Perl itself.
202              
203             The full text of the license can be found in the LICENSE file included
204             with this module.
205              
206             =head1 SEE ALSO
207              
208             datetime@perl.org mailing list
209              
210             http://datetime.perl.org/
211              
212             =cut