| 1 | package DBIx::Class::InflateColumn::DateTime::WithTimeZone; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use base 'DBIx::Class::InflateColumn::DateTime'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use DateTime::TimeZone; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 19 | ▶ | sub register_column { | ||||||||||||||||||||||||||
|
Subroutine
register_column: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 19 | my ( $self, $column, $info, @rest ) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 19 | my $msg = "Column $column:"; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # NOTE: must update $info before calling parent, b/c parent copies $info | ||||||||||||||||||||||||||||
| 17 | 19 | ▶ | if ( my $tz_source = $info->{timezone_source} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # force InflateColumn::DateTime to convert to UTC before storing | ||||||||||||||||||||||||||||
| 20 | 9 | ▶ | $info->{timezone} ||= 'UTC'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$info->{"timezone"} ||= "UTC"
MC/DC100%1/1
$info->{"timezone"} ||= "UTC"
$info->{"timezone"}
Truth table100%2/2
A
A= $info->{"timezone"}
| |||||||||||||||||||||||||||||
| 21 | 9 | ▶ | if ( $info->{timezone} ne 'UTC' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 1 | $self->throw_exception( "$msg saving non-UTC datetimes in database is not supported" ); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 18 | $self->next::method( $column, $info, @rest ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 18 | ▶ | if ( my $tz_source = $info->{timezone_source} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 8 | my $ic_dt_method = $info->{_ic_dt_method}; | |||||||||||||||||||||||||||
| 30 | 8 | ▶ | if ( !$ic_dt_method || !$ic_dt_method =~ /(?: datetime | timestamp )/x ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $ic_dt_method or !$ic_dt_method =~ /(?: datetime | timestamp )/x
MC/DC50%1/2
not $ic_dt_method or !$ic_dt_method =~ /(?: datetime | timestamp )/x
not $ic_dt_method
!$ic_dt_method =~ /(?: datetime | timestamp )/x
Truth table66%2/3
not(A) or not(B)
A= not $ic_dt_methodB= !$ic_dt_method =~ /(?: datetime | timestamp )/x
| |||||||||||||||||||||||||||||
| 31 | 1 | $self->throw_exception( "$msg timezone_source requires datetime data_type"); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 7 | ▶ | if ( !$self->has_column($tz_source) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 1 | $self->throw_exception( "$msg could not find column $tz_source for timezone_source" ); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 6 | my $tz_info = $self->column_info($tz_source); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 6 | ▶ | if ( $info->{is_nullable} && !$tz_info->{is_nullable} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"is_nullable"} and not $tz_info->{"is_nullable"}
MC/DC0%0/2
$info->{"is_nullable"} and not $tz_info->{"is_nullable"}
$info->{"is_nullable"}
not $tz_info->{"is_nullable"}
Truth table66%2/3
A and not(B)
A= $info->{"is_nullable"}B= not $tz_info->{"is_nullable"}
| |||||||||||||||||||||||||||||
| 41 | 0 | $self->throw_exception( "$msg: is nullable, so $tz_source must also be nullable" ); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 6 | my %tz_cache; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | sub _get_cached_tz { | ||||||||||||||||||||||||||
|
Subroutine
_get_cached_tz: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 49 | 2 | my ( $self, $tz ) = @_; | |||||||||||||||||||||||||||
| 50 | 2 | ▶ | return $tz_cache{$tz} ||= DateTime::TimeZone->new( name => $tz ); | ||||||||||||||||||||||||||
|
Condition66%2/3
$tz_cache{$tz} ||= "DateTime::TimeZone"->new("name", $tz)
MC/DC0%0/2
$tz_cache{$tz} ||= "DateTime::TimeZone"->new("name", $tz)
$tz_cache{$tz}
"DateTime::TimeZone"->new("name", $tz)
Truth table66%2/3
A ||= B
A= $tz_cache{$tz}B= "DateTime::TimeZone"->new("name", $tz)
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub _post_inflate_datetime { | ||||||||||||||||||||||||||
|
Subroutine
_post_inflate_datetime: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 54 | 4 | my ( $self, $dt, $info ) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | $dt = $self->next::method( $dt, $info ); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 4 | ▶ | if ( my $tz_src = $info->{timezone_source} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 4 | ▶ | if ( !$self->has_column_loaded($tz_src) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 1 | my $colname = $info->{__dbic_colname}; | |||||||||||||||||||||||||||
| 61 | 1 | $self->throw_exception( | |||||||||||||||||||||||||||
| 62 | "$colname needs time zone from $tz_src, but $tz_src is not loaded: check query?" ); | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 3 | my $tz = $self->get_column($tz_src); | |||||||||||||||||||||||||||
| 65 | 3 | ▶ | if ($tz) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 2 | $dt->set_time_zone( $self->_get_cached_tz($tz) ); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | warn sprintf '%s had null timezone (%s): using UTC', | ||||||||||||||||||||||||||||
| 70 | 1 | $info->{__dbic_colname}, $tz_src; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 3 | return $dt; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 7 | ▶ | sub _pre_deflate_datetime { | ||||||||||||||||||||||||||
|
Subroutine
_pre_deflate_datetime: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 78 | 7 | my ( $self, $dt, $info ) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 7 | ▶ | if ( my $tz_src = $info->{timezone_source} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 7 | $self->set_column( $tz_src, $dt->time_zone->name ); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 7 | ▶ | if ( !$ENV{DBIC_IC_DT_WTZ_MODIFY_TZ} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 5 | $dt = $dt->clone; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 7 | $dt = $self->next::method( $dt, $info ); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 7 | return $dt; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 6 | 1; | |||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||