Blame examples/MySQL.pm

Packit 9002b2
# we need to comment this out or PAUSE might index it
Packit 9002b2
# pack age DateTime::Format::MySQL;
Packit 9002b2
Packit 9002b2
use strict;
Packit 9002b2
Packit 9002b2
use DateTime;
Packit 9002b2
Packit 9002b2
# Builder relevant stuff starts here.
Packit 9002b2
Packit 9002b2
use DateTime::Format::Builder (
Packit 9002b2
    parsers => {
Packit 9002b2
        parse_date => {
Packit 9002b2
            params => [qw( year month day )],
Packit 9002b2
            regex  => qr/^(\d{1,4})-(\d\d)-(\d\d)$/,
Packit 9002b2
        },
Packit 9002b2
Packit 9002b2
        parse_datetime => {
Packit 9002b2
            params => [qw( year month day hour minute second )],
Packit 9002b2
            regex  => qr/^(\d{1,4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/,
Packit 9002b2
            extra  => { time_zone => 'floating' },
Packit 9002b2
        },
Packit 9002b2
Packit 9002b2
        parse_timestamp => [
Packit 9002b2
            {
Packit 9002b2
                length => 14,
Packit 9002b2
                params => [qw( year month day hour minute second )],
Packit 9002b2
                regex  => qr/^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
Packit 9002b2
                extra  => { time_zone => 'floating' },
Packit 9002b2
            },
Packit 9002b2
            {
Packit 9002b2
                length      => 12,
Packit 9002b2
                params      => [qw( year month day hour minute second )],
Packit 9002b2
                regex       => qr/^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
Packit 9002b2
                extra       => { time_zone => 'floating' },
Packit 9002b2
                postprocess => \&_fix_2_digit_year,
Packit 9002b2
            },
Packit 9002b2
            {
Packit 9002b2
                length      => 10,
Packit 9002b2
                params      => [qw( year month day hour minute )],
Packit 9002b2
                regex       => qr/^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
Packit 9002b2
                extra       => { time_zone => 'floating' },
Packit 9002b2
                postprocess => \&_fix_2_digit_year,
Packit 9002b2
            },
Packit 9002b2
            {
Packit 9002b2
                length => 8,
Packit 9002b2
                params => [qw( year month day )],
Packit 9002b2
                regex  => qr/^(\d\d\d\d)(\d\d)(\d\d)$/,
Packit 9002b2
                extra  => { time_zone => 'floating' },
Packit 9002b2
            },
Packit 9002b2
            {
Packit 9002b2
                length      => 6,
Packit 9002b2
                params      => [qw( year month day )],
Packit 9002b2
                regex       => qr/^(\d\d)(\d\d)(\d\d)$/,
Packit 9002b2
                extra       => { time_zone => 'floating' },
Packit 9002b2
                postprocess => \&_fix_2_digit_year,
Packit 9002b2
            },
Packit 9002b2
            {
Packit 9002b2
                length      => 4,
Packit 9002b2
                params      => [qw( year month )],
Packit 9002b2
                regex       => qr/^(\d\d)(\d\d)$/,
Packit 9002b2
                extra       => { time_zone => 'floating' },
Packit 9002b2
                postprocess => \&_fix_2_digit_year,
Packit 9002b2
            },
Packit 9002b2
            {
Packit 9002b2
                length      => 2,
Packit 9002b2
                params      => [qw( year )],
Packit 9002b2
                regex       => qr/^(\d\d)$/,
Packit 9002b2
                extra       => { time_zone => 'floating' },
Packit 9002b2
                postprocess => \&_fix_2_digit_year,
Packit 9002b2
            },
Packit 9002b2
        ],
Packit 9002b2
    },
Packit 9002b2
);
Packit 9002b2
Packit 9002b2
sub _fix_2_digit_year {
Packit 9002b2
    my %p = @_;
Packit 9002b2
Packit 9002b2
    $p{parsed}{year} += $p{parsed}{year} <= 69 ? 2000 : 1900;
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
# Builder relevant stuff ends here.
Packit 9002b2
Packit 9002b2
sub format_date {
Packit 9002b2
    my ( $self, $dt ) = @_;
Packit 9002b2
Packit 9002b2
    return $dt->ymd('-');
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
sub format_time {
Packit 9002b2
    my ( $self, $dt ) = @_;
Packit 9002b2
Packit 9002b2
    return $dt->hms(':');
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
sub format_datetime {
Packit 9002b2
    my ( $self, $dt ) = @_;
Packit 9002b2
Packit 9002b2
    return $self->format_date($dt) . ' ' . $self->format_time($dt);
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
1;
Packit 9002b2
Packit 9002b2
__END__
Packit 9002b2
Packit 9002b2
=head1 NAME
Packit 9002b2
Packit 9002b2
DateTime::Format::MySQL - Parse and format MySQL dates and times
Packit 9002b2
Packit 9002b2
=head1 SYNOPSIS
Packit 9002b2
Packit 9002b2
  use DateTime::Format::MySQL;
Packit 9002b2
Packit 9002b2
  my $dt = DateTime::Format::MySQL->parse_datetime( '2003-01-16 23:12:01' );
Packit 9002b2
Packit 9002b2
  # 2003-01-16 23:12:01
Packit 9002b2
  DateTime::Format::MySQL->format_datetime($dt);
Packit 9002b2
Packit 9002b2
=head1 DESCRIPTION
Packit 9002b2
Packit 9002b2
This module understands the formats used by MySQL for its DATE,
Packit 9002b2
DATETIME, TIME, and TIMESTAMP data types.  It can be used to parse
Packit 9002b2
these formats in order to create DateTime objects, and it can take a
Packit 9002b2
DateTime object and produce a string representing it in the MySQL
Packit 9002b2
format.
Packit 9002b2
Packit 9002b2
=head1 METHODS
Packit 9002b2
Packit 9002b2
This class offers the following methods.  All of the parsing methods
Packit 9002b2
set the returned DateTime object's time zone to the floating time
Packit 9002b2
zone, because MySQL does not provide time zone information.
Packit 9002b2
Packit 9002b2
=over 4
Packit 9002b2
Packit 9002b2
=item * parse_datetime($string)
Packit 9002b2
Packit 9002b2
=item * parse_date($string)
Packit 9002b2
Packit 9002b2
=item * parse_timestamp($string)
Packit 9002b2
Packit 9002b2
Given a value of the appropriate type, this method will return a new
Packit 9002b2
C<DateTime> object.
Packit 9002b2
Packit 9002b2
If given an improperly formatted string, this method may die.
Packit 9002b2
Packit 9002b2
=item * format_date($datetime)
Packit 9002b2
Packit 9002b2
=item * format_time($datetime)
Packit 9002b2
Packit 9002b2
=item * format_datetime($datetime)
Packit 9002b2
Packit 9002b2
Given a C<DateTime> object, this methods returns an appropriately
Packit 9002b2
formatted string.
Packit 9002b2
Packit 9002b2
=back
Packit 9002b2
Packit 9002b2
=head1 SUPPORT
Packit 9002b2
Packit 9002b2
Support for this module is provided via the datetime@perl.org email
Packit 9002b2
list.  See http://lists.perl.org/ for more details.
Packit 9002b2
Packit 9002b2
=head1 AUTHOR
Packit 9002b2
Packit 9002b2
Dave Rolsky <autarch@urth.org>
Packit 9002b2
Packit 9002b2
=head1 COPYRIGHT
Packit 9002b2
Packit 9002b2
Copyright (c) 2003 David Rolsky.  All rights reserved.  This program
Packit 9002b2
is free software; you can redistribute it and/or modify it under the
Packit 9002b2
same terms as Perl itself.
Packit 9002b2
Packit 9002b2
The full text of the license can be found in the LICENSE file included
Packit 9002b2
with this module.
Packit 9002b2
Packit 9002b2
=head1 SEE ALSO
Packit 9002b2
Packit 9002b2
datetime@perl.org mailing list
Packit 9002b2
Packit 9002b2
http://datetime.perl.org/
Packit 9002b2
Packit 9002b2
=cut