Skip to content

Commit

Permalink
Fix briannesbitt#1073 Handle microseconds for locales with coma decim…
Browse files Browse the repository at this point in the history
…al separator

Work-around for bug https://bugs.php.net/bug.php?id=67127
  • Loading branch information
kylekatarnls committed Feb 27, 2018
1 parent ba61af0 commit 0d5a4bf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Carbon/Carbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,16 @@ public function __construct($time = null, $tz = null)
$time = $testInstance->format(static::MOCK_DATETIME_FORMAT);
}

// Work-around for PHP bug https://bugs.php.net/bug.php?id=67127
$comaSeparator = strpos((string) .1, '.') === false;
if ($comaSeparator) {
$locale = setlocale(LC_NUMERIC, '0');
setlocale(LC_NUMERIC, 'C');
}
parent::__construct($time, static::safeCreateDateTimeZone($tz));
if ($comaSeparator) {
setlocale(LC_NUMERIC, $locale);
}
static::setLastErrors(parent::getLastErrors());
}

Expand Down Expand Up @@ -578,11 +587,11 @@ public static function createFromTime($hour = null, $minute = null, $second = nu
public static function createFromFormat($format, $time, $tz = null)
{
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
} else {
$dt = parent::createFromFormat($format, $time);
$tz = static::safeCreateDateTimeZone($tz);
}

$dt = parent::createFromFormat($format, $time, $tz);

$lastErrors = parent::getLastErrors();

if ($dt instanceof DateTime) {
Expand Down Expand Up @@ -639,7 +648,7 @@ public static function createFromTimestamp($timestamp, $tz = null)
public static function createFromTimestampMs($timestamp, $tz = null)
{
return static::createFromFormat('U.u', sprintf('%F', $timestamp / 1000))
->setTimezone(static::safeCreateDateTimeZone($tz));
->setTimezone($tz);
}

/**
Expand Down Expand Up @@ -2581,27 +2590,27 @@ public function addSecond($value = 1)
}

/**
* Remove a second from the instance
* Remove seconds from the instance
*
* @param int $value
*
* @return static
*/
public function subSecond($value = 1)
public function subSeconds($value)
{
return $this->subSeconds($value);
return $this->addSeconds(-1 * $value);
}

/**
* Remove seconds from the instance
* Remove a second from the instance
*
* @param int $value
*
* @return static
*/
public function subSeconds($value)
public function subSecond($value = 1)
{
return $this->addSeconds(-1 * $value);
return $this->subSeconds($value);
}

///////////////////////////////////////////////////////////////////
Expand Down
30 changes: 30 additions & 0 deletions tests/Carbon/CreateFromTimestampTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ public function testCreateFromTimestampMS()
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);
}

public function testComaDecimalSeparatorLocale()
{
$date = new Carbon('2017-07-29T13:57:27.123456Z');
$this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));

$date = Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
$this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
$timestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000 + 321;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);

$locale = setlocale(LC_ALL, '0');
setlocale(LC_ALL, 'fr');

$timestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000 + 321;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);

$date = new Carbon('2017-07-29T13:57:27.123456Z');
$this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));

$date = Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
$this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
$timestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000 + 321;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);

setlocale(LC_ALL, $locale);
}

public function testCreateFromTimestampUsesDefaultTimezone()
{
$d = Carbon::createFromTimestamp(0);
Expand Down

0 comments on commit 0d5a4bf

Please sign in to comment.