-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLunarDateTime.php
216 lines (194 loc) · 5.63 KB
/
LunarDateTime.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
namespace LucNham\LunarCalendar;
use DateMalformedStringException;
use DateTimeZone;
use Exception;
use LucNham\LunarCalendar\Contracts\LunarDateTime as ContractsLunarDateTime;
use LucNham\LunarCalendar\Contracts\LunarDateTimeFormattable;
use LucNham\LunarCalendar\Contracts\LunarGuaranteedAccessible;
use LucNham\LunarCalendar\Contracts\ZoneAccessible;
use LucNham\LunarCalendar\Converters\DateTimeStringToLunarGuaranteed;
use LucNham\LunarCalendar\Converters\LunarDateTimeToDateTimeString;
use LucNham\LunarCalendar\Converters\LunarStringToLunarGuaranteed;
use LucNham\LunarCalendar\Formatters\LunarDateTimeDefaultFormatter;
use LucNham\LunarCalendar\Terms\LunarDateTimeGuaranteed;
/**
* Representation of Lunar calendar date and time
*/
class LunarDateTime implements ContractsLunarDateTime
{
/**
* Timezone offset inseconds
*
* @var integer
*/
private int $offset;
/**
* Lunar date time interval guaranteed
*
* @var LunarDateTimeGuaranteed
*/
private LunarDateTimeGuaranteed $interval;
/**
* Formatter class
*
* @var LunarDateTimeFormattable
*/
protected LunarDateTimeFormattable $formatter;
/**
* Create new Lunar Date Time
*
* @param string $datetime Lunar date time string to parse, default 'now' is current
* @param DateTimeZone|null $timezone DateTimeZone object if needed, default null.
*/
public function __construct(
private string $datetime = 'now',
private ?DateTimeZone $timezone = null,
) {
$this->initComponents();
$this->initFormatter();
}
/**
* Access properties more conveniently
*
* @param string $name
* @return mixed
*/
public function __get(string $name): mixed
{
$value = match ($name) {
'day' => $this->interval->d,
'month' => $this->interval->m,
'year' => $this->interval->y,
'hour' => $this->interval->h,
'minute' => $this->interval->i,
'second' => $this->interval->s,
'leap' => $this->interval->l,
'isLeapMonth' => $this->interval->leap,
'jdn' => $this->interval->j,
'timestamp' => $this->getTimestamp(),
default => null
};
if ($value === null) {
throw new Exception("Property dose not exists.");
}
return $value;
}
/**
* Init necessary Lunar components
*
* @return void
*/
protected function initComponents(): void
{
$datetime = $this->datetime;
if (
str_contains($this->datetime, 'G:') ||
$datetime === '' ||
$datetime === 'now'
) {
$class = DateTimeStringToLunarGuaranteed::class;
$datetime = str_replace('G:', '', $datetime);
} else {
$class = LunarStringToLunarGuaranteed::class;
}
try {
/** @var ZoneAccessible&LunarGuaranteedAccessible */
$component = new $class(
datetime: $datetime,
timezone: $this->timezone
);
$this->interval = $component->getGuaranteedLunarDateTime();
$this->timezone = $component->getTimezone();
$this->offset = $component->getOffset();
} catch (\Throwable $th) {
throw new DateMalformedStringException("Invalid date time format");
}
}
/**
* Initialize default formatter
*
* @return LunarDateTimeFormattable
*/
protected function initFormatter(): void
{
$this->formatter = new LunarDateTimeDefaultFormatter(
lunar: $this->getGuaranteedLunarDateTime(),
timezone: $this->getTimezone(),
offset: $this->getOffset()
);
}
/**
* @inheritDoc
*/
public function getGuaranteedLunarDateTime(): LunarDateTimeGuaranteed
{
return $this->interval;
}
/**
* @inheritDoc
*/
public function format(string $formatter): string
{
return $this->formatter->format($formatter);
}
/**
* @inheritDoc
*/
public function getTimestamp(): int
{
return floor(($this->interval->j - 2440587.5) * 86400);
}
/**
* @inheritDoc
*/
public function getTimezone(): DateTimeZone
{
return $this->timezone;
}
/**
* @inheritDoc
*/
public function getOffset(): int
{
return $this->offset;
}
/**
* Returns corresponding date time string with format: 'Y-m-d H:i:s P'
*
* @return string
*/
public function toDateTimeString(): string
{
return (new LunarDateTimeToDateTimeString(
lunar: $this->getGuaranteedLunarDateTime(),
offset: $this->getOffset()
))->getOutput();
}
/**
* Create new Lunar date time instance from Gregorian date time string
*
* @param string $datetime
* @param DateTimeZone|null $timezone
* @return self
*/
public static function fromGregorian(
string $datetime,
?DateTimeZone $timezone = null
): self {
if ($datetime !== '' && $datetime !== 'now') {
$datetime = 'G:' . $datetime;
}
return new self($datetime, $timezone);
}
/**
* Create new Lunar date time instance with current time
*
* @param DateTimeZone|null $timezone
* @return self
*/
public static function now(?DateTimeZone $timezone = null): self
{
return self::fromGregorian('now', $timezone);
}
}