Skip to content

Commit

Permalink
Add PSR-20 (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Henvald <[email protected]>
  • Loading branch information
fre5h and Artem Henvald authored May 16, 2023
1 parent 39fd4b8 commit 2ea8119
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ jobs:
fail-fast: false
matrix:
php-version:
- '8.1'
- '8.2'
steps:
- name: 'Checkout Code'
Expand Down
2 changes: 1 addition & 1 deletion .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
preset: recommended

version: 8.1
version: 8.2

enabled:
- combine_consecutive_unsets
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PHP library that provides additional functions for processing dates & times.

## Requirements

* PHP 8.1
* PHP 8.2

## Installation 🌱

Expand Down Expand Up @@ -48,6 +48,16 @@ $now5 = $dateTimeHelper->getCurrentDatetimeImmutable(new \DateTimeZone('Europe/K
$now6 = $dateTimeHelper->getCurrentDatetimeImmutableUtc(); // Always in UTC
```

Compatible with [PSR-20: Clock](https://www.php-fig.org/psr/psr-20/).

```php
use Fresh\DateTime\DateTimeHelper;

$dateTimeHelper = new DateTimeHelper();

$now = $dateTimeHelper->now(); // \DateTimeImmutable in UTC
```

### Method for getting current timestamp

```php
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
"issues": "https://github.com/fre5h/datetime-php/issues"
},
"require": {
"php": ">=8.1"
"php": ">=8.2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.3",
"phpstan/phpstan": "^1.3",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3.16",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^10.1",
"psr/clock": "^1.0",
"slam/phpstan-extensions": "^6.0",
"squizlabs/php_codesniffer": "^3.6",
"squizlabs/php_codesniffer": "^3.7",
"thecodingmachine/phpstan-strict-rules": "^1.0"
},
"autoload": {
Expand Down
7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/10.1/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
>
Expand All @@ -11,12 +11,15 @@
</testsuite>
</testsuites>

<coverage includeUncoveredFiles="false">
<source>
<include>
<directory suffix=".php">./src/</directory>
</include>
<exclude>
<file>./src/TimeConstants.php</file>
</exclude>
</source>

<coverage includeUncoveredFiles="false">
</coverage>
</phpunit>
12 changes: 11 additions & 1 deletion src/DateTimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

namespace Fresh\DateTime;

use Psr\Clock\ClockInterface;

/**
* DateTimeHelper.
*
* @author Artem Henvald <[email protected]>
*/
class DateTimeHelper implements DateTimeHelperInterface
class DateTimeHelper implements ClockInterface, DateTimeHelperInterface
{
private const INTERNAL_DATE_FORMAT = 'Y-m-d';

Expand All @@ -26,6 +28,14 @@ class DateTimeHelper implements DateTimeHelperInterface

private ?\DateTimeZone $timeZoneUtc = null;

/**
* {@inheritdoc}
*/
public function now(): \DateTimeImmutable
{
return $this->getCurrentDatetimeImmutableUtc();
}

/**
* {@inheritdoc}
*/
Expand Down
11 changes: 8 additions & 3 deletions tests/DateTimeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
*/
class DateTimeHelperTest extends TestCase
{
/** @var DateRangeInterface|MockObject */
private $dateRange;

private DateRangeInterface|MockObject $dateRange;
private DateTimeHelper $dateTimeHelper;

protected function setUp(): void
Expand Down Expand Up @@ -105,6 +103,13 @@ public function testGetCurrentDatetimeImmutable(): void
self::assertSame('Europe/Kiev', $now->getTimezone()->getName());
}

public function testNow(): void
{
$now = $this->dateTimeHelper->now();
self::assertInstanceOf(\DateTimeImmutable::class, $now);
self::assertSame('UTC', $now->getTimezone()->getName());
}

public function testGetCurrentDatetimeImmutableUtc(): void
{
$now = $this->dateTimeHelper->getCurrentDatetimeImmutableUtc();
Expand Down

0 comments on commit 2ea8119

Please sign in to comment.