| 1 | package Date::Easy::Date; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 19 | our $VERSION = '0.10'; # VERSION | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 19 | our @EXPORT_OK = qw< date today >; | |||||||||||||||||||||||||||
| 12 | 19 | our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 19 | ▶ | use parent 'Date::Easy::Datetime'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 19 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 19 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 19 | ▶ | use Time::Local 1.26, qw< timegm_modern >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | ############################## | ||||||||||||||||||||||||||||
| 22 | # FUNCTIONS (*NOT* METHODS!) # | ||||||||||||||||||||||||||||
| 23 | ############################## | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub date | ||||||||||||||||||||||||||||
| 27 | 273 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
date: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 273 | my $date = shift; | |||||||||||||||||||||||||||
| 29 | 273 | ▶ | if ( $date =~ /^-?\d+$/ ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||
| 31 | 15 | ▶ | if ($date < 29000000 and $date >= 10000000) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$date < 29000000 and $date >= 10000000
MC/DC100%2/2
$date < 29000000 and $date >= 10000000
$date < 29000000
$date >= 10000000
Truth table100%3/3
A and B
A= $date < 29000000B= $date >= 10000000
| |||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | 7 | my @time = $date =~ /^(\d{4})(\d{2})(\d{2})$/; | |||||||||||||||||||||||||||
| 34 | 7 | return Date::Easy::Date->new(@time); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 8 | return Date::Easy::Date->new($date); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | elsif ( $date !~ /\d/ ) | ||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||
| 40 | 20 | my $time = _parsedate($date); | |||||||||||||||||||||||||||
| 41 | 20 | ▶ | croak("Illegal date: $date") unless defined $time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 19 | return Date::Easy::Date->new($time); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | else | ||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||
| 46 | 238 | my (undef,undef,undef, $d, $m, $y) # ignore first 3 values (time portion) | |||||||||||||||||||||||||||
| 47 | = Date::Easy::Datetime::_strptime($date, 'local'); # remember: parse as local, store as UTC | ||||||||||||||||||||||||||||
| 48 | 238 | ▶ | if (defined $y) # they're either all defined, or it's bogus | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||
| 50 | # return value from _strptime for month is still in the funky 0 - 11 range | ||||||||||||||||||||||||||||
| 51 | 154 | return Date::Easy::Date->new($y, $m + 1, $d); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | else | ||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 84 | my $time = _parsedate($date); | |||||||||||||||||||||||||||
| 56 | 84 | ▶ | croak("Illegal date: $date") unless defined $time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 67 | return Date::Easy::Date->new($time); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | die("reached unreachable code"); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 5 | ▶ | sub today () { Date::Easy::Date->new } | ||||||||||||||||||||||||||
|
Subroutine
today: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub _parsedate | ||||||||||||||||||||||||||||
| 67 | 104 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_parsedate: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 104 | require Time::ParseDate; | |||||||||||||||||||||||||||
| 69 | 104 | my $string = shift; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Remove any timezone specifier so we get the date as it was in that timezone. | ||||||||||||||||||||||||||||
| 72 | # I've gathered up all timezone matching code from Time::ParseDate as of v2015.103. | ||||||||||||||||||||||||||||
| 73 | # matching code from Time/ParseDate.pm: | ||||||||||||||||||||||||||||
| 74 | 104 | my $break = qr{(?:\s+|\Z|\b(?![-:.,/]\d))}; # line 67 | |||||||||||||||||||||||||||
| 75 | 104 | $string =~ s/ | |||||||||||||||||||||||||||
| 76 | (?: | ||||||||||||||||||||||||||||
| 77 | [+-] \d\d:?\d\d \s+ \( "? (?: [A-Z]{1,4}[TCW56] | IDLE ) \) # lines 424-435 | ||||||||||||||||||||||||||||
| 78 | | GMT \s* [-+]\d{1,2} # line 441 | ||||||||||||||||||||||||||||
| 79 | | (?: GMT \s* )? [+-] \d\d:?\d\d # line 452 | ||||||||||||||||||||||||||||
| 80 | | "? (?: [A-Z]{1,4}[TCW56] | IDLE ) # line 457 (and 695-700) | ||||||||||||||||||||||||||||
| 81 | ) $break //x; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # We *must* force scalar context. Remember, parsedate called in list context also returns the | ||||||||||||||||||||||||||||
| 84 | # "remainder" of the parsed string (which is often undef, which could wreak havoc with a call | ||||||||||||||||||||||||||||
| 85 | # that incorporates our return value, particularly one to _mktime). | ||||||||||||||||||||||||||||
| 86 | 104 | return scalar Time::ParseDate::parsedate($string, DATE_REQUIRED => 1); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | ####################### | ||||||||||||||||||||||||||||
| 91 | # REGULAR CLASS STUFF # | ||||||||||||||||||||||||||||
| 92 | ####################### | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub new | ||||||||||||||||||||||||||||
| 96 | 2110 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 2110 | my $class = shift; | |||||||||||||||||||||||||||
| 98 | 2110 | my ($y, $m, $d); | |||||||||||||||||||||||||||
| 99 | 2110 | ▶ | if (@_ == 3) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 270 | ($y, $m, $d) = @_; | |||||||||||||||||||||||||||
| 102 | 270 | --$m; # timegm will expect month as 0..11 | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | else | ||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 1840 | my ($time) = @_; | |||||||||||||||||||||||||||
| 107 | 1840 | ▶ | $time = time unless defined $time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1840 | ▶ | if (my $conv_class = blessed $time) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 1739 | ▶ | if ( $time->isa('Time::Piece') ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 1737 | ($d, $m, $y) = ($time->mday, $time->_mon, $time->year); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | else | ||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 2 | croak("Don't know how to convert $conv_class to $class"); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else | ||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 101 | ($d, $m, $y) = (localtime $time)[3..5]; # `Date`s are parsed relative to local time ... | |||||||||||||||||||||||||||
| 122 | 101 | $y += 1900; # (no 2-digit dates allowed!) | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | my $truncated_date = | ||||||||||||||||||||||||||||
| 127 | 2108 | eval { timegm_modern( 0,0,0, $d,$m,$y ) }; # ... but stored as UTC | |||||||||||||||||||||||||||
| 128 | 2108 | ▶ | croak("Illegal date: $y/" . ($m + 1) . "/$d") unless defined $truncated_date; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 2105 | return $class->_mkdate($truncated_date); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub _mkdate | ||||||||||||||||||||||||||||
| 133 | 2105 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_mkdate: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 134 | 2105 | my ($invocant, $epoch) = @_; | |||||||||||||||||||||||||||
| 135 | 2105 | ▶ | my $class = ref $invocant || $invocant; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $invocant || $invocant
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant
$invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
| |||||||||||||||||||||||||||||
| 136 | 2105 | return bless Date::Easy::Datetime->new(UTC => $epoch), $class; # always UTC | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | ############################ | ||||||||||||||||||||||||||||
| 141 | # OVERRIDDEN FROM DATETIME # | ||||||||||||||||||||||||||||
| 142 | ############################ | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub split | ||||||||||||||||||||||||||||
| 146 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
split: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 1 | my $impl = shift->{impl}; | |||||||||||||||||||||||||||
| 148 | 1 | ( $impl->year, $impl->mon, $impl->mday ) | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # override addition and subtraction | ||||||||||||||||||||||||||||
| 153 | # numbers added to a ::Date are days | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 978 | ▶ | sub _add_integer { $_[0]->add_days($_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
_add_integer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | 246 | ▶ | sub _subtract_integer { $_[0]->subtract_days($_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
_subtract_integer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # These are illegal to call. | ||||||||||||||||||||||||||||
| 160 | 2 | ▶ | sub add_seconds { die("cannot call add_seconds on a Date value") } | ||||||||||||||||||||||||||
|
Subroutine
add_seconds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | 2 | ▶ | sub add_minutes { die("cannot call add_minutes on a Date value") } | ||||||||||||||||||||||||||
|
Subroutine
add_minutes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 2 | ▶ | sub add_hours { die("cannot call add_hours on a Date value") } | ||||||||||||||||||||||||||
|
Subroutine
add_hours: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 2 | ▶ | sub subtract_seconds { die("cannot call subtract_seconds on a Date value") } | ||||||||||||||||||||||||||
|
Subroutine
subtract_seconds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | 2 | ▶ | sub subtract_minutes { die("cannot call subtract_minutes on a Date value") } | ||||||||||||||||||||||||||
|
Subroutine
subtract_minutes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | 2 | ▶ | sub subtract_hours { die("cannot call subtract_hours on a Date value") } | ||||||||||||||||||||||||||
|
Subroutine
subtract_hours: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 19 | 1; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # ABSTRACT: easy date class | ||||||||||||||||||||||||||||
| 174 | # COPYRIGHT | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||