DateTi...::Alias(3pm)
π NAME π
Bestie, welcome to the DateTi...::Alias(3pm) manual from the User Contributed Perl Documentation! This page is all about DateTime::TimeZone::Alias β Create aliases for DateTime timezones. Itβs giving timezone glow-up energy. β¨
β¨ SYNOPSIS β¨
Okay so first vibe:
use DateTime::TimeZone::Alias Hawaii => 'Pacific/Honolulu',
EST => 'Australia/Melbourne';
or
DateTime::TimeZone::Alias->add( Fun => 'America/Tijuana', %hash );
or
DateTime::TimeZone::Alias->set( Pain => 'America/Dawson_Creek', %hash );
DateTime::TimeZone::Alias->set( boat => 'floating' );
DateTime::TimeZone::Alias->set( home => 'local' );
DateTime::TimeZone::Alias->set( lulu => 'Z' );
DateTime::TimeZone::Alias->set( cut => 'UTC' );
DateTime::TimeZone::Alias->set( nowhere => '-09:30' );
or
DateTime::TimeZone::Alias->remove( qw( Hawaii EST Pain Fun ) );
or
DateTime::TimeZone::Alias->value( 'EST' );
or
DateTime::TimeZone::Alias->is_defined( 'Hawaii' )
DateTime::TimeZone::Alias->is_alias( 'EST' )
DateTime::TimeZone::Alias->is_timezone( 'Antarctica/Vostok' )
or
DateTime::TimeZone::Alias->aliases
DateTime::TimeZone::Alias->timezones
π DESCRIPTION π
Creates and destroys aliases for DateTime timezones. Periodt. But listen: this module violates the encapsulation of the DateTime internals. User _Beware_. Not me being dramatic β thatβs literally in the notes. π
π IMPORT PARAMETERS π
Accepts a hash representing an alias and the timezone it should map too. Import has the exact same behaviors as the set method. Same energy, same rules. π«Ά
π οΈ METHODS π οΈ
set( alias => timezone, ... )β Accepts a hash representing an alias and the timezone it should map too. If an invalid timezone value or no parameters are passed, an exception is thrown. It is possible to create an alias to another alias. This method will redefine an existing alias or add a new alias that overrides an existing timezone. Main character move: redefining stuff on purpose.
What may be aliased:
aliasesfloatinglocalOffsetOnly- Olson time zones
UTCZ
add( alias => timezone, ... )β Accepts a hash representing an alias and the timezone it should map too. If an invalid timezone value or no parameters are passed, an exception is thrown. It is possible to create an alias to another alias. This method will not redefine an existing alias or allow you remap a timezone. For redefinitions see thesetmethod. No sneaky overwrites here, bestie.remove( alias, ... )β Accepts a list of aliases to be removed. If the name of an alias that doesnβt exist is passed in, then an exception is thrown. Itβs important to note that you can remove aliases but not timezones. However, a redefined timezone is actually an alias and can be removed. Aliases are removable; original timezones are not. Keep that straight.value( $str )β If$stris a known alias then the timezone being aliased is returned, otherwiseundef. Simple lookup, no drama.is_defined( $str )β If$stris a known timezone or alias this method returns1, otherwiseundefis returned.is_alias( $str )β If$stris a known alias this method returns1, otherwiseundefis returned.is_timezone( $str )β If$stris a known timezone this method returns1, otherwiseundefis returned.aliasesβ In list context a hash of all known aliases and their values is returned. In scalar context a reference to a hash is returned. Hash girlies, rise.timezonesβ In list context an array of all known timezones is returned. In scalar context a reference to an array is returned. Array girlies, also rise.
π© GOTCHAS π©
𧨠Import
Multiple use DateTime::TimeZone::Alias statements in the same package may not have the desired result if attempting to redefine existing aliases. If you want to redefine an alias use the set method instead. Do not fight the import. Use set. π
π Aliasing
You can _not_ create circular aliases. E.g.
# TSH actually points to value of HST - 'Pacific/Honolulu'
# if HST wasn't already defined this would be an exception
DateTime::TimeZone::Alias->set( TSH => HST );
# HST still points to 'Pacific/Honolulu'
DateTime::TimeZone::Alias->set( HST => TSH );
Sorry - no evil here, move along. Circular aliases? Not today, bestie. π
π Global Behavior
Itβs important to remember that aliases are global in scope. E.g.
package Foo;
DateTime::TimeZone::Alias->set( EST => 'US/Eastern' );
Bar->bar();
# this will fail because EST is no longer defined
my $dt = DateTime->now( time_zone => 'EST' );
package Bar;
sub bar {
DateTime::TimeZone::Alias->set( EST => 'US/Eastern' );
.
.
DateTime::TimeZone::Alias->remove( qw( EST ) );
}
There are many possible ways to avoid this trap. The best solution is to simply not write code like this. If you insist on this style then you can try one of the suggested work arounds. The best solution is literally βdonβt do that.β π
πͺ Poor Man's DateTime::TimeZone::Alias
Sometimes DateTime::TimeZone::Alias may not be the best solution to your problem. Particularly if youβre in an environment that has poor "sand-boxing" such as mod_perl. E.g.
use constant EST => 'US/Eastern';
or
sub EST () { 'US/Eastern' }
.
.
my $dt = DateTime->new( %params, time_zone => EST );
πΎ store/restore
This probably isnβt thread safe. E.g.
sub bar {
my $my_alias = DateTime::TimeZone::Alias->value( 'EST' );
DateTime::TimeZone::Alias->set( EST => 'US/Eastern' );
.
.
DateTime::TimeZone::Alias->set( EST => $my_alias );
}
If $my_alias is undefined then the set will fail as an alias must point to a valid timezone. Important caveat, no cap.
π§ DEVELOPER NOTES π§
The aliases and timezones methods preserve encapsulation by returning a reference to a copy of the internal data structures instead of a direct reference. Respectful. Protective. Encapsulation girlboss. β¨
π CREDITS π
Ben Bennett (BBENNETT) for a bug report, some API suggestions, documentation suggestions, and being so darn enthusiastic. (not necessary in that order) We love an enthusiastic icon. π
Everyone at the DateTime "Asylum". π«Ά
π SUPPORT π
Support for this module is provided via the datetime@perl.org email list. See http://lists.perl.org/ for more details. Help is out there, bestie.
π AUTHOR π
Joshua Hoblitt <jhoblitt@cpan.org> Author royalty. π
Β©οΈ COPYRIGHT Β©οΈ
Copyright (c) 2003-2010 Joshua Hoblitt. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Same terms as Perl itself β iconic licensing behavior.
The full text of the license can be found in the LICENSE file included with this module.
π SEE ALSO π
datetime@perl.org mailing list
http://datetime.perl.org/ Go forth and timezone, bestie. π
π And thatβs a wrap on the DateTi...::Alias(3pm) page! It comes from perl v5.42.2, dated 2026-02-15. Bye bestie! πππ¦
π¦ where this page comes from
- alpine 3.24: perl-datetime-timezone-alias-doc 0.06-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms