File Coverage

blib/lib/Time/Piece/MySQL.pm
Criterion Covered Total %
statement 51 51 100.0
branch 10 12 83.3
condition 12 18 66.6
subroutine 14 14 100.0
pod 0 8 0.0
total 87 103 84.4


line stmt bran cond sub pod time code
1             package Time::Piece::MySQL;
2 3     3   78232 use strict;
  3         7  
  3         125  
3 3     3   15 use vars qw($VERSION);
  3         7  
  3         199  
4             $VERSION = '0.06';
5              
6 3     3   3183 use Time::Piece;
  3         53347  
  3         18  
7              
8 3     3   27 sub import { shift; @_ = ('Time::Piece', @_); goto &Time::Piece::import }
  3         10  
  3         17  
9              
10             package Time::Piece;
11              
12 3     3   397 use Time::Seconds;
  3         4  
  3         1099  
13              
14             BEGIN
15             {
16             # I don't know what this dst bug is, but the code was here...
17 3     3   17 my $has_dst_bug =
18             Time::Piece->strptime( '20000601120000', '%Y %m %d %H %M %S' )->hour != 12;
19 3     3 0 17 sub HAS_DST_BUG () { $has_dst_bug }
20             }
21              
22             sub mysql_date
23             {
24 8     8 0 1067 my $self = shift;
25 8         19 my $old_sep = $self->date_separator('-');
26 8         69 my $ymd = $self->ymd;
27 8         171 $self->date_separator($old_sep);
28 8         65 return $ymd;
29             }
30              
31             sub mysql_time
32             {
33 7     7 0 579 my $self = shift;
34 7         16 my $old_sep = $self->time_separator(':');
35 7         54 my $hms = $self->hms;
36 7         59 $self->time_separator($old_sep);
37 7         62 return $hms;
38             }
39              
40             sub mysql_datetime
41             {
42 5     5 0 1121 my $self = shift;
43 5         9 return join ' ', $self->mysql_date, $self->mysql_time;
44             }
45              
46              
47             # '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
48              
49             sub from_mysql_date {
50 6     6 0 1608 my ($class, $dt) = @_;
51 6 100 100     66 return unless $dt and $dt ge '1970' and $dt lt '2038';
      100        
52 2         4 my $time = eval {$class->strptime($dt, '%Y-%m-%d')};
  2         8  
53 2         53 return $time;
54             }
55              
56             sub from_mysql_datetime {
57 3     3 0 20 my ($class, $dt) = @_;
58 3 50 33     47 return unless $dt and $dt ge '1970' and $dt lt '2038';
      33        
59 3         7 my $time = eval {$class->strptime($dt, '%Y-%m-%d %H:%M:%S')};
  3         17  
60 3 50 33     103 $time -= ONE_HOUR if HAS_DST_BUG && $time->isdst;
61 3         11 return $time;
62             }
63              
64             sub mysql_timestamp {
65 8     8 0 26 my $self = shift;
66 8         24 return $self->strftime('%Y%m%d%H%M%S');
67             }
68              
69             sub from_mysql_timestamp {
70             # From MySQL version 4.1, timestamps are returned as datetime strings
71 8     8 0 4633 my ($class, $timestamp) = @_;
72 8         12 my $length = length $timestamp;
73 8 100       23 return from_mysql_datetime(@_) if $length == 19;
74             # most timestamps have 2-digit years, except 8 and 14 char ones
75 7 100 100     35 if ( $length != 14 && $length != 8 ) {
76 5 100       21 $timestamp = (substr($timestamp, 0, 2) < 70 ? "20" : "19")
77             . $timestamp;
78             }
79             # now we need to extend this to 14 chars to make sure we get
80             # consistent cross-platform results
81 7         15 $timestamp .= substr("19700101000000", length $timestamp);
82 7         9 my $time = eval {$class->strptime( $timestamp, '%Y %m %d %H %M %S')};
  7         22  
83 7         187 return $time;
84             }
85              
86             1;
87              
88             __END__