Skip to content

Commit d8fab7e

Browse files
committed
feat(runtime): add timestamp integer and float overloads
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 3c0ed2d commit d8fab7e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Runtime/Extension/DateTime/Function/TimestampFunction.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
use Cel\Runtime\Exception\TypeConversionException;
88
use Cel\Runtime\Function\FunctionInterface;
9+
use Cel\Runtime\Value\FloatValue;
10+
use Cel\Runtime\Value\IntegerValue;
911
use Cel\Runtime\Value\StringValue;
1012
use Cel\Runtime\Value\TimestampValue;
1113
use Cel\Runtime\Value\Value;
1214
use Cel\Runtime\Value\ValueKind;
1315
use Cel\Syntax\Member\CallExpression;
1416
use Override;
17+
use Psl\DateTime;
1518
use Psl\DateTime\Exception\RuntimeException;
1619
use Psl\DateTime\FormatPattern;
1720
use Psl\DateTime\Timestamp;
@@ -38,6 +41,23 @@ public function isIdempotent(): bool
3841
#[Override]
3942
public function getOverloads(): iterable
4043
{
44+
yield [ValueKind::Integer] => static function (CallExpression $expr, array $arguments): TimestampValue {
45+
/** @var IntegerValue $seconds */
46+
$seconds = $arguments[0];
47+
48+
return new TimestampValue(Timestamp::fromParts($seconds->value));
49+
};
50+
51+
yield [ValueKind::Float] => static function (CallExpression $expr, array $arguments): TimestampValue {
52+
/** @var FloatValue $seconds */
53+
$seconds = $arguments[0];
54+
55+
$wholeSeconds = (int) $seconds->value;
56+
$nanoseconds = (int) (($seconds->value - $wholeSeconds) * DateTime\NANOSECONDS_PER_SECOND);
57+
58+
return new TimestampValue(Timestamp::fromParts($wholeSeconds, $nanoseconds));
59+
};
60+
4161
yield [ValueKind::String] =>
4262
/**
4363
* @param CallExpression $call The call expression representing the function call.

tests/Runtime/Extension/DateTimeExtensionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@ public static function provideEvaluationCases(): iterable
243243
[],
244244
new EvaluationException('getFullYear: timezone `Mars/Olympus_Mons` is not valid', new Span(0, 71)),
245245
];
246+
247+
/// Timestamp from seconds
248+
yield 'DateTime timestamp(): from integer seconds' => [
249+
'timestamp(1757766605)',
250+
[],
251+
new TimestampValue(Timestamp::fromParts(1757766605)),
252+
];
253+
254+
/// Timestamp from float seconds
255+
yield 'DateTime timestamp(): from float seconds' => [
256+
'timestamp(1757766605.123456)',
257+
[],
258+
new TimestampValue(Timestamp::fromParts(1757766605, 123456001)),
259+
];
246260
}
247261

248262
public function testNowFunctionReturnsCurrentTimestamp(): void

0 commit comments

Comments
 (0)