@@ -29,6 +29,88 @@ public function testCreateFromFormatReturnsInstance()
2929 $ this ->assertTrue ($ d instanceof Chronos);
3030 }
3131
32+ public function testCreateFromFormatWithTestNowMissingYear ()
33+ {
34+ Chronos::setTestNow (new Chronos ('2020-12-01 14:30:45 ' ));
35+ $ d = Chronos::createFromFormat ('m-d H:i:s ' , '10-05 09:15:30 ' );
36+ $ this ->assertDateTime ($ d , 2020 , 10 , 5 , 9 , 15 , 30 );
37+ }
38+
39+ public function testCreateFromFormatWithTestNowMissingDate ()
40+ {
41+ Chronos::setTestNow (new Chronos ('2020-12-01 14:30:45 ' ));
42+ $ d = Chronos::createFromFormat ('H:i:s ' , '09:15:30 ' );
43+ $ this ->assertDateTime ($ d , 2020 , 12 , 1 , 9 , 15 , 30 );
44+ }
45+
46+ public function testCreateFromFormatWithTestNowMissingTime ()
47+ {
48+ Chronos::setTestNow (new Chronos ('2020-12-01 14:30:45 ' ));
49+ $ d = Chronos::createFromFormat ('Y-m-d ' , '2021-06-15 ' );
50+ $ this ->assertDateTime ($ d , 2021 , 6 , 15 , 14 , 30 , 45 );
51+ }
52+
53+ public function testCreateFromFormatWithTestNowPartialDate ()
54+ {
55+ Chronos::setTestNow (new Chronos ('2020-12-01 00:00:00 ' ));
56+ $ d = Chronos::createFromFormat ('m-d ' , '10-05 ' );
57+ $ this ->assertDateTime ($ d , 2020 , 10 , 5 , 0 , 0 , 0 );
58+ }
59+
60+ public function testCreateFromFormatWithTestNowDayOnly ()
61+ {
62+ Chronos::setTestNow (new Chronos ('2020-12-01 00:00:00 ' ));
63+ $ d = Chronos::createFromFormat ('d ' , '05 ' );
64+ $ this ->assertDateTime ($ d , 2020 , 12 , 5 , 0 , 0 , 0 );
65+ }
66+
67+ public function testCreateFromFormatWithTestNowComplete ()
68+ {
69+ // When format is complete, testNow should not affect the result
70+ Chronos::setTestNow (new Chronos ('2020-12-01 14:30:45 ' ));
71+ $ d = Chronos::createFromFormat ('Y-m-d H:i:s ' , '1975-05-21 22:32:11 ' );
72+ $ this ->assertDateTime ($ d , 1975 , 5 , 21 , 22 , 32 , 11 );
73+ }
74+
75+ public function testCreateFromFormatWithTestNowResetModifier ()
76+ {
77+ // The '!' modifier resets to Unix epoch, should not use testNow
78+ Chronos::setTestNow (new Chronos ('2020-12-01 14:30:45 ' ));
79+ $ d = Chronos::createFromFormat ('!Y-m-d ' , '2021-06-15 ' );
80+ $ this ->assertDateTime ($ d , 2021 , 6 , 15 , 0 , 0 , 0 );
81+ }
82+
83+ public function testCreateFromFormatWithTestNowPipeModifier ()
84+ {
85+ // The '|' modifier resets unspecified components to zero, should not use testNow
86+ Chronos::setTestNow (new Chronos ('2020-12-01 14:30:45 ' ));
87+ $ d = Chronos::createFromFormat ('Y-m-d| ' , '2021-06-15 ' );
88+ $ this ->assertDateTime ($ d , 2021 , 6 , 15 , 0 , 0 , 0 );
89+ }
90+
91+ public function testCreateFromFormatWithoutTestNow ()
92+ {
93+ // Without testNow set, behavior should use real current time for missing components
94+ Chronos::setTestNow (null );
95+ $ d = Chronos::createFromFormat ('Y-m-d H:i:s ' , '1975-05-21 22:32:11 ' );
96+ $ this ->assertDateTime ($ d , 1975 , 5 , 21 , 22 , 32 , 11 );
97+ }
98+
99+ public function testCreateFromFormatWithTestNowEscapedCharacters ()
100+ {
101+ // Escaped format characters should not be treated as format specifiers
102+ Chronos::setTestNow (new Chronos ('2020-12-01 14:30:45 ' ));
103+ $ d = Chronos::createFromFormat ('\Y\-m-d ' , 'Y-10-05 ' );
104+ $ this ->assertDateTime ($ d , 2020 , 10 , 5 , 14 , 30 , 45 );
105+ }
106+
107+ public function testCreateFromFormatWithTestNowMicroseconds ()
108+ {
109+ Chronos::setTestNow (new Chronos ('2020-12-01 14:30:45.123456 ' ));
110+ $ d = Chronos::createFromFormat ('Y-m-d H:i:s ' , '2021-06-15 09:15:30 ' );
111+ $ this ->assertSame (123456 , $ d ->micro );
112+ }
113+
32114 public function testCreateFromFormatWithTimezoneString ()
33115 {
34116 $ d = Chronos::createFromFormat ('Y-m-d H:i:s ' , '1975-05-21 22:32:11 ' , 'Europe/London ' );
0 commit comments