| 1 |
NAME |
| 2 |
Date::Simple - a simple date object |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Date::Simple ('date', 'today'); |
| 6 |
|
| 7 |
# Difference in days between two dates: |
| 8 |
$diff = date('2001-08-27') - date('1977-10-05'); |
| 9 |
|
| 10 |
# Offset $n days from now: |
| 11 |
$date = today() + $n; |
| 12 |
print "$date\n"; # uses ISO 8601 format (YYYY-MM-DD) |
| 13 |
|
| 14 |
use Date::Simple (); |
| 15 |
my $date = Date::Simple->new('1972-01-17'); |
| 16 |
my $year = $date->year; |
| 17 |
my $month = $date->month; |
| 18 |
my $day = $date->day; |
| 19 |
|
| 20 |
use Date::Simple (':all'); |
| 21 |
my $date2 = ymd($year, $month, $day); |
| 22 |
my $date3 = d8('19871218'); |
| 23 |
my $today = today(); |
| 24 |
my $tomorrow = $today + 1; |
| 25 |
if ($tomorrow->year != $today->year) { |
| 26 |
print "Today is New Year's Eve!\n"; |
| 27 |
} |
| 28 |
|
| 29 |
if ($today > $tomorrow) { |
| 30 |
die "warp in space-time continuum"; |
| 31 |
} |
| 32 |
|
| 33 |
print "Today is "; |
| 34 |
print(('Sun','Mon','Tues','Wednes','Thurs','Fri','Satur') |
| 35 |
[$today->day_of_week]); |
| 36 |
print "day.\n"; |
| 37 |
|
| 38 |
# you can also do this: |
| 39 |
($date cmp "2001-07-01") |
| 40 |
# and this |
| 41 |
($date <=> [2001, 7, 1]) |
| 42 |
|
| 43 |
INSTALLATION |
| 44 |
|
| 45 |
If your system has the "make" program or a clone: |
| 46 |
|
| 47 |
perl Makefile.PL |
| 48 |
make |
| 49 |
make test |
| 50 |
make install |
| 51 |
|
| 52 |
If you lack "make", copy the "lib/Date" directory to your module |
| 53 |
directory (run "perl -V:sitelib" to find it). |
| 54 |
|
| 55 |
If "make test" fails, perhaps it means your system can't compile C |
| 56 |
code. Try: |
| 57 |
|
| 58 |
make distclean |
| 59 |
perl Makefile.PL noxs |
| 60 |
make |
| 61 |
make test |
| 62 |
make install |
| 63 |
|
| 64 |
This will use the pure-Perl implementation. |
| 65 |
|
| 66 |
DESCRIPTION |
| 67 |
Dates are complex enough without times and timezones. This module may be |
| 68 |
used to create simple date objects. It handles: |
| 69 |
|
| 70 |
Validation. |
| 71 |
Reject 1999-02-29 but accept 2000-02-29. |
| 72 |
|
| 73 |
Interval arithmetic. |
| 74 |
How many days were between two given dates? What date comes N days |
| 75 |
after today? |
| 76 |
|
| 77 |
Day-of-week calculation. |
| 78 |
What day of the week is a given date? |
| 79 |
|
| 80 |
It does not deal with hours, minutes, seconds, and time zones. |
| 81 |
|
| 82 |
A date is uniquely identified by year, month, and day integers within |
| 83 |
valid ranges. This module will not allow the creation of objects for |
| 84 |
invalid dates. Attempting to create an invalid date will return undef. |
| 85 |
Month numbering starts at 1 for January, unlike in C and Java. Years are |
| 86 |
4-digit. |
| 87 |
|
| 88 |
Gregorian dates up to year 9999 are handled correctly, but we rely on |
| 89 |
Perl's builtin "localtime" function when the current date is requested. |
| 90 |
On some platforms, "localtime" may be vulnerable to rollovers such as |
| 91 |
the Unix "time_t" wraparound of 18 January 2038. |
| 92 |
|
| 93 |
Overloading is used so you can compare or subtract two dates using |
| 94 |
standard numeric operators such as "==", and the sum of a date object |
| 95 |
and an integer is another date object. |
| 96 |
|
| 97 |
Date::Simple objects are immutable. After assigning "$date1" to |
| 98 |
"$date2", no change to "$date1" can affect "$date2". This means, for |
| 99 |
example, that there is nothing like a "set_year" operation, and |
| 100 |
"$date++" assigns a new object to "$date". |
| 101 |
|
| 102 |
This module contains various undocumented functions. They may not be |
| 103 |
available on all platforms and are likely to change or disappear in |
| 104 |
future releases. Please let the author know if you think any of them |
| 105 |
should be public. |
| 106 |
|
| 107 |
CONSTRUCTORS |
| 108 |
Several functions take a string or numeric representation and generate a |
| 109 |
corresponding date object. The most general is "new", whose argument |
| 110 |
list may be empty (returning the current date), a string in format |
| 111 |
YYYY-MM-DD or YYYYMMDD, a list or arrayref of year, month, and day |
| 112 |
number, or an existing date object. |
| 113 |
|
| 114 |
Date::Simple->new ([ARG, ...]) |
| 115 |
date ([ARG, ...]) |
| 116 |
my $date = Date::Simple->new('1972-01-17'); |
| 117 |
|
| 118 |
The "new" method will return a date object if the values passed in |
| 119 |
specify a valid date. (See above.) If an invalid date is passed, the |
| 120 |
method returns undef. If the argument is invalid in form as opposed |
| 121 |
to numeric range, "new" dies. |
| 122 |
|
| 123 |
The "date" function provides the same functionality but must be |
| 124 |
imported or qualified as "Date::Simple::date". (To import all public |
| 125 |
functions, do "use Date::Simple (':all');".) This function returns |
| 126 |
undef on all invalid input, rather than dying in some cases like |
| 127 |
"new". |
| 128 |
|
| 129 |
today() |
| 130 |
Returns the current date according to "localtime". |
| 131 |
|
| 132 |
Caution: To get tomorrow's date (or any fixed offset from today), do |
| 133 |
not use "today + 1". Perl parses this as "today(+1)". You need to |
| 134 |
put empty parentheses after the function: "today() + 1". |
| 135 |
|
| 136 |
ymd (YEAR, MONTH, DAY) |
| 137 |
Returns a date object with the given year, month, and day numbers. |
| 138 |
If the arguments do not specify a valid date, undef is returned. |
| 139 |
|
| 140 |
Example: |
| 141 |
|
| 142 |
use Date::Simple ('ymd'); |
| 143 |
$pbd = ymd(1987, 12, 18); |
| 144 |
|
| 145 |
d8 (STRING) |
| 146 |
Parses STRING as "YYYYMMDD" and returns the corresponding date |
| 147 |
object, or undef if STRING has the wrong format or specifies an |
| 148 |
invalid date. |
| 149 |
|
| 150 |
Example: |
| 151 |
|
| 152 |
use Date::Simple ('d8'); |
| 153 |
$doi = d8('17760704'); |
| 154 |
|
| 155 |
Mnemonic: The string matches "/\d{8}/". Also, "d8" spells "date", if |
| 156 |
8 is expanded phonetically. |
| 157 |
|
| 158 |
INSTANCE METHODS |
| 159 |
DATE->next |
| 160 |
my $tomorrow = $today->next; |
| 161 |
|
| 162 |
Returns an object representing tomorrow. |
| 163 |
|
| 164 |
DATE->prev |
| 165 |
my $yesterday = $today->prev; |
| 166 |
|
| 167 |
Returns an object representing yesterday. |
| 168 |
|
| 169 |
DATE->year |
| 170 |
my $year = $date->year; |
| 171 |
|
| 172 |
Return the year of DATE as an integer. |
| 173 |
|
| 174 |
DATE->month |
| 175 |
my $month = $date->month; |
| 176 |
|
| 177 |
Return the month of DATE as an integer from 1 to 12. |
| 178 |
|
| 179 |
DATE->day |
| 180 |
my $day = $date->day; |
| 181 |
|
| 182 |
Return the DATE's day of the month as an integer from 1 to 31. |
| 183 |
|
| 184 |
DATE->day_of_week |
| 185 |
Return a number representing DATE's day of the week from 0 to 6, |
| 186 |
where 0 means Sunday. |
| 187 |
|
| 188 |
DATE->as_ymd |
| 189 |
my ($year, $month, $day) = $date->as_ymd; |
| 190 |
|
| 191 |
Returns a list of three numbers: year, month, and day. |
| 192 |
|
| 193 |
DATE->as_d8 |
| 194 |
Returns the "d8" representation (see "d8"), like |
| 195 |
"$date->format("%Y%m%d")". |
| 196 |
|
| 197 |
DATE->format (STRING) |
| 198 |
DATE->strftime (STRING) |
| 199 |
These functions are equivalent. Return a string representing the |
| 200 |
date, in the format specified. If you don't pass a parameter, an ISO |
| 201 |
8601 formatted date is returned. |
| 202 |
|
| 203 |
my $change_date = $date->format("%d %b %y"); |
| 204 |
my $iso_date1 = $date->format("%Y-%m-%d"); |
| 205 |
my $iso_date2 = $date->format; |
| 206 |
|
| 207 |
The formatting parameter is similar to one you would pass to |
| 208 |
strftime(3). This is because we actually do pass it to strftime to |
| 209 |
format the date. This may result in differing behavior across |
| 210 |
platforms and locales and may not even work everywhere. |
| 211 |
|
| 212 |
OPERATORS |
| 213 |
Some operators can be used with Date::Simple instances. If one side of |
| 214 |
an expression is a date object, and the operator expects two date |
| 215 |
objects, the other side is interpreted as "date(ARG)", so an array |
| 216 |
reference or ISO 8601 string will work. |
| 217 |
|
| 218 |
DATE + NUMBER |
| 219 |
DATE - NUMBER |
| 220 |
You can construct a new date offset by a number of days using the |
| 221 |
"+" and "-" operators. |
| 222 |
|
| 223 |
DATE1 - DATE2 |
| 224 |
You can subtract two dates to find the number of days between them. |
| 225 |
|
| 226 |
DATE1 == DATE2 |
| 227 |
DATE1 < DATE2 |
| 228 |
DATE1 <=> DATE2 |
| 229 |
DATE1 cmp DATE2 |
| 230 |
etc. |
| 231 |
You can compare two dates using the arithmetic or string comparison |
| 232 |
operators. Equality tests ("==" and "eq") return false when one of |
| 233 |
the expressions can not be converted to a date. Other comparison |
| 234 |
tests die in such cases. This is intentional, because in a sense, |
| 235 |
all non-dates are not "equal" to all dates, but in no sense are they |
| 236 |
"greater" or "less" than dates. |
| 237 |
|
| 238 |
DATE += NUMBER |
| 239 |
DATE -= NUMBER |
| 240 |
You can increment or decrement a date by a number of days using the |
| 241 |
+= and -= operators. This actually generates a new date object and |
| 242 |
is equivalent to "$date = $date + $number". |
| 243 |
|
| 244 |
"$date" |
| 245 |
You can interpolate a date instance directly into a string, in the |
| 246 |
format specified by ISO 8601 (eg: 2000-01-17). |
| 247 |
|
| 248 |
UTILITIES |
| 249 |
leap_year (YEAR) |
| 250 |
Returns true if YEAR is a leap year. |
| 251 |
|
| 252 |
days_in_month (YEAR, MONTH) |
| 253 |
Returns the number of days in MONTH, YEAR. |
| 254 |
|
| 255 |
AUTHOR |
| 256 |
Marty Pauley <[email protected]> |
| 257 |
John Tobey <[email protected]> |
| 258 |
|
| 259 |
COPYRIGHT |
| 260 |
Copyright (C) 2001 Kasei |
| 261 |
Copyright (C) 2001,2002 John Tobey. |
| 262 |
|
| 263 |
This program is free software; you can redistribute it and/or |
| 264 |
modify it under the terms of either: |
| 265 |
|
| 266 |
a) the GNU General Public License; |
| 267 |
either version 2 of the License, or (at your option) any later |
| 268 |
version. You should have received a copy of the GNU General |
| 269 |
Public License along with this program; see the file COPYING. |
| 270 |
If not, write to the Free Software Foundation, Inc., 59 |
| 271 |
Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 272 |
|
| 273 |
b) the Perl Artistic License. |
| 274 |
|
| 275 |
This program is distributed in the hope that it will be useful, |
| 276 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 277 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 278 |
|