Skip to content

Commit 1991e14

Browse files
committed
feat(DateMethods): enhance date methods to return UTC formatted strings and add unit tests for validation
1 parent 8dbd11c commit 1991e14

File tree

5 files changed

+401
-5
lines changed

5 files changed

+401
-5
lines changed

src/Kernel/RuleEngine/PHPSandbox/ExecutableCode/Methods/Date/GetISO8601DateTime.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Dtyq\FlowExprEngine\Kernel\RuleEngine\PHPSandbox\ExecutableCode\Methods\Date;
99

10+
use DateTime;
11+
use DateTimeZone;
1012
use Dtyq\FlowExprEngine\Kernel\RuleEngine\PHPSandbox\ExecutableCode\Methods\AbstractMethod;
1113

1214
class GetISO8601DateTime extends AbstractMethod
@@ -19,7 +21,7 @@ class GetISO8601DateTime extends AbstractMethod
1921

2022
protected string $group = '日期/时间';
2123

22-
protected string $desc = '获取ISO 8601格式的日期和时间;如:2021-01-01T00:00:00';
24+
protected string $desc = '获取ISO 8601格式的日期和时间(UTC时间);如:2021-01-01T00:00:00Z';
2325

2426
protected array $args = [
2527
[
@@ -36,7 +38,13 @@ public function getFunction(): ?callable
3638
if (is_string($time)) {
3739
$time = strtotime($time) ?: time();
3840
}
39-
return date('Y-m-d\TH:i:s', $time);
41+
42+
// Create DateTime object from timestamp and convert to UTC
43+
$datetime = new DateTime('@' . $time);
44+
$datetime->setTimezone(new DateTimeZone('UTC'));
45+
46+
// Format as ISO 8601 with UTC timezone (Z suffix)
47+
return $datetime->format('Y-m-d\TH:i:s\Z');
4048
};
4149
}
4250
}

src/Kernel/RuleEngine/PHPSandbox/ExecutableCode/Methods/Date/GetRFC1123DateTime.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Dtyq\FlowExprEngine\Kernel\RuleEngine\PHPSandbox\ExecutableCode\Methods\Date;
99

10+
use DateTime;
11+
use DateTimeZone;
1012
use Dtyq\FlowExprEngine\Kernel\RuleEngine\PHPSandbox\ExecutableCode\Methods\AbstractMethod;
1113

1214
class GetRFC1123DateTime extends AbstractMethod
@@ -36,7 +38,13 @@ public function getFunction(): ?callable
3638
if (is_string($time)) {
3739
$time = strtotime($time) ?: time();
3840
}
39-
return date('D, d M Y H:i:s \G\M\T', $time);
41+
42+
// Create DateTime object from timestamp and set to UTC
43+
$datetime = new DateTime('@' . $time);
44+
$datetime->setTimezone(new DateTimeZone('UTC'));
45+
46+
// Format as RFC 1123 with proper GMT suffix
47+
return $datetime->format('D, d M Y H:i:s') . ' GMT';
4048
};
4149
}
4250
}

tests/Structure/Expression/ExpressionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ public function testGetDate()
346346
true
347347
))->getResult());
348348

349-
$this->assertEquals(date('Y-m-d\TH:i:s'), $builder->build(json_decode(
349+
// Test that get_iso8601_date_time returns UTC time with Z suffix
350+
$result = $builder->build(json_decode(
350351
<<<'JSON'
351352
[
352353
{
@@ -358,7 +359,8 @@ public function testGetDate()
358359
]
359360
JSON,
360361
true
361-
))->getResult());
362+
))->getResult();
363+
$this->assertMatchesRegularExpression('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/', $result);
362364

363365
$this->assertEquals(date('Y-m-d\TH:i:s') . date('P'), $builder->build(json_decode(
364366
<<<'JSON'

tests/Structure/Form/FormTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,50 @@ public function testObjectFormWithArrayMethodExpression()
20302030
$this->assertTrue($value->expressionIsOnlyMethod());
20312031
}
20322032

2033+
public function testGetRFC1123DateTimeWithCorrectTimezone()
2034+
{
2035+
$valueBuilder = new ValueBuilder();
2036+
2037+
// 创建一个使用get_rfc1123_date_time方法的表达式
2038+
$methodExpressionData = json_decode(<<<'JSON'
2039+
{
2040+
"type": "expression",
2041+
"const_value": null,
2042+
"expression_value": [
2043+
{
2044+
"type": "methods",
2045+
"value": "get_rfc1123_date_time",
2046+
"name": "get_rfc1123_date_time",
2047+
"args": [
2048+
{
2049+
"type": "const",
2050+
"const_value": [
2051+
{
2052+
"type": "input",
2053+
"value": "1634799280",
2054+
"name": ""
2055+
}
2056+
],
2057+
"expression_value": null
2058+
}
2059+
]
2060+
}
2061+
]
2062+
}
2063+
JSON, true);
2064+
2065+
$value = $valueBuilder->build($methodExpressionData);
2066+
$result = $value->getResult();
2067+
2068+
// 验证结果格式符合RFC 1123标准
2069+
$this->assertIsString($result);
2070+
// 验证格式: Thu, 21 Oct 2021 07:28:00 GMT
2071+
$this->assertMatchesRegularExpression('/^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/', $result);
2072+
2073+
// 验证时间戳1634799280对应的UTC时间: Thu, 21 Oct 2021 06:54:40 GMT
2074+
$this->assertStringContainsString('21 Oct 2021 06:54:40 GMT', $result);
2075+
}
2076+
20332077
private function getFormJsonArray(): array
20342078
{
20352079
$formJson = <<<'JSON'

0 commit comments

Comments
 (0)