Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inability to express a positive offset of less than an hour #13

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- `Innmind\TimeContinuum\Period::millisecond()` named constructor
- `Innmind\TimeContinuum\PointInTime::microsecond()`
- `Innmind\TimeContinuum\Clock::ofFormat()`
- `Innmind\TimeContinuum\Offset::plus()`
- `Innmind\TimeContinuum\Offset::minus()`

### Changed

Expand Down Expand Up @@ -69,6 +71,7 @@
- `Innmind\TimeContinuum\ElapsedPeriod::maybe()`
- `Innmind\TimeContinuum\ElapsedPeriod::ofPeriod()`
- `Innmind\TimeContinuum\PointInTime::milliseconds()`
- `Innmind\TimeContinuum\Offset::of()`

## 3.4.1 - 2023-09-17

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use Innmind\TimeContinuum\{

function foo(PointInTime $point)
{
$point = $point->changeOffset(Offset::of(1, 0));
$point = $point->changeOffset(Offset::plus(1, 0));
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/time-offsets.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use Innmind\TimeContinuum\{
};

$utc = Clock::live()->now();
$newYork = $utc->changeOffset(Offset::of(-5));
$newYork = $utc->changeOffset(Offset::minus(5));
```

If `#!php $utc` represents `#!php '2024-11-24T14:25:00+00:00'` then `#!php $newYork` represents `#!php '2024-11-24T09:25:00-05:00'`.
2 changes: 1 addition & 1 deletion docs/preface/terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Due to politics a city may change its offset at any time (1). And for economic r
1. This means some points in time don't exist in certain countries or exist twice.
2. Usually they increase/decrease their offset by 1 or 2 hours.

Because asking the offset for a timezone will yeild different results depending on when you ask for it, it's part of the world that changes. This means that timezones are handled at the [clock](#clock) level.
Because asking the offset for a timezone will yield different results depending on when you ask for it, it's part of the world that changes. This means that timezones are handled at the [clock](#clock) level.

## Period

Expand Down
6 changes: 3 additions & 3 deletions docs/upgrade/v3-to-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ These are the main changes, for an extensive list of changes go to the [changelo
$clock->now()->timezone()->minutes();
```

=== "Before"
=== "After"
```php
$clock->now()->offset()->hours();
$clock->now()->offset()->minutes();
Expand All @@ -94,9 +94,9 @@ These are the main changes, for an extensive list of changes go to the [changelo
$clock->now()->changeTimezone(new Earth\Timezone\UTC(2, 0));
```

=== "Before"
=== "After"
```php
$clock->now()->changeOffset(Offset::of(2, 0));
$clock->now()->changeOffset(Offset::plus(2, 0));
```

## Periods
Expand Down
19 changes: 15 additions & 4 deletions src/Offset.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@ private function __construct(
*/
public static function utc(): self
{
return self::of(0, 0);
return self::plus(0, 0);
}

/**
* @psalm-pure
*
* @param int<-12, 14> $hours
* @param int<0, 14> $hours
* @param int<0, 59> $minutes
*/
public static function plus(int $hours, int $minutes = 0): self
{
return new self($hours, $minutes, true);
}

/**
* @psalm-pure
*
* @param int<0, 12> $hours
* @param int<0, 59> $minutes
*/
public static function of(int $hours, int $minutes = 0): self
public static function minus(int $hours, int $minutes = 0): self
{
return new self($hours, $minutes, $hours > 0);
return new self(-$hours, $minutes, false);
}

/**
Expand Down
1 change: 0 additions & 1 deletion tests/ClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function testSwitchTimezone($switch)
$live = Clock::live();
$clock = $live->switch($switch);

$this->assertNotEquals($live, $clock);
$this->assertIsString($clock->now()->format(Format::iso8601()));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/NowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testChangeOffset()
$now = new \DateTimeImmutable;
$now = $now->setTimezone(new \DateTimeZone('-02:30'));
$point = PointInTime::now();
$point2 = $point->changeOffset(Offset::of(-2, 30));
$point2 = $point->changeOffset(Offset::minus(2, 30));

$this->assertNotSame($point, $point2);
$this->assertNotSame($point->year(), $point2->year());
Expand Down
2 changes: 1 addition & 1 deletion tests/PointInTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testFormat()
public function testChangeOffset()
{
$point = PointInTime::at(new \DateTimeImmutable('2016-10-05T08:01:30.123+02:00'));
$point2 = $point->changeOffset(Offset::of(-2, 30));
$point2 = $point->changeOffset(Offset::minus(2, 30));

$this->assertNotSame($point, $point2);
$this->assertNotSame($point->year(), $point2->year());
Expand Down