Skip to content

Commit 61c5552

Browse files
Added caron 3.x support (#6591)
Co-authored-by: 李铭昕 <[email protected]>
1 parent 4722217 commit 61c5552

File tree

7 files changed

+56
-46
lines changed

7 files changed

+56
-46
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"hyperf/stringable": "~3.2.0",
2929
"hyperf/support": "~3.2.0",
3030
"hyperf/tappable": "~3.2.0",
31-
"nesbot/carbon": "^2.0"
31+
"nesbot/carbon": "^2.0 || ^3.0"
3232
},
3333
"suggest": {
3434
"hyperf/command": "Required to use command trigger.",

src/Strategy/Executor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(protected ContainerInterface $container)
6464
public function execute(Crontab $crontab)
6565
{
6666
try {
67-
$diff = Carbon::now()->diffInRealSeconds($crontab->getExecuteTime(), false);
67+
$diff = Carbon::now()->floatDiffInRealSeconds($crontab->getExecuteTime(), false); // @phpstan-ignore-line
6868
$runnable = null;
6969

7070
switch ($crontab->getType()) {

tests/CrontabTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace HyperfTest\Crontab;
1414

15+
use Carbon\Carbon;
1516
use Hyperf\Crontab\Crontab;
1617
use PHPUnit\Framework\Attributes\CoversNothing;
1718
use PHPUnit\Framework\TestCase;
@@ -65,4 +66,19 @@ public function testSerializeAndUnserialize()
6566

6667
$this->assertEquals($crontab, $unserializeCrontab);
6768
}
69+
70+
public function testCreateFromTimestamp()
71+
{
72+
$timezone = date_default_timezone_get();
73+
try {
74+
date_default_timezone_set('Asia/Shanghai');
75+
76+
$t1 = Carbon::make('2025-07-22');
77+
$t2 = Carbon::createFromTimestamp($t1->getTimestamp(), date_default_timezone_get());
78+
79+
$this->assertSame($t1->toDateTimeString(), $t2->toDateTimeString());
80+
} finally {
81+
date_default_timezone_set($timezone);
82+
}
83+
}
6884
}

tests/ExecutorTest.php

-355 Bytes
Binary file not shown.

tests/ParserTest.php

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ class ParserTest extends TestCase
2828

2929
protected function setUp(): void
3030
{
31-
$this->timezone = ini_get('date.timezone');
32-
ini_set('date.timezone', 'Asia/Shanghai');
33-
}
34-
35-
protected function tearDown(): void
36-
{
37-
ini_set('date.timezone', $this->timezone);
31+
$this->timezone = 'Asia/Shanghai';
3832
}
3933

4034
public function testIsValid(): void
@@ -60,116 +54,116 @@ public function testParseSecondLevel()
6054
{
6155
$crontabString = '*/11 * * * * *';
6256
$parser = new Parser();
63-
$startTime = Carbon::createFromTimestamp(1561052867)->startOfMinute();
64-
$result = $parser->parse($crontabString, $startTime->getTimestamp());
57+
$startTime = Carbon::createFromTimestamp(1561052867, $this->timezone)->startOfMinute();
58+
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
6559
$this->assertSame([
6660
'2019-06-21 01:47:00',
6761
'2019-06-21 01:47:11',
6862
'2019-06-21 01:47:22',
6963
'2019-06-21 01:47:33',
7064
'2019-06-21 01:47:44',
7165
'2019-06-21 01:47:55',
72-
], $this->toDatatime($result));
66+
], $this->toDateTime($result));
7367
/** @var Carbon $last */
7468
$last = end($result);
75-
$result = $parser->parse($crontabString, $last->getTimestamp());
69+
$result = $parser->parse($crontabString, $last->getTimestamp(), $this->timezone);
7670
$this->assertSame([
7771
'2019-06-21 01:47:55',
7872
'2019-06-21 01:48:06',
7973
'2019-06-21 01:48:17',
8074
'2019-06-21 01:48:28',
8175
'2019-06-21 01:48:39',
8276
'2019-06-21 01:48:50',
83-
], $this->toDatatime($result));
77+
], $this->toDateTime($result));
8478
}
8579

8680
public function testParseSecondLevelBetween(): void
8781
{
8882
$crontabString = '10-15/1 * * * * *';
8983
$parser = new Parser();
90-
$startTime = Carbon::createFromTimestamp(1591754280)->startOfMinute();
91-
$result = $parser->parse($crontabString, $startTime->getTimestamp());
84+
$startTime = Carbon::createFromTimestamp(1591754280, $this->timezone)->startOfMinute();
85+
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
9286
$this->assertSame([
9387
'2020-06-10 09:58:10',
9488
'2020-06-10 09:58:11',
9589
'2020-06-10 09:58:12',
9690
'2020-06-10 09:58:13',
9791
'2020-06-10 09:58:14',
9892
'2020-06-10 09:58:15',
99-
], $this->toDatatime($result));
93+
], $this->toDateTime($result));
10094
}
10195

10296
public function testParseSecondLevelForComma(): void
10397
{
10498
$crontabString = '10-12/1,14-15/1 * * * * *';
10599
$parser = new Parser();
106-
$startTime = Carbon::createFromTimestamp(1591754280)->startOfMinute();
107-
$result = $parser->parse($crontabString, $startTime->getTimestamp());
100+
$startTime = Carbon::createFromTimestamp(1591754280, $this->timezone)->startOfMinute();
101+
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
108102
$this->assertSame([
109103
'2020-06-10 09:58:10',
110104
'2020-06-10 09:58:11',
111105
'2020-06-10 09:58:12',
112106
'2020-06-10 09:58:14',
113107
'2020-06-10 09:58:15',
114-
], $this->toDatatime($result));
108+
], $this->toDateTime($result));
115109
}
116110

117111
public function testParseSecondLevelWithoutBackslash(): void
118112
{
119113
$crontabString = '10-12,14-15/1 * * * * *';
120114
$parser = new Parser();
121-
$startTime = Carbon::createFromTimestamp(1591754280)->startOfMinute();
122-
$result = $parser->parse($crontabString, $startTime->getTimestamp());
115+
$startTime = Carbon::createFromTimestamp(1591754280, $this->timezone)->startOfMinute();
116+
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
123117
$this->assertSame([
124118
'2020-06-10 09:58:10',
125119
'2020-06-10 09:58:11',
126120
'2020-06-10 09:58:12',
127121
'2020-06-10 09:58:14',
128122
'2020-06-10 09:58:15',
129-
], $this->toDatatime($result));
123+
], $this->toDateTime($result));
130124
}
131125

132126
public function testParseSecondLevelWithEmptyString()
133127
{
134128
$crontabString = '10,14,,15, * * * * *';
135129
$parser = new Parser();
136-
$startTime = Carbon::createFromTimestamp(1591754280)->startOfMinute();
137-
$result = $parser->parse($crontabString, $startTime->getTimestamp());
130+
$startTime = Carbon::createFromTimestamp(1591754280, $this->timezone)->startOfMinute();
131+
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
138132
$this->assertSame([
139133
'2020-06-10 09:58:10',
140134
'2020-06-10 09:58:14',
141135
'2020-06-10 09:58:15',
142-
], $this->toDatatime($result));
136+
], $this->toDateTime($result));
143137
}
144138

145139
public function testParseMinuteLevelBetween(): void
146140
{
147141
$crontabString = '10-15/1 10-12/1 10 * * *';
148142
$parser = new Parser();
149-
$startTime = Carbon::createFromTimestamp(1591755010)->startOfMinute();
150-
$result = $parser->parse($crontabString, $startTime->getTimestamp());
143+
$startTime = Carbon::createFromTimestamp(1591755010, $this->timezone)->startOfMinute();
144+
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
151145
$this->assertSame([
152146
'2020-06-10 10:10:10',
153147
'2020-06-10 10:10:11',
154148
'2020-06-10 10:10:12',
155149
'2020-06-10 10:10:13',
156150
'2020-06-10 10:10:14',
157151
'2020-06-10 10:10:15',
158-
], $this->toDatatime($result));
152+
], $this->toDateTime($result));
159153

160154
$last = end($result);
161-
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute());
155+
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute(), $this->timezone);
162156
$this->assertSame([
163157
'2020-06-10 10:11:10',
164158
'2020-06-10 10:11:11',
165159
'2020-06-10 10:11:12',
166160
'2020-06-10 10:11:13',
167161
'2020-06-10 10:11:14',
168162
'2020-06-10 10:11:15',
169-
], $this->toDatatime($result));
163+
], $this->toDateTime($result));
170164

171165
$last = end($result);
172-
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute());
166+
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute(), $this->timezone);
173167

174168
$this->assertSame([
175169
'2020-06-10 10:12:10',
@@ -178,59 +172,59 @@ public function testParseMinuteLevelBetween(): void
178172
'2020-06-10 10:12:13',
179173
'2020-06-10 10:12:14',
180174
'2020-06-10 10:12:15',
181-
], $this->toDatatime($result));
175+
], $this->toDateTime($result));
182176

183177
$last = end($result);
184-
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute());
178+
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute(), $this->timezone);
185179

186-
$this->assertSame([], $this->toDatatime($result));
180+
$this->assertSame([], $this->toDateTime($result));
187181
}
188182

189183
public function testParseSecondLevelWithCarbonStartTime()
190184
{
191185
$crontabString = '*/11 * * * * *';
192186
$parser = new Parser();
193-
$startTime = Carbon::createFromTimestamp(1561052867)->startOfMinute();
194-
$result = $parser->parse($crontabString, $startTime);
187+
$startTime = Carbon::createFromTimestamp(1561052867, $this->timezone)->startOfMinute();
188+
$result = $parser->parse($crontabString, $startTime, $this->timezone);
195189
$this->assertSame([
196190
'2019-06-21 01:47:00',
197191
'2019-06-21 01:47:11',
198192
'2019-06-21 01:47:22',
199193
'2019-06-21 01:47:33',
200194
'2019-06-21 01:47:44',
201195
'2019-06-21 01:47:55',
202-
], $this->toDatatime($result));
196+
], $this->toDateTime($result));
203197
/** @var Carbon $last */
204198
$last = end($result);
205-
$result = $parser->parse($crontabString, $last);
199+
$result = $parser->parse($crontabString, $last, $this->timezone);
206200
$this->assertSame([
207201
'2019-06-21 01:47:55',
208202
'2019-06-21 01:48:06',
209203
'2019-06-21 01:48:17',
210204
'2019-06-21 01:48:28',
211205
'2019-06-21 01:48:39',
212206
'2019-06-21 01:48:50',
213-
], $this->toDatatime($result));
207+
], $this->toDateTime($result));
214208
}
215209

216210
public function testParseMinuteLevel()
217211
{
218212
$crontabString = '*/11 * * * *';
219213
$parser = new Parser();
220-
$startTime = Carbon::createFromTimestamp(1561052867)->startOfMinute();
221-
$result = $parser->parse($crontabString, $startTime->getTimestamp());
222-
$this->assertSame([], $this->toDatatime($result));
214+
$startTime = Carbon::createFromTimestamp(1561052867, $this->timezone)->startOfMinute();
215+
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
216+
$this->assertSame([], $this->toDateTime($result));
223217

224218
$startTime->minute(33);
225-
$result = $parser->parse($crontabString, $startTime->getTimestamp());
226-
$this->assertSame(['2019-06-21 01:33:00'], $this->toDatatime($result));
219+
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
220+
$this->assertSame(['2019-06-21 01:33:00'], $this->toDateTime($result));
227221
}
228222

229223
/**
230224
* @param Carbon[] $result
231225
* @return string[]
232226
*/
233-
protected function toDatatime(array $result)
227+
protected function toDateTime(array $result)
234228
{
235229
$dates = [];
236230
foreach ($result as $date) {

tests/Stub/cron.data

464 Bytes
Binary file not shown.

tests/Stub/cron22.data

464 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)