Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / DateTime / TimeZone / Local.pm
CommitLineData
3fea05b9 1package DateTime::TimeZone::Local;
2
3use strict;
4use warnings;
5
6use vars qw( $VERSION );
7$VERSION = '0.01';
8
9use DateTime::TimeZone;
10use File::Spec;
11
12
13sub TimeZone
14{
15 my $class = shift;
16
17 my $subclass = $class->_load_subclass();
18
19 for my $meth ( $subclass->Methods() )
20 {
21 my $tz = $subclass->$meth();
22
23 return $tz if $tz;
24 }
25
26 die "Cannot determine local time zone\n";
27}
28
29{
30 # Stolen from File::Spec. My theory is that other folks can write
31 # the non-existent modules if they feel a need, and release them
32 # to CPAN separately.
33 my %subclass = ( MSWin32 => 'Win32',
34 VMS => 'VMS',
35 MacOS => 'Mac',
36 os2 => 'OS2',
37 epoc => 'Epoc',
38 NetWare => 'Win32',
39 symbian => 'Win32',
40 dos => 'OS2',
41 cygwin => 'Unix',
42 );
43
44 sub _load_subclass
45 {
46 my $class = shift;
47
48 my $os_name = $subclass{ $^O } || $^O;
49 my $subclass = $class . '::' . $os_name;
50
51 return $subclass if $subclass->can('Methods');
52
53 local $@;
54 local $SIG{__DIE__};
55 eval "use $subclass";
56 if ( my $e = $@ )
57 {
58 if ( $e =~ /locate.+$os_name/ )
59 {
60 $subclass = $class . '::' . 'Unix';
61
62 eval "use $subclass";
63 my $e2 = $@;
64 die $e2 if $e2;
65 }
66 else
67 {
68 die $e;
69 }
70 }
71
72 return $subclass;
73 }
74}
75
76sub FromEnv
77{
78 my $class = shift;
79
80 foreach my $var ( $class->EnvVars() )
81 {
82 if ( $class->_IsValidName( $ENV{$var} ) )
83 {
84 my $tz;
85 {
86 local $@;
87 local $SIG{__DIE__};
88 $tz = eval { DateTime::TimeZone->new( name => $ENV{$var} ) };
89 }
90 return $tz if $tz;
91 }
92 }
93
94 return;
95}
96
97sub _IsValidName
98{
99 shift;
100
101 return 0 unless defined $_[0];
102 return 0 if $_[0] eq 'local';
103
104 return $_[0] =~ m{^[\w/\-\+]+$};
105}
106
107
108
1091;
110
111__END__
112
113=head1 NAME
114
115DateTime::TimeZone::Local - Determine the local system's time zone
116
117=head1 SYNOPSIS
118
119 my $tz = DateTime::TimeZone->new( name => 'local' );
120
121 my $tz = DateTime::TimeZone::Local->TimeZone();
122
123=head1 DESCRIPTION
124
125This module provides an interface for determining the local system's
126time zone. Most of the functionality for doing this is in OS-specific
127subclasses.
128
129=head1 USAGE
130
131This class provides the following methods:
132
133=head2 DateTime::TimeZone::Local->TimeZone()
134
135This attempts to load an appropriate subclass and asks it to find the
136local time zone. This method is called by when you pass "local" as the
137time zone name to C<< DateTime:TimeZone->new() >>.
138
139If your OS is not explicitly handled, you can create a module with a
140name of the form C<DateTime::TimeZone::Local::$^O>. If it exists, it
141will be used instead of falling back to the Unix subclass.
142
143If no OS-specific module exists, we fall back to using the Unix
144subclass.
145
146See L<DateTime::TimeZone::Local::Unix>,
147L<DateTime::TimeZone::Local::Win32>, and
148L<DateTime::TimeZone::Local::VMS> for OS-specific details.
149
150=head1 SUBCLASSING
151
152If you want to make a new OS-specific subclass, there are several
153methods provided by this module you should know about.
154
155=head2 $class->Methods()
156
157This method should be provided by your class. It should provide a list
158of methods that will be called to try to determine the local time
159zone.
160
161Each of these methods is expected to return a new
162C<DateTime::TimeZone> object if it determines the time zone.
163
164=head2 $class->FromEnv()
165
166This method tries to find a valid time zone in an C<%ENV> value. It
167calls C<< $class->EnvVars() >> to determine which keys to look at.
168
169To use this from a subclass, simply return "FromEnv" as one of the
170items from C<< $class->Methods() >>.
171
172=head2 $class->EnvVars()
173
174This method should be provided by your subclass. It should return a
175list of env vars to be checked by C<< $class->FromEnv() >>.
176
177=head2 $class->_IsValidName($name)
178
179Given a possible time zone name, this returns a boolean indicating
180whether or not the the name looks valid. It always return false for
181"local" in order to avoid infinite loops.
182
183=head1 EXAMPLE SUBCLASS
184
185Here is a simple example subclass:
186
187 package DateTime::TimeZone::SomeOS;
188
189 use strict;
190 use warnings;
191
192 use base 'DateTime::TimeZone::Local';
193
194
195 sub Methods { qw( FromEnv FromEther ) }
196
197 sub EnvVars { qw( TZ ZONE ) }
198
199 sub FromEther
200 {
201 my $class = shift;
202
203 ...
204 }
205
206=head1 AUTHOR
207
208Dave Rolsky, <autarch@urth.org>
209
210=head1 COPYRIGHT & LICENSE
211
212Copyright (c) 2003-2008 David Rolsky. All rights reserved. This
213program is free software; you can redistribute it and/or modify it
214under the same terms as Perl itself.
215
216The full text of the license can be found in the LICENSE file included
217with this module.
218
219=cut