1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\Chrono; |
4
|
|
|
|
5
|
|
|
use Palmtree\Chrono\Option\DatePeriods; |
6
|
|
|
use Palmtree\Chrono\Option\TimePeriods; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @method self add(int $value, string $period) |
10
|
|
|
* @method self subtract(int $value, string $period) |
11
|
|
|
* @method self setDate(int $year, int $month, int $day) |
12
|
|
|
* @method self setDay(int $day) |
13
|
|
|
* @method self setMonth(int $month) |
14
|
|
|
* @method self setYear(int $year) |
15
|
|
|
* @method static self fromNative(\DateTime $dateTime) |
16
|
|
|
* @method static null|self min(...$dates) |
17
|
|
|
* @method static null|self max(...$dates) |
18
|
|
|
*/ |
19
|
|
|
class DateTime extends Date |
20
|
|
|
{ |
21
|
12 |
|
public function __construct(string $time = 'now', $timezone = null) |
22
|
|
|
{ |
23
|
12 |
|
$this->dateTime = $this->createInternalDateTime($time, $timezone); |
24
|
12 |
|
} |
25
|
|
|
|
26
|
|
|
public function setTime(int $hour, int $minute, int $second = 0, int $microseconds = 0): self |
27
|
|
|
{ |
28
|
|
|
$this->dateTime->setTime($hour, $minute, $second, $microseconds); |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setHour(int $hour): self |
34
|
|
|
{ |
35
|
|
|
return $this->setTime($hour, $this->dateTime->format('i'), $this->dateTime->format('s'), $this->dateTime->format('u')); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setMinute(int $minute): self |
39
|
|
|
{ |
40
|
|
|
return $this->setTime($this->dateTime->format('H'), $minute, $this->dateTime->format('s'), $this->dateTime->format('u')); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setSecond(int $second): self |
44
|
|
|
{ |
45
|
|
|
return $this->setTime($this->dateTime->format('H'), $this->dateTime->format('i'), $second, $this->dateTime->format('u')); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setMicroseconds(int $microseconds): self |
49
|
|
|
{ |
50
|
|
|
return $this->setTime($this->dateTime->format('H'), $this->dateTime->format('i'), $this->dateTime->format('s'), $microseconds); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
protected function getFormatFromTimePrecision(?string $precision): string |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
1 |
|
$format = TimePeriods::getDateFormat($precision ?? TimePeriods::SECOND); |
57
|
1 |
|
} catch (\InvalidArgumentException $e) { |
58
|
1 |
|
$format = DatePeriods::getDateFormat($precision ?? DatePeriods::DAY); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return $format; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|