blib/lib/DBIx/Class/InflateColumn/DateTime/WithTimeZone.pm

filter: Statementstmt 97.7% 44 / 45 Branchbran 86.3% 19 / 22 Conditioncond 75.0% 6 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 89.6% 78 / 87 CC 15 SCAR 18.7
CC
15
Cov
92%
register_column
10.1
_post_inflate_datetime
4.0
_pre_deflate_datetime
3.0
CRAP 6.5
↑ Summary Build »
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.02';
8  
9 sub register_column {
10 19 ▶ my ( $self, $column, $info, @rest ) = @_;
Subroutine register_column: called CC=10 SCAR=23.2
Pod documented
11  
12 19 my $msg = "Column $column:";
13  
14 # NOTE: must update $info before calling parent, b/c parent copies $info
15 19 ▶ if ( my $tz_source = $info->{timezone_source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tz_source = $info->{"timezone_source"}) 9 10 19
16  
17 # force InflateColumn::DateTime to convert to UTC before storing
18 9 ▶ $info->{timezone} ||= 'UTC';
Condition100%2/2
$info->{"timezone"} ||= "UTC"
l!l
36
MC/DC100%1/1
$info->{"timezone"} ||= "UTC"
$info->{"timezone"}
Truth table100%2/2
A
A= $info->{"timezone"}
Aresult
11
00
19 9 ▶ if ( $info->{timezone} ne 'UTC' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"timezone"} ne "UTC") 1 8 9
20 1 $self->throw_exception( "$msg saving non-UTC datetimes in database is not supported" );
21 }
22 }
23  
24 18 $self->next::method( $column, $info, @rest );
25  
26 18 ▶ if ( my $tz_source = $info->{timezone_source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tz_source = $info->{"timezone_source"}) 8 10 18
27 8 my $ic_dt_method = $info->{_ic_dt_method};
28 8 ▶ if ( !$ic_dt_method || !$ic_dt_method =~ /(?: datetime | timestamp )/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ic_dt_method or !$ic_dt_method =~ /(?: datetime | timestamp )/x) 1 7 8
Condition66%2/3
not $ic_dt_method or !$ic_dt_method =~ /(?: datetime | timestamp )/x
l!l&&r!l&&!r
107
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
ABresult
1X1
011
000
29 1 $self->throw_exception( "$msg timezone_source requires datetime data_type");
30 }
31  
32 7 ▶ if ( !$self->has_column($tz_source) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_column($tz_source)) 1 6 7
33 1 $self->throw_exception( "$msg could not find column $tz_source for timezone_source" );
34 }
35  
36 6 my $tz_info = $self->column_info($tz_source);
37  
38 6 ▶ if ( $info->{is_nullable} && !$tz_info->{is_nullable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_nullable"} and not $tz_info->{"is_nullable"}) 0 6 6
Condition66%2/3
$info->{"is_nullable"} and not $tz_info->{"is_nullable"}
!ll&&!rl&&r
420
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"}
ABresult
0X0
100
111
39 0 $self->throw_exception( "$msg: is nullable, so $tz_source must also be nullable" );
40 }
41 }
42 }
43  
44 sub _post_inflate_datetime {
45 4 ▶ my ( $self, $dt, $info ) = @_;
Subroutine _post_inflate_datetime: called CC=4 SCAR=13.9
46  
47 4 $dt = $self->next::method( $dt, $info );
48  
49 4 ▶ if ( my $tz_src = $info->{timezone_source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tz_src = $info->{"timezone_source"}) 4 0 4
50 4 ▶ if ( !$self->has_column_loaded($tz_src) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_column_loaded($tz_src)) 1 3 4
51 1 my $colname = $info->{__dbic_colname};
52 1 $self->throw_exception(
53 "$colname needs time zone from $tz_src, but $tz_src is not loaded: check query?" );
54 }
55 3 my $tz = $self->get_column($tz_src);
56 3 ▶ if ($tz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tz) { } 2 1 3
57 2 $dt->set_time_zone($tz);
58 }
59 else {
60 warn sprintf '%s had null timezone (%s): using UTC',
61 1 $info->{__dbic_colname}, $tz_src;
62 }
63 }
64  
65 3 return $dt;
66 }
67  
68 sub _pre_deflate_datetime {
69 7 ▶ my ( $self, $dt, $info ) = @_;
Subroutine _pre_deflate_datetime: called CC=3 SCAR=11.0
70  
71 7 ▶ if ( my $tz_src = $info->{timezone_source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tz_src = $info->{"timezone_source"}) 7 0 7
72 7 $self->set_column( $tz_src, $dt->time_zone->name );
73 }
74  
75 7 ▶ if ( !$ENV{DBIC_IC_DT_WTZ_MODIFY_TZ} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"DBIC_IC_DT_WTZ_MODIFY_TZ"}) 5 2 7
76 5 $dt = $dt->clone;
77 }
78  
79 7 $dt = $self->next::method( $dt, $info );
80  
81 7 return $dt;
82 }
83  
84 6 1;
85 __END__
86  
87 =encoding utf-8
88  
89 =head1 NAME
90  
91 DBIx::Class::InflateColumn::DateTime::WithTimeZone - Store time zones with DateTimes in database
92  
93 =head1 SYNOPSIS
94  
95 Set up table with separate column to store time zone, and set that column as
96 the timezone_source column for the datetime column.
97  
98 package Event;
99 use base 'DBIx::Class::Core';
100  
101 __PACKAGE__->load_components(qw/InflateColumn::DateTime::WithTimeZone/);
102  
103 __PACKAGE_->add_columns(
104 event_time => { data_type => 'timestamp', timezone_source => 'event_tz' },
105 event_tz => { data_type => 'varchar', size => 38 },
106 );
107  
108 Store any DateTime into the database
109  
110 $dt = DateTime->new( year => 2015, month => 6, day => 8, hour => 9, minute => 10
111 time_zone => 'America/Chicago' );
112  
113 $row = $schema->resultset('Event')->create( { event_time => $dt } );
114  
115 In the database, event_time is now set to the UTC time corresponding to the
116 original time (2015-06-08T14:10:00), and event_tz is set to 'America/Chicago'.
117  
118 When retrieved from the database, event_time will be returned as an identical
119 DateTime object, with the same time zone as the original DateTime
120  
121 $row = $schema->resultset('Event')->first;
122  
123 $event_time = $row->event_time;
124  
125 say $event_time . ''; # 2015-06-08T09:10:00
126  
127 say $event_time->time_zone->name; # America/Chicago
128  
129 =head1 DESCRIPTION
130  
131 This component preserves the time zone of DateTime objects when
132 storing and retrieving through DBIx::Class.
133  
134 It uses InflateColumn::DateTime to do the basic inflation and
135 deflation. The time zone is saved into an additional database
136 column, and automatically applied to the DateTime after
137 inflation.
138  
139 =head2 UTC-only
140  
141 The datetime is always converted to UTC before storage in the
142 database. This ensures that the real time is preserved, no
143 matter how the clock time is affected by the time zone.
144  
145 This avoids the problems caused by Daylight Saving Time.
146 If the datetime were stored in any time zone that has Daylight
147 Saving Time, then any datetime that occurs during the
148 transition out of Daylight Saving Time (when the clock goes
149 back one hour) will be ambiguous. DateTime handles this by
150 always using the latest real time for the given clock time
151 (see L<DateTime#Ambiguous-Local-Times>). In this case,
152 any DateTime from the earlier pass through the overlapped times
153 will be converted to the later time when it is read, effectively
154 adding the DST offset to the time.
155  
156 =head1 USAGE NOTES
157  
158 =head2 Interaction with InflateColumn::DateTime
159  
160 =over
161  
162 =item Side effects on DateTime object
163  
164 Currently, if the timezone attribute is set on InflateColumn::DateTime, then
165 the time zone on a DateTime object used to set the column may have its time
166 zone changed to that of the timezone attribute. The time zone change only
167 happens if the DateTime object is deflated for storage.
168 See L<https://rt.cpan.org/Public/Bug/Display.html?id=105154>.
169  
170 By default, this component overrides this IC::DT behavior. The DateTime
171 object used to set the column will not have its time zone changed.
172  
173 If you need this side effect, set the DBIC_IC_DT_WTZ_MODIFY_TZ environment
174 variable, and the IC::DT behavior will be followed: any DateTime used to
175 set the column value will have its time zone set to UTC if it has been
176 deflated for storage in the database.
177  
178 =item timezone
179  
180 The timezone attribute is defaulted to UTC. If a non-UTC timezone
181 is specified, an exception will be thrown, since non-UTC time zones
182 can not guarantee that the retrieved DateTime matches the saved
183 DateTime.
184  
185 =item locale
186  
187 The locale attribute is not affected by this component, so it
188 should work as documented in InflateColumn::DateTime.
189  
190 =back
191  
192 =head2 Interaction with TimeStamp
193  
194 All columns using the TimeStamp plugin will default to using the UTC
195 time zone for all time stamps. To use a different time zone, override
196 the get_timestamp method and set the desired time zone there.
197  
198 =head2 Nullable columns
199  
200 If the datetime column is nullable, the timezone_source column must also
201 be nullable. If it is not, a exception will be thrown when the schema is
202 loaded.
203  
204 =head2 Missing timezone column
205  
206 If a datetime column with a timezone_source is included in a ResultSet,
207 the corresponding timezone_source column must also be included.
208  
209 If the timezone_source column is missing, a runtime exception will be
210 thrown when the datetime column is accessed.
211  
212 =head2 Timezone column size
213  
214 The time zone column must be long enough to store the longest
215 zoneinfo name. Currently, that's 38 characters, but I can't find
216 any guarantee that will not change.
217  
218 This component does not yet validate the timezone column data type
219 or size. This may result in database exceptions if the time zone
220 length is greater than the timezone_source column length.
221  
222 =head2 Implementation Details
223  
224 This component uses internal methods and data from
225 L<DBIx::Class::InflateColumn::DateTime>:
226  
227 =head3 _ic_dt_method
228  
229 Uses the $info->{_ic_dt_method} value set by InflateColumn::DateTime
230 to determine the column datatype, rather than duplicating the
231 detection code.
232  
233 =head3 __dbic_colname
234  
235 Uses the $info->{__dbic_colname} value set by InflateColumn::DateTime
236 to provide the column name in error messages.
237  
238 =head3 register_columns
239  
240 Wraps register_columns to validate the column attributes
241  
242 =head3 _post_inflate_datetime
243  
244 Sets time zone from the timezone_source column DateTime inflation
245  
246 =head3 _pre_deflate_datetime
247  
248 Sets timezone_source column to time zone name before DateTime deflation
249  
250 =head1 TODO
251  
252 =over
253  
254 =item *
255  
256 Expand the tests to validate against databases other than SQLite
257  
258 =item *
259  
260 Investigate and document interaction with locale
261  
262 =item *
263  
264 Add validation of the data_type and size of the timezone_source column
265  
266 =back
267  
268 =head1 AUTHOR
269  
270 Noel Maddy E<lt>zhtwnpanta@gmail.comE<gt>
271  
272 =head1 COPYRIGHT
273  
274 Copyright 2015- Noel Maddy
275  
276 =head1 LICENSE
277  
278 This library is free software; you can redistribute it and/or modify
279 it under the same terms as Perl itself.
280  
281 =head1 SEE ALSO
282  
283 L<DBIx::Class::InflateColumn::DateTime>, L<DBIx::Class::InflateColumn>, L<DateTime>
284  
285 =cut
↑ Summary Build »