|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Andegna; |
|
4
|
|
|
|
|
5
|
|
|
use Andegna\Converter\Converter; |
|
6
|
|
|
use Andegna\Converter\ToJdnConverter; |
|
7
|
|
|
use DateTime as GregorianDateTime; |
|
8
|
|
|
use DateTimeZone; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class DateTimeFactory. |
|
12
|
|
|
* |
|
13
|
|
|
* A factory to create @see \Andegna\DateTime |
|
14
|
|
|
*/ |
|
15
|
|
|
class DateTimeFactory |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Create a @see \Andegna\DateTime representing now. |
|
19
|
|
|
* |
|
20
|
|
|
* @param DateTimeZone|null $dateTimeZone the timezone |
|
21
|
|
|
* |
|
22
|
|
|
* @return DateTime the datetime u wanted |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public static function now(DateTimeZone $dateTimeZone = null) |
|
25
|
|
|
{ |
|
26
|
1 |
|
$dateTimeZone = self::checkForDateTimeZone($dateTimeZone); |
|
27
|
|
|
|
|
28
|
1 |
|
return new DateTime( |
|
29
|
1 |
|
new GregorianDateTime('now', $dateTimeZone) |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param DateTimeZone|null $dateTimeZone the timezone |
|
35
|
|
|
* |
|
36
|
|
|
* @return DateTimeZone a valid timezone |
|
37
|
|
|
*/ |
|
38
|
49 |
|
protected static function checkForDateTimeZone(DateTimeZone $dateTimeZone = null) |
|
39
|
|
|
{ |
|
40
|
49 |
|
if (is_null($dateTimeZone)) { |
|
41
|
|
|
// get the default timezone from z system |
|
42
|
49 |
|
return new DateTimeZone(date_default_timezone_get()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
49 |
|
return $dateTimeZone; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Create a @see \Andegna\DateTime of year month day ... |
|
50
|
|
|
* |
|
51
|
|
|
* @param int $year ethiopian year |
|
52
|
|
|
* @param int $month ethiopian month |
|
53
|
|
|
* @param int $day ethiopian day |
|
54
|
|
|
* @param int $hour hour |
|
55
|
|
|
* @param int $minute minute |
|
56
|
|
|
* @param int $second second |
|
57
|
|
|
* @param DateTimeZone|null $dateTimeZone the timezone |
|
58
|
|
|
* |
|
59
|
|
|
* @return DateTime the datetime u wanted |
|
60
|
|
|
*/ |
|
61
|
49 |
|
public static function of($year, $month, $day, |
|
62
|
|
|
$hour = 0, $minute = 0, $second = 0, |
|
63
|
|
|
DateTimeZone $dateTimeZone = null |
|
64
|
|
|
) { |
|
65
|
|
|
|
|
66
|
|
|
// Convert to JDN |
|
67
|
49 |
|
$jdn = (new ToJdnConverter($day, $month, $year))->getJdn(); |
|
68
|
|
|
|
|
69
|
|
|
// The gregorian date in "month/day/year" format |
|
70
|
49 |
|
$gregorian = jdtogregorian($jdn); |
|
71
|
|
|
|
|
72
|
49 |
|
$dateTimeZone = self::checkForDateTimeZone($dateTimeZone); |
|
73
|
|
|
|
|
74
|
49 |
|
$base = new GregorianDateTime( |
|
75
|
49 |
|
"$gregorian $hour:$minute:$second", |
|
76
|
49 |
|
$dateTimeZone |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
49 |
|
return new DateTime($base); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param int $timestamp timestamp like @see time() |
|
84
|
|
|
* @param DateTimeZone|null $dateTimeZone the timezone |
|
85
|
|
|
* |
|
86
|
|
|
* @return DateTime the datetime u wanted |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public static function fromTimestamp($timestamp, DateTimeZone $dateTimeZone = null) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$base = new GregorianDateTime( |
|
91
|
1 |
|
date('Y-m-d H:i:s', $timestamp), |
|
92
|
1 |
|
self::checkForDateTimeZone($dateTimeZone) |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
1 |
|
return new DateTime($base); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Just for convenience. |
|
100
|
|
|
* |
|
101
|
|
|
* @param GregorianDateTime $gregorian |
|
102
|
|
|
* |
|
103
|
|
|
* @return DateTime the datetime u wanted |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public static function fromDateTime(GregorianDateTime $gregorian) |
|
106
|
|
|
{ |
|
107
|
1 |
|
return new DateTime($gregorian); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Just for convenience. |
|
112
|
|
|
* |
|
113
|
|
|
* @param Converter $con |
|
114
|
|
|
* @param DateTimeZone|null $dateTimeZone |
|
115
|
|
|
* |
|
116
|
|
|
* @return DateTime the datetime u wanted |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public static function fromConverter(Converter $con, DateTimeZone $dateTimeZone = null) |
|
119
|
|
|
{ |
|
120
|
1 |
|
return static::of( |
|
121
|
1 |
|
$con->getYear(), $con->getMonth(), $con->getDay(), |
|
122
|
1 |
|
0, 0, 0, $dateTimeZone |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|