diff --git a/src/agent/src/Toolbox/Tool/Clock.php b/src/agent/src/Toolbox/Tool/Clock.php index 562b3ccf8..b6df32d0a 100644 --- a/src/agent/src/Toolbox/Tool/Clock.php +++ b/src/agent/src/Toolbox/Tool/Clock.php @@ -23,15 +23,22 @@ { public function __construct( private ClockInterface $clock = new SymfonyClock(), + private ?string $timezone = null, ) { } public function __invoke(): string { + $now = $this->clock->now(); + + if (null !== $this->timezone) { + $now = $now->setTimezone(new \DateTimeZone($this->timezone)); + } + return \sprintf( 'Current date is %s (YYYY-MM-DD) and the time is %s (HH:MM:SS).', - $this->clock->now()->format('Y-m-d'), - $this->clock->now()->format('H:i:s'), + $now->format('Y-m-d'), + $now->format('H:i:s'), ); } } diff --git a/src/agent/tests/Toolbox/Tool/ClockTest.php b/src/agent/tests/Toolbox/Tool/ClockTest.php new file mode 100644 index 000000000..3864f1d30 --- /dev/null +++ b/src/agent/tests/Toolbox/Tool/ClockTest.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\AI\Agent\Tests\Toolbox\Tool; + +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; +use Symfony\AI\Agent\Toolbox\Tool\Clock; +use Symfony\Component\Clock\MockClock; + +#[CoversClass(Clock::class)] +class ClockTest extends TestCase +{ + public function testInvokeReturnsCurrentDateTime() + { + $frozen = new MockClock('2024-06-01 12:00:00', new \DateTimeZone('UTC')); + $clock = new Clock($frozen); + $output = $clock(); + + $this->assertSame('Current date is 2024-06-01 (YYYY-MM-DD) and the time is 12:00:00 (HH:MM:SS).', $output); + } + + public function testInvokeWithCustomTimezone() + { + $frozen = new MockClock('2024-06-01 12:00:00', new \DateTimeZone('UTC')); + $clock = new Clock($frozen, 'America/New_York'); + $output = $clock(); + + $this->assertSame('Current date is 2024-06-01 (YYYY-MM-DD) and the time is 08:00:00 (HH:MM:SS).', $output); + } +}