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
10 changes: 7 additions & 3 deletions src/Phaseolies/Database/Entity/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1507,9 +1507,13 @@ protected function updatePivot($id, array $pivotData, string $pivotTable, string

foreach ($pivotData as $key => $value) {
$sets[] = "{$key} = ?";
$params[] = $value instanceof \DateTimeInterface
? $value->format('Y-m-d H:i:s')
: $value;
if ($value instanceof \DateTimeInterface) {
$params[] = $value->format('Y-m-d H:i:s') . ($value->format('u') !== '000000'
? '.' . $value->format('u')
: '');
} else {
$params[] = $value;
}
}

$params[] = $parentKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ public function formatDate($date): string
public function formatDateTime($date, string $type = 'start'): string
{
if ($date instanceof \DateTimeInterface) {
return $date->format('Y-m-d H:i:s');
return $date->format('Y-m-d H:i:s') . ($date->format('u') !== '000000'
? '.' . $date->format('u')
: '');
}

if (is_string($date)) {
Expand Down
5 changes: 3 additions & 2 deletions src/Phaseolies/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,11 +927,12 @@ function abort_if($condition, $code, $message = '', array $headers = []): void
/**
* Get the current timestamp with optional timezone
*
* @param \DateTimeZone|string|null $timezone
* @return \Carbon\Carbon
*/
function now()
function now(\DateTimeZone|string|null $timezone = null): Carbon
{
return Carbon::now();
return Carbon::now($timezone);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/Phaseolies/Support/TimezoneHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public function setTimezone(): void
throw new \InvalidArgumentException("Invalid timezone: {$this->currentTimezone}");
}

$dt = Carbon::now();
$dt->setTimezone($this->currentTimezone);
date_default_timezone_set($this->currentTimezone);
}

/**
Expand All @@ -68,6 +67,6 @@ public function getTimezone(): string
*/
public function now(): Carbon
{
return Carbon::now();
return Carbon::now($this->currentTimezone);
}
}
17 changes: 17 additions & 0 deletions tests/Application/TimezoneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ public function testConstructorWithCustomTimezone()
$this->assertEquals($timezone, date_default_timezone_get());
}

public function testHandlerNowUsesConfiguredTimezoneWithoutPriorGlobalSetup(): void
{
date_default_timezone_set('UTC');

$handler = new TimezoneHandler('Asia/Tokyo');

$this->assertSame('Asia/Tokyo', $handler->now()->timezoneName);
$this->assertSame('Asia/Tokyo', date_default_timezone_get());
}

public function testNowHelperAcceptsAnExplicitTimezone(): void
{
date_default_timezone_set('UTC');

$this->assertSame('Asia/Dhaka', now('Asia/Dhaka')->timezoneName);
}

public function testTimezoneAffectsDateFunctions()
{
$timezone = 'Australia/Sydney';
Expand Down
11 changes: 11 additions & 0 deletions tests/Builder/TimeframeOrConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,17 @@ public function testFormatDateTimeWithDateTimeInterface()
$this->assertEquals('2024-03-20 15:45:00', $b->formatDateTime($dt));
}

public function testFormatDateTimePreservesMicroseconds(): void
{
$b = $this->builder();
$dt = \DateTimeImmutable::createFromFormat(
'Y-m-d H:i:s.u',
'2024-03-20 15:45:00.123456'
);

$this->assertSame('2024-03-20 15:45:00.123456', $b->formatDateTime($dt));
}

public function testFormatDateTimeThrowsOnInvalidInput()
{
$this->expectException(\InvalidArgumentException::class);
Expand Down
Loading