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