-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calendar.php
170 lines (139 loc) · 3.75 KB
/
Calendar.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
<?php declare(strict_types=1);
namespace h4kuna\DataType\Date;
use DateTimeImmutable;
use DateTimeInterface;
use h4kuna\DataType;
use h4kuna\DataType\Exceptions\InvalidArgumentsException;
use h4kuna\Memoize\MemoryStorageStatic;
use Nette\StaticClass;
use Nette\Utils\Strings;
final class Calendar
{
use StaticClass;
use MemoryStorageStatic;
public static string $namesFile = __DIR__ . '/names.php';
private function __construct()
{
}
/**
* @param null|int<0, 7>|string|DateTimeInterface $day
*/
public static function nameOfDay(null|int|string|DateTimeInterface $day = null): string
{
if ($day === null) {
$day = (int) date('w');
} elseif ($day instanceof DateTimeInterface) {
$day = (int) $day->format('w');
} elseif (is_numeric($day)) {
$day = (int) $day;
} else {
throw new InvalidArgumentsException('Input is allowed DateTimeInterface or numeric.');
}
if ($day === 0) {
$day = 7;
}
return self::getDays()[$day] ?? throw new InvalidArgumentsException('Invalid number for day, interval is 0-6, 0 = Sunday');
}
/**
* @return array<string>
*/
public static function getDays(): array
{
return self::memoize(__METHOD__, static function () {
return [
1 => 'Pondělí',
'Úterý',
'Středa',
'Čtvrtek',
'Pátek',
'Sobota',
'Neděle',
];
});
}
/**
* @param null|int<1, 12>|string|DateTimeInterface $month
*/
public static function nameOfMonth(null|int|string|DateTimeInterface $month = null): string
{
if ($month === null) {
$month = (int) date('n');
} elseif ($month instanceof DateTimeInterface) {
$month = (int) $month->format('n');
} elseif (is_numeric($month)) {
$month = (int) $month;
} else {
throw new InvalidArgumentsException('Input is allowed DateTimeInterface or numeric');
}
return self::getMonths()[$month] ?? throw new InvalidArgumentsException('Invalid number for day, interval is 1-12.');
}
/**
* @return array<string>
*/
public static function getMonths(): array
{
return self::memoize(__METHOD__, static function () {
return [
1 => 'Leden',
'Únor',
'Březen',
'Duben',
'Květen',
'Červen',
'Červenec',
'Srpen',
'Září',
'Říjen',
'Listopad',
'Prosinec',
];
});
}
/**
* CZECH FORMAT DD.MM.YYYY[ HH:mm:SS]
*/
public static function czech2DateTime(string $date): DateTimeImmutable
{
$find = Strings::match(trim($date), '/^(?P<d>[0-3]?\d)\.(?P<m>[0-1]?\d)\.(?P<y>\d{4})(?: +(?P<h>[0-6]?\d):(?P<i>[0-6]?\d)(?::(?P<s>[0-6]?\d))?)?$/');
if ($find === null) {
throw new InvalidArgumentsException('Bad czech date format. ' . $date);
}
$find += ['h' => 0, 'i' => 0, 's' => 0];
return new DateTimeImmutable(sprintf('%s-%s-%s %s:%s:%s', $find['y'], $find['m'], $find['d'], $find['h'], $find['i'], $find['s']));
}
public static function februaryOfDay(int|DateTimeInterface $year): int
{
if ($year instanceof DateTimeInterface) {
$year = (int) $year->format('Y');
}
return checkdate(2, 29, $year) ? 29 : 28;
}
/**
* @param ?int<1970, 2037> $year
* @see Easter::monday()
* @deprecated see
*/
public static function easter(?int $year = null): DateTimeImmutable
{
return Easter::monday($year);
}
/**
* Return czech name on name-day.
*/
public static function nameByDate(?DateTimeInterface $date = null): string
{
if ($date === null) {
$date = new DateTimeImmutable();
}
$day = (int) $date->format('j');
$month = (int) $date->format('n');
return self::names()[$month][$day] ?? throw new InvalidArgumentsException(sprintf('Bad month "%s" or day "%s".', $month, $day));
}
/**
* @return array<int, array<int, string>>
*/
public static function names(): array
{
return self::memoize(__METHOD__, static fn () => require_once self::$namesFile);
}
}