Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / DateTime / TimeZone / OffsetOnly.pm
1 package DateTime::TimeZone::OffsetOnly;
2
3 use strict;
4
5 use vars qw ($VERSION);
6 $VERSION = 0.02;
7
8 use DateTime::TimeZone;
9 use base 'DateTime::TimeZone';
10
11 use DateTime::TimeZone::UTC;
12 use Params::Validate qw( validate SCALAR );
13
14 sub new
15 {
16     my $class = shift;
17     my %p = validate( @_, { offset => { type => SCALAR },
18                           } );
19
20     my $offset =
21         DateTime::TimeZone::offset_as_seconds( $p{offset} );
22
23     die "Invalid offset: $p{offset}\n" unless defined $offset;
24
25     return DateTime::TimeZone::UTC->new unless $offset;
26
27     my $self = { name   => DateTime::TimeZone::offset_as_string( $offset ),
28                  offset => $offset,
29                };
30
31     return bless $self, $class;
32 }
33
34 sub is_dst_for_datetime { 0 }
35
36 sub offset_for_datetime { $_[0]->{offset} }
37 sub offset_for_local_datetime { $_[0]->{offset} }
38
39 sub is_utc { 0 }
40
41 sub short_name_for_datetime { $_[0]->name }
42
43 sub category { undef }
44
45
46 sub STORABLE_freeze
47 {
48     my $self = shift;
49
50     return $self->name;
51 }
52
53 sub STORABLE_thaw
54 {
55     my $self = shift;
56     my $cloning = shift;
57     my $serialized = shift;
58
59     my $class = ref $self || $self;
60
61     my $obj;
62     if ( $class->isa(__PACKAGE__) )
63     {
64         $obj = __PACKAGE__->new( offset => $serialized );
65     }
66     else
67     {
68         $obj = $class->new( offset => $serialized );
69     }
70
71     %$self = %$obj;
72
73     return $self;
74 }
75
76
77 1;
78
79 __END__
80
81 =head1 NAME
82
83 DateTime::TimeZone::OffsetOnly - A DateTime::TimeZone object that just contains an offset
84
85 =head1 SYNOPSIS
86
87   my $offset_tz = DateTime::TimeZone->new( name => '-0300' );
88
89 =head1 DESCRIPTION
90
91 This class is used to provide the DateTime::TimeZone API needed by
92 DateTime.pm, but with a fixed offset.  An object in this class always
93 returns the same offset as was given in its constructor, regardless of
94 the date.
95
96 =head1 USAGE
97
98 This class has the same methods as a real time zone object, but the
99 C<category()> method returns undef.
100
101 =head2 DateTime::TimeZone::OffsetOnly->new ( offset => $offset )
102
103 The value given to the offset parameter must be a string such as
104 "+0300".  Strings will be converted into numbers by the
105 C<DateTime::TimeZone::offset_as_seconds()> function.
106
107 =head2 $tz->offset_for_datetime( $datetime )
108
109 No matter what date is given, the offset provided to the constructor
110 is always used.
111
112 =head2 $tz->name()
113
114 =head2 $tz->short_name_for_datetime()
115
116 Both of these methods return the offset in string form.
117
118 =head1 AUTHOR
119
120 Dave Rolsky, <autarch@urth.org>
121
122 =head1 COPYRIGHT & LICENSE
123
124 Copyright (c) 2003-2008 David Rolsky.  All rights reserved.  This
125 program is free software; you can redistribute it and/or modify it
126 under the same terms as Perl itself.
127
128 The full text of the license can be found in the LICENSE file included
129 with this module.
130
131 =cut