| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | Date::ISO8601 - the three ISO 8601 numerical calendars | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | use Date::ISO8601 qw(present_y); | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | print present_y($y); | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | use Date::ISO8601 qw( | ||||||||||||||||||||||||||||||||||||||||||
| 12 | month_days cjdn_to_ymd ymd_to_cjdn present_ymd); | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | $md = month_days(2000, 2); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | ($y, $m, $d) = cjdn_to_ymd(2406029); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | $cjdn = ymd_to_cjdn(1875, 5, 20); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | print present_ymd(2406029); | ||||||||||||||||||||||||||||||||||||||||||
| 18 | print present_ymd(1875, 5, 20); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | use Date::ISO8601 qw(year_days cjdn_to_yd yd_to_cjdn present_yd); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | $yd = year_days(2000); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ($y, $d) = cjdn_to_yd(2406029); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | $cjdn = yd_to_cjdn(1875, 140); | ||||||||||||||||||||||||||||||||||||||||||
| 25 | print present_yd(2406029); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | print present_yd(1875, 140); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | use Date::ISO8601 qw( | ||||||||||||||||||||||||||||||||||||||||||
| 29 | year_weeks cjdn_to_ywd ywd_to_cjdn present_ywd); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | $yw = year_weeks(2000); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ($y, $w, $d) = cjdn_to_ywd(2406029); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | $cjdn = ywd_to_cjdn(1875, 20, 4); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | print present_ywd(2406029); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | print present_ywd(1875, 20, 4); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | The international standard ISO 8601 "Data elements and interchange formats | ||||||||||||||||||||||||||||||||||||||||||
| 40 | - Information interchange - Representation of dates and times" defines | ||||||||||||||||||||||||||||||||||||||||||
| 41 | three distinct calendars by which days can be labelled. It also defines | ||||||||||||||||||||||||||||||||||||||||||
| 42 | textual formats for the representation of dates in these calendars. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | This module provides functions to convert dates between these three | ||||||||||||||||||||||||||||||||||||||||||
| 44 | calendars and Chronological Julian Day Numbers, which is a suitable | ||||||||||||||||||||||||||||||||||||||||||
| 45 | format to do arithmetic with. It also supplies functions that describe | ||||||||||||||||||||||||||||||||||||||||||
| 46 | the shape of these calendars, to assist in calendrical calculations. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | It also supplies functions to represent dates textually in the ISO | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 8601 formats. ISO 8601 also covers time of day and time periods, but | ||||||||||||||||||||||||||||||||||||||||||
| 49 | this module does nothing relating to those parts of the standard; this | ||||||||||||||||||||||||||||||||||||||||||
| 50 | is only about labelling days. | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | The first ISO 8601 calendar divides time up into years, months, and days. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | It corresponds exactly to the Gregorian calendar, invented by Aloysius | ||||||||||||||||||||||||||||||||||||||||||
| 54 | Lilius and promulgated by Pope Gregory XIII in the late sixteenth century, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | with AD (CE) year numbering. This calendar is applied to all time, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | not just to dates after its invention nor just to years 1 and later. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | Thus for ancient dates it is the proleptic Gregorian calendar with | ||||||||||||||||||||||||||||||||||||||||||
| 58 | astronomical year numbering. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | The second ISO 8601 calendar divides time up into the same years as | ||||||||||||||||||||||||||||||||||||||||||
| 61 | the first, but divides the year directly into days, with no months. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | The standard calls this "ordinal dates". Ordinal dates are commonly | ||||||||||||||||||||||||||||||||||||||||||
| 63 | referred to as "Julian dates", a mistake apparently deriving from true | ||||||||||||||||||||||||||||||||||||||||||
| 64 | Julian Day Numbers, which divide time up solely into linearly counted | ||||||||||||||||||||||||||||||||||||||||||
| 65 | days. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | The third ISO 8601 calendar divides time up into years, weeks, and days. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | The years approximate the years of the first two calendars, so they stay | ||||||||||||||||||||||||||||||||||||||||||
| 69 | in step in the long term, but the boundaries differ. This week-based | ||||||||||||||||||||||||||||||||||||||||||
| 70 | calendar is sometimes called "the ISO calendar", apparently in the belief | ||||||||||||||||||||||||||||||||||||||||||
| 71 | that ISO 8601 does not define any other. It is also referred to as | ||||||||||||||||||||||||||||||||||||||||||
| 72 | "business dates", because it is most used by certain businesses to whom | ||||||||||||||||||||||||||||||||||||||||||
| 73 | the week is the most important temporal cycle. | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | The Chronological Julian Day Number is an integral number labelling each | ||||||||||||||||||||||||||||||||||||||||||
| 76 | day, where the day extends from midnight to midnight in whatever time zone | ||||||||||||||||||||||||||||||||||||||||||
| 77 | is of interest. It is a linear count of days, where each day's number | ||||||||||||||||||||||||||||||||||||||||||
| 78 | is one greater than the previous day's number. It is directly related to | ||||||||||||||||||||||||||||||||||||||||||
| 79 | the Julian Date system: in the time zone of the prime meridian, the CJDN | ||||||||||||||||||||||||||||||||||||||||||
| 80 | equals the JD at noon. By way of epoch, the day on which the Convention | ||||||||||||||||||||||||||||||||||||||||||
| 81 | of the Metre was signed, which ISO 8601 defines to be 1875-05-20 (and | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 1875-140 and 1875-W20-4), is CJDN 2406029. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | This module places no limit on the range of dates to which it may be | ||||||||||||||||||||||||||||||||||||||||||
| 85 | applied. All function arguments are permitted to be C<Math::BigInt> or | ||||||||||||||||||||||||||||||||||||||||||
| 86 | C<Math::BigRat> objects in order to achieve arbitrary range. Native Perl | ||||||||||||||||||||||||||||||||||||||||||
| 87 | integers are also permitted, as a convenience when the range of dates | ||||||||||||||||||||||||||||||||||||||||||
| 88 | being handled is known to be sufficiently small. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | package Date::ISO8601; | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 5 | ▶ | { use 5.006; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 5 | our $VERSION = "0.005"; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 5 | ▶ | use parent "Exporter"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 5 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||||
| 104 | present_y | ||||||||||||||||||||||||||||||||||||||||||
| 105 | month_days cjdn_to_ymd ymd_to_cjdn present_ymd | ||||||||||||||||||||||||||||||||||||||||||
| 106 | year_days cjdn_to_yd yd_to_cjdn present_yd | ||||||||||||||||||||||||||||||||||||||||||
| 107 | year_weeks cjdn_to_ywd ywd_to_cjdn present_ywd | ||||||||||||||||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # _numify(A): turn possibly-object number into native Perl integer | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1311 | ▶ | sub _numify($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_numify: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 1311 | my($a) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 1311 | ▶ | return ref($a) eq "" ? $a : $a->numify; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # _fdiv(A, B): divide A by B, flooring remainder | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # B must be a positive Perl integer. A may be a Perl integer, Math::BigInt, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # or Math::BigRat. The result has the same type as A. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 554 | ▶ | sub _fdiv($$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fdiv: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 554 | my($a, $b) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 554 | ▶ | if(ref($a) eq "Math::BigRat") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 82 | return ($a / $b)->bfloor; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 472 | ▶ | if($a < 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 5 | ▶ | use integer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 29 | return -(($b - 1 - $a) / $b); | |||||||||||||||||||||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 5 | ▶ | use integer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 443 | return $a / $b; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # _fmod(A, B): A modulo B, flooring remainder | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # B must be a positive Perl integer. A may be a Perl integer, Math::BigInt, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # or Math::BigRat. The result has the same type as A. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 1339 | ▶ | sub _fmod($$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fmod: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 1339 | my($a, $b) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 1339 | ▶ | if(ref($a) eq "Math::BigRat") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 153 | return $a - $b * ($a / $b)->bfloor; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 1186 | return $a % $b; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | Numbers in this API may be native Perl integers, C<Math::BigInt> objects, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | or integer-valued C<Math::BigRat> objects. All three types are acceptable | ||||||||||||||||||||||||||||||||||||||||||
| 155 | for all parameters, in any combination. In all conversion functions, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | the most-significant part of the result (which is the only part with | ||||||||||||||||||||||||||||||||||||||||||
| 157 | unlimited range) is of the same type as the most-significant part of | ||||||||||||||||||||||||||||||||||||||||||
| 158 | the input. Less-significant parts of results (which have a small range) | ||||||||||||||||||||||||||||||||||||||||||
| 159 | are consistently native Perl integers. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | All functions C<die> if given invalid parameters. | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =head2 Years | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =item present_y(YEAR) | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | Puts the given year number into ISO 8601 textual presentation format. | ||||||||||||||||||||||||||||||||||||||||||
| 170 | For years [0, 9999] this is simply four digits. For years outside that | ||||||||||||||||||||||||||||||||||||||||||
| 171 | range it is a sign followed by at least four digits. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | This is the minimum-length presentation format. If it is desired to | ||||||||||||||||||||||||||||||||||||||||||
| 174 | use a form that is longer than necessary, such as to use at least five | ||||||||||||||||||||||||||||||||||||||||||
| 175 | digits for all year numbers (as the Long Now Foundation does), then the | ||||||||||||||||||||||||||||||||||||||||||
| 176 | right tool is C<sprintf> (see L<perlfunc/sprintf>). | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | This format is unconditionally conformant to all versions of ISO 8601 | ||||||||||||||||||||||||||||||||||||||||||
| 179 | for years [1583, 9999]. For years [0, 1582], preceding the historical | ||||||||||||||||||||||||||||||||||||||||||
| 180 | introduction of the Gregorian calendar, it is conformant only where | ||||||||||||||||||||||||||||||||||||||||||
| 181 | it is mutually agreed that such dates (represented in the proleptic | ||||||||||||||||||||||||||||||||||||||||||
| 182 | Gregorian calendar) are acceptable. For years outside the range [0, | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 9999], where the expanded format must be used, the result is only | ||||||||||||||||||||||||||||||||||||||||||
| 184 | conformant to ISO 8601:2004 (earlier versions lacked these formats), | ||||||||||||||||||||||||||||||||||||||||||
| 185 | and only where it is mutually agreed to use this format. | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 63 | ▶ | sub present_y($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
present_y: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 63 | my($y) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 63 | my($sign, $digits) = ("$y" =~ /\A\+?(-?)0*([0-9]+?)\z/); | |||||||||||||||||||||||||||||||||||||||||
| 192 | 63 | ▶ | $digits = ("0" x (4 - length($digits))).$digits | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | unless length($digits) >= 4; | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 63 | ▶ | $sign = "+" if $sign eq "" && length($digits) > 4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sign eq "" and length $digits > 4
MC/DC100%2/2
$sign eq "" and length $digits > 4
$sign eq ""
length $digits > 4
Truth table100%3/3
A and B
A= $sign eq ""B= length $digits > 4
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 63 | return $sign.$digits; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =head2 Gregorian calendar | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | Each year is divided into twelve months, numbered [1, 12]; month number | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 1 is January. Each month is divided into days, numbered sequentially | ||||||||||||||||||||||||||||||||||||||||||
| 204 | from 1. The month lengths are irregular. The year numbers have | ||||||||||||||||||||||||||||||||||||||||||
| 205 | unlimited range. | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | =item month_days(YEAR, MONTH) | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | The parameters identify a month, and the function returns the number of | ||||||||||||||||||||||||||||||||||||||||||
| 212 | days in that month as a native Perl integer. | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 627 | ▶ | sub _year_leap($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_year_leap: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 627 | my($y) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 627 | ▶ | return _fmod($y, 4) == 0 && | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
&_fmod($y, 4) == 0 && (&_fmod($y, 100) != 0 || &_fmod($y, 400) == 0)
&_fmod($y, 100) != 0 or &_fmod($y, 400) == 0
MC/DC100%3/3
&_fmod($y, 4) == 0 && (&_fmod($y, 100) != 0 || &_fmod($y, 400) == 0)
&_fmod($y, 4) == 0
&_fmod($y, 100) != 0
&_fmod($y, 400) == 0
Truth table100%4/4
A && B or C
A= &_fmod($y, 4) == 0B= &_fmod($y, 100) != 0C= &_fmod($y, 400) == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | (_fmod($y, 100) != 0 || _fmod($y, 400) == 0); | ||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | { | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 5 | my @month_length = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); | |||||||||||||||||||||||||||||||||||||||||
| 224 | 57 | ▶ | sub month_days($$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
month_days: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 57 | my($y, $m) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 57 | ▶ | croak "month number $m is out of the range [1, 12]" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$m >= 1 and $m <= 12
MC/DC0%0/2
$m >= 1 and $m <= 12
$m >= 1
$m <= 12
Truth table33%1/3
A and B
A= $m >= 1B= $m <= 12
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | unless $m >= 1 && $m <= 12; | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 57 | ▶ | if($m == 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 24 | ▶ | return _year_leap($y) ? 29 : 28; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 33 | return $month_length[$m - 1]; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | { | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 5 | my @nonleap_monthstarts = | |||||||||||||||||||||||||||||||||||||||||
| 238 | (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365); | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 5 | my @leap_monthstarts = | |||||||||||||||||||||||||||||||||||||||||
| 240 | (0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366); | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 72 | ▶ | sub _year_monthstarts($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_year_monthstarts: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 72 | my($y) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 72 | ▶ | return _year_leap($y) ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | \@leap_monthstarts : \@nonleap_monthstarts; | ||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | =item cjdn_to_ymd(CJDN) | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | This function takes a Chronological Julian Day Number and returns a list | ||||||||||||||||||||||||||||||||||||||||||
| 251 | of a year, month, and day. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 5 | sub cjdn_to_yd($); | |||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | 35 | ▶ | sub cjdn_to_ymd($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cjdn_to_ymd: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 35 | my($cjdn) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 35 | my($y, $d) = cjdn_to_yd($cjdn); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 35 | my $monthstarts = _year_monthstarts($y); | |||||||||||||||||||||||||||||||||||||||||
| 261 | 35 | my $m = 1; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 35 | ▶ | while($d > $monthstarts->[$m]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 154 | $m++; | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 35 | return ($y, $m, $d - $monthstarts->[$m - 1]); | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | =item ymd_to_cjdn(YEAR, MONTH, DAY) | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | This performs the reverse of the translation that C<cjdn_to_ymd> does. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | It takes year, month, and day numbers, and returns the corresponding CJDN. | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | sub yd_to_cjdn($$); | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 39 | ▶ | sub ymd_to_cjdn($$$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ymd_to_cjdn: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 39 | my($y, $m, $d) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 39 | ▶ | croak "month number $m is out of the range [1, 12]" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$m >= 1 and $m <= 12
MC/DC100%2/2
$m >= 1 and $m <= 12
$m >= 1
$m <= 12
Truth table100%3/3
A and B
A= $m >= 1B= $m <= 12
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | unless $m >= 1 && $m <= 12; | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 37 | $m = _numify($m); | |||||||||||||||||||||||||||||||||||||||||
| 282 | 37 | my $monthstarts = _year_monthstarts($y); | |||||||||||||||||||||||||||||||||||||||||
| 283 | 37 | my $md = $monthstarts->[$m] - $monthstarts->[$m - 1]; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 37 | ▶ | croak "day number $d is out of the range [1, $md]" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$d >= 1 and $d <= $md
MC/DC100%2/2
$d >= 1 and $d <= $md
$d >= 1
$d <= $md
Truth table100%3/3
A and B
A= $d >= 1B= $d <= $md
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | unless $d >= 1 && $d <= $md; | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 33 | $d = _numify($d); | |||||||||||||||||||||||||||||||||||||||||
| 287 | 33 | return yd_to_cjdn($y, $monthstarts->[$m - 1] + $d); | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | =item present_ymd(CJDN) | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | =item present_ymd(YEAR, MONTH, DAY) | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | Puts the given date into ISO 8601 Gregorian textual presentation format. | ||||||||||||||||||||||||||||||||||||||||||
| 295 | The `extended' format (with "-" separators) is used. The conformance | ||||||||||||||||||||||||||||||||||||||||||
| 296 | notes for C<present_y> apply to this function also. | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | If the date is given as a (YEAR, MONTH, DAY) triplet then these are not | ||||||||||||||||||||||||||||||||||||||||||
| 299 | checked for consistency. The MONTH and DAY values are only checked to | ||||||||||||||||||||||||||||||||||||||||||
| 300 | ensure that they fit into the fixed number of digits. This allows the | ||||||||||||||||||||||||||||||||||||||||||
| 301 | use of this function on data other than actual Gregorian dates. | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 11 | ▶ | sub present_ymd($;$$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
present_ymd: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 11 | my($y, $m, $d); | |||||||||||||||||||||||||||||||||||||||||
| 307 | 11 | ▶ | if(@_ == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 2 | ($y, $m, $d) = cjdn_to_ymd($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 309 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 9 | ($y, $m, $d) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 311 | 9 | ▶ | croak "month number $m is out of the displayable range" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$m >= 0 and $m < 100
MC/DC100%2/2
$m >= 0 and $m < 100
$m >= 0
$m < 100
Truth table100%3/3
A and B
A= $m >= 0B= $m < 100
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | unless $m >= 0 && $m < 100; | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 7 | ▶ | croak "day number $d is out of the displayable range" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$d >= 0 and $d < 100
MC/DC100%2/2
$d >= 0 and $d < 100
$d >= 0
$d < 100
Truth table100%3/3
A and B
A= $d >= 0B= $d < 100
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | unless $d >= 0 && $d < 100; | ||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 7 | return sprintf("%s-%02d-%02d", present_y($y), | |||||||||||||||||||||||||||||||||||||||||
| 317 | _numify($m), _numify($d)); | ||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | =head2 Ordinal dates | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | Each year is divided into days, numbered sequentially from 1. The year | ||||||||||||||||||||||||||||||||||||||||||
| 325 | lengths are irregular. The years correspond exactly to those of the | ||||||||||||||||||||||||||||||||||||||||||
| 326 | Gregorian calendar. | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =item year_days(YEAR) | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | The parameter identifies a year, and the function returns the number of | ||||||||||||||||||||||||||||||||||||||||||
| 333 | days in that year as a native Perl integer. | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | 513 | ▶ | sub year_days($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
year_days: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 513 | my($y) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 339 | 513 | ▶ | return _year_leap($y) ? 366 : 365; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | 5 | ▶ | use constant GREGORIAN_ZERO_CJDN => 1721060; # 0000-001 | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | =item cjdn_to_yd(CJDN) | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | This function takes a Chronological Julian Day Number and returns a | ||||||||||||||||||||||||||||||||||||||||||
| 347 | list of a year and ordinal day. | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | 129 | ▶ | sub cjdn_to_yd($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cjdn_to_yd: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 129 | my($cjdn) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 353 | 5 | ▶ | use integer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 129 | my $d = $cjdn - GREGORIAN_ZERO_CJDN; | |||||||||||||||||||||||||||||||||||||||||
| 355 | 129 | my $qcents = _fdiv($d, 365*400 + 97); | |||||||||||||||||||||||||||||||||||||||||
| 356 | 129 | $d = _numify($d - $qcents * (365*400 + 97)); | |||||||||||||||||||||||||||||||||||||||||
| 357 | 129 | my $y = $d / 366; | |||||||||||||||||||||||||||||||||||||||||
| 358 | 129 | my $leaps = ($y + 3) / 4; | |||||||||||||||||||||||||||||||||||||||||
| 359 | 129 | ▶ | $leaps -= ($leaps - 1) / 25 unless $leaps == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | 129 | $d -= 365 * $y + $leaps; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 129 | my $yd = year_days($y); | |||||||||||||||||||||||||||||||||||||||||
| 362 | 129 | ▶ | if($d >= $yd) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 49 | $d -= $yd; | |||||||||||||||||||||||||||||||||||||||||
| 364 | 49 | $y++; | |||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 129 | return ($qcents*400 + $y, 1 + $d); | |||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | =item yd_to_cjdn(YEAR, DAY) | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | This performs the reverse of the translation that C<cjdn_to_yd> does. | ||||||||||||||||||||||||||||||||||||||||||
| 372 | It takes year and ordinal day numbers, and returns the corresponding CJDN. | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | 360 | ▶ | sub yd_to_cjdn($$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
yd_to_cjdn: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 360 | my($y, $d) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 378 | 5 | ▶ | use integer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 360 | my $qcents = _fdiv($y, 400); | |||||||||||||||||||||||||||||||||||||||||
| 380 | 360 | $y = _numify($y - $qcents * 400); | |||||||||||||||||||||||||||||||||||||||||
| 381 | 360 | my $yd = year_days($y); | |||||||||||||||||||||||||||||||||||||||||
| 382 | 360 | ▶ | croak "day number $d is out of the range [1, $yd]" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$d >= 1 and $d <= $yd
MC/DC100%2/2
$d >= 1 and $d <= $yd
$d >= 1
$d <= $yd
Truth table100%3/3
A and B
A= $d >= 1B= $d <= $yd
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | unless $d >= 1 && $d <= $yd; | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 357 | $d = _numify($d); | |||||||||||||||||||||||||||||||||||||||||
| 385 | 357 | my $leaps = ($y + 3) / 4; | |||||||||||||||||||||||||||||||||||||||||
| 386 | 357 | ▶ | $leaps -= ($leaps - 1) / 25 unless $leaps == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | 357 | return (GREGORIAN_ZERO_CJDN + 365*$y + $leaps + ($d - 1)) + | |||||||||||||||||||||||||||||||||||||||||
| 388 | $qcents * (365*400 + 97); | ||||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | =item present_yd(CJDN) | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | =item present_yd(YEAR, DAY) | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | Puts the given date into ISO 8601 ordinal textual presentation format. | ||||||||||||||||||||||||||||||||||||||||||
| 396 | The `extended' format (with "-" separators) is used. The conformance | ||||||||||||||||||||||||||||||||||||||||||
| 397 | notes for C<present_y> apply to this function also. | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | If the date is given as a (YEAR, DAY) pair then these are not checked | ||||||||||||||||||||||||||||||||||||||||||
| 400 | for consistency. The DAY value is only checked to ensure that it fits | ||||||||||||||||||||||||||||||||||||||||||
| 401 | into the fixed number of digits. This allows the use of this function | ||||||||||||||||||||||||||||||||||||||||||
| 402 | on data other than actual ordinal dates. | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | 9 | ▶ | sub present_yd($;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
present_yd: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | 9 | my($y, $d); | |||||||||||||||||||||||||||||||||||||||||
| 408 | 9 | ▶ | if(@_ == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 2 | ($y, $d) = cjdn_to_yd($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 410 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 411 | 7 | ($y, $d) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 412 | 7 | ▶ | croak "day number $d is out of the displayable range" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$d >= 0 and $d < 1000
MC/DC100%2/2
$d >= 0 and $d < 1000
$d >= 0
$d < 1000
Truth table100%3/3
A and B
A= $d >= 0B= $d < 1000
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | unless $d >= 0 && $d < 1000; | ||||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 7 | return sprintf("%s-%03d", present_y($y), _numify($d)); | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | =head2 Week-based calendar | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | Each year is divided into weeks, numbered sequentially from 1. Each week | ||||||||||||||||||||||||||||||||||||||||||
| 423 | is divided into seven days, numbered [1, 7]; day number 1 is Monday. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | The year lengths are irregular. The year numbers have unlimited range. | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | The years correspond to those of the Gregorian calendar. Each week is | ||||||||||||||||||||||||||||||||||||||||||
| 427 | associated with the Gregorian year that contains its Thursday and hence | ||||||||||||||||||||||||||||||||||||||||||
| 428 | contains the majority of its days. | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | =item year_weeks(YEAR) | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | The parameter identifies a year, and the function returns the number of | ||||||||||||||||||||||||||||||||||||||||||
| 435 | weeks in that year as a native Perl integer. | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | # _year_phase(YEAR): find day of week of first day of year | ||||||||||||||||||||||||||||||||||||||||||
| 440 | # | ||||||||||||||||||||||||||||||||||||||||||
| 441 | # The argument must be a native Perl integer. The return value is | ||||||||||||||||||||||||||||||||||||||||||
| 442 | # zero-based, in the range 0 = Monday to 6 = Sunday. | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | 234 | ▶ | sub _year_phase($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_year_phase: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 234 | my($y) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 446 | 234 | return yd_to_cjdn($y, 1) % 7; | |||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | 169 | ▶ | sub year_weeks($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
year_weeks: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 169 | my($y) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 451 | 169 | $y = _numify(_fmod($y, 400)); | |||||||||||||||||||||||||||||||||||||||||
| 452 | 169 | my $phase = _year_phase($y); | |||||||||||||||||||||||||||||||||||||||||
| 453 | 169 | ▶ | return $phase == 3 || ($phase == 2 && _year_leap($y)) ? 53 : 52; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$phase == 3 or $phase == 2 and &_year_leap($y)
$phase == 2 and &_year_leap($y)
MC/DC100%3/3
$phase == 3 or $phase == 2 and &_year_leap($y)
$phase == 3
$phase == 2
&_year_leap($y)
Truth table100%4/4
A or B and C
A= $phase == 3B= $phase == 2C= &_year_leap($y)
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | =item cjdn_to_ywd(CJDN) | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | This function takes a Chronological Julian Day Number and returns a list | ||||||||||||||||||||||||||||||||||||||||||
| 459 | of a year, week, and day. | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | 65 | ▶ | sub cjdn_to_ywd($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cjdn_to_ywd: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | 65 | my($cjdn) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 465 | 65 | my($y, $d) = cjdn_to_yd($cjdn); | |||||||||||||||||||||||||||||||||||||||||
| 466 | 65 | my $py = _numify(_fmod($y, 400)); | |||||||||||||||||||||||||||||||||||||||||
| 467 | 65 | my $phase = _year_phase($py); | |||||||||||||||||||||||||||||||||||||||||
| 468 | 65 | ▶ | my $start_wk1 = ($phase <= 3 ? 1 : 8) - $phase; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 469 | 65 | my $w = _fdiv($d - $start_wk1, 7); | |||||||||||||||||||||||||||||||||||||||||
| 470 | 65 | ▶ | if($w == -1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | 15 | $y--; | |||||||||||||||||||||||||||||||||||||||||
| 472 | 15 | $w = year_weeks($py - 1); | |||||||||||||||||||||||||||||||||||||||||
| 473 | } elsif($w >= year_weeks($py)) { | ||||||||||||||||||||||||||||||||||||||||||
| 474 | 3 | $y++; | |||||||||||||||||||||||||||||||||||||||||
| 475 | 3 | $w = 1; | |||||||||||||||||||||||||||||||||||||||||
| 476 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 477 | 47 | $w++; | |||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||
| 479 | 65 | return ($y, $w, ($d - $start_wk1) % 7 + 1); | |||||||||||||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | =item ywd_to_cjdn(YEAR, WEEK, DAY) | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | This performs the reverse of the translation that C<cjdn_to_ywd> does. | ||||||||||||||||||||||||||||||||||||||||||
| 485 | It takes year, week, and day numbers, and returns the corresponding CJDN. | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | 68 | ▶ | sub ywd_to_cjdn($$$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ywd_to_cjdn: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 68 | my($y, $w, $d) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 491 | 68 | my $yw = year_weeks($y); | |||||||||||||||||||||||||||||||||||||||||
| 492 | 68 | ▶ | croak "week number $w is out of the range [1, $yw]" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$w >= 1 and $w <= $yw
MC/DC100%2/2
$w >= 1 and $w <= $yw
$w >= 1
$w <= $yw
Truth table100%3/3
A and B
A= $w >= 1B= $w <= $yw
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | unless $w >= 1 && $w <= $yw; | ||||||||||||||||||||||||||||||||||||||||||
| 494 | 65 | ▶ | croak "day number $d is out of the range [1, 7]" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$d >= 1 and $d <= 7
MC/DC100%2/2
$d >= 1 and $d <= 7
$d >= 1
$d <= 7
Truth table100%3/3
A and B
A= $d >= 1B= $d <= 7
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | unless $d >= 1 && $d <= 7; | ||||||||||||||||||||||||||||||||||||||||||
| 496 | 63 | my $start_cjdn = yd_to_cjdn($y, 1); | |||||||||||||||||||||||||||||||||||||||||
| 497 | 63 | my $phase = _fmod($start_cjdn, 7); | |||||||||||||||||||||||||||||||||||||||||
| 498 | 63 | ▶ | return $start_cjdn + | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | (($phase <= 3 ? -8 : -1) - $phase + | ||||||||||||||||||||||||||||||||||||||||||
| 500 | _numify($w)*7 + _numify($d)); | ||||||||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | =item present_ywd(CJDN) | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | =item present_ywd(YEAR, WEEK, DAY) | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | Puts the given date into ISO 8601 week-based textual presentation format. | ||||||||||||||||||||||||||||||||||||||||||
| 508 | The `extended' format (with "-" separators) is used. The conformance | ||||||||||||||||||||||||||||||||||||||||||
| 509 | notes for C<present_y> apply to this function also. | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | If the date is given as a (YEAR, WEEK, DAY) triplet then these are not | ||||||||||||||||||||||||||||||||||||||||||
| 512 | checked for consistency. The WEEK and DAY values are only checked to | ||||||||||||||||||||||||||||||||||||||||||
| 513 | ensure that they fit into the fixed number of digits. This allows the | ||||||||||||||||||||||||||||||||||||||||||
| 514 | use of this function on data other than actual week-based dates. | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | 11 | ▶ | sub present_ywd($;$$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
present_ywd: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | 11 | my($y, $w, $d); | |||||||||||||||||||||||||||||||||||||||||
| 520 | 11 | ▶ | if(@_ == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 2 | ($y, $w, $d) = cjdn_to_ywd($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 522 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 523 | 9 | ($y, $w, $d) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 524 | 9 | ▶ | croak "week number $w is out of the displayable range" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$w >= 0 and $w < 100
MC/DC100%2/2
$w >= 0 and $w < 100
$w >= 0
$w < 100
Truth table100%3/3
A and B
A= $w >= 0B= $w < 100
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | unless $w >= 0 && $w < 100; | ||||||||||||||||||||||||||||||||||||||||||
| 526 | 7 | ▶ | croak "day number $d is out of the displayable range" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$d >= 0 and $d < 10
MC/DC100%2/2
$d >= 0 and $d < 10
$d >= 0
$d < 10
Truth table100%3/3
A and B
A= $d >= 0B= $d < 10
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | unless $d >= 0 && $d < 10; | ||||||||||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||||||||||
| 529 | 7 | return sprintf("%s-W%02d-%d", present_y($y), _numify($w), _numify($d)); | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | L<Date::JD>, | ||||||||||||||||||||||||||||||||||||||||||
| 537 | L<DateTime> | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||
| 541 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | Copyright (C) 2006, 2007, 2009, 2011, 2017 | ||||||||||||||||||||||||||||||||||||||||||
| 546 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 551 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||