| 1 | package DateTime::Format::MySQL; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use vars qw ($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | $VERSION = '0.08'; | |||||||||
| 8 | |||||||||||
| 9 | 3 | ▶ | use DateTime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | use DateTime::Format::Builder | ||||||||||
| 11 | 3 | ▶ | ( parsers => | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | { parse_date => | ||||||||||
| 13 | { params => [ qw( year month day ) ], | ||||||||||
| 14 | regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})$/, | ||||||||||
| 15 | }, | ||||||||||
| 16 | |||||||||||
| 17 | parse_datetime => | ||||||||||
| 18 | [ { params => [ qw( year month day hour minute second ) ], | ||||||||||
| 19 | regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})[\sT](\d{1,2})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})$/, | ||||||||||
| 20 | extra => { time_zone => 'floating' }, | ||||||||||
| 21 | }, | ||||||||||
| 22 | { params => [ qw( year month day hour minute second microsecond ) ], | ||||||||||
| 23 | regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})[\sT](\d{1,2})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})\.(\d{1,6})/, | ||||||||||
| 24 | extra => { time_zone => 'floating' }, | ||||||||||
| 25 | postprocess => \&_convert_micro_to_nanosecs, | ||||||||||
| 26 | }, | ||||||||||
| 27 | ], | ||||||||||
| 28 | |||||||||||
| 29 | parse_timestamp => | ||||||||||
| 30 | [ { params => [ qw( year month day hour minute second microsecond ) ], | ||||||||||
| 31 | regex => qr/^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)\.(\d{1,6})$/, | ||||||||||
| 32 | extra => { time_zone => 'floating' }, | ||||||||||
| 33 | postprocess => \&_convert_micro_to_nanosecs, | ||||||||||
| 34 | }, | ||||||||||
| 35 | { params => [ qw( year month day hour minute second microsecond ) ], | ||||||||||
| 36 | regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})[\sT](\d{1,2})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})\.(\d{1,6})/, | ||||||||||
| 37 | extra => { time_zone => 'floating' }, | ||||||||||
| 38 | postprocess => \&_convert_micro_to_nanosecs, | ||||||||||
| 39 | }, | ||||||||||
| 40 | { length => 14, | ||||||||||
| 41 | params => [ qw( year month day hour minute second ) ], | ||||||||||
| 42 | regex => qr/^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/, | ||||||||||
| 43 | extra => { time_zone => 'floating' }, | ||||||||||
| 44 | }, | ||||||||||
| 45 | { | ||||||||||
| 46 | params => [ qw( year month day hour minute second ) ], | ||||||||||
| 47 | regex => qr/^(\d{1,4})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})[\sT](\d{1,2})[[:punct:]](\d{1,2})[[:punct:]](\d{1,2})$/, | ||||||||||
| 48 | extra => { time_zone => 'floating'}, | ||||||||||
| 49 | }, | ||||||||||
| 50 | { length => 12, | ||||||||||
| 51 | params => [ qw( year month day hour minute second ) ], | ||||||||||
| 52 | regex => qr/^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/, | ||||||||||
| 53 | extra => { time_zone => 'floating' }, | ||||||||||
| 54 | postprocess => \&_fix_2_digit_year, | ||||||||||
| 55 | }, | ||||||||||
| 56 | { length => 10, | ||||||||||
| 57 | params => [ qw( year month day hour minute ) ], | ||||||||||
| 58 | regex => qr/^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/, | ||||||||||
| 59 | extra => { time_zone => 'floating' }, | ||||||||||
| 60 | postprocess => \&_fix_2_digit_year, | ||||||||||
| 61 | }, | ||||||||||
| 62 | { length => 8, | ||||||||||
| 63 | params => [ qw( year month day ) ], | ||||||||||
| 64 | regex => qr/^(\d\d\d\d)(\d\d)(\d\d)$/, | ||||||||||
| 65 | extra => { time_zone => 'floating' }, | ||||||||||
| 66 | }, | ||||||||||
| 67 | { length => 6, | ||||||||||
| 68 | params => [ qw( year month day ) ], | ||||||||||
| 69 | regex => qr/^(\d\d)(\d\d)(\d\d)$/, | ||||||||||
| 70 | extra => { time_zone => 'floating' }, | ||||||||||
| 71 | postprocess => \&_fix_2_digit_year, | ||||||||||
| 72 | }, | ||||||||||
| 73 | { length => 4, | ||||||||||
| 74 | params => [ qw( year month ) ], | ||||||||||
| 75 | regex => qr/^(\d\d)(\d\d)$/, | ||||||||||
| 76 | extra => { time_zone => 'floating' }, | ||||||||||
| 77 | postprocess => \&_fix_2_digit_year, | ||||||||||
| 78 | }, | ||||||||||
| 79 | { length => 2, | ||||||||||
| 80 | params => [ qw( year ) ], | ||||||||||
| 81 | regex => qr/^(\d\d)$/, | ||||||||||
| 82 | extra => { time_zone => 'floating' }, | ||||||||||
| 83 | postprocess => \&_fix_2_digit_year, | ||||||||||
| 84 | }, | ||||||||||
| 85 | ], | ||||||||||
| 86 | }, | ||||||||||
| 87 | 3 | ); | |||||||||
| 88 | |||||||||||
| 89 | sub _fix_2_digit_year | ||||||||||
| 90 | 6 | ▶ | { | ||||||||
|
Subroutine
_fix_2_digit_year: called CC=2 SCAR=6.9
| |||||||||||
| 91 | 6 | my %p = @_; | |||||||||
| 92 | |||||||||||
| 93 | 6 | ▶ | $p{parsed}{year} += $p{parsed}{year} <= 69 ? 2000 : 1900; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | sub format_date | ||||||||||
| 97 | 2 | ▶ | { | ||||||||
|
Subroutine
format_date: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 98 | 2 | my ( $self, $dt ) = @_; | |||||||||
| 99 | |||||||||||
| 100 | 2 | return $dt->ymd('-'); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | sub format_time | ||||||||||
| 104 | 11 | ▶ | { | ||||||||
|
Subroutine
format_time: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 105 | 11 | my ( $self, $dt ) = @_; | |||||||||
| 106 | |||||||||||
| 107 | 11 | ▶ | return $dt->microsecond ? join('.', $dt->hms(':'), sprintf("%06d", $dt->microsecond)) : $dt->hms(':'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | sub format_datetime | ||||||||||
| 111 | 1 | ▶ | { | ||||||||
|
Subroutine
format_datetime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 112 | 1 | my ( $self, $dt ) = @_; | |||||||||
| 113 | |||||||||||
| 114 | 1 | return $self->format_date($dt) . ' ' . $self->format_time($dt); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | # DateTime constructor only has nanosecond. MySQL provides micro | ||||||||||
| 118 | sub _convert_micro_to_nanosecs | ||||||||||
| 119 | 5 | ▶ | { | ||||||||
|
Subroutine
_convert_micro_to_nanosecs: called CC=1 SCAR=0.0
| |||||||||||
| 120 | 5 | my %p = @_; | |||||||||
| 121 | 5 | my $micro_secs = delete $p{parsed}{microsecond}; | |||||||||
| 122 | |||||||||||
| 123 | # right pad with zeros | ||||||||||
| 124 | 5 | $micro_secs .= '0' x (6 - length($micro_secs)); | |||||||||
| 125 | 5 | $p{parsed}{nanosecond} = $micro_secs * 1000; | |||||||||
| 126 | 5 | return 1; # parse successful | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | 3 | 1; | |||||||||
| 132 | |||||||||||
| 133 | __END__ | ||||||||||