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