1
|
|
|
<?php |
2
|
|
|
namespace Fisharebest\ExtCalendar; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class JulianCalendar - calculations for the Julian calendar. |
8
|
|
|
* |
9
|
|
|
* @author Greg Roach <[email protected]> |
10
|
|
|
* @copyright (c) 2014-2020 Greg Roach |
11
|
|
|
* @license This program is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
class JulianCalendar implements CalendarInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Determine the number of days in a specified month, allowing for leap years, etc. |
28
|
|
|
* |
29
|
|
|
* @param int $year |
30
|
|
|
* @param int $month |
31
|
|
|
* |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function daysInMonth($year, $month) |
35
|
|
|
{ |
36
|
|
|
if ($year === 0) { |
37
|
|
|
throw new InvalidArgumentException('Year ' . $year . ' is invalid for this calendar'); |
38
|
|
View Code Duplication |
} elseif ($month < 1 || $month > 12) { |
|
|
|
|
39
|
|
|
throw new InvalidArgumentException('Month ' . $month . ' is invalid for this calendar'); |
40
|
|
|
} elseif ($month === 1 || $month === 3 || $month === 5 || $month === 7 || $month === 8 || $month === 10 || $month === 12) { |
41
|
|
|
return 31; |
42
|
|
|
} elseif ($month === 4 || $month === 6 || $month === 9 || $month === 11) { |
43
|
|
|
return 30; |
44
|
|
|
} elseif ($this->isLeapYear($year)) { |
45
|
|
|
return 29; |
46
|
|
|
} else { |
47
|
|
|
return 28; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Determine the number of days in a week. |
53
|
|
|
* |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
|
public function daysInWeek() |
57
|
|
|
{ |
58
|
|
|
return 7; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The escape sequence used to indicate this calendar in GEDCOM files. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function gedcomCalendarEscape() |
67
|
|
|
{ |
68
|
|
|
return '@#DJULIAN@'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Determine whether or not a given year is a leap-year. |
73
|
|
|
* |
74
|
|
|
* @param int $year |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function isLeapYear($year) |
79
|
|
|
{ |
80
|
|
|
if ($year < 0) { |
81
|
|
|
$year++; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $year % 4 == 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* What is the highest Julian day number that can be converted into this calendar. |
89
|
|
|
* |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function jdEnd() |
93
|
|
|
{ |
94
|
|
|
return PHP_INT_MAX; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* What is the lowest Julian day number that can be converted into this calendar. |
99
|
|
|
* |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public function jdStart() |
103
|
|
|
{ |
104
|
|
|
return 1; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Convert a Julian day number into a year/month/day. |
109
|
|
|
* |
110
|
|
|
* @param int $julian_day |
111
|
|
|
* |
112
|
|
|
* @return int[] |
113
|
|
|
*/ |
114
|
|
|
public function jdToYmd($julian_day) |
115
|
|
|
{ |
116
|
|
|
$c = $julian_day + 32082; |
117
|
|
|
$d = (int) ((4 * $c + 3) / 1461); |
118
|
|
|
$e = $c - (int) (1461 * $d / 4); |
119
|
|
|
$m = (int) ((5 * $e + 2) / 153); |
120
|
|
|
|
121
|
|
|
$day = $e - (int) ((153 * $m + 2) / 5) + 1; |
122
|
|
|
$month = $m + 3 - 12 * (int) ($m / 10); |
123
|
|
|
$year = $d - 4800 + (int) ($m / 10); |
124
|
|
|
if ($year < 1) { |
125
|
|
|
// 0 is 1 BCE, -1 is 2 BCE, etc. |
126
|
|
|
$year--; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return array($year, $month, $day); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Determine the number of months in a year (if given), |
134
|
|
|
* or the maximum number of months in any year. |
135
|
|
|
* |
136
|
|
|
* @param int|null $year |
137
|
|
|
* |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
|
|
public function monthsInYear($year = null) |
141
|
|
|
{ |
142
|
|
|
return 12; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Convert a year/month/day to a Julian day number. |
147
|
|
|
* |
148
|
|
|
* @param int $year |
149
|
|
|
* @param int $month |
150
|
|
|
* @param int $day |
151
|
|
|
* |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
|
|
public function ymdToJd($year, $month, $day) |
155
|
|
|
{ |
156
|
|
View Code Duplication |
if ($month < 1 || $month > $this->monthsInYear()) { |
|
|
|
|
157
|
|
|
throw new InvalidArgumentException('Month ' . $month . ' is invalid for this calendar'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($year < 0) { |
161
|
|
|
// 1 BCE is 0, 2 BCE is -1, etc. |
162
|
|
|
++$year; |
163
|
|
|
} |
164
|
|
|
$a = (int) ((14 - $month) / 12); |
165
|
|
|
$year = $year + 4800 - $a; |
166
|
|
|
$month = $month + 12 * $a - 3; |
167
|
|
|
|
168
|
|
|
return $day + (int) ((153 * $month + 2) / 5) + 365 * $year + (int) ($year / 4) - 32083; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the number of days after March 21 that easter falls, for a given year. |
173
|
|
|
* |
174
|
|
|
* Uses the algorithm found in PHP’s ext/calendar/easter.c |
175
|
|
|
* |
176
|
|
|
* @param int $year |
177
|
|
|
* |
178
|
|
|
* @return int |
179
|
|
|
*/ |
180
|
|
|
public function easterDays($year) |
181
|
|
|
{ |
182
|
|
|
// The “golden” number |
183
|
|
|
$golden = 1 + $year % 19; |
184
|
|
|
|
185
|
|
|
// The “dominical” number (finding a Sunday) |
186
|
|
|
$dom = ($year + (int) ($year / 4) + 5) % 7; |
187
|
|
|
if ($dom < 0) { |
188
|
|
|
$dom += 7; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// The uncorrected “Paschal full moon” date |
192
|
|
|
$pfm = (3 - 11 * $golden - 7) % 30; |
193
|
|
|
if ($pfm < 0) { |
194
|
|
|
$pfm += 30; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// The corrected “Paschal full moon” date |
198
|
|
|
if ($pfm === 29 || $pfm === 28 && $golden > 11) { |
199
|
|
|
$pfm--; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$tmp = (4 - $pfm - $dom) % 7; |
203
|
|
|
if ($tmp < 0) { |
204
|
|
|
$tmp += 7; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $pfm + $tmp + 1; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.