DateTime::Format::MySQL - Parse and format MySQL dates and times


DateTime-Format-MySQL documentation Contained in the DateTime-Format-MySQL distribution.

Index


Code Index:

NAME

Top

DateTime::Format::MySQL - Parse and format MySQL dates and times

SYNOPSIS

Top

  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);

DESCRIPTION

Top

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.

METHODS

Top

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.

* parse_datetime($string)
* parse_date($string)
* parse_timestamp($string)

Given a value of the appropriate type, this method will return a new DateTime object. The time zone for this object will always be the floating time zone, because by MySQL stores the local datetime, not UTC.

If given an improperly formatted string, this method may die.

* format_date($datetime)
* format_time($datetime)
* format_datetime($datetime)

Given a DateTime object, this methods returns an appropriately formatted string.

SUPPORT

Top

Support for this module is provided via the datetime@perl.org email list. See http://lists.perl.org/ for more details.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT

Top

SEE ALSO

Top

datetime@perl.org mailing list

http://datetime.perl.org/


DateTime-Format-MySQL documentation Contained in the DateTime-Format-MySQL distribution.

package DateTime::Format::MySQL;

use strict;

use vars qw ($VERSION);

$VERSION = '0.04';

use DateTime;
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' },
          },
          {
            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'},
          },
          { 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;
}

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__