Time-Piece-MySQL-0.06/0000755000175000017500000000000011022523706013373 5ustar martymartyTime-Piece-MySQL-0.06/MANIFEST0000644000175000017500000000026010077500435014525 0ustar martymartyChanges MANIFEST Makefile.PL README lib/Time/Piece/MySQL.pm t/basic.t t/datetime.t t/timestamp.t META.yml Module meta-data (added by MakeMaker) Time-Piece-MySQL-0.06/lib/0000755000175000017500000000000011022523706014141 5ustar martymartyTime-Piece-MySQL-0.06/lib/Time/0000755000175000017500000000000011022523706015037 5ustar martymartyTime-Piece-MySQL-0.06/lib/Time/Piece/0000755000175000017500000000000011022523706016064 5ustar martymartyTime-Piece-MySQL-0.06/lib/Time/Piece/MySQL.pm0000644000175000017500000001015311022522104017356 0ustar martymartypackage Time::Piece::MySQL; use strict; use vars qw($VERSION); $VERSION = '0.06'; use Time::Piece; sub import { shift; @_ = ('Time::Piece', @_); goto &Time::Piece::import } package Time::Piece; use Time::Seconds; BEGIN { # I don't know what this dst bug is, but the code was here... my $has_dst_bug = Time::Piece->strptime( '20000601120000', '%Y %m %d %H %M %S' )->hour != 12; sub HAS_DST_BUG () { $has_dst_bug } } sub mysql_date { my $self = shift; my $old_sep = $self->date_separator('-'); my $ymd = $self->ymd; $self->date_separator($old_sep); return $ymd; } sub mysql_time { my $self = shift; my $old_sep = $self->time_separator(':'); my $hms = $self->hms; $self->time_separator($old_sep); return $hms; } sub mysql_datetime { my $self = shift; return join ' ', $self->mysql_date, $self->mysql_time; } # '1000-01-01 00:00:00' to '9999-12-31 23:59:59' sub from_mysql_date { my ($class, $dt) = @_; return unless $dt and $dt ge '1970' and $dt lt '2038'; my $time = eval {$class->strptime($dt, '%Y-%m-%d')}; return $time; } sub from_mysql_datetime { my ($class, $dt) = @_; return unless $dt and $dt ge '1970' and $dt lt '2038'; my $time = eval {$class->strptime($dt, '%Y-%m-%d %H:%M:%S')}; $time -= ONE_HOUR if HAS_DST_BUG && $time->isdst; return $time; } sub mysql_timestamp { my $self = shift; return $self->strftime('%Y%m%d%H%M%S'); } sub from_mysql_timestamp { # From MySQL version 4.1, timestamps are returned as datetime strings my ($class, $timestamp) = @_; my $length = length $timestamp; return from_mysql_datetime(@_) if $length == 19; # most timestamps have 2-digit years, except 8 and 14 char ones if ( $length != 14 && $length != 8 ) { $timestamp = (substr($timestamp, 0, 2) < 70 ? "20" : "19") . $timestamp; } # now we need to extend this to 14 chars to make sure we get # consistent cross-platform results $timestamp .= substr("19700101000000", length $timestamp); my $time = eval {$class->strptime( $timestamp, '%Y %m %d %H %M %S')}; return $time; } 1; __END__ =head1 NAME Time::Piece::MySQL - Adds MySQL-specific methods to Time::Piece =head1 SYNOPSIS use Time::Piece::MySQL; my $time = localtime; print $time->mysql_datetime; print $time->mysql_date; print $time->mysql_time; my $time = Time::Piece->from_mysql_datetime( $mysql_datetime ); my $time = Time::Piece->from_mysql_date( $mysql_date ); my $time = Time::Piece->from_mysql_timestamp( $mysql_timestamp ); =head1 DESCRIPTION Using this module instead of, or in addition to, C adds a few MySQL-specific date-time methods to C objects. =head1 OBJECT METHODS =head2 mysql_date / mysql_time / mysql_datetime / mysql_timestamp Returns the date and/or time in a format suitable for use by MySQL. =head1 CONSTRUCTORS =head2 from_mysql_date / from_mysql_datetime / from_mysql_timestamp Given a date, datetime, or timestamp value as returned from MySQL, these constructors return a new Time::Piece object. If the value is NULL, they will retrun undef. =head2 CAVEAT C itself only works with times in the Unix epoch, this module has the same limitation. However, MySQL itself handles date and datetime columns from '1000-01-01' to '9999-12-31'. Feeding in times outside of the Unix epoch to any of the constructors has unpredictable results. Also, MySQL doesn't validate dates (because your application should); it only checks that dates are in the right format. So, your database might include dates like 2004-00-00 or 2001-02-31. Passing invalid dates to any of the constructors is a bad idea: on my system the former type (with zeros) returns undef (previous version used to die) while the latter returns a date in the following month. =head1 AUTHOR Original author: Dave Rolsky Current maintainer: Marty Pauley =head1 COPYRIGHT (c) 2002 Dave Rolsky (c) 2004 Marty Pauley This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L =cut Time-Piece-MySQL-0.06/t/0000755000175000017500000000000011022523706013636 5ustar martymartyTime-Piece-MySQL-0.06/t/basic.t0000644000175000017500000000112607761650156015123 0ustar martymarty#!/usr/bin/perl use strict; use Test::More tests => 12; use Time::Piece::MySQL; my $lt = localtime; isa_ok( $lt, 'Time::Piece' ); my $gmt = gmtime; isa_ok( $gmt, 'Time::Piece' ); for my $t ( $lt, $gmt ) { is( $t->mysql_date, $t->ymd ); is( $t->mysql_time, $t->hms ); is( $t->mysql_datetime, join ' ', $t->ymd, $t->hms ); } my $t = Time::Piece->from_mysql_datetime( $lt->mysql_datetime ); isa_ok( $t, 'Time::Piece' ); is( $t->mysql_datetime, $lt->mysql_datetime ); my $t2 = Time::Piece->from_mysql_date( $lt->mysql_date ); isa_ok( $t2, 'Time::Piece' ); is( $t2->ymd, $lt->ymd ); Time-Piece-MySQL-0.06/t/datetime.t0000644000175000017500000000136310007751206015622 0ustar martymarty#!/usr/bin/perl use strict; use warnings; use Test::More tests => 6; use Time::Piece::MySQL; my $t = Time::Piece->from_mysql_datetime('2012-02-11 05:45:37'); isa_ok( $t, 'Time::Piece' ); $t = Time::Piece->from_mysql_date('2012-02-11'); isa_ok( $t, 'Time::Piece' ); my @null = qw/ 0000-00-00 1000-01-01 9999-12-31 /; for my $d (@null) { ok !defined Time::Piece->from_mysql_date($d), "$d is not in range"; } ok !defined Time::Piece->from_mysql_date(undef), "null is not in range"; # # What should we do with these dates? # In some tests, @bad dates produced undef but @ugly dates produced # Time::Piece objects in the following month. # my @bad = qw/ 2001-00-00 2001-00-31 2001-02-00 2001-04-00 /; my @ugly = qw/ 2001-02-31 2001-04-31 2001-11-31 /; Time-Piece-MySQL-0.06/t/timestamp.t0000644000175000017500000000117611022521311016021 0ustar martymarty#!/usr/bin/perl use strict; use Test::More; use Time::Piece::MySQL; my %timestamp = ( '70' => '19700101000000', '1202' => '20120201000000', '120211' => '20120211000000', '20120211' => '20120211000000', '1202110545' => '20120211054500', '120211054537' => '20120211054537', '20120211054537' => '20120211054537', '2005-08-10 23:20:48' => '20050810232048', ); #my @null = qw/ 19691231235959 20380101000000 /; plan tests => scalar keys %timestamp; for my $stamp (keys %timestamp) { my $t = Time::Piece->from_mysql_timestamp($stamp); is $t->mysql_timestamp, $timestamp{$stamp}, "timestamp $stamp"; } Time-Piece-MySQL-0.06/Changes0000644000175000017500000000207711022522550014670 0ustar martymarty0.06 2008-06-07 - added spaces to the strftime formats, as required by POSIX. Thanks to Brandt Kurowski for highlighting the problem. Fixes bug #32580. - support newer MySQL formats. Fixes bug #23840. 0.05 Jul 21, 2004 - It seems that 0.04 was incorrectly bundled onto CPAN after my Solaris testing. 0.04 Feb 02, 2004 - fixed the test failure on sun4-solaris (and probably others) The problem was a bug (IMO) in strptime on Solaris: if you didn't scan the month-day in the date, it filled the struct tm with 0, which causes mktime to go back to the previous month. - the constructors now wrap Time::Piece calls in eval so they won't die when you pass invalid dates from MySQL. Your application should vaidate dates before storing them in MySQL, but (like me) you may have to process data that some less careful programmer has left for you. 0.03 May 02, 2003 - new maintainer: Marty Pauley - added mysql_timestamp method - now using Test::More 0.02 Jun 11, 2002 - Forgot to fill in README. Doh! 0.01 Jun 11, 2002 - First release Time-Piece-MySQL-0.06/README0000644000175000017500000000077410077500413014262 0ustar martymartyTime::Piece::MySQL ================== Time::Piece::MySQL, when used, adds several MySQL-specific methods to the Time::Piece class. INSTALLATION To install this module type the following: perl Makefile.PL make make test make install DEPENDENCIES This module won't do much without Time::Piece! COPYRIGHT AND LICENCE Copyright (c) 2004 Marty Pauley Copyright (c) 2002 David Rolsky This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Time-Piece-MySQL-0.06/Makefile.PL0000644000175000017500000000045711022522703015347 0ustar martymartyuse ExtUtils::MakeMaker; WriteMakefile( NAME => 'Time::Piece::MySQL', VERSION_FROM => 'lib/Time/Piece/MySQL.pm', PREREQ_PM => { 'Time::Piece' => 1.03, 'Test::More' => 0.47 }, ABSTRACT_FROM => 'lib/Time/Piece/MySQL.pm', AUTHOR => 'Marty Pauley ', ); Time-Piece-MySQL-0.06/META.yml0000664000175000017500000000076711022523707014661 0ustar martymarty--- #YAML:1.0 name: Time-Piece-MySQL version: 0.06 abstract: Adds MySQL-specific methods to Time::Piece license: ~ author: - Marty Pauley generated_by: ExtUtils::MakeMaker version 6.42 distribution_type: module requires: Test::More: 0.47 Time::Piece: 1.03 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.3.html version: 1.3