|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Andegna\Ethiopian; |
|
4
|
|
|
|
|
5
|
|
|
use Andegna\Converter\ToJdnConverter; |
|
6
|
|
|
use Andegna\Validator\LeapYearValidator; |
|
7
|
|
|
use DateTime as BaseDateTime; |
|
8
|
|
|
use DateTimeZone; |
|
9
|
|
|
|
|
10
|
|
|
class DateTimeFactory |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Create a date time representing now. |
|
14
|
|
|
* |
|
15
|
|
|
* @param DateTimeZone|null $dateTimeZone |
|
16
|
|
|
* |
|
17
|
|
|
* @return DateTime |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function now(DateTimeZone $dateTimeZone = null) |
|
20
|
|
|
{ |
|
21
|
|
|
$dateTimeZone = self::defaultDateTimeZone($dateTimeZone); |
|
22
|
|
|
|
|
23
|
|
|
return new DateTime( |
|
24
|
|
|
new BaseDateTime('now', $dateTimeZone) |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $dateTimeZone |
|
30
|
|
|
* |
|
31
|
|
|
* @return DateTimeZone |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static function defaultDateTimeZone($dateTimeZone) |
|
34
|
|
|
{ |
|
35
|
|
|
if (is_null($dateTimeZone)) { |
|
36
|
|
|
return new DateTimeZone(date_default_timezone_get()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $dateTimeZone; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the last day of year and month. |
|
44
|
|
|
* |
|
45
|
|
|
* @param $year |
|
46
|
|
|
* @param int $month |
|
47
|
|
|
* |
|
48
|
|
|
* @return DateTime |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function lastDayOf($year, $month = 13) |
|
51
|
|
|
{ |
|
52
|
|
|
$leapYear = (new LeapYearValidator($year))->isValid(); |
|
53
|
|
|
$lastDay = 30; |
|
54
|
|
|
|
|
55
|
|
|
if ($month == 13) { |
|
56
|
|
|
$lastDay = $leapYear ? 6 : 5; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return static::of($year, $month, $lastDay); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Create a DateTime of year month day ... |
|
64
|
|
|
* |
|
65
|
|
|
* @param int $year |
|
66
|
|
|
* @param int $month |
|
67
|
|
|
* @param int $day |
|
68
|
|
|
* @param int $hour |
|
69
|
|
|
* @param int $minute |
|
70
|
|
|
* @param int $second |
|
71
|
|
|
* @param DateTimeZone|null $dateTimeZone |
|
72
|
|
|
* |
|
73
|
|
|
* @return DateTime |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function of( |
|
76
|
|
|
$year, |
|
77
|
|
|
$month, |
|
78
|
|
|
$day, |
|
79
|
|
|
$hour = 0, |
|
80
|
|
|
$minute = 0, |
|
81
|
|
|
$second = 0, |
|
82
|
|
|
DateTimeZone $dateTimeZone = null |
|
83
|
|
|
) { |
|
84
|
|
|
|
|
85
|
|
|
// Convert to Julian Date Number |
|
86
|
|
|
$jdn = (new ToJdnConverter($day, $month, $year))->getJdn(); |
|
87
|
|
|
|
|
88
|
|
|
// The gregorian date in "month/day/year" format |
|
89
|
|
|
$gregorian = \jdtogregorian($jdn); |
|
90
|
|
|
|
|
91
|
|
|
$dateTimeZone = self::defaultDateTimeZone($dateTimeZone); |
|
92
|
|
|
|
|
93
|
|
|
$base = new BaseDateTime("$gregorian $hour:$minute:$second", $dateTimeZone); |
|
94
|
|
|
|
|
95
|
|
|
return new DateTime($base); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|