| 1 | package DBIx::QuickORM::Type::DateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 65 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 65 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 65 | our $VERSION = '0.000033'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 65 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 65 | ▶ | use Hash::Util::FieldHash qw/fieldhash/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 65 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 65 | ▶ | use DBIx::QuickORM::Type::DateTime::Lazy(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 65 | ▶ | use Role::Tiny::With qw/with/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 65 | 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 | =item $db_type = $type->db_type_for($dialect) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | C<timestamptz> on PostgreSQL and DuckDB, C<datetime(6)> on MySQL and MariaDB, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | C<datetime> on SQLite. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 65 | my %DB_TYPE = (PostgreSQL => 'timestamptz', DuckDB => 'timestamptz', MySQL => 'datetime(6)', MariaDB => 'datetime(6)', SQLite => 'datetime'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 430 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 92 | ▶ | 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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 20 | ▶ | sub inflated_is_immutable_for_cache { 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inflated_is_immutable_for_cache: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 6 | ▶ | sub db_type_for { $DB_TYPE{$_[1]->name} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
db_type_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | =item $lazy = $type->inflate($raw, \%field, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | A L<DBIx::QuickORM::Type::DateTime::Lazy> over the raw string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 26 | ▶ | sub inflate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inflate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 26 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 26 | my ($raw, $field, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 26 | my ($parser, undef, $kind) = $class->_facts($field, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 26 | my $extra = $conn->datetime_parsers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 26 | my $zone = $class->_zone($kind, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | return DBIx::QuickORM::Type::DateTime::Lazy->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 24 | ▶ | sub { $class->_parse($parser, $extra, $kind, $raw, $zone, $field) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 26 | $kind, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | =item $raw = $type->deflate($datetime, \%field, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | The DateTime formatted for the field. A date keeps only its date. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | Croaks for anything but a DateTime. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 35 | ▶ | sub deflate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
deflate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 35 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 35 | my ($value, $field, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 35 | $value = $class->_unwrap($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 35 | ▶ | 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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 33 | my (undef, $formatter, $kind) = $class->_facts($field, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 33 | return $class->_format($formatter, $kind, $value, $class->_zone($kind, $conn)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | =item $key = $type->canonical_key_value($value, \%field, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | The key form of a value given as database text or as a DateTime (a lazy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | value counts). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | my $type = 'DBIx::QuickORM::Type::DateTime'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | my $conn = $orm->connection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | my $stamp = $type->canonical_key_value('2024-02-03 04:05:06.5', {type => 'timestamp'}, $conn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # 2024-02-03T04:05:06.500000000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | my $instant = $type->canonical_key_value('2024-02-03 04:05:06-07', {type => 'timestamptz'}, $conn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # 2024-02-03T11:05:06.000000000Z | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # no offset: taken to be in the ORM's time_zone, UTC by default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | my $utc = $type->canonical_key_value('2024-02-03 04:05:06', {type => 'timestamptz'}, $conn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # 2024-02-03T04:05:06.000000000Z | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | my $day = $type->canonical_key_value('2024-02-03', {type => 'date'}, $conn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # 2024-02-03 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | Database text is parsed as C<inflate> parses it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | A timestamp with time zone gives its UTC instant: C<YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | A timestamp without one gives its wall clock as it is, the same without the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | C<Z>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | A date gives C<YYYY-MM-DD>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | A date or timestamp given as a DateTime with a zone gives its own wall clock, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | as C<deflate> writes it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | The fraction always has nine digits, the nanoseconds a DateTime holds. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | A DateTime finer than the column stores is not the key of the stored, rounded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | value. Nor is one whose fraction has a leading zero on DuckDB: DBD::DuckDB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | prints 500 microseconds as C<.500>, which reads back as another instant. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 61 | ▶ | sub canonical_key_value { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
canonical_key_value: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 61 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 61 | my ($value, $field, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 61 | my ($parser, undef, $kind) = $class->_facts($field, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 61 | my $zone = $class->_zone($kind, $conn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 61 | my $dt = $class->_unwrap($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 61 | ▶ | $dt = $class->_parse($parser, $conn->datetime_parsers, $kind, $value, $zone, $field) unless $class->is_inflated($dt); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 61 | ▶ | if (defined $zone) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 16 | $dt = $dt->clone; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 16 | ▶ | $dt->set_time_zone($zone) if $dt->time_zone->is_floating; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 16 | $dt->set_time_zone('UTC'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 61 | ▶ | return $dt->ymd if $kind eq 'date'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 42 | ▶ | return $dt->ymd . 'T' . $dt->hms . sprintf('.%09d', $dt->nanosecond) . (defined($zone) ? 'Z' : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | =item $value = $type->_unwrap($value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | A lazy value's DateTime (its C<datetime_ref>), or any other value as given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 96 | ▶ | sub _unwrap { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unwrap: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 96 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 96 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 96 | ▶ | 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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | =item ($parser, $formatter, $kind) = $type->_facts(\%field, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | The dialect's C<datetime_format> parser and formatter, and the field's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | C<_kind>. Worked out on the first use for a field and dialect, then cached | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | per field record. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | # Keyed by field record: [$dialect, $parser, $formatter, $kind]. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 65 | fieldhash my %FACTS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 120 | ▶ | sub _facts { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_facts: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 120 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 120 | my ($field, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 120 | my $dialect = $conn->dialect; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 120 | my $facts = $FACTS{$field}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 120 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 33 | my ($parser, $formatter, $instants) = $dialect->datetime_format; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 33 | $FACTS{$field} = [$dialect, $parser, $formatter, $class->_kind($field, $instants)]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 33 | return @{$FACTS{$field}}[1 .. 3]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | =item $kind = $type->_kind(\%field, \%instants) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | C<date>, C<timestamptz>, or C<timestamp>. A base type in C<%instants> (the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | dialect's, from C<datetime_format>) is a C<timestamptz>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 41 | ▶ | sub _kind { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_kind: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 41 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 41 | my ($field, $instants) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 41 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 41 | ▶ | return 'date' if $type eq 'date'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 31 | ▶ | 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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 24 | return 'timestamp'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | =item $zone = $type->_zone($kind, $conn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | The zone a value of this kind is put in when it has none: the ORM's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | C<time_zone> for a timestamp with time zone. Undef for a date or timestamp, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | which stay floating, their wall clock as given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 120 | ▶ | sub _zone { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_zone: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 120 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 120 | my ($kind, $conn) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 120 | ▶ | return $kind eq 'timestamptz' ? $conn->time_zone : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | =item $type->_load_datetime | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | Loads L<DateTime> and the dialects' formatters. C<_parse> and C<_format> call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | it, so loading this type (the default C<autotype> does) does not load them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | Leaves C<$@> alone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 123 | ▶ | sub _load_datetime { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_datetime: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 123 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 123 | require DateTime; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 123 | require DateTime::Format::ISO8601; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 123 | require DateTime::Format::MySQL; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 123 | require DateTime::Format::Pg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | =item $dt = $type->_parse($parser, \@parsers, $kind, $raw, $zone, \%field) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | The DateTime for a raw value. One without a zone of its own is put in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | C<$zone>, keeping its wall clock. An undef C<$zone> leaves it floating. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | Tries the dialect's C<$parser>, then L<DateTime::Format::ISO8601>, then each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | of C<@parsers> (the ORM's C<datetime_parsers>). A parser fails by returning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | undef or dying. The first DateTime wins, and the caller's C<$@> is left | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | alone. Croaks when one returns anything else, and when none reads the text, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | naming the field when C<%field> has its C<table> and C<name>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | C<parse_datetime> stands in for any method the dialect's parser lacks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | (L<DateTime::Format::MySQL> has no C<parse_timestamptz>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 84 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 84 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 84 | my ($parser, $extra, $kind, $raw, $zone, $field) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 84 | $class->_load_datetime; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 84 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 84 | ▶ | my $method = $parser->can("parse_$kind") ? "parse_$kind" : 'parse_datetime'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | my @tries = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 84 | ▶ | sub { $parser->$method($_[0]) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 12 | ▶ | sub { DateTime::Format::ISO8601->parse_datetime($_[0]) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 84 | my $p = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 2 | ▶ | ref($p) eq 'CODE' ? $p : sub { $p->parse_datetime($_[0]) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 14 | ▶ | } @$extra, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 84 | for my $try (@tries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | # Failing is how a parser says the text is not its format. The next one gets it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 106 | ▶ | 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)
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 82 | ▶ | 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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 81 | ▶ | $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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 81 | return $dt; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 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}}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | =item $raw = $type->_format($formatter, $kind, $dt, $zone) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | The database form of a DateTime, its wall clock as it is. A floating one is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | put in C<$zone> first, when C<$zone> is defined. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | C<format_datetime> stands in for a method the formatter lacks. It writes no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | offset, so a C<timestamptz> is then converted to C<$zone>, the session's zone | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | (L<DateTime::Format::MySQL> has no C<format_timestamptz>). An undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | C<$formatter> means C<_format_own>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 39 | ▶ | sub _format { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 39 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 39 | my ($formatter, $kind, $dt, $zone) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 39 | $class->_load_datetime; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 39 | $dt = $dt->clone; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 39 | ▶ | $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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 39 | ▶ | return $class->_format_own($kind, $dt) unless $formatter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 5 | my $method = "format_$kind"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 5 | ▶ | return $formatter->$method($dt) if $formatter->can($method); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 2 | ▶ | $dt->set_time_zone($zone) if $kind eq 'timestamptz'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 2 | return $formatter->format_datetime($dt); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | =item $raw = $type->_format_own($kind, $dt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | This type's own form of a DateTime already in its zone, used for SQLite. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 34 | ▶ | sub _format_own { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format_own: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 34 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 34 | my ($kind, $dt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 34 | ▶ | return $dt->ymd if $kind eq 'date'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 25 | my $out = $dt->ymd . ' ' . $dt->hms; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 25 | ▶ | $out .= sprintf('.%09d', $dt->nanosecond) =~ s/0+$//r if $dt->nanosecond; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 25 | ▶ | $out .= DateTime::TimeZone->offset_as_string($dt->offset, ':') if $kind eq 'timestamptz'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 25 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 65 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||