blib/lib/Date/Simple.pm

filter: Statementstmt 80.9% 89 / 110 Branchbran 56.8% 25 / 44 Conditioncond 55.1% 16 / 29 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 85.7% 24 / 28 Podpod 100% 14 / 14 totaltotal 70.3% 171 / 243 CC 30 SCAR 16.7
CC
30
Cov
71%
_new
11.5
default_format
7.3
format
6.8
CRAP 5.3
« blib/lib/Date/Simple/NoXS.pm ↑ Summary blib/lib/Date/Simple/D8.pm »
1 # Date::Simple - a simple date object
2  
3 package Date::Simple;
4  
5 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $VERSION = '3.03';
7 }
8  
9 3 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 @ISA = ('Exporter');
11  
12 3 @EXPORT_OK = qw(
13 today ymd d8 leap_year days_in_month
14 date date_fmt date_d8 date_iso
15 );
16  
17 3 %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
18  
19 # Try to load the C code. If that fails, fall back to Date::Simple::NoXS.
20 3 ▶if ( !defined(&_add) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &_add) 3 0 3
21 3 my $err = $Date::Simple::NoXS;
22 3 ▶ unless ($err) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 3 0 3
23  
24 # Use DynaLoader instead of XSLoader for pre-5.005.
25 3 local ($@);
26 3 local @ISA = ('DynaLoader');
27 3 require DynaLoader;
28 3 eval { __PACKAGE__->bootstrap($VERSION); };
29 3 $err = $@;
30 }
31 3 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 3 3
32 0 $Date::Simple::NoXs = 1;
33 0 require Date::Simple::NoXS;
34 }
35 }
36  
37 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 use overload
40 3 ▶ '+' => '_add',
Subroutine BEGIN: called CC=1 SCAR=0.0
41 '-' => '_subtract',
42 '==' => '_eq',
43 '!=' => '_ne',
44 '<=>' => '_compare',
45 'eq' => '_eq',
46 'ne' => '_ne',
47 'cmp' => '_compare',
48 68 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 3 '""' => 'as_iso';
50  
51 3 ▶use Scalar::Util qw(refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 require Date::Simple::Fmt;
54 3 require Date::Simple::ISO;
55 3 require Date::Simple::D8;
56  
57 6 ▶sub d8 {
Subroutine d8: called CC=3 SCAR=17.3
Pod documented
58  
59 # called as function
60 6 ▶ if ( $#_ == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 0) { } 6 0 6
61 6 return __PACKAGE__->_d8(@_);
62 }
63  
64 # called as method
65 else {
66 0 ▶ if ( ref $_[0] eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'SCALAR') { } 0 0 0
67 0 return $_[0]->SUPER::_d8(@_);
68 }
69 else {
70 0 return $_[0]->_d8(@_);
71 }
72 }
73 }
74  
75 1 ▶sub today {
Subroutine today: called CC=2 SCAR=8.1
Pod documented
76 1 ▶ if ( $#_ == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == -1) { } 1 0 1
77 1 return __PACKAGE__->_today(@_);
78 }
79 else {
80 0 return shift->_today(@_);
81 }
82 }
83  
84 33 ▶sub ymd {
Subroutine ymd: called CC=3 SCAR=17.3
Pod documented
85  
86 # called as function
87 33 ▶ if ( $#_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 2) { } 33 0 33
88 33 return __PACKAGE__->_ymd(@_);
89 }
90  
91 # called as method
92 else {
93 0 ▶ if ( ref $_[0] eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'SCALAR') { } 0 0 0
94 0 return $_[0]->SUPER::_ymd(@_);
95 }
96 else {
97 0 return $_[0]->_ymd(@_);
98 }
99 }
100 }
101  
102 6 ▶sub _today {
Subroutine _today: called CC=1 SCAR=0.0
103 6 my ( $y, $m, $d ) = (localtime)[ 5, 4, 3 ];
104 6 $y += 1900;
105 6 $m += 1;
106 6 return $_[0]->_ymd( $y, $m, $d );
107 }
108  
109 0 ▶sub _inval {
Subroutine _inval: not called CC=2 SCAR=17.9
110 0 my ($first);
111 0 $first = shift;
112 0 ▶ Carp::croak( "Invalid "
Condition0%0/3
ref $first || $first
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $first || $first
ref $first $first
Truth table0%0/3
A || B
A= ref $firstB= $first
ABresult
1X1
011
000
113 . ( ref($first) || $first )
114 . " constructor args: ('"
115 . join( "', '", @_ )
116 . "')" );
117 }
118  
119 66 ▶sub _new {
Subroutine _new: called CC=11 SCAR=24.4
120 66 my ( $that, @ymd ) = @_;
121  
122 66 ▶ my $class = ref($that) || $that;
Condition66%2/3
ref $that || $that
l!l&&r!l&&!r
11550
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table66%2/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
123  
124 66 ▶ if ( @ymd == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ymd == 1) 28 38 66
125 28 my $x = $ymd[0];
126 28 ▶ if ( ref $x and reftype($x) eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $x and builtin::reftype($x) eq 'ARRAY') { } 2 26 28
elsif (UNIVERSAL::isa($x, __PACKAGE__)) { } 0 26 26
elsif ($x =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/ or $x =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) { } 24 2 26
Condition66%2/3
ref $x and builtin::reftype($x) eq "ARRAY"
!ll&&!rl&&r
2602
MC/DC50%1/2
ref $x and builtin::reftype($x) eq "ARRAY"
ref $x builtin::reftype($x) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $xB= builtin::reftype($x) eq "ARRAY"
ABresult
0X0
100
111
127 2 @ymd = @$x;
128 }
129 elsif ( UNIVERSAL::isa( $x, __PACKAGE__ ) ) {
130 0 return ($x);
131 }
132 ▶ elsif ($x =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/
Condition100%3/3
$x =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/ or $x =~ /^(\d\d\d\d)(\d\d)(\d\d)$/
l!l&&r!l&&!r
2222
MC/DC100%2/2
$x =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/ or $x =~ /^(\d\d\d\d)(\d\d)(\d\d)$/
$x =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/ $x =~ /^(\d\d\d\d)(\d\d)(\d\d)$/
Truth table100%3/3
A or B
A= $x =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/B= $x =~ /^(\d\d\d\d)(\d\d)(\d\d)$/
ABresult
1X1
011
000
133 || $x =~ /^(\d\d\d\d)(\d\d)(\d\d)$/ ) {
134 24 @ymd = ( $1, $2, $3 );
135 }
136 else {
137 2 return (undef);
138 }
139 } # we fall through here...
140  
141 # note we can end up here is they pass in [] as the date
142 64 ▶ return $class->_today() unless @ymd;
Branch: 1 branch
BranchTrueFalseTotal
unless @ymd 4 60 64
143  
144 # to get here, we had one arg which was split,
145 # or 3 in the first place
146 60 ▶ if ( @ymd == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ymd == 3) 60 0 60
147 60 my $days = ymd_to_days(@ymd);
148 60 ▶ return undef if !defined($days);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $days 18 42 60
149 42 return ( bless( \$days, $class ) );
150 }
151  
152 0 $class->_inval(@ymd);
153 }
154  
155 2 ▶sub date { scalar __PACKAGE__->_new(@_) }
Subroutine date: called CC=1 SCAR=0.0
Pod documented
156  
157 0 ▶sub date_fmt {
Subroutine date_fmt: not called CC=2 SCAR=17.9
Pod documented
158 0 my $format = shift;
159 0 my $obj = Date::Simple::Fmt->_new(@_);
160 0 ▶ $obj->default_format($format)
Branch: 1 branch
BranchTrueFalseTotal
if $obj 0 0 0
161 if $obj;
162 0 $obj;
163 }
164  
165 0 ▶sub date_d8 { scalar Date::Simple::D8->_new(@_) }
Subroutine date_d8: not called CC=1 SCAR=6.9
Pod documented
166 0 ▶sub date_iso { scalar Date::Simple::ISO->_new(@_) }
Subroutine date_iso: not called CC=1 SCAR=6.9
Pod documented
167  
168 # Same as date() but it's a method and croaks on error if called with
169 # one arg.
170 55 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
171 55 my ( $class, $date );
172  
173 55 $date = &_new;
174 55 ▶ if ( !$date && scalar(@_) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $date and scalar @_ == 1) 0 55 55
Condition66%2/3
not $date and scalar @_ == 1
!ll&&!rl&&r
37180
MC/DC0%0/2
not $date and scalar @_ == 1
not $date scalar @_ == 1
Truth table66%2/3
not(A) and B
A= not $dateB= scalar @_ == 1
ABresult
0X0
100
111
175 0 Carp::croak( "'" . shift() . "' is not a valid ISO formated date" );
176 }
177 55 return ($date);
178 }
179  
180 2 ▶sub next { return ( $_[0] + 1 ); }
Subroutine next: called CC=1 SCAR=0.0
Pod documented
181 2 ▶sub prev { return ( $_[0] - 1 ); }
Subroutine prev: called CC=1 SCAR=0.0
Pod documented
182  
183 32 ▶sub _gmtime {
Subroutine _gmtime: called CC=1 SCAR=0.0
184 32 my ( $y, $m, $d ) = days_to_ymd( ${ $_[0] } );
185 32 $y -= 1900;
186 32 $m -= 1;
187 32 return ( 0, 0, 0, $d, $m, $y );
188 }
189  
190 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
191 3 our $Standard_Format = "%Y-%m-%d";
192 3 my %fmts = ( # Inside out parameter
193 'Date::Simple' => $Standard_Format,
194 'Date::Simple::ISO' => $Standard_Format,
195 'Date::Simple::D8' => "%Y%m%d",
196 'Date::Simple::Fmt' => $Standard_Format,
197 );
198  
199 32 ▶ sub format {
Subroutine format: called CC=6 SCAR=19.1
Pod documented
200 32 my ( $self, $format ) = @_;
201  
202 $format =
203 $fmts{ refaddr($self) || '' }
204 32 ▶ || $fmts{ ref($self) }
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 10 22 32
Condition50%4/8
$fmts{builtin::refaddr($self) or ''} || $fmts{ref $self} || $Date::Simple::Standard_Format
l!l&&r!l&&!r
1000
$fmts{builtin::refaddr($self) or ""} or $fmts{ref $self}
l!l&&r!l&&!r
640
builtin::refaddr($self) || ''
l!l
100
MC/DC0%0/3
$fmts{builtin::refaddr($self) or ''} || $fmts{ref $self} || $Date::Simple::Standard_Format
$fmts{builtin::refaddr($self) or ""} $fmts{ref $self} $Date::Simple::Standard_Format
MC/DC0%0/1
builtin::refaddr($self) || ''
builtin::refaddr($self)
Truth table0%0/4
A or B || C
A= $fmts{builtin::refaddr($self) or ""}B= $fmts{ref $self}C= $Date::Simple::Standard_Format
ABCresult
1XX1
01X1
0011
0000
Truth table50%1/2
A
A= builtin::refaddr($self)
Aresult
11
00
205 || $Standard_Format
206 if @_ == 1;
207  
208 32 ▶ return "$self" unless defined($format);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 32 32
209 32 require POSIX;
210 32 local $ENV{TZ} = 'UTC+0';
211 32 return POSIX::strftime( $format, _gmtime($self) );
212 }
213  
214 15 ▶ sub strftime { &format }
Subroutine strftime: called CC=1 SCAR=0.0
Pod documented
215 11 ▶ sub as_str { &format }
Subroutine as_str: called CC=1 SCAR=0.0
Pod documented
216  
217 43 ▶ sub default_format {
Subroutine default_format: called CC=6 SCAR=19.9
Pod documented
218 43 my ( $self, $val ) = @_;
219  
220 43 ▶ my $o = refaddr($self) || $self;
Condition33%1/3
builtin::refaddr($self) || $self
l!l&&r!l&&!r
4300
MC/DC0%0/2
builtin::refaddr($self) || $self
builtin::refaddr($self) $self
Truth table33%1/3
A || B
A= builtin::refaddr($self)B= $self
ABresult
1X1
011
000
221  
222 43 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 23 20 43
223 23 $fmts{$o} = $val;
224 23 ▶ warnings::warnif "Setting class specific date format '$o' to" . "'"
Branch: 2 branches
BranchTrueFalseTotal
unless ref $self 0 23 23
defined $val ? : 0 0 0
225 . ( defined $val ? $val : 'undef' ) . "'"
226 unless ref $self;
227 }
228  
229 43 ▶ return $fmts{$o} || $Standard_Format;
Condition66%2/3
$fmts{$o} || $Date::Simple::Standard_Format
l!l&&r!l&&!r
4030
MC/DC0%0/2
$fmts{$o} || $Date::Simple::Standard_Format
$fmts{$o} $Date::Simple::Standard_Format
Truth table66%2/3
A || B
A= $fmts{$o}B= $Date::Simple::Standard_Format
ABresult
1X1
011
000
230 }
231  
232 109 ▶ sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
233 109 delete $fmts{ refaddr $_[0] };
234 }
235 }
236  
237 3 1;
238  
239 =head1 NAME
240  
241 Date::Simple - a simple date object
242  
243 =head1 SYNOPSIS
244  
245 use Date::Simple ('date', 'today');
246  
247 # Difference in days between two dates:
248 $diff = date('2001-08-27') - date('1977-10-05');
249  
250 # Offset $n days from now:
251 $date = today() + $n;
252 print "$date\n"; # uses ISO 8601 format (YYYY-MM-DD)
253  
254 use Date::Simple ();
255 my $date = Date::Simple->new('1972-01-17');
256 my $year = $date->year;
257 my $month = $date->month;
258 my $day = $date->day;
259  
260 use Date::Simple (':all');
261 my $date2 = ymd($year, $month, $day);
262 my $date3 = d8('19871218');
263 my $today = today();
264 my $tomorrow = $today + 1;
265 if ($tomorrow->year != $today->year) {
266 print "Today is New Year's Eve!\n";
267 }
268  
269 if ($today > $tomorrow) {
270 die "warp in space-time continuum";
271 }
272  
273 print "Today is ";
274 print(('Sun','Mon','Tues','Wednes','Thurs','Fri','Satur')
275 [$today->day_of_week]);
276 print "day.\n";
277  
278 # you can also do this:
279 ($date cmp "2001-07-01")
280 # and this
281 ($date <=> [2001, 7, 1])
282  
283 =begin text
284  
285 INSTALLATION
286  
287 If your system has the "make" program or a clone:
288  
289 perl Makefile.PL
290 make
291 make test
292 make install
293  
294 If you lack "make", copy the "lib/Date" directory to your module
295 directory (run "perl -V:sitelib" to find it).
296  
297 If "make test" fails, perhaps it means your system can't compile C
298 code. Try:
299  
300 make distclean
301 perl Makefile.PL noxs
302 make
303 make test
304 make install
305  
306 This will use the pure-Perl implementation.
307  
308 =end text
309  
310 =head1 DESCRIPTION
311  
312 Dates are complex enough without times and timezones. This module may
313 be used to create simple date objects. It handles:
314  
315 =over 4
316  
317 =item Validation.
318  
319 Reject 1999-02-29 but accept 2000-02-29.
320  
321 =item Interval arithmetic.
322  
323 How many days were between two given dates? What date comes N days
324 after today?
325  
326 =item Day-of-week calculation.
327  
328 What day of the week is a given date?
329  
330 =item Transparent date formatting.
331  
332 How should a date object be formatted.
333  
334 =back
335  
336 It does B<not> deal with hours, minutes, seconds, and time zones.
337  
338 A date is uniquely identified by year, month, and day integers within
339 valid ranges. This module will not allow the creation of objects for
340 invalid dates. Attempting to create an invalid date will return
341 undef. Month numbering starts at 1 for January, unlike in C and Java.
342 Years are 4-digit.
343  
344 Gregorian dates up to year 9999 are handled correctly, but we rely on
345 Perl's builtin C<localtime> function when the current date is
346 requested. On some platforms, C<localtime> may be vulnerable to
347 rollovers such as the Unix C<time_t> wraparound of 18 January 2038.
348  
349 Overloading is used so you can compare or subtract two dates using
350 standard numeric operators such as C<==>, and the sum of a date object
351 and an integer is another date object.
352  
353 Date::Simple objects are immutable. After assigning C<$date1> to
354 C<$date2>, no change to C<$date1> can affect C<$date2>. This means,
355 for example, that there is nothing like a C<set_year> operation, and
356 C<$date++> assigns a new object to C<$date>.
357  
358 This module contains various undocumented functions. They may not be
359 available on all platforms and are likely to change or disappear in
360 future releases. Please let the author know if you think any of them
361 should be public.
362  
363 =head2 Controlling output format.
364  
365 As of version 3.0 new ways of controlling the output formats of Date::Simple
366 objects has been provided. However Date::Simple has traditionally provided
367 few ways of stringification, a primary one via the format() method and another
368 primary one via direct stringification. However the later is currently
369 implemented as an XS routine and the former is implemented through a perl routine.
370 This means that using format() is more expensive than stringification and
371 that the stringification format is class specific.
372  
373 In order to alleviate some of these problems a new mechanism has been introduced
374 to Date::Simple that allows for a per object level format default. In addition
375 a set of utility classes that have different stringification overloads provided.
376 These classes are simple subclasses of Date::Simple and beside the default format()
377 and the overloaded stringification behaviour are identical to Date::Simple. In fact
378 one is totally identical to Date::Simple and is provided mostly for completeness.
379  
380 The classes included are:
381  
382 =over 4
383  
384 =item Date::Simple::ISO
385  
386 Identical to Date::Simple in every respect but name.
387  
388 =item Date::Simple::D8
389  
390 Uses the D8 format (%Y%m%d) as the default format for printing. Uses XS for the
391 overloaded stringification.
392  
393 =item Date::Simple::Fmt
394  
395 Uses the perl implemented format() as the default stringification mechanism. The first
396 argument to the constructor is expected to be the format to use for the object.
397  
398 =back
399  
400 B<NOTE> its important to remember that the primary difference between the behaviour
401 of objects of the different classes is how they are stringified when quoted, and what
402 date format is used by default when the format() method is called. Nothing else differs.
403  
404 =head1 CONSTRUCTORS
405  
406 Several functions take a string or numeric representation and generate
407 a corresponding date object. The most general is C<new>, whose
408 argument list may be empty (returning the current date), a string in
409 format YYYY-MM-DD or YYYYMMDD, a list or arrayref of year, month, and
410 day number, or an existing date object.
411  
412 =over 4
413  
414 =item Date::Simple->new ([ARG, ...])
415  
416 =item date ([ARG, ...])
417  
418 my $date = Date::Simple->new('1972-01-17');
419  
420 The C<new> method will return a date object if the values passed in
421 specify a valid date. (See above.) If an invalid date is passed, the
422 method returns undef. If the argument is invalid in form as opposed
423 to numeric range, C<new> dies.
424  
425 The C<date> function provides the same functionality but must be
426 imported or qualified as C<Date::Simple::date>. (To import all public
427 functions, do C<use Date::Simple (':all');>.) This function returns
428 undef on all invalid input, rather than dying in some cases like
429 C<new>.
430  
431 =item date_fmt (FMT,[ARG, ...])
432  
433 Equivelent to C<date> but creates a Date::Simple::Fmt object instead. The
434 format is expected to be a valid POSIX::strftime format string.
435  
436 =item date_iso ([ARG, ...])
437  
438 Identical to C<date> but creates a Date::Simple::ISO object instead.
439  
440 =item date_d8 ([ARG, ...])
441  
442 Equivelent to C<date> but creates a Date::Simple::D8 object instead.
443  
444 =item today()
445  
446 Returns the current date according to C<localtime>.
447  
448 B<Caution:> To get tomorrow's date (or any fixed offset from today),
449 do not use C<today + 1>. Perl parses this as C<today(+1)>. You need
450 to put empty parentheses after the function: C<today() + 1>.
451  
452 =item ymd (YEAR, MONTH, DAY)
453  
454 Returns a date object with the given year, month, and day numbers. If
455 the arguments do not specify a valid date, undef is returned.
456  
457 Example:
458  
459 use Date::Simple ('ymd');
460 $pbd = ymd(1987, 12, 18);
461  
462 =item d8 (STRING)
463  
464 Parses STRING as "YYYYMMDD" and returns the corresponding date object,
465 or undef if STRING has the wrong format or specifies an invalid date.
466  
467 Example:
468  
469 use Date::Simple ('d8');
470 $doi = d8('17760704');
471  
472 Mnemonic: The string matches C</\d{8}/>. Also, "d8" spells "date", if
473 8 is expanded phonetically.
474  
475 =back
476  
477 =head1 INSTANCE METHODS
478  
479 =over 4
480  
481 =item DATE->next
482  
483 my $tomorrow = $today->next;
484  
485 Returns an object representing tomorrow.
486  
487 =item DATE->prev
488  
489 my $yesterday = $today->prev;
490  
491 Returns an object representing yesterday.
492  
493 =item DATE->year
494  
495 my $year = $date->year;
496  
497 Return the year of DATE as an integer.
498  
499 =item DATE->month
500  
501 my $month = $date->month;
502  
503 Return the month of DATE as an integer from 1 to 12.
504  
505 =item DATE->day
506  
507 my $day = $date->day;
508  
509 Return the DATE's day of the month as an integer from 1 to 31.
510  
511 =item DATE->day_of_week
512  
513 Return a number representing DATE's day of the week from 0 to 6, where
514 0 means Sunday.
515  
516 =item DATE->as_ymd
517  
518 my ($year, $month, $day) = $date->as_ymd;
519  
520 Returns a list of three numbers: year, month, and day.
521  
522 =item DATE->as_d8
523  
524 Returns the "d8" representation (see C<d8>), like
525 C<$date-E<gt>format("%Y%m%d")>.
526  
527 =item DATE->as_iso
528  
529 Returns the ISO 8601 representation of the date (eg '2004-01-01'),
530 like C<$date-E<gt>format("%Y-%m-%d")>. This is in fact the default
531 overloaded stringification mechanism and is provided mostly so
532 other subclasses with different overloading can still do fast
533 ISO style date output.
534  
535 =item DATE->as_str ([STRING])
536  
537 =item DATE->format ([STRING])
538  
539 =item DATE->strftime ([STRING])
540  
541 These functions are equivalent. Return a string representing the
542 date, in the format specified. If you don't pass a parameter, the default
543 date format for the object is used if one has been specified, otherwise
544 uses the default date format for the class the object is a member of, or as
545 a last fallback uses the $Date::Simple::Standard_Format which is changeable,
546 but probably shouldn't be modified. See C<default_format> for details.
547  
548 my $change_date = $date->format("%d %b %y");
549 my $iso_date1 = $date->format("%Y-%m-%d");
550 my $iso_date2 = $date->format;
551  
552 The formatting parameter is similar to one you would pass to
553 strftime(3). This is because we actually do pass it to strftime to
554 format the date. This may result in differing behavior across
555 platforms and locales and may not even work everywhere.
556  
557 =item DATE->default_format ([FORMAT])
558  
559 This method sets or gets the default_format for the DATE object or class
560 that it is called on.
561  
562 =back
563  
564 =head1 OPERATORS
565  
566 Some operators can be used with Date::Simple instances. If one side
567 of an expression is a date object, and the operator expects two date
568 objects, the other side is interpreted as C<date(ARG)>, so an array
569 reference or ISO 8601 string will work.
570  
571 =over 4
572  
573 =item DATE + NUMBER
574  
575 =item DATE - NUMBER
576  
577 You can construct a new date offset by a number of days using the C<+>
578 and C<-> operators.
579  
580 =item DATE1 - DATE2
581  
582 You can subtract two dates to find the number of days between them.
583  
584 =item DATE1 == DATE2
585  
586 =item DATE1 < DATE2
587  
588 =item DATE1 <=> DATE2
589  
590 =item DATE1 cmp DATE2
591  
592 =item etc.
593  
594 You can compare two dates using the arithmetic or string comparison
595 operators. Equality tests (C<==> and C<eq>) return false when one of
596 the expressions can not be converted to a date. Other comparison
597 tests die in such cases. This is intentional, because in a sense, all
598 non-dates are not "equal" to all dates, but in no sense are they
599 "greater" or "less" than dates.
600  
601 =item DATE += NUMBER
602  
603 =item DATE -= NUMBER
604  
605 You can increment or decrement a date by a number of days using the +=
606 and -= operators. This actually generates a new date object and is
607 equivalent to C<$date = $date + $number>.
608  
609 =item "$date"
610  
611 You can interpolate a date instance directly into a string, in the
612 format specified by ISO 8601 (eg: 2000-01-17) for Date::Simple and
613 Date::Simple::ISO, for Date::Simple::D8 this is the same as calling
614 as_d8() on the object, and for Date::Simple::Fmt this is the same as
615 calling format() on the object.
616  
617 =back
618  
619 =head1 UTILITIES
620  
621 =over 4
622  
623 =item leap_year (YEAR)
624  
625 Returns true if YEAR is a leap year.
626  
627 =item days_in_month (YEAR, MONTH)
628  
629 Returns the number of days in MONTH, YEAR.
630  
631 =back
632  
633 =over 4
634  
635 =item leap_year (YEAR)
636  
637 Returns true if YEAR is a leap year.
638  
639 =item days_in_month (YEAR, MONTH)
640  
641 Returns the number of days in MONTH, YEAR.
642  
643 =back
644  
645  
646 =head1 AUTHOR
647  
648 Marty Pauley <marty@kasei.com>
649 John Tobey <jtobey@john-edwin-tobey.org>
650 Yves Orton <demerphq@hotmail.com>
651  
652 =head1 COPYRIGHT
653  
654 Copyright (C) 2001 Kasei.
655 Copyright (C) 2001,2002 John Tobey.
656 Copyright (C) 2004 Yves Orton.
657  
658 This program is free software; you can redistribute it and/or
659 modify it under the terms of either:
660  
661 a) the GNU General Public License;
662 either version 2 of the License, or (at your option) any later
663 version. You should have received a copy of the GNU General
664 Public License along with this program; see the file COPYING.
665 If not, write to the Free Software Foundation, Inc., 59
666 Temple Place, Suite 330, Boston, MA 02111-1307 USA
667  
668 b) the Perl Artistic License.
669  
670 This program is distributed in the hope that it will be useful,
671 but WITHOUT ANY WARRANTY; without even the implied warranty of
672 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
673  
674 =head1 SEE ALSO
675  
676 L<Date::Simple::Fmt> L<Date::Simple::ISO> L<Date::Simple::D8>
677 and of course L<perl>
678  
679 =cut
« blib/lib/Date/Simple/NoXS.pm ↑ Summary blib/lib/Date/Simple/D8.pm »