Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/Worker/Environment/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Environment implements EnvironmentInterface

public function __construct()
{
$this->tickTime = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
$this->tickTime = new \DateTimeImmutable('now');
}

public function now(): \DateTimeInterface
Expand Down
6 changes: 3 additions & 3 deletions src/Worker/Transport/Codec/JsonCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ final class JsonCodec implements CodecInterface
private int $maxDepth;
private Decoder $parser;
private Encoder $serializer;
private \DateTimeZone $hostTimeZone;

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

$this->parser = new Decoder($dataConverter);
$this->serializer = new Encoder($dataConverter);
$this->hostTimeZone = new \DateTimeZone(\date_default_timezone_get());
}

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

public function decode(string $batch, array $headers = []): iterable
{
static $tz = new \DateTimeZone('UTC');

try {
$commands = \json_decode($batch, true, $this->maxDepth, \JSON_THROW_ON_ERROR);

foreach ($commands as $command) {
/** @psalm-suppress ArgumentTypeCoercion */
$info = new TickInfo(
time: new \DateTimeImmutable($headers['tickTime'] ?? 'now', $tz),
time: (new \DateTimeImmutable($headers['tickTime'] ?? 'now'))->setTimezone($this->hostTimeZone),
historyLength: (int) ($headers['history_length'] ?? 0),
historySize: (int) ($headers['history_size'] ?? 0),
continueAsNewSuggested: (bool) ($headers['continue_as_new_suggested'] ?? false),
Expand Down
6 changes: 3 additions & 3 deletions src/Worker/Transport/Codec/ProtoCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ final class ProtoCodec implements CodecInterface
{
private Decoder $parser;
private Encoder $encoder;
private \DateTimeZone $hostTimeZone;

public function __construct(DataConverterInterface $dataConverter)
{
$this->parser = new Decoder($dataConverter);
$this->encoder = new Encoder($dataConverter);
$this->hostTimeZone = new \DateTimeZone(\date_default_timezone_get());
}

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

public function decode(string $batch, array $headers = []): iterable
{
static $tz = new \DateTimeZone('UTC');

try {
$frame = new Frame();
$frame->mergeFromString($batch);

foreach ($frame->getMessages() as $msg) {
/** @psalm-suppress ArgumentTypeCoercion */
$info = new TickInfo(
time: new \DateTimeImmutable($headers['tickTime'] ?? $msg->getTickTime(), $tz),
time: (new \DateTimeImmutable($headers['tickTime'] ?? $msg->getTickTime()))->setTimezone($this->hostTimeZone),
historyLength: (int) ($headers['history_length'] ?? $msg->getHistoryLength()),
historySize: (int) ($headers['history_size'] ?? $msg->getHistorySize()),
continueAsNewSuggested: (bool) ($headers['continue_as_new_suggested'] ?? $msg->getContinueAsNewSuggested()),
Expand Down
52 changes: 52 additions & 0 deletions tests/Acceptance/Extra/Workflow/DateTimeZoneWorkflowTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Acceptance\Extra\Workflow\DateTimeZoneWorkflow;

use PHPUnit\Framework\Attributes\Test;
use Temporal\Client\WorkflowStubInterface;
use Temporal\Tests\Acceptance\App\Attribute\Stub;
use Temporal\Tests\Acceptance\App\TestCase;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowInterface;
use Temporal\Workflow\WorkflowMethod;

class DateTimeZoneWorkflowTest extends TestCase
{
#[Test]
public static function currentTime(
#[Stub('Extra_Workflow_DateTimeZoneWorkflow')]
WorkflowStubInterface $stub,
): void {
$result = $stub->getResult(type: 'array');

self::assertEquals($result['system'], $result['current']);
}
}

#[WorkflowInterface]
class MainWorkflow
{
#[WorkflowMethod('Extra_Workflow_DateTimeZoneWorkflow')]
public function run()
{
yield Workflow::timer('1 seconds');

/**
* @var \DateTimeImmutable $currentDate
*/
$currentDate = yield Workflow::sideEffect(static fn(): \DateTimeImmutable => new \DateTimeImmutable());

return yield [
'current' => [
'timestamp' => $currentDate->getTimestamp(),
'timezone.offset' => $currentDate->getTimeZone()->getOffset($currentDate),
],
'system' => [
'timestamp' => Workflow::now()->getTimestamp(),
'timezone.offset' => Workflow::now()->getTimezone()->getOffset(Workflow::now()),
],
];
}
}
Loading