The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DateTimeX::Lite::TimeZone - Time zone object base class and factory

SYNOPSIS

  use DateTimeX::Lite;
  use DateTimeX::Lite::TimeZone;

  my $tz = DateTimeX::Lite::TimeZone->new( name => 'America/Chicago' );

  my $dt = DateTime->now();
  my $offset = $tz->offset_for_datetime($dt);

DESCRIPTION

This class is the base class for all time zone objects. A time zone is represented internally as a set of observances, each of which describes the offset from GMT for a given time period.

Note that without the DateTime.pm module, this module does not do much. It's primary interface is through a DateTime object, and most users will not need to directly use DateTimeX::Lite::TimeZone methods.

USAGE

This class has the following methods:

DateTimeX::Lite::TimeZone->new( name => $tz_name )

Given a valid time zone name, this method returns a new time zone blessed into the appropriate subclass. Subclasses are named for the given time zone, so that the time zone "America/Chicago" is the DateTimeX::Lite::TimeZone::America::Chicago class.

If the name given is a "link" name in the Olson database, the object created may have a different name. For example, there is a link from the old "EST5EDT" name to "America/New_York".

When loading a time zone from the Olson database, the constructor checks the version of the loaded class to make sure it matches the version of the current DateTimeX::Lite::TimeZone installation. If they do not match it will issue a warning. This is useful because time zone names may fall out of use, but you may have an old module file installed for that time zone.

There are also several special values that can be given as names.

If the "name" parameter is "floating", then a DateTimeX::Lite::TimeZone::Floating object is returned. A floating time zone does have any offset, and is always the same time. This is useful for calendaring applications, which may need to specify that a given event happens at the same local time, regardless of where it occurs. See RFC 2445 for more details.

If the "name" parameter is "UTC", then a DateTimeX::Lite::TimeZone::UTC object is returned.

If the "name" is an offset string, it is converted to a number, and a DateTimeX::Lite::TimeZone::OffsetOnly object is returned.

The "local" time zone

If the "name" parameter is "local", then the module attempts to determine the local time zone for the system.

The method for finding the local zone varies by operating system. See the appropriate module for details of how we check for the local time zone.

If a local time zone is not found, then an exception will be thrown.

$tz->offset_for_datetime( $dt )

Given a DateTime object, this method returns the offset in seconds for the given datetime. This takes into account historical time zone information, as well as Daylight Saving Time. The offset is determined by looking at the object's UTC Rata Die days and seconds.

$tz->offset_for_local_datetime( $dt )

Given a DateTime object, this method returns the offset in seconds for the given datetime. Unlike the previous method, this method uses the local time's Rata Die days and seconds. This should only be done when the corresponding UTC time is not yet known, because local times can be ambiguous due to Daylight Saving Time rules.

$tz->name

Returns the name of the time zone. If this value is passed to the new() method, it is guaranteed to create the same object.

$tz->short_name_for_datetime( $dt )

Given a DateTime object, this method returns the "short name" for the current observance and rule this datetime is in. These are names like "EST", "GMT", etc.

It is strongly recommended that you do not rely on these names for anything other than display. These names are not official, and many of them are simply the invention of the Olson database maintainers. Moreover, these names are not unique. For example, there is an "EST" at both -0500 and +1000/+1100.

$tz->is_floating

Returns a boolean indicating whether or not this object represents a floating time zone, as defined by RFC 2445.

$tz->is_utc

Indicates whether or not this object represents the UTC (GMT) time zone.

$tz->has_dst_changes

Indicates whether or not this zone has ever had a change to and from DST, either in the past or future.

$tz->is_olson

Returns true if the time zone is a named time zone from the Olson database.

$tz->category

Returns the part of the time zone name before the first slash. For example, the "America/Chicago" time zone would return "America".

DateTimeX::Lite::TimeZone->is_valid_name($name)

Given a string, this method returns a boolean value indicating whether or not the string is a valid time zone name. If you are using DateTimeX::Lite::TimeZone::Alias, any aliases you've created will be valid.

DateTimeX::Lite::TimeZone->all_names

This returns a pre-sorted list of all the time zone names. This list does not include link names. In scalar context, it returns an array reference, while in list context it returns an array.

DateTimeX::Lite::TimeZone->categories

This returns a list of all time zone categories. In scalar context, it returns an array reference, while in list context it returns an array.

This returns a hash of all time zone links, where the keys are the old, deprecated names, and the values are the new names. In scalar context, it returns a hash reference, while in list context it returns a hash.

DateTimeX::Lite::TimeZone->names_in_category( $category )

Given a valid category, this method returns a list of the names in that category, without the category portion. So the list for the "America" category would include the strings "Chicago", "Kentucky/Monticello", and "New_York". In scalar context, it returns an array reference, while in list context it returns an array.

The list is returned in order of population by zone, which should mean that this order will be the best to use for most UIs.

DateTimeX::Lite::TimeZone->countries()

Returns a sorted list of all the valid country codes (in lower-case) which can be passed to names_in_country(). In scalar context, it returns an array reference, while in list context it returns an array.

If you need to convert country codes to names or vice versa you can use Locale::Country to do so.

DateTimeX::Lite::TimeZone->names_in_country( $country_code )

Given a two-letter ISO3066 country code, this method returns a list of time zones used in that country. The country code may be of any case. In scalar context, it returns an array reference, while in list context it returns an array.

DateTimeX::Lite::TimeZone->offset_as_seconds( $offset )

Given an offset as a string, this returns the number of seconds represented by the offset as a positive or negative number. Returns undef if $offset is not in the range -99:59:59 to +99:59:59.

The offset is expected to match either /^([\+\-])?(\d\d?):(\d\d)(?::(\d\d))?$/ or /^([\+\-])?(\d\d)(\d\d)(\d\d)?$/. If it doesn't match either of these, undef will be returned.

This means that if you want to specify hours as a single digit, then each element of the offset must be separated by a colon (:).

DateTimeX::Lite::TimeZone->offset_as_string( $offset )

Given an offset as a number, this returns the offset as a string. Returns undef if $offset is not in the range -359999 to 359999.

Storable Hooks

This module provides freeze and thaw hooks for Storable so that the huge data structures for Olson time zones are not actually stored in the serialized structure.

If you subclass DateTimeX::Lite::TimeZone, you will inherit its hooks, which may not work for your module, so please test the interaction of your module with Storable.

AUTHOR

Original DateTime.pm:

Copyright (c) 2003-2008 David Rolsky <autarch@urth.org>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

DateTimeX::Lite tweaks

Daisuke Maki <daisuke@endeworks.jp> This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.