Skip to content

Commit 628723a

Browse files
committed
fix: sync temporal with default timezone
1 parent d00a397 commit 628723a

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

src/Worker/Environment/Environment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Environment implements EnvironmentInterface
2020

2121
public function __construct()
2222
{
23-
$this->tickTime = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
23+
$this->tickTime = new \DateTimeImmutable('now');
2424
}
2525

2626
public function now(): \DateTimeInterface

src/Worker/Transport/Codec/JsonCodec.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ final class JsonCodec implements CodecInterface
2323
private int $maxDepth;
2424
private Decoder $parser;
2525
private Encoder $serializer;
26+
private \DateTimeZone $hostTimeZone;
2627

2728
public function __construct(DataConverterInterface $dataConverter, int $maxDepth = 64)
2829
{
2930
$this->maxDepth = $maxDepth;
3031

3132
$this->parser = new Decoder($dataConverter);
3233
$this->serializer = new Encoder($dataConverter);
34+
$this->hostTimeZone = new \DateTimeZone(\date_default_timezone_get());
3335
}
3436

3537
public function encode(iterable $commands): string
@@ -50,15 +52,13 @@ public function encode(iterable $commands): string
5052

5153
public function decode(string $batch, array $headers = []): iterable
5254
{
53-
static $tz = new \DateTimeZone('UTC');
54-
5555
try {
5656
$commands = \json_decode($batch, true, $this->maxDepth, \JSON_THROW_ON_ERROR);
5757

5858
foreach ($commands as $command) {
5959
/** @psalm-suppress ArgumentTypeCoercion */
6060
$info = new TickInfo(
61-
time: new \DateTimeImmutable($headers['tickTime'] ?? 'now', $tz),
61+
time: (new \DateTimeImmutable($headers['tickTime'] ?? 'now'))->setTimezone($this->hostTimeZone),
6262
historyLength: (int) ($headers['history_length'] ?? 0),
6363
historySize: (int) ($headers['history_size'] ?? 0),
6464
continueAsNewSuggested: (bool) ($headers['continue_as_new_suggested'] ?? false),

src/Worker/Transport/Codec/ProtoCodec.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ final class ProtoCodec implements CodecInterface
2626
{
2727
private Decoder $parser;
2828
private Encoder $encoder;
29+
private \DateTimeZone $hostTimeZone;
2930

3031
public function __construct(DataConverterInterface $dataConverter)
3132
{
3233
$this->parser = new Decoder($dataConverter);
3334
$this->encoder = new Encoder($dataConverter);
35+
$this->hostTimeZone = new \DateTimeZone(\date_default_timezone_get());
3436
}
3537

3638
public function encode(iterable $commands): string
@@ -54,16 +56,14 @@ public function encode(iterable $commands): string
5456

5557
public function decode(string $batch, array $headers = []): iterable
5658
{
57-
static $tz = new \DateTimeZone('UTC');
58-
5959
try {
6060
$frame = new Frame();
6161
$frame->mergeFromString($batch);
6262

6363
foreach ($frame->getMessages() as $msg) {
6464
/** @psalm-suppress ArgumentTypeCoercion */
6565
$info = new TickInfo(
66-
time: new \DateTimeImmutable($headers['tickTime'] ?? $msg->getTickTime(), $tz),
66+
time: (new \DateTimeImmutable($headers['tickTime'] ?? $msg->getTickTime()))->setTimezone($this->hostTimeZone),
6767
historyLength: (int) ($headers['history_length'] ?? $msg->getHistoryLength()),
6868
historySize: (int) ($headers['history_size'] ?? $msg->getHistorySize()),
6969
continueAsNewSuggested: (bool) ($headers['continue_as_new_suggested'] ?? $msg->getContinueAsNewSuggested()),
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Tests\Acceptance\Extra\Workflow\DateTimeZoneWorkflow;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Temporal\Client\WorkflowStubInterface;
9+
use Temporal\Tests\Acceptance\App\Attribute\Stub;
10+
use Temporal\Tests\Acceptance\App\TestCase;
11+
use Temporal\Workflow;
12+
use Temporal\Workflow\WorkflowInterface;
13+
use Temporal\Workflow\WorkflowMethod;
14+
15+
class DateTimeZoneWorkflowTest extends TestCase
16+
{
17+
#[Test]
18+
public static function currentTime(
19+
#[Stub('Extra_Workflow_DateTimeZoneWorkflow')]
20+
WorkflowStubInterface $stub,
21+
): void {
22+
$result = $stub->getResult(type: 'array');
23+
24+
self::assertEquals($result['system'], $result['current']);
25+
}
26+
}
27+
28+
#[WorkflowInterface]
29+
class MainWorkflow
30+
{
31+
#[WorkflowMethod('Extra_Workflow_DateTimeZoneWorkflow')]
32+
public function run()
33+
{
34+
yield Workflow::timer('1 seconds');
35+
36+
/**
37+
* @var \DateTimeImmutable $currentDate
38+
*/
39+
$currentDate = yield Workflow::sideEffect(static fn(): \DateTimeImmutable => new \DateTimeImmutable());
40+
41+
return yield [
42+
'current' => [
43+
'timestamp' => $currentDate->getTimestamp(),
44+
'timezone.offset' => $currentDate->getTimeZone()->getOffset($currentDate),
45+
],
46+
'system' => [
47+
'timestamp' => Workflow::now()->getTimestamp(),
48+
'timezone.offset' => Workflow::now()->getTimezone()->getOffset(Workflow::now()),
49+
],
50+
];
51+
}
52+
}

0 commit comments

Comments
 (0)