1
|
|
|
<?php |
2
|
|
|
namespace Fisharebest\ExtCalendar; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class FrenchCalendar - calculations for the French Republican 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 FrenchCalendar 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 > 13) { |
|
|
|
|
39
|
|
|
throw new InvalidArgumentException('Month ' . $month . ' is invalid for this calendar'); |
40
|
|
|
} elseif ($month !== 13) { |
41
|
|
|
return 30; |
42
|
|
|
} elseif ($this->isLeapYear($year)) { |
43
|
|
|
return 6; |
44
|
|
|
} else { |
45
|
|
|
return 5; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Determine the number of days in a week. |
51
|
|
|
* |
52
|
|
|
* @return int |
53
|
|
|
*/ |
54
|
|
|
public function daysInWeek() |
55
|
|
|
{ |
56
|
|
|
return 10; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The escape sequence used to indicate this calendar in GEDCOM files. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function gedcomCalendarEscape() |
65
|
|
|
{ |
66
|
|
|
return '@#DFRENCH R@'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Determine whether or not a given year is a leap-year. |
71
|
|
|
* |
72
|
|
|
* Leap years were based on astronomical observations. Only years 3, 7 and 11 |
73
|
|
|
* were ever observed. Moves to a gregorian-like (fixed) system were proposed |
74
|
|
|
* but never implemented. |
75
|
|
|
* |
76
|
|
|
* @param int $year |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isLeapYear($year) |
81
|
|
|
{ |
82
|
|
|
return $year % 4 == 3; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* What is the highest Julian day number that can be converted into this calendar. |
87
|
|
|
* |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public function jdEnd() |
91
|
|
|
{ |
92
|
|
|
return 2380687; // 31 DEC 1805 = 10 NIVO 0014 |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* What is the lowest Julian day number that can be converted into this calendar. |
97
|
|
|
* |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
public function jdStart() |
101
|
|
|
{ |
102
|
|
|
return 2375840; // 22 SEP 1792 = 01 VEND 0001 |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Convert a Julian day number into a year/month/day. |
107
|
|
|
* |
108
|
|
|
* @param int $julian_day |
109
|
|
|
* |
110
|
|
|
* @return int[] |
111
|
|
|
*/ |
112
|
|
|
public function jdToYmd($julian_day) |
113
|
|
|
{ |
114
|
|
|
$year = (int) (($julian_day - 2375109) * 4 / 1461) - 1; |
115
|
|
|
$month = (int) (($julian_day - 2375475 - $year * 365 - (int) ($year / 4)) / 30) + 1; |
116
|
|
|
$day = $julian_day - 2375444 - $month * 30 - $year * 365 - (int) ($year / 4); |
117
|
|
|
|
118
|
|
|
return array($year, $month, $day); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Determine the number of months in a year (if given), |
123
|
|
|
* or the maximum number of months in any year. |
124
|
|
|
* |
125
|
|
|
* @param int|null $year |
126
|
|
|
* |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
|
|
public function monthsInYear($year = null) |
130
|
|
|
{ |
131
|
|
|
return 13; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Convert a year/month/day to a Julian day number. |
136
|
|
|
* |
137
|
|
|
* @param int $year |
138
|
|
|
* @param int $month |
139
|
|
|
* @param int $day |
140
|
|
|
* |
141
|
|
|
* @return int |
142
|
|
|
*/ |
143
|
|
|
public function ymdToJd($year, $month, $day) |
144
|
|
|
{ |
145
|
|
View Code Duplication |
if ($month < 1 || $month > $this->monthsInYear()) { |
|
|
|
|
146
|
|
|
throw new InvalidArgumentException('Month ' . $month . ' is invalid for this calendar'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return 2375444 + $day + $month * 30 + $year * 365 + (int) ($year / 4); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.