| 1 | package DBIO::Storage::DateTimeFormat; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Strptime-backed datetime format base for driver format classes | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use base 'DBIO::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use DBIO::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | __PACKAGE__->mk_group_accessors('inherited' => qw/ | |||||||||||||||||||||||||||
| 14 | preferred_format_class | ||||||||||||||||||||||||||||
| 15 | datetime_parse_pattern datetime_format_pattern | ||||||||||||||||||||||||||||
| 16 | date_parse_pattern date_format_pattern | ||||||||||||||||||||||||||||
| 17 | /); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 6 | my %preferred_loadable; | |||||||||||||||||||||||||||
| 21 | my %strptime_cache; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 27 | ▶ | sub _preferred { | ||||||||||||||||||||||||||
|
Subroutine
_preferred: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 24 | 27 | my $class = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 27 | ▶ | my $preferred = $class->preferred_format_class | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | or return undef; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | $preferred_loadable{$preferred} //= $class->load_optional_class($preferred) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$preferred_loadable{$preferred} //= $class->load_optional_class($preferred) ? 1 : 0
MC/DC100%1/1
$preferred_loadable{$preferred} //= $class->load_optional_class($preferred) ? 1 : 0
$preferred_loadable{$preferred}
Truth table100%2/2
A
A= $preferred_loadable{$preferred}
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | return $preferred_loadable{$preferred} ? $preferred : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 25 | ▶ | sub _strptime_for { | ||||||||||||||||||||||||||
|
Subroutine
_strptime_for: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 36 | 25 | my ($class, $slot, $accessor) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 25 | ▶ | return $strptime_cache{"$class|$slot"} //= do { | ||||||||||||||||||||||||||
|
Condition100%3/3
$strptime_cache{"$class|$slot"} //= do {
my $pattern = $class->$accessor or "DBIO::Exception"->throw("$class defines no $accessor and no loadable preferred_format_class");
unless (eval {
do {
require DateTime::Format::Strptime;
1
}
}) {
my $err = $@;
"DBIO::Exception"->throw("$class needs DateTime::Format::Strptime to parse/format datetimes, " . 'but it is not installed. Install DateTime::Format::Strptime (part ' . "of the optional DBIO::Timestamp/DateTime feature) and retry." . "\n\nUnderlying error: $err");
};
"DateTime::Format::Strptime"->new("pattern", $pattern, "on_error", "croak")
}
MC/DC100%2/2
$strptime_cache{"$class|$slot"} //= do {
my $pattern = $class->$accessor or "DBIO::Exception"->throw("$class defines no $accessor and no loadable preferred_format_class");
unless (eval {
do {
require DateTime::Format::Strptime;
1
}
}) {
my $err = $@;
"DBIO::Exception"->throw("$class needs DateTime::Format::Strptime to parse/format datetimes, " . 'but it is not installed. Install DateTime::Format::Strptime (part ' . "of the optional DBIO::Timestamp/DateTime feature) and retry." . "\n\nUnderlying error: $err");
};
"DateTime::Format::Strptime"->new("pattern", $pattern, "on_error", "croak")
}
$strptime_cache{"$class|$slot"}
do {
my $pattern = $class->$accessor or "DBIO::Exception"->throw("$class defines no $accessor and no loadable preferred_format_class");
unless (eval {
do {
require DateTime::Format::Strptime;
1
}
}) {
my $err = $@;
"DBIO::Exception"->throw("$class needs DateTime::Format::Strptime to parse/format datetimes, " . 'but it is not installed. Install DateTime::Format::Strptime (part ' . "of the optional DBIO::Timestamp/DateTime feature) and retry." . "\n\nUnderlying error: $err");
};
"DateTime::Format::Strptime"->new("pattern", $pattern, "on_error", "croak")
}
Truth table100%3/3
A //= B
A= $strptime_cache{"$class|$slot"}B= do {
my $pattern = $class->$accessor or "DBIO::Exception"->throw("$class defines no $accessor and no loadable preferred_format_class");
unless (eval {
do {
require DateTime::Format::Strptime;
1
}
}) {
my $err = $@;
"DBIO::Exception"->throw("$class needs DateTime::Format::Strptime to parse/format datetimes, " . 'but it is not installed. Install DateTime::Format::Strptime (part ' . "of the optional DBIO::Timestamp/DateTime feature) and retry." . "\n\nUnderlying error: $err");
};
"DateTime::Format::Strptime"->new("pattern", $pattern, "on_error", "croak")
}
| |||||||||||||||||||||||||||||
| 39 | 19 | ▶ | my $pattern = $class->$accessor | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | or DBIO::Exception->throw( | ||||||||||||||||||||||||||||
| 41 | "$class defines no $accessor and no loadable preferred_format_class" | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Loaded lazily, not at compile time: a bare DBIO install that | ||||||||||||||||||||||||||||
| 45 | # never inflates datetimes carries no DateTime stack at all. Only a driver | ||||||||||||||||||||||||||||
| 46 | # format class that actually parses/formats via strptime patterns pulls it | ||||||||||||||||||||||||||||
| 47 | # in — and only at that point, so the missing-pattern guard above still | ||||||||||||||||||||||||||||
| 48 | # fires without DateTime::Format::Strptime present. | ||||||||||||||||||||||||||||
| 49 | 14 | ▶ | unless (eval { require DateTime::Format::Strptime; 1 }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 2 | my $err = $@; | |||||||||||||||||||||||||||
| 51 | 2 | DBIO::Exception->throw( | |||||||||||||||||||||||||||
| 52 | "$class needs DateTime::Format::Strptime to parse/format datetimes, " | ||||||||||||||||||||||||||||
| 53 | . "but it is not installed. Install DateTime::Format::Strptime (part " | ||||||||||||||||||||||||||||
| 54 | . "of the optional DBIO::Timestamp/DateTime feature) and retry." | ||||||||||||||||||||||||||||
| 55 | . "\n\nUnderlying error: $err" | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | DateTime::Format::Strptime->new( | ||||||||||||||||||||||||||||
| 60 | 12 | pattern => $pattern, | |||||||||||||||||||||||||||
| 61 | on_error => 'croak' | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 10 | ▶ | sub parse_datetime { | ||||||||||||||||||||||||||
|
Subroutine
parse_datetime: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 10 | my ($class, $value) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 10 | ▶ | if (my $preferred = $class->_preferred) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 1 | return $preferred->parse_datetime($value); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 9 | return $class->_strptime_for(datetime_parse => 'datetime_parse_pattern') | |||||||||||||||||||||||||||
| 75 | ->parse_datetime($value); | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 10 | ▶ | sub format_datetime { | ||||||||||||||||||||||||||
|
Subroutine
format_datetime: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 10 | my ($class, $dt) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 10 | ▶ | if (my $preferred = $class->_preferred) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | return $preferred->format_datetime($dt); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 9 | return $class->_strptime_for(datetime_format => 'datetime_format_pattern') | |||||||||||||||||||||||||||
| 87 | ->format_datetime($dt); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 3 | ▶ | sub parse_date { | ||||||||||||||||||||||||||
|
Subroutine
parse_date: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 3 | my ($class, $value) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 3 | my $preferred = $class->_preferred; | |||||||||||||||||||||||||||
| 95 | 3 | ▶ | return $preferred->parse_date($value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$preferred and $preferred->can("parse_date")
MC/DC0%0/2
$preferred and $preferred->can("parse_date")
$preferred
$preferred->can("parse_date")
Truth table66%2/3
A and B
A= $preferredB= $preferred->can("parse_date")
| |||||||||||||||||||||||||||||
| 96 | if $preferred and $preferred->can('parse_date'); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | return $class->_strptime_for(date_parse => 'date_parse_pattern') | |||||||||||||||||||||||||||
| 99 | ->parse_datetime($value); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 4 | ▶ | sub format_date { | ||||||||||||||||||||||||||
|
Subroutine
format_date: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 4 | my ($class, $dt) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 4 | my $preferred = $class->_preferred; | |||||||||||||||||||||||||||
| 107 | 4 | ▶ | return $preferred->format_date($dt) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$preferred and $preferred->can("format_date")
MC/DC0%0/2
$preferred and $preferred->can("format_date")
$preferred
$preferred->can("format_date")
Truth table33%1/3
A and B
A= $preferredB= $preferred->can("format_date")
| |||||||||||||||||||||||||||||
| 108 | if $preferred and $preferred->can('format_date'); | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | return $class->_strptime_for(date_format => 'date_format_pattern') | |||||||||||||||||||||||||||
| 111 | ->format_datetime($dt); | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 6 | 1; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||