Debian Codegraph @

lib/DateTime/TimeZone/OffsetOnly.pm

raw
1 package DateTime::TimeZone::OffsetOnly;
2
3 use strict;
4 use warnings;
5 use namespace::autoclean;
6
7 our $VERSION = '2.69';
8
9 use parent 'DateTime::TimeZone';
10
11 use DateTime::TimeZone::UTC;
12 use Params::ValidationCompiler 0.13 qw( validation_for );
13 use Specio::Library::String;
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 my $class = shift;
28 my %p = $validator->(@_);
29
30 my $offset = DateTime::TimeZone::offset_as_seconds( $p{offset} );
31
32 die "Invalid offset: $p{offset}\n" unless defined $offset;
33
34 return DateTime::TimeZone::UTC->new unless $offset;
35
36 my $self = {
37 name => DateTime::TimeZone::offset_as_string($offset),
38 offset => $offset,
39 };
40
41 return bless $self, $class;
42 }
43 }
44
45 sub is_dst_for_datetime {0}
46
47 sub offset_for_datetime { $_[0]->{offset} }
48 sub offset_for_local_datetime { $_[0]->{offset} }
49
50 sub is_utc {0}
51
52 sub short_name_for_datetime { $_[0]->name }
53
54 sub category {undef}
55
56 sub STORABLE_freeze {
57 my $self = shift;
58
59 return $self->name;
60 }
61
62 sub STORABLE_thaw {
63 my $self = shift;
64 shift;
65 my $serialized = shift;
66
67 my $class = ref $self || $self;
68
69 my $obj;
70 if ( $class->isa(__PACKAGE__) ) {
71 $obj = __PACKAGE__->new( offset => $serialized );
72 }
73 else {
74 $obj = $class->new( offset => $serialized );
75 }
76
77 %$self = %$obj;
78
79 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.69
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 <[email protected]>
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