Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
fre5h committed Sep 11, 2024
1 parent 9beb382 commit 1ccc31c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ use Fresh\DateTime\DateTimeHelper;
$dateTimeHelper = new DateTimeHelper();

$now1 = $dateTimeHelper->getCurrentDatetime();
$now2 = $dateTimeHelper->getCurrentDatetime(new \DateTimeZone('Europe/Kiev')); // Or with custom timezone
$now2 = $dateTimeHelper->getCurrentDatetime(new \DateTimeZone('Europe/Kyiv')); // Or with custom timezone
$now3 = $dateTimeHelper->getCurrentDatetimeUtc(); // Always in UTC
$now4 = $dateTimeHelper->getCurrentDatetimeImmutable();
$now5 = $dateTimeHelper->getCurrentDatetimeImmutable(new \DateTimeZone('Europe/Kiev')); // Or with custom timezone
$now5 = $dateTimeHelper->getCurrentDatetimeImmutable(new \DateTimeZone('Europe/Kyiv')); // Or with custom timezone
$now6 = $dateTimeHelper->getCurrentDatetimeImmutableUtc(); // Always in UTC
```

Expand All @@ -69,6 +69,24 @@ $timestamp = $dateTimeHelper->getCurrentTimestamp();

```

### Method for creation \DateTime or \DateTimeImmutable from format

```php
use Fresh\DateTime\DateTimeHelper;

$dateTimeHelper = new DateTimeHelper();

// By default with format Y-m-d H:i:s and UTC timezone
$dateTime = $dateTimeHelper->createDateTimeFromFormat('2000-01-01 00:00:00');
$dateTimeImmutable = $dateTimeHelper->createDateTimeImmutableFromFormat('2000-01-01 00:00:00');

// With custom format
$dateTime = $dateTimeHelper->createDateTimeFromFormat('01.01.2000 00:00:00', 'd.m.Y H:i:s');

// With custom timezone
$dateTime = $dateTimeHelper->createDateTimeFromFormat('01.01.2000 00:00:00', 'd.m.Y H:i:s', new \DateTimeZone('Europe/Kyiv'));
```

### Method for creating `\DateTimeZone` object

If you create a `\DateTimeZone` object directly in your code, you will not be able to mock it in tests.
Expand All @@ -80,7 +98,7 @@ use Fresh\DateTime\DateTimeHelper;
$dateTimeHelper = new DateTimeHelper();

$dateTimeZone1 = $dateTimeHelper->createDateTimeZone(); // UTC by default
$dateTimeZone2 = $dateTimeHelper->createDateTimeZone('Europe/Kiev'); // Or with custom timezone
$dateTimeZone2 = $dateTimeHelper->createDateTimeZone('Europe/Kyiv'); // Or with custom timezone
$dateTimeZone3 = $dateTimeHelper->createDateTimeZoneUtc(); // Another method to get UTC timezone
```

Expand All @@ -95,7 +113,7 @@ This library provides a `DateRange` immutable class, which is not able to be cha
use Fresh\DateTime\DateRange;

$dateRange1 = new DateRange(new \DateTime('yesterday'), new \DateTime('tomorrow'));
$dateRange2 = new DateRange(new \DateTime('yesterday'), new \DateTime('tomorrow', new \DateTimeZone('Europe/Kiev')));
$dateRange2 = new DateRange(new \DateTime('yesterday'), new \DateTime('tomorrow', new \DateTimeZone('Europe/Kyiv')));

// There is also the `isEqual` method to compare two DateRange objects.
$dateRange1->isEqual($dateRange2); // Returns FALSE, because date ranges have different timezones
Expand All @@ -109,7 +127,7 @@ This library provides also immutable class `DateTimeRange`, instead of `DateRan
use Fresh\DateTime\DateTimeRange;

$dateTimeRange1 = new DateTimeRange(new \DateTime('2000-01-01 19:00:00'), new \DateTime('2000-01-01 21:00:00'));
$dateTimeRange2 = new DateTimeRange(new \DateTime('2000-01-01 19:00:00'), new \DateTime('2000-01-01 21:00:00', new \DateTimeZone('Europe/Kiev')));
$dateTimeRange2 = new DateTimeRange(new \DateTime('2000-01-01 19:00:00'), new \DateTime('2000-01-01 21:00:00', new \DateTimeZone('Europe/Kyiv')));
$dateTimeRange3 = new DateTimeRange(new \DateTime('2000-01-01 20:00:00'), new \DateTime('2000-01-01 22:00:00'));

// There is also the `isEqual` method to compare two DateTimeRange objects.
Expand Down
6 changes: 3 additions & 3 deletions tests/DateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function constructor(): void
#[Test]
public function constructorWithExceptionForDifferentTimezones(): void
{
$since = new \DateTime('now', new \DateTimeZone('Europe/Kiev'));
$since = new \DateTime('now', new \DateTimeZone('Europe/Kyiv'));
$till = new \DateTime('now', new \DateTimeZone('Europe/Warsaw'));

$this->expectException(LogicException::class);
Expand Down Expand Up @@ -76,8 +76,8 @@ public function isEqual(): void
new \DateTime('now', new \DateTimeZone('UTC'))
);
$dateRange6 = new DateRange(
new \DateTime('now', new \DateTimeZone('Europe/Kiev')),
new \DateTime('tomorrow', new \DateTimeZone('Europe/Kiev'))
new \DateTime('now', new \DateTimeZone('Europe/Kyiv')),
new \DateTime('tomorrow', new \DateTimeZone('Europe/Kyiv'))
);

$this->assertTrue($dateRange1->isEqual($dateRange2), 'Since/till timezones are same, time is same');
Expand Down
4 changes: 2 additions & 2 deletions tests/DateTimeClonerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DateTimeClonerTest extends TestCase
#[Test]
public function cloneIntoDateTime(): void
{
$datetime = new \DateTime('now', new \DateTimeZone('Europe/Kiev'));
$datetime = new \DateTime('now', new \DateTimeZone('Europe/Kyiv'));
$clonedDateTime = DateTimeCloner::cloneIntoDateTime($datetime);

$this->assertInstanceOf(\DateTime::class, $clonedDateTime);
Expand Down Expand Up @@ -59,7 +59,7 @@ public function coneIntoDateTimeWithException(): void
#[Test]
public function cloneIntoDateTimeImmutable(): void
{
$datetime = new \DateTime('now', new \DateTimeZone('Europe/Kiev'));
$datetime = new \DateTime('now', new \DateTimeZone('Europe/Kyiv'));
$cloneIntoDateTimeImmutable = DateTimeCloner::cloneIntoDateTimeImmutable($datetime);

$this->assertInstanceOf(\DateTimeImmutable::class, $cloneIntoDateTimeImmutable);
Expand Down
10 changes: 5 additions & 5 deletions tests/DateTimeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function createDateTimeZone(): void
$this->assertInstanceOf(\DateTimeZone::class, $timezone);
$this->assertSame('UTC', $timezone->getName());

$timezone = $this->dateTimeHelper->createDateTimeZone('Europe/Kiev');
$timezone = $this->dateTimeHelper->createDateTimeZone('Europe/Kyiv');
$this->assertInstanceOf(\DateTimeZone::class, $timezone);
}

Expand Down Expand Up @@ -89,9 +89,9 @@ public function getCurrentDatetime(): void
$now = $this->dateTimeHelper->getCurrentDatetime();
$this->assertInstanceOf(\DateTime::class, $now);

$now = $this->dateTimeHelper->getCurrentDatetime(new \DateTimeZone('Europe/Kiev'));
$now = $this->dateTimeHelper->getCurrentDatetime(new \DateTimeZone('Europe/Kyiv'));
$this->assertInstanceOf(\DateTime::class, $now);
$this->assertSame('Europe/Kiev', $now->getTimezone()->getName());
$this->assertSame('Europe/Kyiv', $now->getTimezone()->getName());
}

#[Test]
Expand All @@ -108,9 +108,9 @@ public function getCurrentDatetimeImmutable(): void
$now = $this->dateTimeHelper->getCurrentDatetimeImmutable();
$this->assertInstanceOf(\DateTimeImmutable::class, $now);

$now = $this->dateTimeHelper->getCurrentDatetimeImmutable(new \DateTimeZone('Europe/Kiev'));
$now = $this->dateTimeHelper->getCurrentDatetimeImmutable(new \DateTimeZone('Europe/Kyiv'));
$this->assertInstanceOf(\DateTimeImmutable::class, $now);
$this->assertSame('Europe/Kiev', $now->getTimezone()->getName());
$this->assertSame('Europe/Kyiv', $now->getTimezone()->getName());
}

#[Test]
Expand Down
22 changes: 11 additions & 11 deletions tests/DateTimeRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function constructor(): void
#[Test]
public function constructorWithExceptionForDifferentTimezones(): void
{
$since = new \DateTime('now', new \DateTimeZone('Europe/Kiev'));
$since = new \DateTime('now', new \DateTimeZone('Europe/Kyiv'));
$till = new \DateTime('now', new \DateTimeZone('Europe/Warsaw'));

$this->expectException(LogicException::class);
Expand All @@ -56,23 +56,23 @@ public function constructorWithExceptionForDifferentTimezones(): void
#[Test]
public function getTimezone(): void
{
$since = new \DateTime('now', new \DateTimeZone('Europe/Kiev'));
$till = new \DateTime('now', new \DateTimeZone('Europe/Kiev'));
$since = new \DateTime('now', new \DateTimeZone('Europe/Kyiv'));
$till = new \DateTime('now', new \DateTimeZone('Europe/Kyiv'));

$dateTimeRange = new DateTimeRange($since, $till);

$this->assertEquals('Europe/Kiev', $dateTimeRange->getTimezone()->getName());
$this->assertEquals('Europe/Kyiv', $dateTimeRange->getTimezone()->getName());
}

#[Test]
public function getTimezoneName(): void
{
$since = new \DateTime('now', new \DateTimeZone('Europe/Kiev'));
$till = new \DateTime('now', new \DateTimeZone('Europe/Kiev'));
$since = new \DateTime('now', new \DateTimeZone('Europe/Kyiv'));
$till = new \DateTime('now', new \DateTimeZone('Europe/Kyiv'));

$dateTimeRange = new DateTimeRange($since, $till);

$this->assertEquals('Europe/Kiev', $dateTimeRange->getTimezoneName());
$this->assertEquals('Europe/Kyiv', $dateTimeRange->getTimezoneName());
}

#[Test]
Expand All @@ -99,8 +99,8 @@ public function isEqual(): void
new \DateTime('2000-01-01 21:00:00', new \DateTimeZone('UTC'))
);
$dateTimeRange6 = new DateTimeRange(
new \DateTime('2000-01-01 14:45:00', new \DateTimeZone('Europe/Kiev')),
new \DateTime('2000-01-01 15:00:00', new \DateTimeZone('Europe/Kiev'))
new \DateTime('2000-01-01 14:45:00', new \DateTimeZone('Europe/Kyiv')),
new \DateTime('2000-01-01 15:00:00', new \DateTimeZone('Europe/Kyiv'))
);

$this->assertTrue($dateTimeRange1->isEqual($dateTimeRange2), 'Since/till timezones are same, time is same');
Expand Down Expand Up @@ -372,8 +372,8 @@ public function intersectsWithExceptionForDifferentTimezones(): void
new \DateTime('2000-01-01 21:00:00', new \DateTimeZone('UTC'))
);
$dateRange2 = new DateTimeRange(
new \DateTime('2000-01-01 21:00:00', new \DateTimeZone('Europe/Kiev')),
new \DateTime('2000-01-01 23:00:00', new \DateTimeZone('Europe/Kiev'))
new \DateTime('2000-01-01 21:00:00', new \DateTimeZone('Europe/Kyiv')),
new \DateTime('2000-01-01 23:00:00', new \DateTimeZone('Europe/Kyiv'))
);

$this->expectException(LogicException::class);
Expand Down

0 comments on commit 1ccc31c

Please sign in to comment.