-
Notifications
You must be signed in to change notification settings - Fork 0
/
Romanian.php
216 lines (204 loc) · 7.62 KB
/
Romanian.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Daniel Popiniuc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
namespace danielgp\bank_holidays;
/**
* Return a list of all Romanian Holidays between 2001 and 2020
*
* @author Daniel Popiniuc
*/
trait Romanian
{
/**
*
* @param int $year
* @return type
*/
private function getEasterDatetime($year)
{
$base = new \DateTime("$year-03-21");
$days = easter_days($year);
return $base->add(new \DateInterval("P{$days}D"));
}
/**
* returns an array with non-standard holidays from a JSON file
*
* @param string $fileBaseName
* @return mixed
*/
protected function readTypeFromJsonFileUniversal($filePath, $fileBaseName)
{
$fName = $filePath . DIRECTORY_SEPARATOR . $fileBaseName . '.min.json';
$fJson = fopen($fName, 'r');
$jSonContent = fread($fJson, filesize($fName));
fclose($fJson);
return json_decode($jSonContent, true);
}
/**
* List of legal holidays
*
* @param \DateTime $lngDate
* @param boolean $inclCatholicEaster
* @return array
*/
protected function setHolidays(\DateTime $lngDate, $inclCatholicEaster = false, $inclWorkingHolidays = false)
{
$givenYear = $lngDate->format('Y');
$daying = array_merge($this->setHolidaysOrthodoxEaster($lngDate), $this->setHolidaysFixed($lngDate));
if ($inclWorkingHolidays) {
$daying = array_merge($daying, $this->setHolidaysFixedButWorking($lngDate));
}
if ($inclCatholicEaster) { // Catholic easter is already known by PHP
$firstEasterDate = strtotime($this->getEasterDatetime($givenYear)->format('Y-m-d'));
$secondEasterDate = strtotime('+1 day', $firstEasterDate);
$daying = array_merge($daying, [
$firstEasterDate,
$secondEasterDate,
]);
}
sort($daying);
return array_unique($daying); // remove duplicate for when catholic and orthodox easter match
}
/**
* List of all Romanian fixed holidays
* (where fixed means every single year occur on same day of the month)
*
* @param \DateTime $lngDate
* @return array
*/
private function setHolidaysFixed(\DateTime $lngDate)
{
$daying = [
mktime(0, 0, 0, 1, 1, $lngDate->format('Y')), // Happy New Year
mktime(0, 0, 0, 1, 2, $lngDate->format('Y')), // recovering from New Year party
mktime(0, 0, 0, 5, 1, $lngDate->format('Y')), // May 1st
mktime(0, 0, 0, 12, 1, $lngDate->format('Y')), // Romanian National Day
mktime(0, 0, 0, 12, 25, $lngDate->format('Y')), // Christmas Day
mktime(0, 0, 0, 12, 26, $lngDate->format('Y')), // Christmas 2nd Day
];
if ($lngDate->format('Y') >= 2009) {
$daying[] = mktime(0, 0, 0, 8, 15, $lngDate->format('Y')); // St. Marry
if ($lngDate->format('Y') >= 2012) {
$daying[] = mktime(0, 0, 0, 11, 30, $lngDate->format('Y')); // St. Andrew
if ($lngDate->format('Y') >= 2017) {
$daying[] = mktime(0, 0, 0, 1, 24, $lngDate->format('Y')); // Union Day
$daying[] = mktime(0, 0, 0, 6, 1, $lngDate->format('Y')); // Child's Day
}
}
}
sort($daying);
return $daying;
}
/**
* List of Romanian fixed holidays that are still working (weird ones)
* (where fixed means every single year occur on same day of the month)
*
* @param \DateTime $lngDate
* @return array
*/
private function setHolidaysFixedButWorking(\DateTime $lngDate)
{
$daying = [];
$givenYear = $lngDate->format('Y');
if ($givenYear >= 2015) {
$daying[] = mktime(0, 0, 0, 1, 24, $givenYear); // Unirea Principatelor Romane
}
if ($givenYear >= 2016) {
$daying[] = mktime(0, 0, 0, 2, 19, $givenYear); // Constantin Brancusi birthday
}
return $daying;
}
/**
* List of all Orthodox holidays and Pentecost
*
* @param \DateTime $lngDate
* @return array
*/
private function setHolidaysOrthodoxEaster(\DateTime $lngDate)
{
$givenYear = $lngDate->format('Y');
$daying = [];
$configPath = __DIR__ . DIRECTORY_SEPARATOR . 'json';
$statmentsArray = $this->readTypeFromJsonFileUniversal($configPath, 'RomanianBankHolidays');
if (array_key_exists($givenYear, $statmentsArray)) {
foreach ($statmentsArray[$givenYear] as $value) {
$daying[] = strtotime($value);
}
}
return $daying;
}
/**
* returns bank holidays in a given month
*
* @param \DateTime $lngDate
* @param boolean $inclCatholicEaster
* @return int
*/
protected function setHolidaysInMonth(\DateTime $lngDate, $inclCatholicEaster = false)
{
$holidaysInGivenYear = $this->setHolidays($lngDate, $inclCatholicEaster);
$thisMonthDayArray = $this->setMonthAllDaysIntoArray($lngDate);
$holidays = 0;
foreach ($thisMonthDayArray as $value) {
if (in_array($value, $holidaysInGivenYear)) {
$holidays += 1;
}
}
return $holidays;
}
/**
* return an array with all days within a month from a given date
*
* @param \DateTime $lngDate
* @return array
*/
protected function setMonthAllDaysIntoArray(\DateTime $lngDate)
{
$firstDayGivenMonth = strtotime($lngDate->modify('first day of this month')->format('Y-m-d'));
$lastDayInGivenMonth = strtotime($lngDate->modify('last day of this month')->format('Y-m-d'));
$secondsInOneDay = 24 * 60 * 60;
return range($firstDayGivenMonth, $lastDayInGivenMonth, $secondsInOneDay);
}
/**
* returns working days in a given month
*
* @param \DateTime $lngDate
* @param boolean $inclCatholicEaster
* @return int
*/
protected function setWorkingDaysInMonth(\DateTime $lngDate, $inclCatholicEaster = false)
{
$holidaysInGivenYear = $this->setHolidays($lngDate, $inclCatholicEaster);
$thisMonthDayArray = $this->setMonthAllDaysIntoArray($lngDate);
$workingDays = 0;
foreach ($thisMonthDayArray as $value) {
if (!in_array(strftime('%w', $value), [0, 6]) && !in_array($value, $holidaysInGivenYear)) {
$workingDays += 1;
}
}
return $workingDays;
}
}