| 1 |
# NAME |
| 2 |
|
| 3 |
DateTime::TimeZone - Time zone object base class and factory |
| 4 |
|
| 5 |
# VERSION |
| 6 |
|
| 7 |
version 2.70 |
| 8 |
|
| 9 |
# SYNOPSIS |
| 10 |
|
| 11 |
use DateTime; |
| 12 |
use DateTime::TimeZone; |
| 13 |
|
| 14 |
my $tz = DateTime::TimeZone->new( name => 'America/Chicago' ); |
| 15 |
|
| 16 |
my $dt = DateTime->now(); |
| 17 |
my $offset = $tz->offset_for_datetime($dt); |
| 18 |
|
| 19 |
# DESCRIPTION |
| 20 |
|
| 21 |
This class is the base class for all time zone objects. A time zone is |
| 22 |
represented internally as a set of observances, each of which describes the |
| 23 |
offset from GMT for a given time period. |
| 24 |
|
| 25 |
Note that without the [DateTime](https://metacpan.org/pod/DateTime) module, this module does not do much. It's |
| 26 |
primary interface is through a [DateTime](https://metacpan.org/pod/DateTime) object, and most users will not need |
| 27 |
to directly use `DateTime::TimeZone` methods. |
| 28 |
|
| 29 |
## Special Case Platforms |
| 30 |
|
| 31 |
If you are on the Win32 platform, you will want to also install |
| 32 |
[DateTime::TimeZone::Local::Win32](https://metacpan.org/pod/DateTime%3A%3ATimeZone%3A%3ALocal%3A%3AWin32). This will enable you to specify a time |
| 33 |
zone of `'local'` when creating a [DateTime](https://metacpan.org/pod/DateTime) object. |
| 34 |
|
| 35 |
If you are on HPUX, install [DateTime::TimeZone::HPUX](https://metacpan.org/pod/DateTime%3A%3ATimeZone%3A%3AHPUX). This provides support |
| 36 |
for HPUX style time zones like `'MET-1METDST'`. |
| 37 |
|
| 38 |
# USAGE |
| 39 |
|
| 40 |
This class has the following methods: |
| 41 |
|
| 42 |
## DateTime::TimeZone->new( name => $tz\_name ) |
| 43 |
|
| 44 |
Given a valid time zone name, this method returns a new time zone blessed into |
| 45 |
the appropriate subclass. Subclasses are named for the given time zone, so |
| 46 |
that the time zone "America/Chicago" is the |
| 47 |
DateTime::TimeZone::America::Chicago class. |
| 48 |
|
| 49 |
If the name given is a "link" name in the Olson database, the object created |
| 50 |
may have a different name. For example, there is a link from the old "EST5EDT" |
| 51 |
name to "America/New\_York". |
| 52 |
|
| 53 |
When loading a time zone from the Olson database, the constructor checks the |
| 54 |
version of the loaded class to make sure it matches the version of the current |
| 55 |
DateTime::TimeZone installation. If they do not match it will issue a warning. |
| 56 |
This is useful because time zone names may fall out of use, but you may have an |
| 57 |
old module file installed for that time zone. |
| 58 |
|
| 59 |
There are also several special values that can be given as names. |
| 60 |
|
| 61 |
If the "name" parameter is "floating", then a `DateTime::TimeZone::Floating` |
| 62 |
object is returned. A floating time zone does not have _any_ offset, and is |
| 63 |
always the same time. This is useful for calendaring applications, which may |
| 64 |
need to specify that a given event happens at the same _local_ time, |
| 65 |
regardless of where it occurs. See [RFC |
| 66 |
2445](https://www.ietf.org/rfc/rfc2445.txt) for more details. |
| 67 |
|
| 68 |
If the "name" parameter is "UTC", then a `DateTime::TimeZone::UTC` object is |
| 69 |
returned. |
| 70 |
|
| 71 |
If the "name" is an offset string, it is converted to a number, and a |
| 72 |
`DateTime::TimeZone::OffsetOnly` object is returned. |
| 73 |
|
| 74 |
### The "local" time zone |
| 75 |
|
| 76 |
If the "name" parameter is "local", then the module attempts to determine the |
| 77 |
local time zone for the system. |
| 78 |
|
| 79 |
The method for finding the local zone varies by operating system. See the |
| 80 |
appropriate module for details of how we check for the local time zone. |
| 81 |
|
| 82 |
- [DateTime::TimeZone::Local::Unix](https://metacpan.org/pod/DateTime%3A%3ATimeZone%3A%3ALocal%3A%3AUnix) |
| 83 |
- [DateTime::TimeZone::Local::Android](https://metacpan.org/pod/DateTime%3A%3ATimeZone%3A%3ALocal%3A%3AAndroid) |
| 84 |
- [DateTime::TimeZone::Local::hpux](https://metacpan.org/pod/DateTime%3A%3ATimeZone%3A%3ALocal%3A%3Ahpux) |
| 85 |
- [DateTime::TimeZone::Local::Win32](https://metacpan.org/pod/DateTime%3A%3ATimeZone%3A%3ALocal%3A%3AWin32) |
| 86 |
- [DateTime::TimeZone::Local::VMS](https://metacpan.org/pod/DateTime%3A%3ATimeZone%3A%3ALocal%3A%3AVMS) |
| 87 |
|
| 88 |
If a local time zone is not found, then an exception will be thrown. This |
| 89 |
exception will always stringify to something containing the text `"Cannot |
| 90 |
determine local time zone"`. |
| 91 |
|
| 92 |
If you are writing code for users to run on systems you do not control, you |
| 93 |
should try to account for the possibility that this exception may be thrown. |
| 94 |
Falling back to UTC might be a reasonable alternative. |
| 95 |
|
| 96 |
When writing tests for your modules that might be run on others' systems, you |
| 97 |
are strongly encouraged to either not use `local` when creating [DateTime](https://metacpan.org/pod/DateTime) |
| 98 |
objects or to set `$ENV{TZ}` to a known value in your test code. All of the |
| 99 |
per-OS classes check this environment variable. |
| 100 |
|
| 101 |
## $tz->offset\_for\_datetime( $dt ) |
| 102 |
|
| 103 |
Given a `DateTime` object, this method returns the offset in seconds for the |
| 104 |
given datetime. This takes into account historical time zone information, as |
| 105 |
well as Daylight Saving Time. The offset is determined by looking at the |
| 106 |
object's UTC Rata Die days and seconds. |
| 107 |
|
| 108 |
## $tz->offset\_for\_local\_datetime( $dt ) |
| 109 |
|
| 110 |
Given a `DateTime` object, this method returns the offset in seconds for the |
| 111 |
given datetime. Unlike the previous method, this method uses the local time's |
| 112 |
Rata Die days and seconds. This should only be done when the corresponding UTC |
| 113 |
time is not yet known, because local times can be ambiguous due to Daylight |
| 114 |
Saving Time rules. |
| 115 |
|
| 116 |
## $tz->is\_dst\_for\_datetime( $dt ) |
| 117 |
|
| 118 |
Given a `DateTime` object, this method returns true if the DateTime is |
| 119 |
currently in Daylight Saving Time. |
| 120 |
|
| 121 |
## $tz->name |
| 122 |
|
| 123 |
Returns the name of the time zone. |
| 124 |
|
| 125 |
## $tz->short\_name\_for\_datetime( $dt ) |
| 126 |
|
| 127 |
Given a `DateTime` object, this method returns the "short name" for the |
| 128 |
current observance and rule this datetime is in. These are names like "EST", |
| 129 |
"GMT", etc. |
| 130 |
|
| 131 |
It is **strongly** recommended that you do not rely on these names for anything |
| 132 |
other than display. These names are not official, and many of them are simply |
| 133 |
the invention of the Olson database maintainers. Moreover, these names are not |
| 134 |
unique. For example, there is an "EST" at both -0500 and +1000/+1100. |
| 135 |
|
| 136 |
## $tz->is\_floating |
| 137 |
|
| 138 |
Returns a boolean indicating whether or not this object represents a floating |
| 139 |
time zone, as defined by [RFC 2445](https://www.ietf.org/rfc/rfc2445.txt). |
| 140 |
|
| 141 |
## $tz->is\_utc |
| 142 |
|
| 143 |
Indicates whether or not this object represents the UTC (GMT) time zone. |
| 144 |
|
| 145 |
## $tz->has\_dst\_changes |
| 146 |
|
| 147 |
Indicates whether or not this zone has _ever_ had a change to and from DST, |
| 148 |
either in the past or future. |
| 149 |
|
| 150 |
## $tz->is\_olson |
| 151 |
|
| 152 |
Returns true if the time zone is a named time zone from the Olson database. |
| 153 |
|
| 154 |
## $tz->category |
| 155 |
|
| 156 |
Returns the part of the time zone name before the first slash. For example, |
| 157 |
the "America/Chicago" time zone would return "America". |
| 158 |
|
| 159 |
## DateTime::TimeZone->is\_valid\_name($name) |
| 160 |
|
| 161 |
Given a string, this method returns a boolean value indicating whether or not |
| 162 |
the string is a valid time zone name. If you are using |
| 163 |
`DateTime::TimeZone::Alias`, any aliases you've created will be valid. |
| 164 |
|
| 165 |
## DateTime::TimeZone->all\_names |
| 166 |
|
| 167 |
This returns a pre-sorted list of all the time zone names. This list does not |
| 168 |
include link names. In scalar context, it returns an array reference, while in |
| 169 |
list context it returns an array. |
| 170 |
|
| 171 |
## DateTime::TimeZone->categories |
| 172 |
|
| 173 |
This returns a list of all time zone categories. In scalar context, it returns |
| 174 |
an array reference, while in list context it returns an array. |
| 175 |
|
| 176 |
## DateTime::TimeZone->links |
| 177 |
|
| 178 |
This returns a hash of all time zone links, where the keys are the old, |
| 179 |
deprecated names, and the values are the new names. In scalar context, it |
| 180 |
returns a hash reference, while in list context it returns a hash. |
| 181 |
|
| 182 |
## DateTime::TimeZone->names\_in\_category( $category ) |
| 183 |
|
| 184 |
Given a valid category, this method returns a list of the names in that |
| 185 |
category, without the category portion. So the list for the "America" category |
| 186 |
would include the strings "Chicago", "Kentucky/Monticello", and "New\_York". In |
| 187 |
scalar context, it returns an array reference, while in list context it returns |
| 188 |
an array. |
| 189 |
|
| 190 |
## DateTime::TimeZone->countries() |
| 191 |
|
| 192 |
Returns a sorted list of all the valid country codes (in lower-case) which can |
| 193 |
be passed to `names_in_country()`. In scalar context, it returns an array |
| 194 |
reference, while in list context it returns an array. |
| 195 |
|
| 196 |
If you need to convert country codes to names or vice versa you can use |
| 197 |
`Locale::Country` to do so. Note that one of the codes returned is "uk", which |
| 198 |
is an alias for the country code "gb", and is not a valid ISO country code. |
| 199 |
|
| 200 |
## DateTime::TimeZone->names\_in\_country( $country\_code ) |
| 201 |
|
| 202 |
Given a two-letter ISO3166 country code, this method returns a list of time |
| 203 |
zones used in that country. The country code may be of any case. In scalar |
| 204 |
context, it returns an array reference, while in list context it returns an |
| 205 |
array. |
| 206 |
|
| 207 |
This list is returned in an order vaguely based on geography and population. In |
| 208 |
general, the least used zones come last, but there are not guarantees of a |
| 209 |
specific order from one release to the next. This order is probably the best |
| 210 |
option for presenting zones names to end users. |
| 211 |
|
| 212 |
## DateTime::TimeZone->offset\_as\_seconds( $offset ) |
| 213 |
|
| 214 |
Given an offset as a string, this returns the number of seconds represented by |
| 215 |
the offset as a positive or negative number. Returns `undef` if $offset is |
| 216 |
not in the range `-99:59:59` to `+99:59:59`. |
| 217 |
|
| 218 |
The offset is expected to match either |
| 219 |
`/^([\+\-])?(\d\d?):(\d\d)(?::(\d\d))?$/` or |
| 220 |
`/^([\+\-])?(\d\d)(\d\d)(\d\d)?$/`. If it doesn't match either of these, |
| 221 |
`undef` will be returned. |
| 222 |
|
| 223 |
This means that if you want to specify hours as a single digit, then each |
| 224 |
element of the offset must be separated by a colon (:). |
| 225 |
|
| 226 |
## DateTime::TimeZone->offset\_as\_string( $offset, $sep ) |
| 227 |
|
| 228 |
Given an offset as a number, this returns the offset as a string. Returns |
| 229 |
`undef` if $offset is not in the range `-359999` to `359999`. |
| 230 |
|
| 231 |
You can also provide an optional separator which will go between the hours, |
| 232 |
minutes, and seconds (if applicable) portions of the offset. |
| 233 |
|
| 234 |
## Storable Hooks |
| 235 |
|
| 236 |
This module provides freeze and thaw hooks for `Storable` so that the huge |
| 237 |
data structures for Olson time zones are not actually stored in the serialized |
| 238 |
structure. |
| 239 |
|
| 240 |
If you subclass `DateTime::TimeZone`, you will inherit its hooks, which may |
| 241 |
not work for your module, so please test the interaction of your module with |
| 242 |
Storable. |
| 243 |
|
| 244 |
# LOADING TIME ZONES IN A PRE-FORKING SYSTEM |
| 245 |
|
| 246 |
If you are running an application that does pre-forking (for example with |
| 247 |
Starman), then you should try to load all the time zones that you'll need in |
| 248 |
the parent process. Time zones are loaded on-demand, so loading them once in |
| 249 |
each child will waste memory that could otherwise be shared. |
| 250 |
|
| 251 |
# CREDITS |
| 252 |
|
| 253 |
This module was inspired by Jesse Vincent's work on Date::ICal::Timezone, and |
| 254 |
written with much help from the [email protected] list. |
| 255 |
|
| 256 |
# SEE ALSO |
| 257 |
|
| 258 |
[email protected] mailing list |
| 259 |
|
| 260 |
The tools directory of the DateTime::TimeZone distribution includes two scripts |
| 261 |
that may be of interest to some people. They are parse\_olson and |
| 262 |
tests\_from\_zdump. Please run them with the --help flag to see what they can be |
| 263 |
used for. |
| 264 |
|
| 265 |
# SUPPORT |
| 266 |
|
| 267 |
Support for this module is provided via the [email protected] email list. |
| 268 |
|
| 269 |
Bugs may be submitted at [https://github.com/houseabsolute/DateTime-TimeZone/issues](https://github.com/houseabsolute/DateTime-TimeZone/issues). |
| 270 |
|
| 271 |
# SOURCE |
| 272 |
|
| 273 |
The source code repository for DateTime-TimeZone can be found at [https://github.com/houseabsolute/DateTime-TimeZone](https://github.com/houseabsolute/DateTime-TimeZone). |
| 274 |
|
| 275 |
# DONATIONS |
| 276 |
|
| 277 |
If you'd like to thank me for the work I've done on this module, please |
| 278 |
consider making a "donation" to me via PayPal. I spend a lot of free time |
| 279 |
creating free software, and would appreciate any support you'd care to offer. |
| 280 |
|
| 281 |
Please note that **I am not suggesting that you must do this** in order for me |
| 282 |
to continue working on this particular software. I will continue to do so, |
| 283 |
inasmuch as I have in the past, for as long as it interests me. |
| 284 |
|
| 285 |
Similarly, a donation made in this way will probably not make me work on this |
| 286 |
software much more, unless I get so many donations that I can consider working |
| 287 |
on free software full time (let's all have a chuckle at that together). |
| 288 |
|
| 289 |
To donate, log into PayPal and send money to [email protected], or use the |
| 290 |
button at [https://houseabsolute.com/foss-donations/](https://houseabsolute.com/foss-donations/). |
| 291 |
|
| 292 |
# AUTHOR |
| 293 |
|
| 294 |
Dave Rolsky <[email protected]> |
| 295 |
|
| 296 |
# CONTRIBUTORS |
| 297 |
|
| 298 |
- Alexey Molchanov <[email protected]> |
| 299 |
- Alfie John <[email protected]> |
| 300 |
- Andrew Paprocki <[email protected]> |
| 301 |
- Brian Fraser <[email protected]> |
| 302 |
- Bron Gondwana <[email protected]> |
| 303 |
- Daisuke Maki <[email protected]> |
| 304 |
- David Pinkowitz <[email protected]> |
| 305 |
- guillermo.galindo <[email protected]> |
| 306 |
- Iain Truskett <deceased> |
| 307 |
- Jakub Wilk <[email protected]> |
| 308 |
- James E Keenan <[email protected]> |
| 309 |
- Joshua Hoblitt <[email protected]> |
| 310 |
- Karen Etheridge <[email protected]> |
| 311 |
- karupanerura <[email protected]> |
| 312 |
- kclaggett <[email protected]> |
| 313 |
- Matthew Horsfall <[email protected]> |
| 314 |
- Mohammad S Anwar <[email protected]> |
| 315 |
- Olaf Alders <[email protected]> |
| 316 |
- Peter Rabbitson <[email protected]> |
| 317 |
- Tom Wyant <[email protected]> |
| 318 |
|
| 319 |
# COPYRIGHT AND LICENSE |
| 320 |
|
| 321 |
This software is copyright (c) 2026 by Dave Rolsky. |
| 322 |
|
| 323 |
This is free software; you can redistribute it and/or modify it under |
| 324 |
the same terms as the Perl 5 programming language system itself. |
| 325 |
|
| 326 |
The full text of the license can be found in the |
| 327 |
`LICENSE` file included with this distribution. |