| 1 |
# Copyright (c) 2008-2026 Sullivan Beck. All rights reserved. |
| 2 |
# This program is free software; you can redistribute it and/or modify it |
| 3 |
# under the same terms as Perl itself. |
| 4 |
|
| 5 |
=pod |
| 6 |
|
| 7 |
=head1 NAME |
| 8 |
|
| 9 |
Date::Manip::Objects - A description of the various Date::Manip objects |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
The Date::Manip package consist of several modules, each of which |
| 14 |
perform a set of operations on a specific class of objects. This |
| 15 |
document describes how the various modules work together. |
| 16 |
|
| 17 |
=head1 DESCRIPTION |
| 18 |
|
| 19 |
Date::Manip consists of the following primary modules: |
| 20 |
|
| 21 |
=over 4 |
| 22 |
|
| 23 |
=item L<Date::Manip::Obj> |
| 24 |
|
| 25 |
The L<Date::Manip::Obj> module is not intended for direct use. It is used |
| 26 |
as a base class for all other Date::Manip classes described below. |
| 27 |
|
| 28 |
The L<Date::Manip::Obj> module contains some functions which are |
| 29 |
inherited by all these classes, so to understand all of the methods |
| 30 |
available to any of the classes below, you must include those |
| 31 |
documented in the L<Date::Manip::Obj> class. |
| 32 |
|
| 33 |
=item L<Date::Manip::Base> |
| 34 |
|
| 35 |
The L<Date::Manip::Base> is used to perform basic operations including |
| 36 |
basic date operations, management of configuration options, handling |
| 37 |
the definitions used in different languages, etc. |
| 38 |
|
| 39 |
A L<Date::Manip::Base> object does not, of itself, contain any date |
| 40 |
information. Instead, it contains configuration information which |
| 41 |
determines how the Date::Manip package performs date operations. The |
| 42 |
configuration information is documented in the L<Date::Manip::Config> |
| 43 |
document. |
| 44 |
|
| 45 |
The L<Date::Manip::Base> object has one other property that is very |
| 46 |
important. When performing basic date operations, some intermediate |
| 47 |
results are cached in the object which leads to significant |
| 48 |
performance increases in later operations. As such, it is important to |
| 49 |
reuse the object as much as possible, rather than creating new |
| 50 |
L<Date::Manip::Base> objects all the time. |
| 51 |
|
| 52 |
Much of the information in this document is related to this issue, and |
| 53 |
tells how to create various higher-level objects in order to get the |
| 54 |
most efficient reuse of this cached data. |
| 55 |
|
| 56 |
Because all other objects depend on a L<Date::Manip::Base> object, a |
| 57 |
L<Date::Manip::Base> object is embedded in all other objects, and the |
| 58 |
same Base object can be shared by any number of objects to achieve |
| 59 |
maximum performance. |
| 60 |
|
| 61 |
=item L<Date::Manip::TZ> |
| 62 |
|
| 63 |
The L<Date::Manip::TZ> module adds support for time zones. It is used to |
| 64 |
verify date and time zone information, convert dates from one time |
| 65 |
zone to another, and handle all daylight saving time transitions. |
| 66 |
|
| 67 |
Similar to the L<Date::Manip::Base> object, a great deal of information |
| 68 |
is cached in the L<Date::Manip::TZ> object. This includes lists of all |
| 69 |
time zones, offsets, and abbreviations for all time zones. It also |
| 70 |
includes more a more detailed description of every time zone that has |
| 71 |
actually been worked used. |
| 72 |
|
| 73 |
A L<Date::Manip::TZ> object relies on a L<Date::Manip::Base> object (and a |
| 74 |
L<Date::Manip::Base> object is always embedded in a L<Date::Manip::TZ> |
| 75 |
object). All higher level objects (those listed next) depend on both |
| 76 |
a L<Date::Manip::Base> and L<Date::Manip::TZ> object, so a L<Date::Manip::TZ> |
| 77 |
object is embedded in them. |
| 78 |
|
| 79 |
In order to achieve maximum performance, and minimize memory usage, |
| 80 |
a L<Date::Manip::TZ> object can be shared by any number of higher |
| 81 |
level objects, and in fact, it is desirable to reuse the same L<Date::Manip::TZ> |
| 82 |
object as often as possible. |
| 83 |
|
| 84 |
=item L<Date::Manip::Date> |
| 85 |
|
| 86 |
=item L<Date::Manip::Delta> |
| 87 |
|
| 88 |
=item L<Date::Manip::Recur> |
| 89 |
|
| 90 |
These are the primary modules which are used to perform all high level |
| 91 |
date operations. |
| 92 |
|
| 93 |
The L<Date::Manip::Date> class performs operations on dates (which includes |
| 94 |
a date, time, and time zone). The L<Date::Manip::Delta> class performs |
| 95 |
operations with deltas (amounts of time). The L<Date::Manip::Recur> class |
| 96 |
performs operations on recurring events. |
| 97 |
|
| 98 |
As mentioned above, each of these high level classes rely on both a |
| 99 |
L<Date::Manip::TZ> object and a L<Date::Manip::Base> object, so a |
| 100 |
L<Date::Manip::TZ> object is embedded in each one (and the |
| 101 |
L<Date::Manip::TZ> object has a L<Date::Manip::Base> object embedded in |
| 102 |
it). |
| 103 |
|
| 104 |
A L<Date::Manip::Date> object contains a single date, so in order to |
| 105 |
work with multiple dates, multiple L<Date::Manip::Date> objects will |
| 106 |
need to be created. In order to make the most effective use of cached |
| 107 |
information in the L<Date::Manip::Base> object, the same L<Date::Manip::TZ> |
| 108 |
object can be embedded in each of the higher level objects. |
| 109 |
|
| 110 |
The same goes for multiple L<Date::Manip::Delta> and L<Date::Manip::Recur> |
| 111 |
objects. |
| 112 |
|
| 113 |
=back |
| 114 |
|
| 115 |
There are also many secondary modules including: |
| 116 |
|
| 117 |
Date::Manip::TZ_Base |
| 118 |
Date::Manip::TZdata |
| 119 |
Date::Manip::Zones |
| 120 |
Date::Manip::Lang::* |
| 121 |
Date::Manip::TZ::* |
| 122 |
Date::Manip::Offset::* |
| 123 |
|
| 124 |
None of these are intended to be used directly. |
| 125 |
|
| 126 |
=head1 WORKING WITH DATE::MANIP OBJECTS (SINGLE CONFIGURATION) |
| 127 |
|
| 128 |
By far the most common usage of Date::Manip involves setting a single |
| 129 |
local time zone, parsing dates in a single language, and having all |
| 130 |
other configuration parameters set to a single value that doesn't |
| 131 |
change over the course of the program. |
| 132 |
|
| 133 |
Whenever this is the case, you can use the methods listed in this |
| 134 |
section to create any number of Date::Manip objects. It will automatically |
| 135 |
optimize the use of cached data to get the best performance. |
| 136 |
|
| 137 |
If you do need to work with multiple different configurations (such as |
| 138 |
parsing dates from multiple languages), please refer to the next |
| 139 |
section L</"WORKING WITH DATE::MANIP OBJECTS (MULTIPLE CONFIGURATION)">. |
| 140 |
|
| 141 |
=over 4 |
| 142 |
|
| 143 |
=item B<Working with high level objects> |
| 144 |
|
| 145 |
The most common situation is one where you will need to use one or |
| 146 |
more high level objects (Date, Delta, or Recur objects). In addition, you |
| 147 |
may want to use the lower level (Base or TZ) objects. |
| 148 |
|
| 149 |
The first thing you should do is to create your initial object. Create the |
| 150 |
highest level object you will be using. For example if you will be working with |
| 151 |
dates, create the first date object with: |
| 152 |
|
| 153 |
$date = new Date::Manip::Date; |
| 154 |
|
| 155 |
The next step is to set the configuration values. Use the config method to |
| 156 |
do this: |
| 157 |
|
| 158 |
$date->config(ARGS); |
| 159 |
|
| 160 |
Although you can call the config method later, it is strongly |
| 161 |
suggested that the configuration be set soon after the initial object |
| 162 |
is created and not altered later. Every time you alter the |
| 163 |
configuration, some of the cached data is cleared, so for optimal |
| 164 |
performance, you don't want to alter the configuration if possible. |
| 165 |
|
| 166 |
Additional high-level objects can be created using the calls: |
| 167 |
|
| 168 |
$date2 = $date->new_date(); |
| 169 |
$delta = $date->new_delta(); |
| 170 |
$recur = $date->new_recur(); |
| 171 |
|
| 172 |
To access the embedded L<Date::Manip::TZ> and L<Date::Manip::Base> objects, |
| 173 |
use the calls: |
| 174 |
|
| 175 |
$tz = $date->tz(); |
| 176 |
$base = $date->base(); |
| 177 |
|
| 178 |
=item B<Working with low level objects only> |
| 179 |
|
| 180 |
If you will only be working with low level objects, create them with one |
| 181 |
of the calls: |
| 182 |
|
| 183 |
$tz = new Date::Manip::TZ; |
| 184 |
$base = new Date::Manip::Base; |
| 185 |
|
| 186 |
To get the base object embedded in a L<Date::Manip::TZ> object, use: |
| 187 |
|
| 188 |
$base = $tz->base(); |
| 189 |
|
| 190 |
=back |
| 191 |
|
| 192 |
For a more complete description of the methods used here, refer to the |
| 193 |
L<Date::Manip::Obj> document. |
| 194 |
|
| 195 |
=head1 WORKING WITH DATE::MANIP OBJECTS (MULTIPLE CONFIGURATION) |
| 196 |
|
| 197 |
Occasionally, it may be useful to have multiple sets of configurations. |
| 198 |
In order to do this, multiple L<Date::Manip::Base> objects must be |
| 199 |
created (each with their own set of configuration options), and then |
| 200 |
new Date::Manip objects are created with the appropriate L<Date::Manip::Base> |
| 201 |
object embedded in them. |
| 202 |
|
| 203 |
Possible reasons include: |
| 204 |
|
| 205 |
=over 4 |
| 206 |
|
| 207 |
=item B<Parsing multiple languages> |
| 208 |
|
| 209 |
A L<Date::Manip::Base> object includes information about a single |
| 210 |
language. If you need to parse dates from two (or more) languages, |
| 211 |
a L<Date::Manip::Base> object needs to be created for each one. This |
| 212 |
could be done as: |
| 213 |
|
| 214 |
$date_eng1 = new Date::Manip::Date; |
| 215 |
$date_eng1->config("language","English"); |
| 216 |
|
| 217 |
$date_spa1 = new Date::Manip::Date; |
| 218 |
$date_spa1->config("language","Spanish"); |
| 219 |
|
| 220 |
Any additional Date::Manip objects created from the first will work |
| 221 |
with English. Additional objects created from the second will work in |
| 222 |
Spanish. |
| 223 |
|
| 224 |
=item B<Business modes for different countries and/or businesses> |
| 225 |
|
| 226 |
If you are doing business mode calculations (see L<Date::Manip::Calc>) |
| 227 |
for two different businesses which have different holiday lists, |
| 228 |
work weeks, or business days, you can create different objects |
| 229 |
which read different config files (see L<Date::Manip::Config>) with |
| 230 |
the appropriate description of each. |
| 231 |
|
| 232 |
=back |
| 233 |
|
| 234 |
The primary issue when dealing with multiple configurations is |
| 235 |
that it is necessary for the programmer to manually keep track of |
| 236 |
which Date::Manip objects work with each configuration. For |
| 237 |
example, refer to the following lines: |
| 238 |
|
| 239 |
$date1 = new Date::Manip::Date [$opt1,$val1]; |
| 240 |
$date2 = new Date::Manip::Date $date1, [$opt2,$val2]; |
| 241 |
$date3 = new Date::Manip::Date $date1; |
| 242 |
$date4 = new Date::Manip::Date $date2; |
| 243 |
|
| 244 |
The first line creates 3 objects: a L<Date::Manip::Base> object, a |
| 245 |
L<Date::Manip::TZ> object, and a L<Date::Manip::Date> object). The |
| 246 |
L<Date::Manip::Base> object has the configuration set to contain the |
| 247 |
value(s) passed in as the final list reference argument. |
| 248 |
|
| 249 |
The second line creates 3 new objects (a second L<Date::Manip::Base> |
| 250 |
object, a second L<Date::Manip::TZ> object, and a second |
| 251 |
L<Date::Manip::Date> object). Since a list reference containing config |
| 252 |
variables is passed in, a new L<Date::Manip::Base> object is created, |
| 253 |
rather than reusing the first one. The second L<Date::Manip::Base> object |
| 254 |
contains all the config from the first, as well as the config |
| 255 |
variables passed in in the list reference argument. |
| 256 |
|
| 257 |
The third line creates another L<Date::Manip::Date> object which uses the |
| 258 |
first L<Date::Manip::Base> and L<Date::Manip::TZ> objects embedded in it. |
| 259 |
|
| 260 |
The fourth line creates another L<Date::Manip::Date> object which uses |
| 261 |
the second L<Date::Manip::Base> and L<Date::Manip::TZ> objects embedded in |
| 262 |
it. |
| 263 |
|
| 264 |
Most of the time there will only be one set of configuration options |
| 265 |
used, so this complexity is really for a very special, and not widely |
| 266 |
used, bit of functionality. |
| 267 |
|
| 268 |
=head1 WORKING WITH DATE::MANIP OBJECTS (ADDITIONAL NOTES) |
| 269 |
|
| 270 |
=over 4 |
| 271 |
|
| 272 |
=item B<object reuse> |
| 273 |
|
| 274 |
In order to create additional Date::Manip objects, a previously |
| 275 |
created object should be passed in as the first argument. This will |
| 276 |
allow the same Base object to be embedded in both in order to maximize |
| 277 |
data reuse of the cached intermediate results, and will result in much |
| 278 |
better performance. For example: |
| 279 |
|
| 280 |
$date1 = new Date::Manip::Date; |
| 281 |
$date2 = new Date::Manip::Date $date1; |
| 282 |
|
| 283 |
This is important for two reasons. First is memory usage. The |
| 284 |
L<Date::Manip::Base> object is quite large. It stores a large number of |
| 285 |
precompile regular expressions for language parsing, and as date |
| 286 |
operations are done, intermediate results are cached which can be |
| 287 |
reused later to improve performance. The L<Date::Manip::TZ> object is |
| 288 |
even larger and contains information about all known time zones indexed |
| 289 |
several different ways (by offset, by abbreviation, etc.). As |
| 290 |
time zones are actually used, a description of all of the time change |
| 291 |
rules are loaded and added to this object. |
| 292 |
|
| 293 |
Since these objects are so large, it is important to reuse them, rather |
| 294 |
than to create lots of copies of them. It should be noted that because |
| 295 |
these objects are embedded in each of the high level object (L<Date::Manip::Date> |
| 296 |
for example), it makes these objects appear quite large. |
| 297 |
|
| 298 |
The second reason to reuse L<Date::Manip::Base> objects is |
| 299 |
performance. Since intermediate results are cached there, many date |
| 300 |
operations only need to be done once and then they can be reused any |
| 301 |
number of times. In essence, this is doing the same function as the |
| 302 |
Memoize module, but in a more efficient manner. Memoize caches results |
| 303 |
for function calls. For Date::Manip, this would often work, but if you |
| 304 |
change a config variable, the return value may change, so Memoize |
| 305 |
could cause things to break. In addition, Memoize caches primarily at |
| 306 |
the function level, but Date::Manip stores caches intermediate results |
| 307 |
wherever performance increase is seen. Every time I consider caching a |
| 308 |
result, I run a test to see if it increases performance. If it |
| 309 |
doesn't, or it doesn't make a significant impact, I don't cache it. |
| 310 |
|
| 311 |
Because the caching is quite finely tuned, it's much more efficient |
| 312 |
than using a generic (though useful) tool such as Memoize. |
| 313 |
|
| 314 |
=item B<configuration changes> |
| 315 |
|
| 316 |
As a general rule, you should only pass in configuration options |
| 317 |
when the first object is created. In other words, the following |
| 318 |
behavior is discouraged: |
| 319 |
|
| 320 |
$date = new Date::Manip::Date; |
| 321 |
$date->config(@opts); |
| 322 |
|
| 323 |
... do some stuff |
| 324 |
|
| 325 |
$date->config(@opts); |
| 326 |
|
| 327 |
... do some other stuff |
| 328 |
|
| 329 |
Because some of the cached results are configuration specific, when a |
| 330 |
configuration change is made, some of the cached data must be discarded |
| 331 |
necessitating those results to be recalculated. |
| 332 |
|
| 333 |
If you really need to change configuration in the middle of execution, |
| 334 |
it is certainly allowed of course, but if you can define the configuration |
| 335 |
once immediately after the object is first created, and then leave the |
| 336 |
configuration alone, performance will be optimized. |
| 337 |
|
| 338 |
=back |
| 339 |
|
| 340 |
=head1 BUGS AND QUESTIONS |
| 341 |
|
| 342 |
Please refer to the L<Date::Manip::Problems> documentation for |
| 343 |
information on submitting bug reports or questions to the author. |
| 344 |
|
| 345 |
=head1 SEE ALSO |
| 346 |
|
| 347 |
L<Date::Manip> - main module documentation |
| 348 |
|
| 349 |
=head1 LICENSE |
| 350 |
|
| 351 |
This script is free software; you can redistribute it and/or |
| 352 |
modify it under the same terms as Perl itself. |
| 353 |
|
| 354 |
=head1 AUTHOR |
| 355 |
|
| 356 |
Sullivan Beck (https://github.com/SBECK-github/) |
| 357 |
|
| 358 |
=cut |