DateTime::TimeZone

Time zone object base class and factory

Latest version: 2.63 registry icon
Maintenance score
46
Safety score
100
Popularity score
15
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
2.63 0 0 0 0 0
2.62 0 0 0 0 0
2.61 0 0 0 0 0
2.60 0 0 0 0 0
2.59 0 0 0 0 0
2.58 0 0 0 0 0
2.57 0 0 0 0 0
2.56 0 0 0 0 0
2.55 0 0 0 0 0
2.54 0 0 0 0 0
2.53 0 0 0 0 0
2.52 0 0 0 0 0
2.51 0 0 0 0 0
2.50 0 0 0 0 0
2.49 0 0 0 0 0
2.48 0 0 0 0 0
2.47 0 0 0 0 0
2.46 0 0 0 0 0
2.45 0 0 0 0 0
2.44 0 0 0 0 0
2.43 0 0 0 0 0
2.42 0 0 0 0 0
2.41 0 0 0 0 0
2.40 0 0 0 0 0
2.39 0 0 0 0 0
2.38 0 0 0 0 0
2.37 0 0 0 0 0
2.36 0 0 0 0 0
2.35 0 0 0 0 0
2.34 0 0 0 0 0
2.23 0 0 0 0 0
2.22 0 0 0 0 0
2.21 0 0 0 0 0
2.20 0 0 0 0 0
2.19 0 0 0 0 0
2.18 0 0 0 0 0
2.17 0 0 0 0 0
2.16 0 0 0 0 0
2.15 0 0 0 0 0
2.14 0 0 0 0 0
2.13 0 0 0 0 0
2.12 0 0 0 0 0
2.11 0 0 0 0 0
2.10 0 0 0 0 0
2.09 0 0 0 0 0
2.08 0 0 0 0 0
2.07 0 0 0 0 0
2.06 0 0 0 0 0
2.05 0 0 0 0 0
2.04 0 0 0 0 0
2.03 0 0 0 0 0
2.02 0 0 0 0 0
2.01 0 0 0 0 0
2.00 0 0 0 0 0
1.99 0 0 0 0 0
1.98 0 0 0 0 0
1.97 0 0 0 0 0
1.96 0 0 0 0 0
1.95 0 0 0 0 0
1.94 0 0 0 0 0
1.93 0 0 0 0 0
1.92 0 0 0 0 0
1.91 0 0 0 0 0
1.90 0 0 0 0 0
1.89 0 0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.64 0 0 0 0 0
0.63 0 0 0 0 0
0.62 0 0 0 0 0
0.61 0 0 0 0 0
0.60 0 0 0 0 0
0.59 0 0 0 0 0
0.58 0 0 0 0 0
0.57 0 0 0 0 0
0.56 0 0 0 0 0
0.55 0 0 0 0 0
0.54 0 0 0 0 0
0.53 0 0 0 0 0
0.52 0 0 0 0 0
0.51 0 0 0 0 0
0.50 0 0 0 0 0
0.49 0 0 0 0 0
0.48 0 0 0 0 0
0.47 0 0 0 0 0
0.46 0 0 0 0 0
0.45 0 0 0 0 0
0.44 0 0 0 0 0
0.43 0 0 0 0 0
0.42 0 0 0 0 0
0.41 0 0 0 0 0
0.40 0 0 0 0 0
0.39 0 0 0 0 0
0.38 0 0 0 0 0
0.37 0 0 0 0 0
0.36 0 0 0 0 0
0.35 0 0 0 0 0
0.34 0 0 0 0 0
0.33 0 0 0 0 0
0.32 0 0 0 0 0
0.31 0 0 0 0 0
0.30 0 0 0 0 0
0.29 0 0 0 0 0
0.28 0 0 0 0 0
0.27 0 0 0 0 0
0.26 0 0 0 0 0
0.25 0 0 0 0 0
0.24 0 0 0 0 0
0.23 0 0 0 0 0
0.22 0 0 0 0 0
0.21 0 0 0 0 0
0.20 0 0 0 0 0
0.19 0 0 0 0 0
0.18 0 0 0 0 0
0.17 0 0 0 0 0
0.16 0 0 0 0 0
0.15 0 0 0 0 0
0.14 0 0 0 0 0
0.13 0 0 0 0 0
0.12 0 0 0 0 0
0.11 0 0 0 0 0
0.10 0 0 0 0 0
0.09 0 0 0 0 0
0.08 0 0 0 0 0
0.07 0 0 0 0 0
0.06 0 0 0 0 0
0.05 0 0 0 0 0
0.04 0 0 0 0 0
0.03 0 0 0 0 0
0.02 0 0 0 0 0
0.01_01 0 0 0 0 0
0.01_00 0 0 0 0 0

Stability
Latest release:

2.63 - This version may not be safe as it has not been updated for a long time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

Artistic-1.0   -   Artistic License 1.0

Not a wildcard

Not proprietary

OSI Compliant


GPL-1.0-or-later   -   GNU General Public License v1.0 or later

Not a wildcard

Not proprietary

OSI Compliant



NAME

DateTime::TimeZone - Time zone object base class and factory

VERSION

version 2.63

SYNOPSIS

use DateTime;
use DateTime::TimeZone;

my $tz = DateTime::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 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 DateTime::TimeZone methods.

Special Case Platforms

If you are on the Win32 platform, you will want to also install DateTime::TimeZone::Local::Win32. This will enable you to specify a time zone of 'local' when creating a DateTime object.

If you are on HPUX, install DateTime::TimeZone::HPUX. This provides support for HPUX style time zones like 'MET-1METDST'.

USAGE

This class has the following methods:

DateTime::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 DateTime::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 DateTime::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 DateTime::TimeZone::Floating object is returned. A floating time zone does not 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 DateTime::TimeZone::UTC object is returned.

If the "name" is an offset string, it is converted to a number, and a DateTime::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. This exception will always stringify to something containing the text "Cannot determine local time zone".

If you are writing code for users to run on systems you do not control, you should try to account for the possibility that this exception may be thrown. Falling back to UTC might be a reasonable alternative.

When writing tests for your modules that might be run on others' systems, you are strongly encouraged to either not use local when creating DateTime objects or to set $ENV{TZ} to a known value in your test code. All of the per-OS classes check this environment variable.

$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->is_dst_for_datetime( $dt )

Given a DateTime object, this method returns true if the DateTime is currently in Daylight Saving Time.

$tz->name

Returns the name of the time zone.

$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".

DateTime::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 DateTime::TimeZone::Alias, any aliases you've created will be valid.

DateTime::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.

DateTime::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.

DateTime::TimeZone->links

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.

DateTime::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.

DateTime::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. Note that one of the codes returned is "uk", which is an alias for the country code "gb", and is not a valid ISO country code.

DateTime::TimeZone->names_in_country( $country_code )

Given a two-letter ISO3166 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.

This list is returned in an order vaguely based on geography and population. In general, the least used zones come last, but there are not guarantees of a specific order from one release to the next. This order is probably the best option for presenting zones names to end users.

DateTime::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 (:).

DateTime::TimeZone->offset_as_string( $offset, $sep )

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.

You can also provide an optional separator which will go between the hours, minutes, and seconds (if applicable) portions of the offset.

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 DateTime::TimeZone, you will inherit its hooks, which may not work for your module, so please test the interaction of your module with Storable.

LOADING TIME ZONES IN A PRE-FORKING SYSTEM

If you are running an application that does pre-forking (for example with Starman), then you should try to load all the time zones that you'll need in the parent process. Time zones are loaded on-demand, so loading them once in each child will waste memory that could otherwise be shared.

CREDITS

This module was inspired by Jesse Vincent's work on Date::ICal::Timezone, and written with much help from the datetime@perl.org list.

SEE ALSO

datetime@perl.org mailing list

The tools directory of the DateTime::TimeZone distribution includes two scripts that may be of interest to some people. They are parse_olson and tests_from_zdump. Please run them with the --help flag to see what they can be used for.

SUPPORT

Support for this module is provided via the datetime@perl.org email list.

Bugs may be submitted at https://github.com/houseabsolute/DateTime-TimeZone/issues.

SOURCE

The source code repository for DateTime-TimeZone can be found at https://github.com/houseabsolute/DateTime-TimeZone.

DONATIONS

If you'd like to thank me for the work I've done on this module, please consider making a "donation" to me via PayPal. I spend a lot of free time creating free software, and would appreciate any support you'd care to offer.

Please note that I am not suggesting that you must do this in order for me to continue working on this particular software. I will continue to do so, inasmuch as I have in the past, for as long as it interests me.

Similarly, a donation made in this way will probably not make me work on this software much more, unless I get so many donations that I can consider working on free software full time (let's all have a chuckle at that together).

To donate, log into PayPal and send money to autarch@urth.org, or use the button at https://houseabsolute.com/foss-donations/.

AUTHOR

Dave Rolsky autarch@urth.org

CONTRIBUTORS

COPYRIGHT AND LICENSE

This software is copyright (c) 2024 by Dave Rolsky.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

The full text of the license can be found in the LICENSE file included with this distribution.