| 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::Obj - Base class for Date::Manip objects |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
The Date::Manip::Obj class is the base class used for the following |
| 14 |
Date::Manip classes: |
| 15 |
|
| 16 |
=over 4 |
| 17 |
|
| 18 |
=item L<Date::Manip::Base> |
| 19 |
|
| 20 |
=item L<Date::Manip::TZ> |
| 21 |
|
| 22 |
=item L<Date::Manip::Date> |
| 23 |
|
| 24 |
=item L<Date::Manip::Delta> |
| 25 |
|
| 26 |
=item L<Date::Manip::Recur> |
| 27 |
|
| 28 |
=back |
| 29 |
|
| 30 |
This module is not intended to be called directly and performs no |
| 31 |
useful function by itself. Instead, use the various derived classes |
| 32 |
which inherit from it. |
| 33 |
|
| 34 |
=head1 DESCRIPTION |
| 35 |
|
| 36 |
This module contains a set of methods used by all Date::Manip classes |
| 37 |
listed above. |
| 38 |
|
| 39 |
You should be familiar with the L<Date::Manip::Objects> and |
| 40 |
L<Date::Manip::Config> documentation. |
| 41 |
|
| 42 |
In the method descriptions below, L<Date::Manip::Date> objects will |
| 43 |
usually be used as examples, but (unless otherwise stated), all of the |
| 44 |
classes listed above have the same methods, and work in the same |
| 45 |
fashion. |
| 46 |
|
| 47 |
=head1 METHODS FOR CREATING OBJECTS |
| 48 |
|
| 49 |
In the examples below, any variable named some variation of C<$date> (C<$date>, |
| 50 |
C<$date1>, C<$date2>, ...) is a L<Date::Manip::Date> object. Similarly, C<$delta>, |
| 51 |
C<$recur>, C<$tz>, and C<$base> refer to objects in the appropriate class. |
| 52 |
|
| 53 |
Any C<$obj> variable refers to an object in any of the classes. |
| 54 |
|
| 55 |
=over 4 |
| 56 |
|
| 57 |
=item B<new> |
| 58 |
|
| 59 |
There are two ways to use the new method. They are: |
| 60 |
|
| 61 |
$obj2 = new CLASS ($obj1,$string,@parse_opts,\@opts); |
| 62 |
$obj2 = $obj1->new($string,@parse_opts,\@opts) |
| 63 |
|
| 64 |
In both cases, all arguments are optional. |
| 65 |
|
| 66 |
Both methods are used to create a new object of a given class. In the |
| 67 |
first case, B<CLASS> is the class of the new object. For example: |
| 68 |
|
| 69 |
$date = new Date::Manip::Date; |
| 70 |
$delta = new Date::Manip::Delta; |
| 71 |
|
| 72 |
In the second method, the class of the new object will be derived from |
| 73 |
the first object. For example: |
| 74 |
|
| 75 |
$date1 = new Date::Manip::Date; |
| 76 |
$date2 = $date1->new(); |
| 77 |
|
| 78 |
the class of the second object (C<$date2>) is L<Date::Manip::Date> |
| 79 |
because that is the class of the object (C<$date1>) used to create it. |
| 80 |
|
| 81 |
In both first method (when a C<$obj1> is passed in) and always in the |
| 82 |
second method, the new object will share as much information from the |
| 83 |
old object (C<$obj1>) as possible. |
| 84 |
|
| 85 |
For example, if you call either of these: |
| 86 |
|
| 87 |
$date2 = new Date::Manip::Date $date1; |
| 88 |
$date2 = $date1->new(); |
| 89 |
|
| 90 |
the new date object will use the same embedded L<Date::Manip::TZ> and |
| 91 |
L<Date::Manip::Base> objects. |
| 92 |
|
| 93 |
When specifying CLASS and including an old object, objects do not need to |
| 94 |
be of the same class. For example, the following are all valid: |
| 95 |
|
| 96 |
$date = new Date::Manip::Date $delta; |
| 97 |
$date = new Date::Manip::Date $tz; |
| 98 |
|
| 99 |
You can even do: |
| 100 |
|
| 101 |
$date = new Date::Manip::Date $base; |
| 102 |
|
| 103 |
but this will have to create a completely new L<Date::Manip::TZ> object, |
| 104 |
which means that optimal performance may not be achieved if a |
| 105 |
L<Date::Manip::TZ> object already exists. |
| 106 |
|
| 107 |
There are two special cases. Either of the following will create |
| 108 |
a new L<Date::Manip::Base> object for handling multiple configurations: |
| 109 |
|
| 110 |
$base2 = new Date::Manip::Base $base1; |
| 111 |
$base2 = $base1->new(); |
| 112 |
|
| 113 |
Either of the following will create a new L<Date::Manip::TZ> object with |
| 114 |
the same L<Date::Manip::Base> object embedded in it: |
| 115 |
|
| 116 |
$tz2 = new Date::Manip::TZ $tz1; |
| 117 |
$tz2 = $tz1->new(); |
| 118 |
|
| 119 |
The new base object will initially have the same configuration as the |
| 120 |
original base object, but changing it's configuration will not |
| 121 |
affect the original base object. |
| 122 |
|
| 123 |
If the C<\@opts> argument is passed in, it is a list reference containing |
| 124 |
a list suitable for passing to the B<config> method (described below). In |
| 125 |
this case, a new L<Date::Manip::Base> object (and perhaps L<Date::Manip::TZ> |
| 126 |
object) will be created. The new Base object will start as identical |
| 127 |
to the original one (if a previously defined object was used to create |
| 128 |
the new object) with the additional options in C<@opts> added. |
| 129 |
|
| 130 |
In other words, the following are equivalent: |
| 131 |
|
| 132 |
$date = new Date::Manip::Date $obj,\@opts; |
| 133 |
|
| 134 |
$base = $obj->base(); |
| 135 |
$base2 = $base->new(); |
| 136 |
$date = new Date::Manip::Date $base2; |
| 137 |
$date->config(@opts); |
| 138 |
|
| 139 |
It should be noted that the options are applied to the NEW L<Date::Manip::Base> |
| 140 |
object, not the old one. |
| 141 |
|
| 142 |
An optional string (C<$string> and parse opts C<@parse_opts>) may be |
| 143 |
passed in only when creating a L<Date::Manip::Date>, |
| 144 |
L<Date::Manip::Delta>, or L<Date::Manip::Recur> object. If passed in |
| 145 |
when creating a L<Date::Manip::TZ> or L<Date::Manip::Base> object, a |
| 146 |
warning will be issued, but execution will continue. |
| 147 |
|
| 148 |
If the string is included, it will be parsed to give an initial value |
| 149 |
to the object. This will only be done AFTER any options are handled, |
| 150 |
so the following are equivalent: |
| 151 |
|
| 152 |
$date = new Date::Manip::Date $string,@parse_opts,\@opts; |
| 153 |
|
| 154 |
$date = new Date::Manip::Date; |
| 155 |
$date->config(@opts); |
| 156 |
$date->parse($string,@parse_opts); |
| 157 |
|
| 158 |
Once a L<Date::Manip::Date> object (or any object in any other |
| 159 |
Date::Manip class) is created, it should always be used to create |
| 160 |
additional objects in order to preserve cached data for optimal |
| 161 |
performance and memory usage. |
| 162 |
|
| 163 |
The one caveat is if you are working with multiple configurations |
| 164 |
as described in the L<Date::Manip::Objects> document. In that case, |
| 165 |
you may need to create completely new objects to allow multiple |
| 166 |
L<Date::Manip::Base> objects to be used. |
| 167 |
|
| 168 |
=item B<new_config> |
| 169 |
|
| 170 |
$obj2 = $obj1->new_config($string,\@opts); |
| 171 |
|
| 172 |
This creates a new instance with a new L<Date::Manip::Base> object (and possibly |
| 173 |
a new L<Date::Manip::TZ> object). |
| 174 |
|
| 175 |
For example, |
| 176 |
|
| 177 |
$date2 = $date1->new_config(); |
| 178 |
|
| 179 |
creates a new L<Date::Manip::Date> object with a new L<Date::Manip::TZ> (and |
| 180 |
L<Date::Manip::Base>) object. Initially, it is the same configuration as |
| 181 |
the original object. |
| 182 |
|
| 183 |
If the object is a L<Date::Manip::Base> object, the following are equivalent: |
| 184 |
|
| 185 |
$base2 = $base1->new_config(); |
| 186 |
|
| 187 |
$base2 = $base1->new(); |
| 188 |
|
| 189 |
Both C<$string> and C<\@opts> are optional. They are used in the same way they |
| 190 |
are used in the new method. |
| 191 |
|
| 192 |
=item B<new_date> |
| 193 |
|
| 194 |
=item B<new_delta> |
| 195 |
|
| 196 |
=item B<new_recur> |
| 197 |
|
| 198 |
These are shortcuts for specifying the class. The following sets of |
| 199 |
calls are all equivalent: |
| 200 |
|
| 201 |
$date = $obj->new_date(); |
| 202 |
$date = new Date::Manip::Date($obj); |
| 203 |
|
| 204 |
$delta = $obj->new_delta(); |
| 205 |
$delta = new Date::Manip::Date($obj); |
| 206 |
|
| 207 |
These methods all allow optional C<($string,\@opts)> arguments. |
| 208 |
|
| 209 |
=back |
| 210 |
|
| 211 |
=head1 OTHER METHODS |
| 212 |
|
| 213 |
=over 4 |
| 214 |
|
| 215 |
=item B<base> |
| 216 |
|
| 217 |
=item B<tz> |
| 218 |
|
| 219 |
$base = $obj->base(); |
| 220 |
|
| 221 |
This returns the L<Date::Manip::Base> object associated with the |
| 222 |
given object. |
| 223 |
|
| 224 |
If C<$obj> is a L<Date::Manip::Base> object, nothing is returned (i.e. it doesn't |
| 225 |
create a new copy of the object). |
| 226 |
|
| 227 |
$tz = $obj->tz(); |
| 228 |
|
| 229 |
This returns the L<Date::Manip::TZ> object associated with the |
| 230 |
given object. If C<$obj> is a L<Date::Manip::TZ> or L<Date::Manip::Base> object, |
| 231 |
nothing is returned. |
| 232 |
|
| 233 |
=item B<config> |
| 234 |
|
| 235 |
$obj->config($var1,$val1,$var2,$val2,...); |
| 236 |
|
| 237 |
This will set the value of any configuration variables. Please refer to the |
| 238 |
L<Date::Manip::Config> manual for a list of all configuration variables and their |
| 239 |
description. |
| 240 |
|
| 241 |
=item B<get_config> |
| 242 |
|
| 243 |
@var = $obj->get_config(); |
| 244 |
$val = $obj->get_config($var1); |
| 245 |
@val = $obj->get_config($var1,$var2,...); |
| 246 |
|
| 247 |
This queries the current config values. With no argument, it will return |
| 248 |
the list of config variables (all lowercase). |
| 249 |
|
| 250 |
With one or more arguments, it returns the current values for the config |
| 251 |
variables passed in (case insensitive). |
| 252 |
|
| 253 |
=item B<err> |
| 254 |
|
| 255 |
$err = $obj->err(); |
| 256 |
|
| 257 |
This will return the full error message if the previous operation failed |
| 258 |
for any reason. |
| 259 |
|
| 260 |
$obj->err(1); |
| 261 |
|
| 262 |
will clear the error code. |
| 263 |
|
| 264 |
=item B<is_date> |
| 265 |
|
| 266 |
=item B<is_delta> |
| 267 |
|
| 268 |
=item B<is_recur> |
| 269 |
|
| 270 |
$flag = $obj->is_date(); |
| 271 |
|
| 272 |
Returns 0 or 1, depending on the object. For example, a L<Date::Manip::Date> |
| 273 |
object returns 1 with the is_date method, and 0 for the other two. |
| 274 |
|
| 275 |
=item B<version> |
| 276 |
|
| 277 |
$vers = $obj->version($flag); |
| 278 |
|
| 279 |
This returns the version of Date::Manip. |
| 280 |
|
| 281 |
If C<$flag> is passed in, and C<$obj> is not a L<Date::Manip::Base> object, the |
| 282 |
version and timezone information will be passed back. |
| 283 |
|
| 284 |
=back |
| 285 |
|
| 286 |
=head1 KNOWN BUGS |
| 287 |
|
| 288 |
None known. |
| 289 |
|
| 290 |
=head1 BUGS AND QUESTIONS |
| 291 |
|
| 292 |
Please refer to the L<Date::Manip::Problems> documentation for |
| 293 |
information on submitting bug reports or questions to the author. |
| 294 |
|
| 295 |
=head1 SEE ALSO |
| 296 |
|
| 297 |
L<Date::Manip> - main module documentation |
| 298 |
|
| 299 |
=head1 LICENSE |
| 300 |
|
| 301 |
This script is free software; you can redistribute it and/or |
| 302 |
modify it under the same terms as Perl itself. |
| 303 |
|
| 304 |
=head1 AUTHOR |
| 305 |
|
| 306 |
Sullivan Beck ([email protected]) |
| 307 |
|
| 308 |
=cut |