blib/lib/Date/Darian/Mars.pm

filter: Statementstmt 100% 107 / 107 Branchbran 97.8% 45 / 46 Conditioncond 93.9% 31 / 33 MC/DCmcdc 90.0% 18 / 20 Subroutinesub 100% 23 / 23 Podpod 100% 9 / 9 totaltotal 97.9% 233 / 238 CC 35 SCAR 12.9
CC
35
Cov
98%
month_days
6.2
present_ymd
6.0
ymd_to_cmsdn
5.0
CRAP 3.6
↑ Summary
1 =head1 NAME
2  
3 Date::Darian::Mars - the Darian calendar for Mars
4  
5 =head1 SYNOPSIS
6  
7 use Date::Darian::Mars qw(present_y);
8  
9 print present_y($y);
10  
11 use Date::Darian::Mars qw(
12 month_days cmsdn_to_ymd ymd_to_cmsdn present_ymd);
13  
14 $md = month_days(209, 23);
15 ($y, $m, $d) = cmsdn_to_ymd(546236);
16 $cmsdn = ymd_to_cmsdn(209, 23, 18);
17 print present_ymd(546236);
18 print present_ymd(209, 23, 18);
19  
20 use Date::Darian::Mars qw(
21 year_days cmsdn_to_yd yd_to_cmsdn present_yd);
22  
23 $yd = year_days(209);
24 ($y, $d) = cmsdn_to_yd(546236);
25 $cmsdn = yd_to_cmsdn(209, 631);
26 print present_yd(546236);
27 print present_yd(209, 631);
28  
29 =head1 DESCRIPTION
30  
31 The Darian calendar for Mars is a mechanism by which Martian solar days
32 (also known as "sols") can be labelled in a manner useful to inhabitants
33 of Mars. This module provides functions to convert dates between the
34 Darian calendar and Chronological Mars Solar Day Numbers, which is a
35 suitable format to do arithmetic with. It also supplies functions that
36 describe the shape of the Darian calendar, to assist in calendrical
37 calculations. It also supplies functions to represent Darian dates
38 textually in a conventional format.
39  
40 The Darian calendar divides time up into years, months, and days.
41 This module also supports dividing the Darian year directly into days,
42 with no months.
43  
44 The Chronological Mars Solar Day Number is an integral number labelling
45 each Martian day, where the day extends from midnight to midnight in
46 whatever time zone is of interest. It is a linear count of days, where
47 each day's number is one greater than the previous day's number.
48  
49 This module places no limit on the range of dates to which it may be
50 applied. All function arguments are permitted to be C<Math::BigInt> or
51 C<Math::BigRat> objects in order to achieve arbitrary range. Native Perl
52 integers are also permitted, as a convenience when the range of dates
53 being handled is known to be sufficiently small.
54  
55 =head1 DARIAN CALENDAR FOR MARS
56  
57 The main cycle in the Darian calendar is the year. It approximates the
58 length of a Martian tropical year (specifically, the northward equinoctal
59 year), and the year starts approximately on the northward equinox.
60 Years are either 668 or 669 Martian solar days long. 669-day years are
61 referred to as "leap years".
62  
63 Each year is divided into 24 months, of nearly equal length. The months
64 are purely nominal: they do not correspond to any astronomical cycle.
65 Each quarter of the year consists of five months of 28 days followed by
66 one month of 27 days, except that the last month of a leap year contains
67 28 days instead of 27.
68  
69 All odd-numbered years are leap years. Even-numbered years are not leap
70 years, except for years divisible by ten which are leap years, except
71 for years divisible by 100 which are not, except for years divisible by
72 500 which are.
73  
74 Days within each month are numbered sequentially, starting at 1.
75 The months have names (in fact several competing sets of names), but this
76 module does not deal with the names. In this module, months within each
77 year are numbered sequentially from 1.
78  
79 Years are numbered sequentially. Year 0 is the year in which the first
80 known telescopic observations of Mars occurred. Specifically, year 0
81 started at the midnight that occurred on the Airy meridian (the Martian
82 prime meridian) at approximately MJD -91195.22 in Terrestrial Time.
83  
84 The calendar is described canonically, and in more detail, at
85 L<http://pweb.jps.net/~tgangale/mars/converter/calendar_clock.htm>.
86  
87 The day when Mars Exploration Rover "Opportunity" landed in Meridiani
88 Planum was 0209-23-18 or 0209-631 in the Darian calendar, and CMSDN
89 546236.
90  
91 =cut
92  
93 package Date::Darian::Mars;
94  
95 4 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
96 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 4 our $VERSION = "0.004";
102  
103 4 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
104 4 our @EXPORT_OK = qw(
105 present_y
106 month_days cmsdn_to_ymd ymd_to_cmsdn present_ymd
107 year_days cmsdn_to_yd yd_to_cmsdn present_yd
108 );
109  
110 # _numify(A): turn possibly-object number into native Perl integer
111  
112 248 ▶sub _numify($) {
Subroutine _numify: called CC=2 SCAR=6.9
113 248 my($a) = @_;
114 248 ▶ return ref($a) eq "" ? $a : $a->numify;
Branch: 1 branch
BranchTrueFalseTotal
ref $a eq '' ? : 176 72 248
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 188 ▶sub _fdiv($$) {
Subroutine _fdiv: called CC=3 SCAR=11.0
123 188 my($a, $b) = @_;
124 188 ▶ if(ref($a) eq "Math::BigRat") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'Math::BigRat') { } 36 152 188
125 36 return ($a / $b)->bfloor;
126 } else {
127 152 ▶ if($a < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a < 0) { } 8 144 152
128 4 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 8 return -(($b - 1 - $a) / $b);
130 } else {
131 4 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 144 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 572 ▶sub _fmod($$) {
Subroutine _fmod: called CC=2 SCAR=6.9
143 572 my($a, $b) = @_;
144 572 ▶ if(ref($a) eq "Math::BigRat") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'Math::BigRat') { } 74 498 572
145 74 return $a - $b * ($a / $b)->bfloor;
146 } else {
147 498 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 the conventional textual presentation
170 format. For years [0, 9999] this is simply four digits. For years
171 outside that range it is a sign followed by at least four digits.
172  
173 This is the minimum-length presentation format. If it is desired
174 to use a form that is longer than necessary, such as to use at least
175 five digits for all year numbers, then the right tool is C<sprintf>
176 (see L<perlfunc/sprintf>).
177  
178 =cut
179  
180 56 ▶sub present_y($) {
Subroutine present_y: called CC=4 SCAR=13.9
Pod documented
181 56 my($y) = @_;
182 56 my($sign, $digits) = ("$y" =~ /\A\+?(-?)0*([0-9]+?)\z/);
183 56 ▶ $digits = ("0" x (4 - length($digits))).$digits
Branch: 1 branch
BranchTrueFalseTotal
unless length $digits >= 4 32 24 56
184 unless length($digits) >= 4;
185 56 ▶ $sign = "+" if $sign eq "" && length($digits) > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "" and length $digits > 4 6 50 56
Condition100%3/3
$sign eq "" and length $digits > 4
!ll&&!rl&&r
18326
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
ABresult
0X0
100
111
186 56 return $sign.$digits;
187 }
188  
189 =back
190  
191 =head2 Darian calendar
192  
193 Each year is divided into 24 months, numbered [1, 24]. Each month is
194 divided into days, numbered sequentially from 1. The month lengths
195 are irregular. The year numbers have unlimited range.
196  
197 =over
198  
199 =item month_days(YEAR, MONTH)
200  
201 The parameters identify a month, and the function returns the number of
202 days in that month as a native Perl integer.
203  
204 =cut
205  
206 210 ▶sub _year_leap($) {
Subroutine _year_leap: called CC=4 SCAR=13.9
207 210 my($y) = @_;
208 210 ▶ return _fmod($y, 2) == 1 ||
Condition100%9/9
&_fmod($y, 2) == 1 || &_fmod($y, 10) == 0 && (&_fmod($y, 100) != 0 || &_fmod($y, 500) == 0)
l!l&&r!l&&!r
716079
&_fmod($y, 10) == 0 and &_fmod($y, 100) != 0 || &_fmod($y, 500) == 0
!ll&&!rl&&r
671260
&_fmod($y, 100) != 0 or &_fmod($y, 500) == 0
l!l&&r!l&&!r
332712
MC/DC100%4/4
&_fmod($y, 2) == 1 || &_fmod($y, 10) == 0 && (&_fmod($y, 100) != 0 || &_fmod($y, 500) == 0)
&_fmod($y, 2) == 1 &_fmod($y, 10) == 0 &_fmod($y, 100) != 0 &_fmod($y, 500) == 0
Truth table100%5/5
A || B and C or D
A= &_fmod($y, 2) == 1B= &_fmod($y, 10) == 0C= &_fmod($y, 100) != 0D= &_fmod($y, 500) == 0
ABCDresult
1XXX1
011X1
01011
00XX0
01000
209 (_fmod($y, 10) == 0 &&
210 (_fmod($y, 100) != 0 || _fmod($y, 500) == 0));
211 }
212  
213 {
214 142 ▶ sub month_days($$) {
Subroutine month_days: called CC=6 SCAR=18.3
Pod documented
215 142 my($y, $m) = @_;
216 142 ▶ croak "month number $m is out of the range [1, 24]"
Branch: 1 branch
BranchTrueFalseTotal
unless $m >= 1 and $m <= 24 0 142 142
Condition33%1/3
$m >= 1 and $m <= 24
!ll&&!rl&&r
00142
MC/DC0%0/2
$m >= 1 and $m <= 24
$m >= 1 $m <= 24
Truth table33%1/3
A and B
A= $m >= 1B= $m <= 24
ABresult
0X0
100
111
217 unless $m >= 1 && $m <= 24;
218 142 ▶ if($m == 24) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m == 24) { } 53 89 142
219 53 ▶ return _year_leap($y) ? 28 : 27;
Branch: 1 branch
BranchTrueFalseTotal
&_year_leap($y) ? : 31 22 53
220 } else {
221 89 ▶ return _fmod($m, 6) == 0 ? 27 : 28;
Branch: 1 branch
BranchTrueFalseTotal
&_fmod($m, 6) == 0 ? : 10 79 89
222 }
223 }
224 }
225  
226 =item cmsdn_to_ymd(CMSDN)
227  
228 This function takes a Chronological Mars Solar Day Number and returns
229 a list of a year, month, and day.
230  
231 =cut
232  
233 4 sub cmsdn_to_yd($);
234  
235 29 ▶sub cmsdn_to_ymd($) {
Subroutine cmsdn_to_ymd: called CC=2 SCAR=6.9
Pod documented
236 29 my($cmsdn) = @_;
237 29 my($y, $d) = cmsdn_to_yd($cmsdn);
238 29 ▶ return ($y, 24, 28) if $d == 669;
Branch: 1 branch
BranchTrueFalseTotal
if $d == 669 6 23 29
239 23 $d--;
240 23 my $sixm = _fdiv($d, 28*6 - 1);
241 23 $d -= $sixm * (28*6 - 1);
242 23 my $m = _fdiv($d, 28);
243 23 return ($y, 1 + 6*$sixm + $m, 1 + _fmod($d, 28));
244 }
245  
246 =item ymd_to_cmsdn(YEAR, MONTH, DAY)
247  
248 This performs the reverse of the translation that C<cmsdn_to_ymd> does.
249 It takes year, month, and day numbers, and returns the corresponding
250 CMSDN.
251  
252 =cut
253  
254 sub yd_to_cmsdn($$);
255  
256 33 ▶sub ymd_to_cmsdn($$$) {
Subroutine ymd_to_cmsdn: called CC=5 SCAR=16.1
Pod documented
257 33 my($y, $m, $d) = @_;
258 33 ▶ croak "month number $m is out of the range [1, 24]"
Branch: 1 branch
BranchTrueFalseTotal
unless $m >= 1 and $m <= 24 2 31 33
Condition100%3/3
$m >= 1 and $m <= 24
!ll&&!rl&&r
1131
MC/DC100%2/2
$m >= 1 and $m <= 24
$m >= 1 $m <= 24
Truth table100%3/3
A and B
A= $m >= 1B= $m <= 24
ABresult
0X0
100
111
259 unless $m >= 1 && $m <= 24;
260 31 $m = _numify($m);
261 31 my $md = month_days($y, $m);
262 31 ▶ croak "day number $d is out of the range [1, $md]"
Branch: 1 branch
BranchTrueFalseTotal
unless $d >= 1 and $d <= $md 4 27 31
Condition100%3/3
$d >= 1 and $d <= $md
!ll&&!rl&&r
1327
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
ABresult
0X0
100
111
263 unless $d >= 1 && $d <= $md;
264 27 return yd_to_cmsdn($y, ($m - 1) * 28 - _fdiv($m - 1, 6) + _numify($d));
265 }
266  
267 =item present_ymd(CMSDN)
268  
269 =item present_ymd(YEAR, MONTH, DAY)
270  
271 Puts the given date into conventional Darian textual presentation format.
272  
273 If the date is given as a (YEAR, MONTH, DAY) triplet then these are not
274 checked for consistency. The MONTH and DAY values are only checked to
275 ensure that they fit into the fixed number of digits. This allows the
276 use of this function on data other than actual Darian dates.
277  
278 =cut
279  
280 11 ▶sub present_ymd($;$$) {
Subroutine present_ymd: called CC=6 SCAR=17.9
Pod documented
281 11 my($y, $m, $d);
282 11 ▶ if(@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 2 9 11
283 2 ($y, $m, $d) = cmsdn_to_ymd($_[0]);
284 } else {
285 9 ($y, $m, $d) = @_;
286 9 ▶ croak "month number $m is out of the displayable range"
Branch: 1 branch
BranchTrueFalseTotal
unless $m >= 0 and $m < 100 2 7 9
Condition100%3/3
$m >= 0 and $m < 100
!ll&&!rl&&r
117
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
ABresult
0X0
100
111
287 unless $m >= 0 && $m < 100;
288 7 ▶ croak "day number $d is out of the displayable range"
Branch: 1 branch
BranchTrueFalseTotal
unless $d >= 0 and $d < 100 2 5 7
Condition100%3/3
$d >= 0 and $d < 100
!ll&&!rl&&r
115
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
ABresult
0X0
100
111
289 unless $d >= 0 && $d < 100;
290 }
291 7 return sprintf("%s-%02d-%02d", present_y($y),
292 _numify($m), _numify($d));
293 }
294  
295 =back
296  
297 =head2 Ordinal dates
298  
299 Each year is divided into days, numbered sequentially from 1. The year
300 lengths are irregular. The years correspond exactly to those of the
301 Darian calendar.
302  
303 =over
304  
305 =item year_days(YEAR)
306  
307 The parameter identifies a year, and the function returns the number of
308 days in that year as a native Perl integer.
309  
310 =cut
311  
312 157 ▶sub year_days($) {
Subroutine year_days: called CC=2 SCAR=6.9
Pod documented
313 157 my($y) = @_;
314 157 ▶ return _year_leap($y) ? 669 : 668;
Branch: 1 branch
BranchTrueFalseTotal
&_year_leap($y) ? : 100 57 157
315 }
316  
317 4 ▶use constant DARIAN_ZERO_CMSDN => 405871; # 0000-001
Subroutine BEGIN: called CC=1 SCAR=0.0
318  
319 =item cmsdn_to_yd(CMSDN)
320  
321 This function takes a Chronological Mars Solar Day Number and returns
322 a list of a year and ordinal day.
323  
324 =cut
325  
326 58 ▶sub cmsdn_to_yd($) {
Subroutine cmsdn_to_yd: called CC=3 SCAR=11.0
Pod documented
327 58 my($cmsdn) = @_;
328 4 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
329 58 my $d = $cmsdn - DARIAN_ZERO_CMSDN;
330 58 my $qcents = _fdiv($d, 668*500 + 296);
331 58 $d = _numify($d - $qcents * (668*500 + 296));
332 58 my $y = $d / 669;
333 58 ▶ my $leaps = ($y / 2) + ($y+9) / 10 - ($y+99) / 100 + ($y == 0 ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$y == 0 ? : 6 52 58
334 58 $d -= 668 * $y + $leaps;
335 58 my $yd = year_days($y);
336 58 ▶ if($d >= $yd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d >= $yd) 20 38 58
337 20 $d -= $yd;
338 20 $y++;
339 }
340 58 return ($qcents*500 + $y, 1 + $d);
341 }
342  
343 =item yd_to_cmsdn(YEAR, DAY)
344  
345 This performs the reverse of the translation that C<cmsdn_to_yd> does.
346 It takes year and ordinal day numbers, and returns the corresponding
347 CMSDN.
348  
349 =cut
350  
351 57 ▶sub yd_to_cmsdn($$) {
Subroutine yd_to_cmsdn: called CC=4 SCAR=13.9
Pod documented
352 57 my($y, $d) = @_;
353 4 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
354 57 my $qcents = _fdiv($y, 500);
355 57 $y = _numify($y - $qcents * 500);
356 57 my $yd = year_days($y);
357 57 ▶ croak "day number $d is out of the range [1, $yd]"
Branch: 1 branch
BranchTrueFalseTotal
unless $d >= 1 and $d <= $yd 3 54 57
Condition100%3/3
$d >= 1 and $d <= $yd
!ll&&!rl&&r
1254
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
ABresult
0X0
100
111
358 unless $d >= 1 && $d <= $yd;
359 54 $d = _numify($d);
360 54 ▶ my $leaps = ($y / 2) + ($y+9) / 10 - ($y+99) / 100 + ($y == 0 ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$y == 0 ? : 6 48 54
361 54 return (DARIAN_ZERO_CMSDN + 668*$y + $leaps + ($d - 1)) +
362 $qcents * (668*500 + 296);
363 }
364  
365 =item present_yd(CMSDN)
366  
367 =item present_yd(YEAR, DAY)
368  
369 Puts the given date into the conventional ordinal textual presentation
370 format.
371  
372 If the date is given as a (YEAR, DAY) pair then these are not checked
373 for consistency. The DAY value is only checked to ensure that it fits
374 into the fixed number of digits. This allows the use of this function
375 on data other than actual ordinal dates.
376  
377 =cut
378  
379 9 ▶sub present_yd($;$) {
Subroutine present_yd: called CC=4 SCAR=13.9
Pod documented
380 9 my($y, $d);
381 9 ▶ if(@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 2 7 9
382 2 ($y, $d) = cmsdn_to_yd($_[0]);
383 } else {
384 7 ($y, $d) = @_;
385 7 ▶ croak "day number $d is out of the displayable range"
Branch: 1 branch
BranchTrueFalseTotal
unless $d >= 0 and $d < 1000 2 5 7
Condition100%3/3
$d >= 0 and $d < 1000
!ll&&!rl&&r
115
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
ABresult
0X0
100
111
386 unless $d >= 0 && $d < 1000;
387 }
388 7 return sprintf("%s-%03d", present_y($y), _numify($d));
389 }
390  
391 =back
392  
393 =head1 SEE ALSO
394  
395 L<Date::MSD>,
396 L<http://pweb.jps.net/~tgangale/mars/converter/calendar_clock.htm>,
397 L<http://www.fysh.org/~zefram/time/define_cmsd.txt>
398  
399 =head1 AUTHOR
400  
401 Andrew Main (Zefram) <zefram@fysh.org>
402  
403 =head1 COPYRIGHT
404  
405 Copyright (C) 2007, 2009, 2011, 2017
406 Andrew Main (Zefram) <zefram@fysh.org>
407  
408 =head1 LICENSE
409  
410 This module is free software; you can redistribute it and/or modify it
411 under the same terms as Perl itself.
412  
413 =cut
414  
415 4 1;
↑ Summary