Skip to content

Commit

Permalink
Implement createFromTimeString briannesbitt#919
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Mar 13, 2018
1 parent d12da8c commit 2c4b597
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Carbon/Carbon.php
Expand Up @@ -699,6 +699,21 @@ public static function createFromTime($hour = null, $minute = null, $second = nu
return static::create(null, null, null, $hour, $minute, $second, $tz);
}

/**
* Create a Carbon instance from a time string. The date portion is set to today.
*
* @param string $time
* @param \DateTimeZone|string|null $tz
*
* @throws \InvalidArgumentException
*
* @return static
*/
public static function createFromTimeString($time, $tz = null)
{
return static::today($tz)->setTimeFromTimeString($time);
}

/**
* Create a Carbon instance from a specific format.
*
Expand Down Expand Up @@ -760,7 +775,7 @@ public static function getLastErrors()
*/
public static function createFromTimestamp($timestamp, $tz = null)
{
return static::now($tz)->setTimestamp($timestamp);
return static::today($tz)->setTimestamp($timestamp);
}

/**
Expand Down Expand Up @@ -1080,13 +1095,11 @@ public function setDateTime($year, $month, $day, $hour, $minute, $second = 0)
*/
public function setTimeFromTimeString($time)
{
$time = explode(':', $time);

$hour = $time[0];
$minute = isset($time[1]) ? $time[1] : 0;
$second = isset($time[2]) ? $time[2] : 0;
if (strpos($time, ':') === false) {
$time .= ':0';
}

return $this->setTime($hour, $minute, $second);
return $this->modify($time);
}

/**
Expand Down
64 changes: 64 additions & 0 deletions tests/Carbon/CreateFromTimeStringTest.php
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\Carbon;

use Carbon\Carbon;
use DateTimeZone;
use Tests\AbstractTestCase;

class CreateFromTimeStringTest extends AbstractTestCase
{
public function testCreateFromTimeString()
{
$d = Carbon::createFromTimeString('22:45');
$this->assertSame(22, $d->hour);
$this->assertSame(45, $d->minute);
$this->assertSame(0, $d->second);
$this->assertSame(0, $d->micro);
}

public function testCreateFromTimeStringWithSecond()
{
$d = Carbon::createFromTimeString('22:45:12');
$this->assertSame(22, $d->hour);
$this->assertSame(45, $d->minute);
$this->assertSame(12, $d->second);
$this->assertSame(0, $d->micro);
}

public function testCreateFromTimeStringWithMicroSecond()
{
if (version_compare(PHP_VERSION, '7.1.0-dev', '<')) {
$this->markTestSkipped();
}

$d = Carbon::createFromTimeString('22:45:00.625341');
$this->assertSame(22, $d->hour);
$this->assertSame(45, $d->minute);
$this->assertSame(0, $d->second);
$this->assertSame(625341, $d->micro);
}

public function testCreateFromTimeStringWithDateTimeZone()
{
$d = Carbon::createFromTimeString('12:20:30', new DateTimeZone('Europe/London'));
$this->assertCarbon($d, Carbon::now()->year, Carbon::now()->month, Carbon::now()->day, 12, 20, 30, 0);
$this->assertSame('Europe/London', $d->tzName);
}

public function testCreateFromTimeStringWithTimeZoneString()
{
$d = Carbon::createFromTimeString('12:20:30', 'Europe/London');
$this->assertCarbon($d, Carbon::now()->year, Carbon::now()->month, Carbon::now()->day, 12, 20, 30, 0);
$this->assertSame('Europe/London', $d->tzName);
}
}
3 changes: 3 additions & 0 deletions tests/Carbon/SettersTest.php
Expand Up @@ -299,6 +299,9 @@ public function dataProviderTestSetTimeFromTimeString()
array(9, 15, 30, '09:15:30'),
array(9, 15, 0, '09:15'),
array(9, 0, 0, '09'),
array(9, 5, 3, '9:5:3'),
array(9, 5, 0, '9:5'),
array(9, 0, 0, '9'),
);
}

Expand Down

0 comments on commit 2c4b597

Please sign in to comment.