Completed
Push — develop ( b6d6fd...b7f617 )
by Greg
01:53
created

PersianCalendar::jdEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Fisharebest\ExtCalendar;
3
4
use InvalidArgumentException;
5
6
/**
7
 * Class PersianCalendar - calculations for the Persian (Jalali) calendar.
8
 *
9
 * Algorithms for Julian days based on https://www.fourmilab.ch/documents/calendar/
10
 *
11
 * @author    Greg Roach <[email protected]>
12
 * @copyright (c) 2014-2017 Greg Roach
13
 * @license   This program is free software: you can redistribute it and/or modify
14
 *            it under the terms of the GNU General Public License as published by
15
 *            the Free Software Foundation, either version 3 of the License, or
16
 *            (at your option) any later version.
17
 *
18
 *            This program is distributed in the hope that it will be useful,
19
 *            but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 *            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 *            GNU General Public License for more details.
22
 *
23
 *            You should have received a copy of the GNU General Public License
24
 *            along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 */
26
class PersianCalendar implements CalendarInterface {
27
	/**
28
	 * In each 128 year cycle, the following years are leap years.
29
	 *
30
	 * @var integer[]
31
	 */
32
	private static $LEAP_YEAR_CYCLE = array(
33
		0, 5, 9, 13, 17, 21, 25, 29, 34, 38, 42, 46, 50, 54, 58, 62, 67, 71, 75, 79, 83, 87, 91, 95, 100, 104, 108, 112, 116, 120, 124
34
	);
35
36
	public function daysInMonth($year, $month) {
37
		if ($month <= 6) {
38
			return 31;
39
		} elseif ($month <= 11 || $this->isLeapYear($year)) {
40
			return 30;
41
		} else {
42
			return 29;
43
		}
44
	}
45
46
	public function daysInWeek() {
47
		return 7;
48
	}
49
50
	public function gedcomCalendarEscape() {
51
		return '@#DJALALI@';
52
	}
53
54
	public function isLeapYear($year) {
55
		return in_array((($year + 2346) % 2820) % 128, self::$LEAP_YEAR_CYCLE);
56
	}
57
58
	public function jdEnd() {
59
		return PHP_INT_MAX;
60
	}
61
62
	public function jdStart() {
63
		return 1948321; // 1 Farvardīn 0001 AP, 19 MAR 0622 AD
64
	}
65
66
	public function jdToYmd($julian_day) {
67
		$depoch = $julian_day - 2121446; // 1 Farvardīn 475
68
		$cycle  = (int) floor($depoch / 1029983);
69
		$cyear  = $this->mod($depoch, 1029983);
70
		if ($cyear == 1029982) {
71
			$ycycle = 2820;
72
		} else {
73
			$aux1   = (int) ($cyear / 366);
74
			$aux2   = $cyear % 366;
75
			$ycycle = (int) (((2134 * $aux1) + (2816 * $aux2) + 2815) / 1028522) + $aux1 + 1;
76
		}
77
		$year = $ycycle + (2820 * $cycle) + 474;
78
79
		// If we allowed negative years, we would deal with them here.
80
		$yday  = $julian_day - $this->ymdToJd($year, 1, 1) + 1;
81
		$month = ($yday <= 186) ? ceil($yday / 31) : ceil(($yday - 6) / 30);
82
		$day   = $julian_day - $this->ymdToJd($year, $month, 1) + 1;
83
84
		return array((int) $year, (int) $month, (int) $day);
85
	}
86
87
	public function monthsInYear() {
88
		return 12;
89
	}
90
91
	public function ymdToJd($year, $month, $day) {
92 View Code Duplication
		if ($month < 1 || $month > $this->monthsInYear()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
93
			throw new InvalidArgumentException('Month ' . $month . ' is invalid for this calendar');
94
		}
95
96
		$epbase = $year - (($year >= 0) ? 474 : 473);
97
		$epyear = 474 + $this->mod($epbase, 2820);
98
99
		return
100
			$day +
101
			(($month <= 7) ? (($month - 1) * 31) : ((($month - 1) * 30) + 6)) +
102
			(int) ((($epyear * 682) - 110) / 2816) +
103
			($epyear - 1) * 365 +
104
			(int) (floor($epbase / 2820)) * 1029983 +
105
			$this->jdStart() - 1;
106
	}
107
108
	/**
109
	 * The PHP modulus function returns a negative modulus for a negative dividend.
110
	 * This algorithm requires a "traditional" modulus function where the modulus is
111
	 * always positive.
112
	 *
113
	 * @param number $dividend
114
	 * @param number $divisor
115
	 * @return number
116
	 */
117 View Code Duplication
	public function mod($dividend, $divisor) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
118
		if ($divisor === 0) return 0;
119
120
		$modulus = $dividend % $divisor;
121
		if($modulus < 0) {
122
			$modulus += $divisor;
123
		}
124
125
		return $modulus;
126
	}
127
}
128