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