Skip to content

Commit a2b2cf4

Browse files
committed
feature #638 [Agent] Support setting timezone on clock tool (chr-hertel)
This PR was merged into the main branch. Discussion ---------- [Agent] Support setting timezone on clock tool | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | | License | MIT Commits ------- def087d Support setting timezone on clock tool
2 parents 7736543 + def087d commit a2b2cf4

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/agent/src/Toolbox/Tool/Clock.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,22 @@
2323
{
2424
public function __construct(
2525
private ClockInterface $clock = new SymfonyClock(),
26+
private ?string $timezone = null,
2627
) {
2728
}
2829

2930
public function __invoke(): string
3031
{
32+
$now = $this->clock->now();
33+
34+
if (null !== $this->timezone) {
35+
$now = $now->setTimezone(new \DateTimeZone($this->timezone));
36+
}
37+
3138
return \sprintf(
3239
'Current date is %s (YYYY-MM-DD) and the time is %s (HH:MM:SS).',
33-
$this->clock->now()->format('Y-m-d'),
34-
$this->clock->now()->format('H:i:s'),
40+
$now->format('Y-m-d'),
41+
$now->format('H:i:s'),
3542
);
3643
}
3744
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Agent\Tests\Toolbox\Tool;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\AI\Agent\Toolbox\Tool\Clock;
17+
use Symfony\Component\Clock\MockClock;
18+
19+
#[CoversClass(Clock::class)]
20+
class ClockTest extends TestCase
21+
{
22+
public function testInvokeReturnsCurrentDateTime()
23+
{
24+
$frozen = new MockClock('2024-06-01 12:00:00', new \DateTimeZone('UTC'));
25+
$clock = new Clock($frozen);
26+
$output = $clock();
27+
28+
$this->assertSame('Current date is 2024-06-01 (YYYY-MM-DD) and the time is 12:00:00 (HH:MM:SS).', $output);
29+
}
30+
31+
public function testInvokeWithCustomTimezone()
32+
{
33+
$frozen = new MockClock('2024-06-01 12:00:00', new \DateTimeZone('UTC'));
34+
$clock = new Clock($frozen, 'America/New_York');
35+
$output = $clock();
36+
37+
$this->assertSame('Current date is 2024-06-01 (YYYY-MM-DD) and the time is 08:00:00 (HH:MM:SS).', $output);
38+
}
39+
}

0 commit comments

Comments
 (0)