Skip to content

Commit

Permalink
Merge pull request #2823 from briannesbitt/fix/issue-2821-create-from…
Browse files Browse the repository at this point in the history
…-format-time

Make createFromFormat work the same with mocked time
  • Loading branch information
kylekatarnls authored Jul 12, 2023
2 parents 7ce9ddc + e198336 commit eeb761b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ jobs:
- php: 8.3
setup: lowest

- php: 8.0
- php: 8.1
laravel: true
reference: 9.x
reference: 10.x

- php: 8.0
- php: 8.1
laravel: true
reference: 9.x
reference: 10.x
classmap-authoritative: true

- php: 8.2
laravel: true
reference: 9.x
reference: master

- php: 8.2
laravel: true
reference: 9.x
reference: master
classmap-authoritative: true

- php: 8.2
Expand Down
8 changes: 5 additions & 3 deletions src/Carbon/Traits/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,14 @@ public static function rawCreateFromFormat($format, $time, $tz = null)
$tz = clone $mock->getTimezone();
}

// Set microseconds to zero to match behavior of DateTime::createFromFormat()
// See https://bugs.php.net/bug.php?id=74332
$mock = $mock->copy()->microsecond(0);
$mock = $mock->copy();

// Prepend mock datetime only if the format does not contain non escaped unix epoch reset flag.
if (!preg_match("/{$nonEscaped}[!|]/", $format)) {
if (preg_match('/[HhGgisvuB]/', $format)) {
$mock = $mock->setTime(0, 0);
}

$format = static::MOCK_DATETIME_FORMAT.' '.$format;
$time = ($mock instanceof self ? $mock->rawFormat(static::MOCK_DATETIME_FORMAT) : $mock->format(static::MOCK_DATETIME_FORMAT)).' '.$time;
}
Expand Down
12 changes: 5 additions & 7 deletions tests/Carbon/TestingAidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ public function testCreateFromPartialFormat()
$this->assertSame('2013-09-01T10:20:30-07:00', Carbon::createFromFormat('H:i:s', '10:20:30')->toIso8601String());

// Custom timezone.
$this->assertSame('2013-09-01T10:20:15+03:00', Carbon::createFromFormat('H:i e', '10:20 Europe/Kiev')->toIso8601String());
$this->assertSame('2013-09-01T10:20:15+01:00', Carbon::createFromFormat('H:i', '10:20', 'Europe/London')->toIso8601String());
$this->assertSame('2013-09-01T10:20:00+03:00', Carbon::createFromFormat('H:i e', '10:20 Europe/Kiev')->toIso8601String());
$this->assertSame('2013-09-01T10:20:00+01:00', Carbon::createFromFormat('H:i', '10:20', 'Europe/London')->toIso8601String());
$this->assertSame('2013-09-01T11:30:00+07:00', Carbon::createFromFormat('H:i:s e', '11:30:00+07:00')->toIso8601String());
$this->assertSame('2013-09-01T11:30:00+05:00', Carbon::createFromFormat('H:i:s', '11:30:00', '+05:00')->toIso8601String());

Expand All @@ -255,7 +255,7 @@ public function testCreateFromPartialFormat()

// Weird format, naive modify would fail here.
$this->assertSame('2005-08-09T05:10:15-07:00', Carbon::createFromFormat('l jS \of F Y', 'Tuesday 9th of August 2005')->toIso8601String());
$this->assertSame('2013-09-01T05:12:13-07:00', Carbon::createFromFormat('i:s', '12:13')->toIso8601String());
$this->assertSame('2013-09-01T00:12:13-07:00', Carbon::createFromFormat('i:s', '12:13')->toIso8601String());
$this->assertSame('2018-09-05T05:10:15-07:00', Carbon::createFromFormat('Y/d', '2018/5')->toIso8601String());

// Resetting to epoch.
Expand Down Expand Up @@ -283,17 +283,15 @@ public function testCreateFromPartialFormatWithMicroseconds()
{
Carbon::setTestNowAndTimezone(Carbon::parse('2013-09-01 05:10:15.123456', 'America/Vancouver'));

// Set microseconds to zero to match behavior of DateTime::createFromFormat()
// See https://bugs.php.net/bug.php?id=74332
$this->assertSame('2018-05-06 05:10:15.000000', Carbon::createFromFormat('Y-m-d', '2018-05-06')->format('Y-m-d H:i:s.u'));
$this->assertSame('2018-05-06 05:10:15.123456', Carbon::createFromFormat('Y-m-d', '2018-05-06')->format('Y-m-d H:i:s.u'));
$this->assertSame('2013-09-01 10:20:30.654321', Carbon::createFromFormat('H:i:s.u', '10:20:30.654321')->format('Y-m-d H:i:s.u'));
}

public function testCreateFromDateTimeInterface()
{
Carbon::setTestNowAndTimezone(date_create('2013-09-01 05:10:15.123456', new DateTimeZone('America/Vancouver')));

$this->assertSame('2018-05-06 05:10:15.000000', Carbon::createFromFormat('Y-m-d', '2018-05-06')->format('Y-m-d H:i:s.u'));
$this->assertSame('2018-05-06 05:10:15.123456', Carbon::createFromFormat('Y-m-d', '2018-05-06')->format('Y-m-d H:i:s.u'));
$this->assertSame('2013-09-01 10:20:30.654321', Carbon::createFromFormat('H:i:s.u', '10:20:30.654321')->format('Y-m-d H:i:s.u'));
}

Expand Down
15 changes: 8 additions & 7 deletions tests/CarbonImmutable/TestingAidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ public function testCreateFromPartialFormat()
$this->assertSame('2013-09-01T10:20:30-07:00', Carbon::createFromFormat('H:i:s', '10:20:30')->toIso8601String());

// Custom timezone.
$this->assertSame('2013-09-01T10:20:15+03:00', Carbon::createFromFormat('H:i e', '10:20 Europe/Kiev')->toIso8601String());
$this->assertSame('2013-09-01T10:20:15+01:00', Carbon::createFromFormat('H:i', '10:20', 'Europe/London')->toIso8601String());
$this->assertSame('2013-09-01T10:20:00+03:00', Carbon::createFromFormat('H:i e', '10:20 Europe/Kiev')->toIso8601String());
$this->assertSame('2013-09-01T10:20:00+01:00', Carbon::createFromFormat('H:i', '10:20', 'Europe/London')->toIso8601String());
$this->assertSame('2013-09-01T11:30:00+07:00', Carbon::createFromFormat('H:i:s e', '11:30:00+07:00')->toIso8601String());
$this->assertSame('2013-09-01T11:30:00+05:00', Carbon::createFromFormat('H:i:s', '11:30:00', '+05:00')->toIso8601String());

Expand All @@ -229,7 +229,7 @@ public function testCreateFromPartialFormat()

// Weird format, naive modify would fail here.
$this->assertSame('2005-08-09T05:10:15-07:00', Carbon::createFromFormat('l jS \of F Y', 'Tuesday 9th of August 2005')->toIso8601String());
$this->assertSame('2013-09-01T05:12:13-07:00', Carbon::createFromFormat('i:s', '12:13')->toIso8601String());
$this->assertSame('2013-09-01T00:12:13-07:00', Carbon::createFromFormat('i:s', '12:13')->toIso8601String());
$this->assertSame('2018-09-05T05:10:15-07:00', Carbon::createFromFormat('Y/d', '2018/5')->toIso8601String());

// Resetting to epoch.
Expand All @@ -251,23 +251,24 @@ public function testCreateFromPartialFormat()
$this->assertSame('2013-09-01T05:10:15-07:00', Carbon::createFromFormat('\|', '|')->toIso8601String());
$this->assertSame('2013-09-01T05:10:15-07:00', Carbon::createFromFormat('\!', '!')->toIso8601String());
$this->assertSame('2013-09-01T05:10:15+03:00', Carbon::createFromFormat('e \!', 'Europe/Kiev !')->toIso8601String());

Carbon::setTestNow('2023-12-05 21:09:54');
$this->assertSame('2023-12-05 15:00:00.000000', Carbon::createFromFormat('H', '15')->format('Y-m-d H:i:s.u'));
}

public function testCreateFromPartialFormatWithMicroseconds()
{
Carbon::setTestNowAndTimezone(Carbon::parse('2013-09-01 05:10:15.123456', 'America/Vancouver'));

// Set microseconds to zero to match behavior of DateTime::createFromFormat()
// See https://bugs.php.net/bug.php?id=74332
$this->assertSame('2018-05-06 05:10:15.000000', Carbon::createFromFormat('Y-m-d', '2018-05-06')->format('Y-m-d H:i:s.u'));
$this->assertSame('2018-05-06 05:10:15.123456', Carbon::createFromFormat('Y-m-d', '2018-05-06')->format('Y-m-d H:i:s.u'));
$this->assertSame('2013-09-01 10:20:30.654321', Carbon::createFromFormat('H:i:s.u', '10:20:30.654321')->format('Y-m-d H:i:s.u'));
}

public function testCreateFromDateTimeInterface()
{
Carbon::setTestNowAndTimezone(date_create_immutable('2013-09-01 05:10:15.123456', new DateTimeZone('America/Vancouver')));

$this->assertSame('2018-05-06 05:10:15.000000', Carbon::createFromFormat('Y-m-d', '2018-05-06')->format('Y-m-d H:i:s.u'));
$this->assertSame('2018-05-06 05:10:15.123456', Carbon::createFromFormat('Y-m-d', '2018-05-06')->format('Y-m-d H:i:s.u'));
$this->assertSame('2013-09-01 10:20:30.654321', Carbon::createFromFormat('H:i:s.u', '10:20:30.654321')->format('Y-m-d H:i:s.u'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ projects:
source:
type: git
url: https://github.com/laravel/framework.git
reference: 9.x
reference: 10.x
success_only: true
install:
- composer require psr/log:2.0.0 --no-update
Expand Down
1 change: 0 additions & 1 deletion tests/Laravel/laravel-master.multi-tester.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ projects:
reference: master
success_only: true
install:
- composer require psr/log:2.0.0 --no-update
- composer update --prefer-dist --no-interaction --prefer-stable
- echo -e "\nREDIS_HOST=127.0.0.1\nREDIS_PORT=6379" >> .env

0 comments on commit eeb761b

Please sign in to comment.