1 package DateTime::TimeZone::Local;
6 use vars qw( $VERSION );
9 use DateTime::TimeZone;
17 my $subclass = $class->_load_subclass();
19 for my $meth ( $subclass->Methods() )
21 my $tz = $subclass->$meth();
26 die "Cannot determine local time zone\n";
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
33 my %subclass = ( MSWin32 => 'Win32',
48 my $os_name = $subclass{ $^O } || $^O;
49 my $subclass = $class . '::' . $os_name;
51 return $subclass if $subclass->can('Methods');
58 if ( $e =~ /locate.+$os_name/ )
60 $subclass = $class . '::' . 'Unix';
80 foreach my $var ( $class->EnvVars() )
82 if ( $class->_IsValidName( $ENV{$var} ) )
88 $tz = eval { DateTime::TimeZone->new( name => $ENV{$var} ) };
101 return 0 unless defined $_[0];
102 return 0 if $_[0] eq 'local';
104 return $_[0] =~ m{^[\w/\-\+]+$};
115 DateTime::TimeZone::Local - Determine the local system's time zone
119 my $tz = DateTime::TimeZone->new( name => 'local' );
121 my $tz = DateTime::TimeZone::Local->TimeZone();
125 This module provides an interface for determining the local system's
126 time zone. Most of the functionality for doing this is in OS-specific
131 This class provides the following methods:
133 =head2 DateTime::TimeZone::Local->TimeZone()
135 This attempts to load an appropriate subclass and asks it to find the
136 local time zone. This method is called by when you pass "local" as the
137 time zone name to C<< DateTime:TimeZone->new() >>.
139 If your OS is not explicitly handled, you can create a module with a
140 name of the form C<DateTime::TimeZone::Local::$^O>. If it exists, it
141 will be used instead of falling back to the Unix subclass.
143 If no OS-specific module exists, we fall back to using the Unix
146 See L<DateTime::TimeZone::Local::Unix>,
147 L<DateTime::TimeZone::Local::Win32>, and
148 L<DateTime::TimeZone::Local::VMS> for OS-specific details.
152 If you want to make a new OS-specific subclass, there are several
153 methods provided by this module you should know about.
155 =head2 $class->Methods()
157 This method should be provided by your class. It should provide a list
158 of methods that will be called to try to determine the local time
161 Each of these methods is expected to return a new
162 C<DateTime::TimeZone> object if it determines the time zone.
164 =head2 $class->FromEnv()
166 This method tries to find a valid time zone in an C<%ENV> value. It
167 calls C<< $class->EnvVars() >> to determine which keys to look at.
169 To use this from a subclass, simply return "FromEnv" as one of the
170 items from C<< $class->Methods() >>.
172 =head2 $class->EnvVars()
174 This method should be provided by your subclass. It should return a
175 list of env vars to be checked by C<< $class->FromEnv() >>.
177 =head2 $class->_IsValidName($name)
179 Given a possible time zone name, this returns a boolean indicating
180 whether or not the the name looks valid. It always return false for
181 "local" in order to avoid infinite loops.
183 =head1 EXAMPLE SUBCLASS
185 Here is a simple example subclass:
187 package DateTime::TimeZone::SomeOS;
192 use base 'DateTime::TimeZone::Local';
195 sub Methods { qw( FromEnv FromEther ) }
197 sub EnvVars { qw( TZ ZONE ) }
208 Dave Rolsky, <autarch@urth.org>
210 =head1 COPYRIGHT & LICENSE
212 Copyright (c) 2003-2008 David Rolsky. All rights reserved. This
213 program is free software; you can redistribute it and/or modify it
214 under the same terms as Perl itself.
216 The full text of the license can be found in the LICENSE file included