| 1 | package DBIx::QuickORM::Type::DateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 59 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 59 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 59 | our $VERSION = '0.000032'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 59 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 59 | ▶ | use Hash::Util::FieldHash qw/fieldhash/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 59 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 59 | ▶ | use DBIx::QuickORM::Type::DateTime::Lazy(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 59 | ▶ | use Role::Tiny::With qw/with/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 59 | with 'DBIx::QuickORM::Role::Type'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | DBIx::QuickORM::Type::DateTime - Date and timestamp fields as DateTime objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | Inflates date, timestamp (or datetime), and timestamp with time zone fields | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | into L<DateTime> objects, each wrapped in a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | L<DBIx::QuickORM::Type::DateTime::Lazy> that builds the DateTime only when it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | is used. Call DateTime methods on its C<datetime> (a copy to change) or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | C<datetime_ref> (read only). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | Dates and timestamps without a zone are floating wall clocks, kept as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | stored. They inflate to floating DateTimes, unless the stored text carries an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | offset (SQLite text such as C<2024-02-03T04:05:06Z>, read by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | L<DateTime::Format::ISO8601>), which keeps that zone. A DateTime is written | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | as its own wall clock, whatever its zone: nothing converts them. A wall clock | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | the ORM's zone skips (a daylight saving gap) still works.X<banned-words-ok> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | A timestamp with time zone keeps its offset both ways. PostgreSQL and DuckDB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | store the instant, so it comes back in another offset, as the same instant. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | floating DateTime written to one is taken to be in the ORM's C<time_zone>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | On MySQL and MariaDB, C<TIMESTAMP> is the timestamp with time zone: the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | server stores the instant and reads and writes it in the session zone, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ORM's C<time_zone>. A DateTime is converted to that zone when written, and a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | value comes back in it. C<DATETIME> is the one without a zone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | A MySQL or MariaDB C<TIMESTAMP> in the hour a daylight saving zone repeats | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | (clocks set back) is ambiguous: the session zone cannot say which of the two | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | instants it is, so it may read back an hour off. For exact instants keep the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | ORM's C<time_zone> at its default, C<UTC>, and convert for display with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | C<< $value->datetime->set_time_zone($zone) >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | L<DateTime::Format::MySQL> 0.08 silently ignores an offset after fractional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | seconds: C<'2024-02-03 15:05:06.12-07:00'> reads as 15:05:06.12 with no zone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | Pass such a value (a primary key given as text, say) as a DateTime, or without | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | the fraction. This is reported upstream. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | C<time> and C<interval> fields are left alone: a MySQL C<TIME> is a duration, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | not a time of day. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | Only a DateTime (a lazy value counts) is deflated. A plain string is sent as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | given, so it must already be in a form the database accepts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | Values are parsed and formatted by the Format classes the dialect names (see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | C<datetime_format> in L<DBIx::QuickORM::Dialect>): L<DateTime::Format::Pg> on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | PostgreSQL and DuckDB, L<DateTime::Format::MySQL> on MySQL and MariaDB. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | Text the dialect's parser cannot read goes to L<DateTime::Format::ISO8601>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | then to the ORM's C<datetime_parsers> in order. The first DateTime wins. Text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | none of them reads croaks when the DateTime is built. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | On SQLite, values are parsed by L<DateTime::Format::Pg> and written by this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | type as C<YYYY-MM-DD HH:MM:SS.fraction+HH:MM>. The fraction appears only when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | there is one, and the offset only for a timestamp with time zone. SQLite's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | date functions need the colon in the offset. SQLite stores whatever is sent, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | and its C<CURRENT_TIMESTAMP> is in UTC whatever the ORM's zone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | use DateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | my $ann = $orm->insert( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | users => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | name => 'Ann', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | created => DateTime->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | year => 2024, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | month => 2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | day => 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | my $when = $ann->field('created'); # date and timestamp fields are typed by default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | my $year = $when->datetime_ref->year; # 2024 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | my $next = $when->datetime->add(days => 1); # a copy, so $when is unchanged | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | $ann->update({created => $next}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | =item @types = $type->base_types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | C<date>, C<datetime>, C<timestamp>, C<timestamptz>, C<timestamp without time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | zone>, and C<timestamp with time zone>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | =item $bool = $type->is_inflated($value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | True for a DateTime, including a lazy value. A lazy value is not parsed to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | answer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | =item $bool = $type->inflated_is_immutable_for_cache | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | True: nothing changes a lazy value, and its C<datetime> is a copy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 313 | ▶ | sub base_types { ('date', 'datetime', 'timestamp', 'timestamptz', 'timestamp without time zone', 'timestamp with time zone') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
base_types: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 91 | ▶ | sub is_inflated { blessed($_[1]) && ($_[1]->isa('DateTime') || $_[1]->isa('DBIx::QuickORM::Type::DateTime::Lazy')) ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_inflated: called CC=4 SCAR=14.3
Pod
documented
Branch: 1 branch
Condition66%4/6
builtin::blessed($_[1]) and $_[1]->isa("DateTime") || $_[1]->isa("DBIx::QuickORM::Type::DateTime::Lazy")
$_[1]->isa("DateTime") or $_[1]->isa("DBIx::QuickORM::Type::DateTime::Lazy")
MC/DC33%1/3
builtin::blessed($_[1]) and $_[1]->isa("DateTime") || $_[1]->isa("DBIx::QuickORM::Type::DateTime::Lazy")
builtin::blessed($_[1])
$_[1]->isa("DateTime")
$_[1]->isa("DBIx::QuickORM::Type::DateTime::Lazy")
Truth table75%3/4
A and B or C
A= builtin::blessed($_[1])B= $_[1]->isa("DateTime")C= $_[1]->isa("DBIx::QuickORM::Type::DateTime::Lazy")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 20 | ▶ | sub inflated_is_immutable_for_cache { 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inflated_is_immutable_for_cache: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | =item $lazy = $type->inflate($raw, \%field, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | A L<DBIx::QuickORM::Type::DateTime::Lazy> over the raw string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 26 | ▶ | sub inflate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inflate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 26 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 26 | my ($raw, $field, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 26 | my ($parser, undef, $kind) = $class->_facts($field, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 26 | my $extra = $conn->datetime_parsers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 26 | my $zone = $class->_zone($kind, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | return DBIx::QuickORM::Type::DateTime::Lazy->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 24 | ▶ | sub { $class->_parse($parser, $extra, $kind, $raw, $zone, $field) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 26 | $kind, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | =item $raw = $type->deflate($datetime, \%field, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | The DateTime formatted for the field. A date keeps only its date. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | Croaks for anything but a DateTime. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 34 | ▶ | sub deflate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
deflate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 34 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 34 | my ($value, $field, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 34 | $value = $class->_unwrap($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 34 | ▶ | croak "Expected a DateTime object" . $class->_for_field($field) . ", got '$value'" unless blessed($value) && $value->isa('DateTime'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($value) and $value->isa("DateTime")
MC/DC50%1/2
builtin::blessed($value) and $value->isa("DateTime")
builtin::blessed($value)
$value->isa("DateTime")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("DateTime")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 32 | my (undef, $formatter, $kind) = $class->_facts($field, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 32 | return $class->_format($formatter, $kind, $value, $class->_zone($kind, $conn)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | =item $key = $type->canonical_key_value($value, \%field, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | The key form of a value given as database text or as a DateTime (a lazy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | value counts). Database text is parsed as C<inflate> parses it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | A timestamp with time zone gives its UTC instant: C<YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | A timestamp without one gives its wall clock as it is, the same without the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | C<Z>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | A date gives C<YYYY-MM-DD>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | A date or timestamp given as a DateTime with a zone gives its own wall clock, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | as C<deflate> writes it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | The fraction always has nine digits, the nanoseconds a DateTime holds. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | A DateTime finer than the column stores is not the key of the stored, rounded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | value. Nor is one whose fraction has a leading zero on DuckDB: DBD::DuckDB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | prints 500 microseconds as C<.500>, which reads back as another instant. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 60 | ▶ | sub canonical_key_value { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
canonical_key_value: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 60 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 60 | my ($value, $field, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 60 | my ($parser, undef, $kind) = $class->_facts($field, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 60 | my $zone = $class->_zone($kind, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 60 | my $dt = $class->_unwrap($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 60 | ▶ | $dt = $class->_parse($parser, $conn->datetime_parsers, $kind, $value, $zone, $field) unless $class->is_inflated($dt); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 60 | ▶ | if (defined $zone) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 15 | $dt = $dt->clone; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 15 | ▶ | $dt->set_time_zone($zone) if $dt->time_zone->is_floating; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 15 | $dt->set_time_zone('UTC'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 60 | ▶ | return $dt->ymd if $kind eq 'date'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 41 | ▶ | return $dt->ymd . 'T' . $dt->hms . sprintf('.%09d', $dt->nanosecond) . (defined($zone) ? 'Z' : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | =item $value = $type->_unwrap($value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | A lazy value's DateTime (its C<datetime_ref>), or any other value as given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 94 | ▶ | sub _unwrap { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unwrap: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 94 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 94 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 94 | ▶ | return blessed($value) && $value->isa('DBIx::QuickORM::Type::DateTime::Lazy') ? $value->datetime_ref : $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($value) and $value->isa("DBIx::QuickORM::Type::DateTime::Lazy")
MC/DC100%2/2
builtin::blessed($value) and $value->isa("DBIx::QuickORM::Type::DateTime::Lazy")
builtin::blessed($value)
$value->isa("DBIx::QuickORM::Type::DateTime::Lazy")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("DBIx::QuickORM::Type::DateTime::Lazy")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | =item ($parser, $formatter, $kind) = $type->_facts(\%field, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | The dialect's C<datetime_format> parser and formatter, and the field's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | C<_kind>. Worked out on the first use for a field and dialect, then cached | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | per field record. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # Keyed by field record: [$dialect, $parser, $formatter, $kind]. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 59 | fieldhash my %FACTS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 118 | ▶ | sub _facts { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_facts: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 118 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 118 | my ($field, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 118 | my $dialect = $conn->dialect; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 118 | my $facts = $FACTS{$field}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 118 | ▶ | return @{$facts}[1 .. 3] if $facts && $facts->[0] == $dialect; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$facts and $facts->[0] == $dialect
MC/DC100%2/2
$facts and $facts->[0] == $dialect
$facts
$facts->[0] == $dialect
Truth table100%3/3
A and B
A= $factsB= $facts->[0] == $dialect
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 32 | my ($parser, $formatter, $instants) = $dialect->datetime_format; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 32 | $FACTS{$field} = [$dialect, $parser, $formatter, $class->_kind($field, $instants)]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 32 | return @{$FACTS{$field}}[1 .. 3]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | =item $kind = $type->_kind(\%field, \%instants) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | C<date>, C<timestamptz>, or C<timestamp>. A base type in C<%instants> (the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | dialect's, from C<datetime_format>) is a C<timestamptz>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 40 | ▶ | sub _kind { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_kind: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 40 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 40 | my ($field, $instants) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 40 | ▶ | my $type = $field->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$field->{"type"} // ""
MC/DC100%1/1
$field->{"type"} // ""
$field->{"type"}
Truth table100%2/2
A
A= $field->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 40 | ▶ | return 'date' if $type eq 'date'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 30 | ▶ | return 'timestamptz' if $type eq 'timestamptz' || $type =~ m/with time zone$/ || ($instants && $instants->{$type}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$type eq "timestamptz" or $type =~ /with time zone$/ or $instants and $instants->{$type}
$type eq "timestamptz" or $type =~ /with time zone$/
$instants and $instants->{$type}
MC/DC100%4/4
$type eq "timestamptz" or $type =~ /with time zone$/ or $instants and $instants->{$type}
$type eq "timestamptz"
$type =~ /with time zone$/
$instants
$instants->{$type}
Truth table100%5/5
A or B or C and D
A= $type eq "timestamptz"B= $type =~ /with time zone$/C= $instantsD= $instants->{$type}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 23 | return 'timestamp'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | =item $zone = $type->_zone($kind, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | The zone a value of this kind is put in when it has none: the ORM's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | C<time_zone> for a timestamp with time zone. Undef for a date or timestamp, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | which stay floating, their wall clock as given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 118 | ▶ | sub _zone { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_zone: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 118 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 118 | my ($kind, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 118 | ▶ | return $kind eq 'timestamptz' ? $conn->time_zone : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | =item $type->_load_datetime | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | Loads L<DateTime> and the dialects' formatters. C<_parse> and C<_format> call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | it, so loading this type (the default C<autotype> does) does not load them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | Leaves C<$@> alone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 121 | ▶ | sub _load_datetime { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_datetime: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 121 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 121 | require DateTime; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 121 | require DateTime::Format::ISO8601; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 121 | require DateTime::Format::MySQL; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 121 | require DateTime::Format::Pg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | =item $dt = $type->_parse($parser, \@parsers, $kind, $raw, $zone, \%field) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | The DateTime for a raw value. One without a zone of its own is put in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | C<$zone>, keeping its wall clock. An undef C<$zone> leaves it floating. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | Tries the dialect's C<$parser>, then L<DateTime::Format::ISO8601>, then each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | of C<@parsers> (the ORM's C<datetime_parsers>). A parser fails by returning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | undef or dying. The first DateTime wins, and the caller's C<$@> is left | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | alone. Croaks when one returns anything else, and when none reads the text, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | naming the field when C<%field> has its C<table> and C<name>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | C<parse_datetime> stands in for any method the dialect's parser lacks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | (L<DateTime::Format::MySQL> has no C<parse_timestamptz>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 83 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 83 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 83 | my ($parser, $extra, $kind, $raw, $zone, $field) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 83 | $class->_load_datetime; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 83 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 83 | ▶ | my $method = $parser->can("parse_$kind") ? "parse_$kind" : 'parse_datetime'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | my @tries = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 83 | ▶ | sub { $parser->$method($_[0]) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 12 | ▶ | sub { DateTime::Format::ISO8601->parse_datetime($_[0]) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 83 | my $p = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 2 | ▶ | ref($p) eq 'CODE' ? $p : sub { $p->parse_datetime($_[0]) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 14 | ▶ | } @$extra, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 83 | for my $try (@tries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | # Failing is how a parser says the text is not its format. The next one gets it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 105 | ▶ | my $dt = eval { $try->($raw) } // next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
eval {
do {
&$try($raw)
}
} // (next)
MC/DC100%1/1
eval {
do {
&$try($raw)
}
} // (next)
eval {
do {
&$try($raw)
}
}
Truth table100%2/2
A
A= eval {
do {
&$try($raw)
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 81 | ▶ | croak "A datetime parser returned '$dt' for '$raw'" . $class->_for_field($field) . ", not a DateTime" unless blessed($dt) && $dt->isa('DateTime'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($dt) and $dt->isa("DateTime")
MC/DC50%1/2
builtin::blessed($dt) and $dt->isa("DateTime")
builtin::blessed($dt)
$dt->isa("DateTime")
Truth table66%2/3
A and B
A= builtin::blessed($dt)B= $dt->isa("DateTime")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 80 | ▶ | $dt->set_time_zone($zone) if defined($zone) && $dt->time_zone->is_floating; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $zone and $dt->time_zone->is_floating
MC/DC100%2/2
defined $zone and $dt->time_zone->is_floating
defined $zone
$dt->time_zone->is_floating
Truth table100%3/3
A and B
A= defined $zoneB= $dt->time_zone->is_floating
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 80 | return $dt; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 2 | croak "Cannot parse '$raw' as a $kind" . $class->_for_field($field) . ": add a parser that reads it to the ORM's datetime_parsers, or leave the field untyped with types => {\$table => {\$field => undef}}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | =item $raw = $type->_format($formatter, $kind, $dt, $zone) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | The database form of a DateTime, its wall clock as it is. A floating one is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | put in C<$zone> first, when C<$zone> is defined. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | C<format_datetime> stands in for a method the formatter lacks. It writes no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | offset, so a C<timestamptz> is then converted to C<$zone>, the session's zone | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | (L<DateTime::Format::MySQL> has no C<format_timestamptz>). An undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | C<$formatter> means C<_format_own>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 38 | ▶ | sub _format { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 38 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 38 | my ($formatter, $kind, $dt, $zone) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 38 | $class->_load_datetime; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 38 | $dt = $dt->clone; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 38 | ▶ | $dt->set_time_zone($zone) if defined($zone) && $dt->time_zone->is_floating; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $zone and $dt->time_zone->is_floating
MC/DC100%2/2
defined $zone and $dt->time_zone->is_floating
defined $zone
$dt->time_zone->is_floating
Truth table100%3/3
A and B
A= defined $zoneB= $dt->time_zone->is_floating
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 38 | ▶ | return $class->_format_own($kind, $dt) unless $formatter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 5 | my $method = "format_$kind"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 5 | ▶ | return $formatter->$method($dt) if $formatter->can($method); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 2 | ▶ | $dt->set_time_zone($zone) if $kind eq 'timestamptz'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 2 | return $formatter->format_datetime($dt); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | =item $raw = $type->_format_own($kind, $dt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | This type's own form of a DateTime already in its zone, used for SQLite. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 33 | ▶ | sub _format_own { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format_own: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 33 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 33 | my ($kind, $dt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 33 | ▶ | return $dt->ymd if $kind eq 'date'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 24 | my $out = $dt->ymd . ' ' . $dt->hms; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 24 | ▶ | $out .= sprintf('.%09d', $dt->nanosecond) =~ s/0+$//r if $dt->nanosecond; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 24 | ▶ | $out .= DateTime::TimeZone->offset_as_string($dt->offset, ':') if $kind eq 'timestamptz'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 24 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 59 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||