Skip to content

Commit

Permalink
Default Chronos::create() time components to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Sep 24, 2023
1 parent af68da5 commit cc941e2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
34 changes: 14 additions & 20 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,27 +563,21 @@ public static function create(
?int $year = null,
?int $month = null,
?int $day = null,
?int $hour = null,
?int $minute = null,
?int $second = null,
?int $microsecond = null,
?int $hour = 0,
?int $minute = 0,
?int $second = 0,
?int $microsecond = 0,
DateTimeZone|string|null $timezone = null
): static {
$now = static::now();
$year = $year ?? (int)$now->format('Y');
$month = $month ?? $now->format('m');
$day = $day ?? $now->format('d');

if ($hour === null) {
$hour = $now->format('H');
$minute = $minute ?? $now->format('i');
$second = $second ?? $now->format('s');
$microsecond = $microsecond ?? $now->format('u');
} else {
$minute = $minute ?? 0;
$second = $second ?? 0;
$microsecond = $microsecond ?? 0;
}
$hour = $hour ?? $now->format('H');
$minute = $minute ?? $now->format('i');
$second = $second ?? $now->format('s');
$microsecond = $microsecond ?? $now->format('u');

$instance = static::createFromFormat(
'Y-m-d H:i:s.u',
Expand All @@ -595,7 +589,7 @@ public static function create(
}

/**
* Create an instance from just a date. The time portion is set to now.
* Create an instance from just a date. The time portion is set to 00:00:00.000000.
*
* @param int|null $year The year to create an instance with.
* @param int|null $month The month to create an instance with.
Expand All @@ -609,7 +603,7 @@ public static function createFromDate(
?int $day = null,
DateTimeZone|string|null $timezone = null
): static {
return static::create($year, $month, $day, null, null, null, null, $timezone);
return static::create($year, $month, $day, timezone: $timezone);
}

/**
Expand All @@ -623,10 +617,10 @@ public static function createFromDate(
* @return static
*/
public static function createFromTime(
?int $hour = null,
?int $minute = null,
?int $second = null,
?int $microsecond = null,
?int $hour = 0,
?int $minute = 0,
?int $second = 0,
?int $microsecond = 0,
DateTimeZone|string|null $timezone = null
): static {
return static::create(null, null, null, $hour, $minute, $second, $microsecond, $timezone);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/DateTime/CreateFromDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CreateFromDateTest extends TestCase
public function testCreateFromDateWithDefaults()
{
$d = Chronos::createFromDate();
$this->assertSame($d->timestamp, Chronos::create(null, null, null, null, null, null, null)->timestamp);
$this->assertSame($d->timestamp, Chronos::create(null, null, null)->timestamp);
}

public function testCreateFromDate()
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/DateTime/CreateFromTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CreateFromTimeTest extends TestCase
public function testCreateFromDateWithDefaults()
{
$d = Chronos::createFromTime();
$this->assertSame($d->timestamp, Chronos::create(null, null, null, null, null, null)->timestamp);
$this->assertSame($d->timestamp, Chronos::create(null, null, null)->timestamp);
}

public function testCreateFromDate()
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase/DateTime/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public function testCreateReturnsDatingInstance()
public function testCreateWithDefaults()
{
$d = Chronos::create();
$this->assertSame($d->timestamp, Chronos::now()->timestamp);
$this->assertSame($d->timestamp, Chronos::now()->setTime(0, 0)->timestamp);

$d = Chronos::create(2023, 1, 1, minute: 1);
$this->assertSame($d->timestamp, Chronos::parse('2023-01-01 00:01:00')->timestamp);
}

public function testCreateWithYear()
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/DateTime/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function testDiffInDaysFilteredNegativeNoSignWithSecondObject()

public function testDiffInDaysFilteredNegativeWithSignWithMutated()
{
$dt = Chronos::createFromDate(2000, 1, 31);
$dt = Chronos::create(2000, 1, 31, hour: 12);
$this->assertSame(-5, $dt->diffInDaysFiltered(function ($date) {
return $date->dayOfWeek === 1;
}, $dt->startOfMonth(), false));
Expand Down Expand Up @@ -330,13 +330,13 @@ public function testDiffInWeekdaysPositive()

public function testDiffInWeekdaysNegativeNoSign()
{
$dt = Chronos::createFromDate(2000, 1, 31);
$dt = Chronos::create(2000, 1, 31, hour: 12);
$this->assertSame(21, $dt->diffInWeekdays($dt->startOfMonth()));
}

public function testDiffInWeekdaysNegativeWithSign()
{
$dt = Chronos::createFromDate(2000, 1, 31);
$dt = Chronos::create(2000, 1, 31, hour: 12);
$this->assertSame(-21, $dt->diffInWeekdays($dt->startOfMonth(), false));
}

Expand Down

0 comments on commit cc941e2

Please sign in to comment.