| 1 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | package Date::Span 1.129; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: deal with date/time ranges than span multiple dates | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | BEGIN { our @ISA = 'Exporter' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | our @EXPORT = qw(range_expand range_durations range_from_unit); ## no critic | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 13 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 14 | #pod use Date::Span; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 16 | #pod @spanned = range_expand($start, $end); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 18 | #pod print "from $_->[0] to $_->[1]\n" for (@spanned); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 20 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod This module provides code for dealing with datetime ranges that span multiple | ||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod calendar days. This is useful for computing, for example, the amount of | ||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod seconds spent performing a task on each day. Given the following table: | ||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod event | begun | ended | ||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod ---------+------------------+------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod loading | 2004-01-01 00:00 | 2004-01-01 12:45 | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod venting | 2004-01-01 12:45 | 2004-01-02 21:15 | ||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod running | 2004-01-02 21:15 | 2004-01-03 00:00 | ||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 32 | #pod We may want to gather the following data: | ||||||||||||||||||||||||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 34 | #pod date | event | time spent | ||||||||||||||||||||||||||||||||||||||||||
| 35 | #pod ------------+---------+---------------- | ||||||||||||||||||||||||||||||||||||||||||
| 36 | #pod 2004-01-01 | loading | 12.75 hours | ||||||||||||||||||||||||||||||||||||||||||
| 37 | #pod 2004-01-01 | venting | 11.25 hours | ||||||||||||||||||||||||||||||||||||||||||
| 38 | #pod 2004-01-02 | venting | 21.25 hours | ||||||||||||||||||||||||||||||||||||||||||
| 39 | #pod 2004-01-02 | running | 2.75 hours | ||||||||||||||||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 41 | #pod Date::Span takes a data like the first and produces data more like the second. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | #pod (Details on exact interface are below.) | ||||||||||||||||||||||||||||||||||||||||||
| 43 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 44 | #pod =func range_durations | ||||||||||||||||||||||||||||||||||||||||||
| 45 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 46 | #pod my @durations = range_durations($start, $end) | ||||||||||||||||||||||||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 48 | #pod Given C<$start> and C<$end> as timestamps (in epoch seconds), | ||||||||||||||||||||||||||||||||||||||||||
| 49 | #pod C<range_durations> returns a list of arrayrefs. Each arrayref is a date | ||||||||||||||||||||||||||||||||||||||||||
| 50 | #pod (expressed as epoch seconds at midnight) and the number of seconds for which | ||||||||||||||||||||||||||||||||||||||||||
| 51 | #pod the given range intersects with the date. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 53 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 8 | ▶ | sub _date_time { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_date_time: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 8 | my $date = $_[0] - (my $time = $_[0] % 86400); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 8 | ($date, $time) | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | sub range_durations { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
range_durations: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | my ($start, $end) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | return if $end < $start; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | my ($start_date, $start_time) = _date_time($start); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | my ($end_date, $end_time) = _date_time($end); | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | push my @results, [ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | $start_date, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | (( $end_date != $start_date ) ? ( 86400 - $start_time ) : ($end - $start)) | ||||||||||||||||||||||||||||||||||||||||||
| 70 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | push @results, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | map { [ $start_date + 86400 * $_, 86400 ] } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | (1 .. ($end_date - $start_date - 86400) / 86400) | ||||||||||||||||||||||||||||||||||||||||||
| 75 | if ($end_date - $start_date > 86400); | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | push @results, [ $end_date, $end_time ] if $start_date != $end_date; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | return @results; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | #pod =func range_expand | ||||||||||||||||||||||||||||||||||||||||||
| 83 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 84 | #pod my @endpoint_pairs = range_expand($start, $end); | ||||||||||||||||||||||||||||||||||||||||||
| 85 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 86 | #pod Given C<$start> and C<$end> as timestamps (in epoch seconds), | ||||||||||||||||||||||||||||||||||||||||||
| 87 | #pod C<range_durations> returns a list of arrayrefs. Each arrayref is a start and | ||||||||||||||||||||||||||||||||||||||||||
| 88 | #pod end timestamp. No pair of start and end times will cross a date boundary, and | ||||||||||||||||||||||||||||||||||||||||||
| 89 | #pod the set of ranges as a whole will be identical to the passed start and end. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 91 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | sub range_expand { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
range_expand: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | my ($start, $end) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | ▶ | return if $end < $start; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | my ($start_date, $start_time) = _date_time($start); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | my ($end_date, $end_time) = _date_time($end); | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | push my @results, [ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | $start, ( ( $end_date != $start_date ) ? ( $start_date + 86399 ) : $end ) | ||||||||||||||||||||||||||||||||||||||||||
| 102 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | push @results, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | ▶ | map { [ $start_date + 86400 * $_, $start_date + 86400 * $_ + 86399 ] } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | (1 .. ($end_date - $start_date - 86400) / 86400) | ||||||||||||||||||||||||||||||||||||||||||
| 107 | if ($end_date - $start_date > 86400); | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | push @results, [ $end_date, $end ] if $start_date != $end_date; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | return @results; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | #pod =func range_from_unit | ||||||||||||||||||||||||||||||||||||||||||
| 115 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 116 | #pod my ($start, $end) = range_from_unit(@date_unit) | ||||||||||||||||||||||||||||||||||||||||||
| 117 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 118 | #pod C<@date_unit> is a specification of a unit of time, in the form: | ||||||||||||||||||||||||||||||||||||||||||
| 119 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 120 | #pod @date_unit = ($year, $month, $day, $hour, $minute); | ||||||||||||||||||||||||||||||||||||||||||
| 121 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 122 | #pod Only C<$year> is mandatory; other arguments may be added, in order. Month is | ||||||||||||||||||||||||||||||||||||||||||
| 123 | #pod given in the range (0 .. 11). This function will return the first and last | ||||||||||||||||||||||||||||||||||||||||||
| 124 | #pod second of the given unit. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 126 | #pod A code reference may be passed as the last object. It will be used to convert | ||||||||||||||||||||||||||||||||||||||||||
| 127 | #pod the date specification to a starting time. If no coderef is passed, a simple | ||||||||||||||||||||||||||||||||||||||||||
| 128 | #pod one using Time::Local (and C<timegm>) will be used. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 130 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 2 | my @monthdays = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 7 | ▶ | sub _is_leap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_leap: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 7 | ▶ | not($_[0] % 4) and (($_[0] % 100) or not($_[0] % 400)) and $_[0] > 0 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $_[0] % 4 and $_[0] % 100 || !($_[0] % 400)
$_[0] % 100 or not $_[0] % 400
MC/DC100%3/3
not $_[0] % 4 and $_[0] % 100 || !($_[0] % 400)
not $_[0] % 4
$_[0] % 100
not $_[0] % 400
Truth table100%4/4
not(A) and B or not(C)
A= not $_[0] % 4B= $_[0] % 100C= not $_[0] % 400
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 5 | ▶ | sub _leap_secs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_leap_secs: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 5 | ▶ | _is_leap($_[0]) && $_[1] == 1 ? 86400 : 0 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_is_leap($_[0]) and $_[1] == 1
MC/DC100%2/2
_is_leap($_[0]) and $_[1] == 1
_is_leap($_[0])
$_[1] == 1
Truth table100%3/3
A and B
A= _is_leap($_[0])B= $_[1] == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 9 | ▶ | sub _begin_secs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_begin_secs: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 9 | require Time::Local; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 9 | ▶ | Time::Local::timegm( | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%8/8
$_[4] || 0
$_[3] || 0
$_[2] || 1
$_[1] || 0
MC/DC100%1/1
$_[4] || 0
$_[4]
MC/DC100%1/1
$_[3] || 0
$_[3]
MC/DC100%1/1
$_[2] || 1
$_[2]
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[4]
Truth table100%2/2
A
A= $_[3]
Truth table100%2/2
A
A= $_[2]
Truth table100%2/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0, # $sec | ||||||||||||||||||||||||||||||||||||||||||
| 146 | $_[4]||0, # $min | ||||||||||||||||||||||||||||||||||||||||||
| 147 | $_[3]||0, # $hour | ||||||||||||||||||||||||||||||||||||||||||
| 148 | $_[2]||1, # $mday | ||||||||||||||||||||||||||||||||||||||||||
| 149 | $_[1]||0, # $mon | ||||||||||||||||||||||||||||||||||||||||||
| 150 | $_[0] # $year | ||||||||||||||||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 11 | ▶ | sub range_from_unit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
range_from_unit: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 11 | ▶ | my $code = (ref($_[-1])||'' eq 'CODE') ? pop : \&_begin_secs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $_[-1] or
MC/DC100%1/1
ref $_[-1] or
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 11 | ▶ | return unless @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 10 | my ($year,$month,$day,$hour,$min) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 10 | my $begin_secs = $code->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 10 | ▶ | my $length = defined $min ? 60 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | : defined $hour ? 3600 | ||||||||||||||||||||||||||||||||||||||||||
| 161 | : defined $day ? 86400 | ||||||||||||||||||||||||||||||||||||||||||
| 162 | : defined $month ? 86400 * $monthdays[$month+0] | ||||||||||||||||||||||||||||||||||||||||||
| 163 | + _leap_secs($year, $month) | ||||||||||||||||||||||||||||||||||||||||||
| 164 | : 86400 * (_is_leap($year) ? 366 : 365); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 10 | return ($begin_secs, $begin_secs + $length - 1); | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | #pod =head1 TODO | ||||||||||||||||||||||||||||||||||||||||||
| 170 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 171 | #pod This code was just yanked out of a general purpose set of utility functions | ||||||||||||||||||||||||||||||||||||||||||
| 172 | #pod I've compiled over the years. It should be refactored (internally) and | ||||||||||||||||||||||||||||||||||||||||||
| 173 | #pod further tested. The interface should stay pretty stable, though. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 175 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||||||||||||||||||||||||