4
4
5
5
namespace Pauci \DateTime ;
6
6
7
+ use DateTimeImmutable ;
7
8
use DateTimeZone ;
9
+ use Exception ;
8
10
9
- class DateTime extends \ DateTimeImmutable implements DateTimeInterface
11
+ class DateTime extends DateTimeImmutable implements DateTimeInterface
10
12
{
11
- private static ?DateTimeFactoryInterface $ factory ;
13
+ protected static ?ClockInterface $ clock = null ;
12
14
13
- public static function getFactory (): DateTimeFactoryInterface
15
+ public static function getClock (): ClockInterface
14
16
{
15
- return self ::$ factory ??= new DateTimeFactory ();
17
+ return static ::$ clock ??= new SystemClock ();
16
18
}
17
19
18
- public static function setFactory ( DateTimeFactoryInterface $ factory ): void
20
+ public static function setClock ( ClockInterface $ clock ): void
19
21
{
20
- self ::$ factory = $ factory ;
22
+ static ::$ clock = $ clock ;
21
23
}
22
24
23
- public static function now (): DateTimeInterface
25
+ final public function __construct ( string $ datetime = ' now ' , ? DateTimeZone $ timezone = null )
24
26
{
25
- return self ::getFactory ()->now ();
26
- }
27
+ if ('now ' === $ datetime
28
+ && null !== static ::$ clock
29
+ && !static ::$ clock instanceof SystemClock
30
+ ) {
31
+ $ datetime = static ::getClock ()->now ()->toString ();
32
+ }
27
33
28
- public static function microsecondsNow (): DateTimeInterface
29
- {
30
- return self ::getFactory ()->microsecondsNow ();
34
+ parent ::__construct ($ datetime , $ timezone );
31
35
}
32
36
33
- public static function fromString ( string $ time , DateTimeZone $ timezone = null ): DateTimeInterface
37
+ public static function now ( ): DateTimeInterface
34
38
{
35
- return self :: getFactory ()->fromString ( $ time , $ timezone );
39
+ return static :: getClock ()->now ( );
36
40
}
37
41
38
- public static function fromFormat (string $ format , string $ time , DateTimeZone $ timezone = null ): DateTimeInterface
42
+ /**
43
+ * @throws Exception
44
+ */
45
+ public static function fromString (string $ time , DateTimeZone $ timezone = null ): static
39
46
{
40
- return self :: getFactory ()-> fromFormat ( $ format , $ time , $ timezone );
47
+ return new static ( $ time , $ timezone );
41
48
}
42
49
43
- public static function fromTimestamp (int $ timestamp , DateTimeZone $ timezone = null ): DateTimeInterface
50
+ /**
51
+ * @throws Exception
52
+ */
53
+ public static function fromTimestamp (int $ timestamp , DateTimeZone $ timezone = null ): static
44
54
{
45
- return self ::getFactory ()->fromTimestamp ($ timestamp , $ timezone );
55
+ return (new static ('@ ' . $ timestamp ))
56
+ ->setTimezone ($ timezone ?? new DateTimeZone (date_default_timezone_get ()));
46
57
}
47
58
48
- public static function fromFloatTimestamp (float $ timestamp , DateTimeZone $ timezone = null ): DateTimeInterface
59
+ public static function fromFloatTimestamp (float $ timestamp , DateTimeZone $ timezone = null ): static
49
60
{
50
- return self :: getFactory ()-> fromFloatTimestamp ($ timestamp, $ timezone );
51
- }
61
+ $ integerPart = ( int ) floor ($ timestamp );
62
+ $ fractionalPart = substr ( sprintf ( ' %.6f ' , $ timestamp - $ integerPart ), 2 );
52
63
53
- public static function fromDateTime ( \ DateTimeInterface $ dateTime): DateTimeInterface
54
- {
55
- return self :: getFactory ()-> fromDateTime ( $ dateTime ) ;
64
+ $ dateTime = new static ( date ( ' Y-m-d H:i:s. ' . $ fractionalPart , $ integerPart ));
65
+
66
+ return $ timezone ? $ dateTime -> setTimezone ( $ timezone ) : $ dateTime ;
56
67
}
57
68
58
69
#[\ReturnTypeWillChange]
59
70
public static function createFromFormat (
60
71
string $ format ,
61
72
string $ datetime ,
62
73
DateTimeZone $ timezone = null
63
- ): DateTimeInterface {
64
- return self :: fromFormat ($ format , $ datetime , $ timezone );
74
+ ): static | false {
75
+ return parent :: createFromFormat ($ format , $ datetime , $ timezone );
65
76
}
66
77
67
78
#[\ReturnTypeWillChange]
68
- public static function createFromMutable (\DateTime $ object ): DateTimeInterface
79
+ public static function createFromMutable (\DateTime $ object ): static
69
80
{
70
- return self :: fromDateTime ($ object );
81
+ return parent :: createFromMutable ($ object );
71
82
}
72
83
73
84
/**
74
- * @throws \ Exception
85
+ * @throws Exception
75
86
*/
76
87
public function diff (\DateTimeInterface $ targetObject , bool $ absolute = false ): DateInterval
77
88
{
@@ -90,17 +101,9 @@ public function sub(\DateInterval $interval): static
90
101
return parent ::sub ($ interval );
91
102
}
92
103
93
- public function modify (string $ modifier ): static
104
+ public function modify (string $ modifier ): static | false
94
105
{
95
- try {
96
- $ dateTime = parent ::modify ($ modifier );
97
- } catch (\Throwable $ e ) {
98
- $ message = strtr ($ e ->getMessage (), [\DateTimeImmutable::class => static ::class]);
99
-
100
- throw new Exception \FailedToModifyException ($ message , previous: $ e );
101
- }
102
-
103
- return $ dateTime ;
106
+ return parent ::modify ($ modifier );
104
107
}
105
108
106
109
public function setDate (int $ year , int $ month , int $ day ): static
@@ -133,7 +136,7 @@ public function equals(DateTimeInterface $dateTime): bool
133
136
return $ this == $ dateTime ;
134
137
}
135
138
136
- public function inDefaultTimezone (): DateTimeInterface
139
+ public function inDefaultTimezone (): static
137
140
{
138
141
return $ this ->setTimezone (new DateTimeZone (date_default_timezone_get ()));
139
142
}
0 commit comments