Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/agent/src/Toolbox/Tool/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
}
}
39 changes: 39 additions & 0 deletions src/agent/tests/Toolbox/Tool/ClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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);
}
}