| 1 | package Date::Lectionary::Time; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 6 | ▶ | use v5.22; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 6 | ▶ | use Exporter::Easy ( OK => [qw(nextSunday prevSunday closestSunday isSunday)], ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 6 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 6 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 6 | ▶ | use Time::Piece; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 6 | ▶ | use Time::Seconds; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | Date::Lectionary::Time - Find your way in time relative to Sundays. | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 VERSION | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | Version 1.20170311 | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 6 | our $VERSION = '1.20170311'; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | Working in the liturgical time of the lectionary means tracking time relative to Sundays. This is a quick utility to find the next, previous, or the closest Sunday to a given date. Further, it can determine if the date given is a Sunday or not. | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | use Time::Piece; | ||||||||||||||||||||
| 31 | use Date::Lectionary::Time qw(nextSunday prevSunday closestSunday isSunday); | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | my $christmasDay = Time::Piece->strptime("2015-12-25", "%Y-%m-%d"); | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | if (isSunday($christmasDay)) { | ||||||||||||||||||||
| 36 | say "Christmas is on a Sunday!"; | ||||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||
| 39 | say "Christmas isn't on a Sunday."; | ||||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | my $sundayAfterChristmas = nextSunday($christmasDay); | ||||||||||||||||||||
| 43 | my $sundayBeforeChristmas = prevSunday($christmasDay); | ||||||||||||||||||||
| 44 | my $sundayClosestToChristmas = closestSunday($christmasDay); | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =head1 EXPORTS | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | nextSunday | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | prevSunday | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | closestSunday | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | isSunday | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | use Date::Lectionary::Time qw(nextSunday prevSunday closestSunday isSunday); | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =head2 nextSunday | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | For a given Time::Piece date returns a Time::Piece object of the date of the Sunday immediately following the given date. | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub nextSunday { | ||||||||||||||||||||
| 67 | 11 | ▶ | my ( $class, @params ) = @_; | ||||||||||||||||||
|
Subroutine
nextSunday: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 68 | 11 | ▶ | my $date = $params[0] // $class; | ||||||||||||||||||
|
Condition66%2/3
$params[0] // $class
MC/DC50%1/2
$params[0] // $class
$params[0]
$class
Truth table66%2/3
A // B
A= $params[0]B= $class
| |||||||||||||||||||||
| 69 | 11 | my $nextSunday = undef; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 11 | ▶ | if ( !length $date ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 1 | croak | |||||||||||||||||||
| 73 | "Method [nextSunday] expects an input argument of type Time::Piece. The given type could not be determined."; | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 10 | ▶ | if ( $date->isa('Time::Piece') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | try { | ||||||||||||||||||||
| 78 | 9 | ▶ | my $daysToAdd = 7 - $date->_wday; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | 9 | my $secondsToAdd = $daysToAdd * ONE_DAY; | |||||||||||||||||||
| 80 | 9 | $nextSunday = $date + $secondsToAdd; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | catch { | ||||||||||||||||||||
| 83 | 0 | ▶ | croak "Could not calculate the next Sunday after $date."; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 84 | 9 | }; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||
| 87 | 1 | croak | |||||||||||||||||||
| 88 | "Method [nextSunday] expects an input argument of type Time::Piece."; | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 9 | return $nextSunday; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =head2 prevSunday | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | For a given Time::Piece date returns a Time::Piece object of the date of the Sunday immediately before the given date. | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | sub prevSunday { | ||||||||||||||||||||
| 101 | 11 | ▶ | my ( $class, @params ) = @_; | ||||||||||||||||||
|
Subroutine
prevSunday: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 102 | 11 | ▶ | my $date = $params[0] // $class; | ||||||||||||||||||
|
Condition66%2/3
$params[0] // $class
MC/DC50%1/2
$params[0] // $class
$params[0]
$class
Truth table66%2/3
A // B
A= $params[0]B= $class
| |||||||||||||||||||||
| 103 | 11 | my $prevSunday = undef; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 11 | ▶ | if ( !length $date ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 1 | croak | |||||||||||||||||||
| 107 | "Method [prevSunday] expects an input argument of type Time::Piece. The given type could not be determined."; | ||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 10 | ▶ | if ( $date->isa('Time::Piece') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | try { | ||||||||||||||||||||
| 112 | 9 | ▶ | my $daysToSubtract = $date->_wday; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 113 | 9 | ▶ | if ( $daysToSubtract == 0 ) { $daysToSubtract = 7; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 9 | my $secondsToSubtract = $daysToSubtract * ONE_DAY; | |||||||||||||||||||
| 115 | 9 | $prevSunday = $date - $secondsToSubtract; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | catch { | ||||||||||||||||||||
| 118 | 0 | ▶ | carp "Could not calculate the previous Sunday before $date."; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 119 | 9 | }; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||
| 122 | 1 | croak | |||||||||||||||||||
| 123 | "Method [prevSunday] expects an input argument of type Time::Piece."; | ||||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 9 | return $prevSunday; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head2 closestSunday | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | For a given Time::Piece date returns a Time::Piece object of the date of the Sunday closest to the given date. | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | sub closestSunday { | ||||||||||||||||||||
| 136 | 4 | ▶ | my ( $class, @params ) = @_; | ||||||||||||||||||
|
Subroutine
closestSunday: called CC=4 SCAR=14.4
Pod
documented
| |||||||||||||||||||||
| 137 | 4 | ▶ | my $date = $params[0] // $class; | ||||||||||||||||||
|
Condition33%1/3
$params[0] // $class
MC/DC0%0/2
$params[0] // $class
$params[0]
$class
Truth table33%1/3
A // B
A= $params[0]B= $class
| |||||||||||||||||||||
| 138 | 4 | my $closestSunday = undef; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 4 | ▶ | if ( !length $date ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 0 | croak | |||||||||||||||||||
| 142 | "Method [closestSunday] expects an input argument of type Time::Piece. The given type could not be determined."; | ||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 4 | ▶ | if ( $date->isa('Time::Piece') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | try { | ||||||||||||||||||||
| 147 | 4 | ▶ | my $nextSunday = nextSunday($date); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 148 | 4 | my $prevSunday = prevSunday($date); | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 4 | my ( $dif1, $dif2 ); | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 4 | $dif1 = abs( $date - $nextSunday ); | |||||||||||||||||||
| 153 | 4 | $dif2 = abs( $prevSunday - $date ); | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 4 | ▶ | if ( $dif1 < $dif2 ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 156 | 2 | $closestSunday = $nextSunday; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | elsif ( $dif1 == $dif2 ) { | ||||||||||||||||||||
| 159 | 1 | $closestSunday = $date; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||
| 162 | 1 | $closestSunday = $prevSunday; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | catch { | ||||||||||||||||||||
| 166 | 0 | ▶ | carp "Could not calculate the Sunday closest to $date."; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 167 | 4 | }; | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||
| 170 | 0 | croak | |||||||||||||||||||
| 171 | "Method [closestSunday] expects an input argument of type Time::Piece."; | ||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 4 | return $closestSunday; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =head2 isSunday | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | For a given Time::Piece date returns C<1> if the date is a Sunday or C<0> if the date isn't a Sunday. | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | sub isSunday { | ||||||||||||||||||||
| 184 | 3 | ▶ | my ( $class, @params ) = @_; | ||||||||||||||||||
|
Subroutine
isSunday: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||
| 185 | 3 | ▶ | my $date = $params[0] // $class; | ||||||||||||||||||
|
Condition33%1/3
$params[0] // $class
MC/DC0%0/2
$params[0] // $class
$params[0]
$class
Truth table33%1/3
A // B
A= $params[0]B= $class
| |||||||||||||||||||||
| 186 | 3 | my $isSunday = 0; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 3 | ▶ | if ( !length $date ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | 0 | croak | |||||||||||||||||||
| 190 | "Method [isSunday] expects an input argument of type Time::Piece. The given type could not be determined."; | ||||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 3 | ▶ | if ( $date->isa('Time::Piece') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | try { | ||||||||||||||||||||
| 195 | 3 | ▶ | if ($date->wday == 1) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||
| 196 | 1 | $isSunday = 1; | |||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | catch { | ||||||||||||||||||||
| 200 | 0 | ▶ | carp "Could not calculate the Sunday closest to $date."; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 201 | 3 | }; | |||||||||||||||||||
| 202 | } | ||||||||||||||||||||
| 203 | else { | ||||||||||||||||||||
| 204 | 0 | croak | |||||||||||||||||||
| 205 | "Method [isSunday] expects an input argument of type Time::Piece."; | ||||||||||||||||||||
| 206 | } | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 3 | return $isSunday; | |||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | =head1 AUTHOR | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | Michael Wayne Arnold, C<< <marmanold at cpan.org> >> | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | =head1 BUGS | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | Please report any bugs or feature requests to C<bug-date-lectionary-time at rt.cpan.org>, or through | ||||||||||||||||||||
| 218 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Date-Lectionary-Time>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | =head1 SUPPORT | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | perldoc Date::Lectionary::Time | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | The development of this module is hosted on GitHub -- L<https://github.com/marmanold/Date-Lectionary-Time> -- and tested via TravisCI. | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | =for html <a href='https://travis-ci.org/marmanold/Date-Lectionary-Time'><img src='https://travis-ci.org/marmanold/Date-Lectionary-Time.svg?branch=master' /></a> | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | =for html <a href='https://coveralls.io/github/marmanold/Date-Lectionary-Time?branch=master'><img src='https://coveralls.io/repos/github/marmanold/Date-Lectionary-Time/badge.svg?branch=master' alt='Coverage Status' /></a> | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | You can also look for information at: | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | =over 4 | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Date-Lectionary-Time> | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | L<http://annocpan.org/dist/Date-Lectionary-Time> | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | =item * CPAN Ratings | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | L<http://cpanratings.perl.org/d/Date-Lectionary-Time> | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | =item * Search CPAN | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | L<http://search.cpan.org/dist/Date-Lectionary-Time/> | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | =back | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | Many thanks to my beautiful wife, Jennifer, and my amazing daughter, Rosemary. But, above all, SOLI DEO GLORIA! | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | Copyright 2016 Michael Wayne Arnold. | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 263 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||
| 264 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | =cut | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | 6 | 1; # End of Date::Lectionary::Time | |||||||||||||||||||