File Coverage

blib/lib/DateTime/TimeZone/OffsetOnly.pm
Criterion Covered Total %
statement 41 46 89.1
branch 5 6 83.3
condition 1 3 33.3
subroutine 12 16 75.0
pod 7 9 77.7
total 66 80 82.5


line stmt bran cond sub pod time code
1             package DateTime::TimeZone::OffsetOnly;
2              
3 20     20   140159 use strict;
  20         43  
  20         761  
4 20     20   109 use warnings;
  20         37  
  20         990  
5 20     20   464 use namespace::autoclean;
  20         19163  
  20         129  
6              
7             our $VERSION = '2.67';
8              
9 20     20   2162 use parent 'DateTime::TimeZone';
  20         306  
  20         124  
10              
11 20     20   10451 use DateTime::TimeZone::UTC;
  20         68  
  20         1088  
12 20     20   905 use Params::ValidationCompiler 0.13 qw( validation_for );
  20         43688  
  20         1492  
13 20     20   782 use Specio::Library::String;
  20         168039  
  20         233  
14              
15             {
16             my $validator = validation_for(
17             name => '_check_new_params',
18             name_is_optional => 1,
19             params => {
20             offset => {
21             type => t('NonEmptyStr'),
22             },
23             },
24             );
25              
26             sub new {
27 254     254 1 1370711 my $class = shift;
28 254         9040 my %p = $validator->(@_);
29              
30 254         8070 my $offset = DateTime::TimeZone::offset_as_seconds( $p{offset} );
31              
32 254 100       2036 die "Invalid offset: $p{offset}\n" unless defined $offset;
33              
34 93 100       253 return DateTime::TimeZone::UTC->new unless $offset;
35              
36 82         261 my $self = {
37             name => DateTime::TimeZone::offset_as_string($offset),
38             offset => $offset,
39             };
40              
41 82         516 return bless $self, $class;
42             }
43             }
44              
45 0     0 1 0 sub is_dst_for_datetime {0}
46              
47 56     56 1 542 sub offset_for_datetime { $_[0]->{offset} }
48 0     0 1 0 sub offset_for_local_datetime { $_[0]->{offset} }
49              
50 5824     5824 1 409671 sub is_utc {0}
51              
52 0     0 1 0 sub short_name_for_datetime { $_[0]->name }
53              
54 0     0 1 0 sub category {undef}
55              
56             sub STORABLE_freeze {
57 4     4 0 45 my $self = shift;
58              
59 4         9 return $self->name;
60             }
61              
62             sub STORABLE_thaw {
63 2     2 0 22 my $self = shift;
64 2         3 shift;
65 2         3 my $serialized = shift;
66              
67 2   33     6 my $class = ref $self || $self;
68              
69 2         2 my $obj;
70 2 50       8 if ( $class->isa(__PACKAGE__) ) {
71 2         4 $obj = __PACKAGE__->new( offset => $serialized );
72             }
73             else {
74 0         0 $obj = $class->new( offset => $serialized );
75             }
76              
77 2         6 %$self = %$obj;
78              
79 2         15 return $self;
80             }
81              
82             1;
83              
84             # ABSTRACT: A DateTime::TimeZone object that just contains an offset
85              
86             __END__
87              
88             =pod
89              
90             =encoding UTF-8
91              
92             =head1 NAME
93              
94             DateTime::TimeZone::OffsetOnly - A DateTime::TimeZone object that just contains an offset
95              
96             =head1 VERSION
97              
98             version 2.67
99              
100             =head1 SYNOPSIS
101              
102             my $offset_tz = DateTime::TimeZone->new( name => '-0300' );
103              
104             =head1 DESCRIPTION
105              
106             This class is used to provide the DateTime::TimeZone API needed by DateTime.pm,
107             but with a fixed offset. An object in this class always returns the same
108             offset as was given in its constructor, regardless of the date.
109              
110             =head1 USAGE
111              
112             This class has the same methods as a real time zone object, but the
113             C<category()> method returns undef.
114              
115             =head2 DateTime::TimeZone::OffsetOnly->new ( offset => $offset )
116              
117             The value given to the offset parameter must be a string such as "+0300".
118             Strings will be converted into numbers by the
119             C<DateTime::TimeZone::offset_as_seconds()> function.
120              
121             =head2 $tz->offset_for_datetime( $datetime )
122              
123             No matter what date is given, the offset provided to the constructor is always
124             used.
125              
126             =head2 $tz->name()
127              
128             This returns the offset string form.
129              
130             =head2 $tz->short_name_for_datetime()
131              
132             This returns the offset string form.
133              
134             =head1 SUPPORT
135              
136             Bugs may be submitted at L<https://github.com/houseabsolute/DateTime-TimeZone/issues>.
137              
138             =head1 SOURCE
139              
140             The source code repository for DateTime-TimeZone can be found at L<https://github.com/houseabsolute/DateTime-TimeZone>.
141              
142             =head1 AUTHOR
143              
144             Dave Rolsky <autarch@urth.org>
145              
146             =head1 COPYRIGHT AND LICENSE
147              
148             This software is copyright (c) 2026 by Dave Rolsky.
149              
150             This is free software; you can redistribute it and/or modify it under
151             the same terms as the Perl 5 programming language system itself.
152              
153             The full text of the license can be found in the
154             F<LICENSE> file included with this distribution.
155              
156             =cut