Coding Domain

Perl Modules: Time::Zones


Downloads
Perl Modules
Name
Time::Zones - Converting times to different timezones.

Synopsis
# Importing methods.
use Time::Zones;
use Time::Zones qw(English);
use Time::Zones qw(Dutch);

# Getting different timezone values
my @GMTTimeArray   = tztime("GMT");   # same as gmtime()
my @WestEuropeTime = tztime("WEUR");  # +01:00
my @AlaskaTime     = tztime("ALASK"); # -09:00
my @NepalTime      = tztime("NEPAL"); # +05:45

# Printing all timezone names sorted.
my @tzConstants    = sort timezones tzkeys();
my $nameshash      = tznames();
foreach my $tz (@tzConstants) {
  print $nameshash->{$tz} . "\n";
}

Description
This module provides a tztime() function in your namespace. This function works exactly like the gmtime() and localtime() functions defaultly available in Perl. The difference however, is the fact that it accepts a timezone id as first parameter.

When a language parameter is provided, the module attemps to load timezone names from a language file. The hash will be available as the %Time::Zones::Names variable. The language file however, should be present at the users system (Time::Zones::LanguageName)

Exported Functions
tztime("CONSTANT"[, TIME])
Behaves exactly like the build-in functions gmtime() and localtime(). The first parameter simply refers to a key in the %Time::Zones::Settings or %Time::Zones::Names hash. The second, optional parameter contains the epoch-seconds-time, like the value returned by the time() function. If this value is omitted, the current time() return value will be used.

In scalar context, the function will return a time string, just like gmtime() and localtime(). For more details about the return values, please check the documentation of localtime() or gmtime().

tzkeys()
Returns all the timezone setting identifiers, like GMT, WEUR. ALASK, NEPAL. In fact, this value is the same as calling keys %Time::Zones::Settings.

timezones
This is a comparing algorithm that can used to sort the timezones. The function compares the timezone offsets of two timezone keys.
Example1:   my @tzsort = sort timezones tzkeys() # As Of Perl 5.005;
Example2:   my @tzsort = sort { timezones($a, $b) } tzkeys();

tznames([ID])
There are two different ways of using this function. If you provide the ID parameter, the function will return the name (a scalar) for the ID.

The function returns a reference to the timezone names hash when the first parameter is omitted. You can use that reference when you need to know much more of those names, theirby saving a lot of function calls.

my $names  = tznames();       # Getting a hash reference
my $weur1a = $names->{WEUR};  # Using that reference
my $weur1b = tznames->{WEUR}; # Same, not saving the reference
my $weur2  = tznames("WEUR"); # Using tznames' return value

Available Module Variables
%Time::Zones::Settings
A hash containing the timezone settings. The settings from the Windows (!) tzedit.exe program have been used for this. The hash consists of the following key/values:
CONSTANT_ID => DATA_ARRAY_REFERENCE  #1: Basic Structure
"CONSTANT"  => [( DATA_ARRAY )]      #2: Written in Perl

The data array has one of the following formats:

  Time Zone Offset from GMT, without any DST.
  [+-]H,M

  Offset    Start DST (Nth weekday)   End of DST            DST Offset
  [+-]H,M,  [1st-5th],DAY,MON,H,M,S,  [1-5],DAY,MON,H,M,S,  [+-]H,M

%Time::Zones::Names
This hash is filled only when the Time::Zones module is loaded with the language parameter. It can be used in your user-interface to display a appropriate name for the timezone constants. The data in this hash can also be retreived using the tzkeys() and tznames() functions.

Exported Tags
The following tags can be exported aswell. This loads some constants, also used internally in this module, into your namespace.
:timeelements
Names for the elements for the array returned by localtime(), gmtime() and tztime(). The names are: SEC, MIN, HOUR, DAY, MONTH, YEAR, WEEKDAY, YEARDAY, ISDST

:days
Names for the day elements. (sunday=0, monday=1, etc.) The names are: SUN, MON, TUE, WED, THU, FRI, SAT

:months
Names for the month elements (January=0, etc.) The names are: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC

Author
Copyright (c) 2001, Diederik van der Boor - All Rights Reserved