Skip to content

Commit

Permalink
Implement shiftTimezone
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Aug 13, 2018
1 parent 446bb0e commit 99976db
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,8 @@ public static function setWeekendDays($days);

public function settings(array $settings);

public function shiftTimezone($value);

public static function shouldOverflowMonths();

public static function shouldOverflowYears();
Expand Down
15 changes: 15 additions & 0 deletions src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,21 @@ public function setTimezone($value)
return $date;
}

/**
* Set the instance's timezone from a string or object and add/subtract the offset difference.
*
* @param \DateTimeZone|string $value
*
* @return static
*/
public function shiftTimezone($value)
{
$offset = $this->offset;
$date = $this->setTimezone($value);

return $date->addRealSeconds($offset - $date->offset);
}

/**
* Set the instance's timezone to UTC.
*
Expand Down
13 changes: 10 additions & 3 deletions src/Carbon/Traits/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@

namespace Carbon\Traits;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;

/**
* Trait Options.
*
* Embed base methods to change settings of Carbon classes.
*
* Depends on the following methods:
*
* @method static Carbon|CarbonImmutable setTimezone
*/
trait Options
{
Expand Down Expand Up @@ -287,13 +293,14 @@ public function settings(array $settings)
$this->localMonthsOverflow = $settings['monthOverflow'] ?? null;
$this->localYearsOverflow = $settings['yearOverflow'] ?? null;
$this->localHumanDiffOptions = $settings['humanDiffOptions'] ?? null;
$date = $this;
if (isset($settings['locale'])) {
$this->locale($settings['locale']);
$date = $date->locale($settings['locale']);
}
if (isset($settings['timezone'])) {
$this->setTimezone($settings['timezone']);
$date = $date->shiftTimezone($settings['timezone']);
}

return $this;
return $date;
}
}
14 changes: 14 additions & 0 deletions tests/Carbon/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,20 @@ public function testSetTimezoneUsingString()
$this->assertSame('America/Toronto', $d->tzName);
}

public function testShiftTimezone()
{
$d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
$d2 = $d->copy()->setTimezone('America/Toronto');
$this->assertSame(0, $d2->getTimestamp() - $d->getTimestamp());
$this->assertSame('04:53:12', $d2->format('H:i:s'));

$d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
$d2 = $d->copy()->shiftTimezone('America/Toronto');
$this->assertSame(21600, $d2->getTimestamp() - $d->getTimestamp());
$this->assertSame('America/Toronto', $d2->tzName);
$this->assertSame('10:53:12', $d2->format('H:i:s'));
}

public function testTimezoneUsingString()
{
$d = Carbon::now();
Expand Down
8 changes: 4 additions & 4 deletions tests/Carbon/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ class SettingsTest extends AbstractTestCase
{
public function testSettings()
{
$paris = Carbon::parse('2018-01-31')->settings([
$paris = Carbon::parse('2018-01-31 00:00:00')->settings([
'timezone' => 'Europe/Paris',
'locale' => 'fr_FR',
'monthOverflow' => true,
'yearOverflow' => true,
]);
$chicago = Carbon::parse('2018-01-31')->settings([
$saoPaulo = Carbon::parse('2018-01-31 00:00:00')->settings([
'timezone' => 'America/Sao_Paulo',
'locale' => 'pt',
'monthOverflow' => false,
'yearOverflow' => false,
]);

$this->assertSame('un jour 19 heures avant', $paris->addMonth()->from(Carbon::parse('2018-03-05', 'UTC'), null, false, 3));
$this->assertSame('4 dias 18 horas antes', $chicago->addMonth()->from(Carbon::parse('2018-03-05', 'UTC'), null, false, 3));
$this->assertSame('2 jours une heure avant', $paris->addMonth()->from(Carbon::parse('2018-03-05', 'UTC'), null, false, 3));
$this->assertSame('4 dias 21 horas antes', $saoPaulo->addMonth()->from(Carbon::parse('2018-03-05', 'UTC'), null, false, 3));
}
}
14 changes: 14 additions & 0 deletions tests/CarbonImmutable/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ public function testSetTimezoneUsingString()
$this->assertSame('America/Toronto', $d->tzName);
}

public function testShiftTimezone()
{
$d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
$d2 = $d->copy()->setTimezone('America/Toronto');
$this->assertSame(0, $d2->getTimestamp() - $d->getTimestamp());
$this->assertSame('04:53:12', $d2->format('H:i:s'));

$d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
$d2 = $d->copy()->shiftTimezone('America/Toronto');
$this->assertSame(21600, $d2->getTimestamp() - $d->getTimestamp());
$this->assertSame('America/Toronto', $d2->tzName);
$this->assertSame('10:53:12', $d2->format('H:i:s'));
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Carbon\CarbonImmutable class is immutable.
Expand Down
8 changes: 4 additions & 4 deletions tests/CarbonImmutable/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ class SettingsTest extends AbstractTestCase
{
public function testSettings()
{
$paris = Carbon::parse('2018-01-31')->settings([
$paris = Carbon::parse('2018-01-31 00:00:00')->settings([
'timezone' => 'Europe/Paris',
'locale' => 'fr_FR',
'monthOverflow' => true,
'yearOverflow' => true,
]);
$chicago = Carbon::parse('2018-01-31')->settings([
$saoPaulo = Carbon::parse('2018-01-31 00:00:00')->settings([
'timezone' => 'America/Sao_Paulo',
'locale' => 'pt',
'monthOverflow' => false,
'yearOverflow' => false,
]);

$this->assertSame('un jour 19 heures avant', $paris->addMonth()->from(Carbon::parse('2018-03-05', 'UTC'), null, false, 3));
$this->assertSame('4 dias 18 horas antes', $chicago->addMonth()->from(Carbon::parse('2018-03-05', 'UTC'), null, false, 3));
$this->assertSame('2 jours une heure avant', $paris->addMonth()->from(Carbon::parse('2018-03-05', 'UTC'), null, false, 3));
$this->assertSame('4 dias 21 horas antes', $saoPaulo->addMonth()->from(Carbon::parse('2018-03-05', 'UTC'), null, false, 3));
}
}

0 comments on commit 99976db

Please sign in to comment.