From f6863ddcbcedbe522e5bcaedfc5f41370a067c4e Mon Sep 17 00:00:00 2001 From: Remco Tolsma <869674+remcotolsma@users.noreply.github.com> Date: Tue, 19 Oct 2021 15:41:40 +0200 Subject: [PATCH 1/8] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index faca94f..2010083 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,5 @@ Source: https://developer.wordpress.org/reference/functions/date_i18n/#comment-2 * https://github.com/woocommerce/woocommerce/blob/3.3.5/includes/class-wc-datetime.php * https://github.com/Rarst/wpdatetime + +[![Pronamic - Work with us](https://github.com/pronamic/brand-resources/blob/main/banners/pronamic-work-with-us-leaderboard-728x90%404x.png)](https://www.pronamic.eu/contact/) From af2d50d96dee75c00072c46e50e20d3048137e6f Mon Sep 17 00:00:00 2001 From: Remco Tolsma <869674+remcotolsma@users.noreply.github.com> Date: Thu, 4 Nov 2021 12:03:26 +0100 Subject: [PATCH 2/8] Introduce `create_from_interface( \DateTimeInterface $object )` method. --- src/DateTime.php | 25 ++++++++++++++++------- src/DateTimeImmutable.php | 25 ++++++++++++++++------- tests/src/DateTimeTest.php | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/src/DateTime.php b/src/DateTime.php index a8598fb..17c3581 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -31,19 +31,30 @@ class DateTime extends \DateTime implements DateTimeInterface { * @return self */ public static function createFromImmutable( $object ) { - return self::create_from_immutable( $object ); + return self::create_from_interface( $object ); } /** - * Create from immutable. + * Overrides upstream method to correct returned instance type to the inheriting one. + * + * {@inheritdoc} * - * @link https://www.php.net/manual/en/datetimeimmutable.createfrommutable.php - * @param \DateTimeImmutable $object The immutable DateTimeImmutable object that needs to be converted to a mutable version. + * @param \DateTime $object Object. * @return self */ - public static function create_from_immutable( \DateTimeImmutable $object ) { - $instance = new self( '@' . $object->getTimestamp() ); + public static function createFromInterface( \DateTimeInterface $object ) { + return self::create_from_interface( $object ); + } - return $instance->setTimezone( $object->getTimezone() ); + /** + * Create from interface. + * + * @link https://www.php.net/manual/en/datetime.createfrominterface.php + * @link https://php.watch/versions/8.0/datetime-immutable-createfrominterface + * @param \DateTime $object The mutable DateTime object that you want to convert to an immutable version. + * @return self + */ + public static function create_from_interface( \DateTimeInterface $object ) { + return new self( $object->format('Y-m-d H:i:s.u'), $object->getTimezone() ); } } diff --git a/src/DateTimeImmutable.php b/src/DateTimeImmutable.php index 5a25792..f88a619 100644 --- a/src/DateTimeImmutable.php +++ b/src/DateTimeImmutable.php @@ -31,19 +31,30 @@ class DateTimeImmutable extends \DateTimeImmutable implements DateTimeInterface * @return self */ public static function createFromMutable( $object ) { - return self::create_from_mutable( $object ); + return self::create_from_interface( $object ); } /** - * Create from mutable. + * Overrides upstream method to correct returned instance type to the inheriting one. * - * @link https://www.php.net/manual/en/datetimeimmutable.createfrommutable.php - * @param \DateTime $object The mutable DateTime object that you want to convert to an immutable version. + * {@inheritdoc} + * + * @param \DateTime $object Object. * @return self */ - public static function create_from_mutable( \DateTime $object ) { - $instance = new self( '@' . $object->getTimestamp() ); + public static function createFromInterface( \DateTimeInterface $object ) { + return self::create_from_interface( $object ); + } - return $instance->setTimezone( $object->getTimezone() ); + /** + * Create from interface. + * + * @link https://www.php.net/manual/en/datetime.createfrominterface.php + * @link https://php.watch/versions/8.0/datetime-immutable-createfrominterface + * @param \DateTime $object The mutable DateTime object that you want to convert to an immutable version. + * @return self + */ + public static function create_from_interface( \DateTimeInterface $object ) { + return new self( $object->format('Y-m-d H:i:s.u'), $object->getTimezone() ); } } diff --git a/tests/src/DateTimeTest.php b/tests/src/DateTimeTest.php index df0c849..356c54b 100644 --- a/tests/src/DateTimeTest.php +++ b/tests/src/DateTimeTest.php @@ -274,4 +274,46 @@ public function test_backslash_at_end_in_translation() { $this->assertEquals( $expected, \date_i18n( '123 - F - Y', \strtotime( $string ) ) ); } + + /** + * Test create mutable from interface. + */ + public function test_create_mutable_from_interface() { + $date = new \DateTime( '2005-05-05 10:15:00', new \DateTimeZone( 'Europe/Amsterdam' ) ); + + $test = DateTime::create_from_interface( $date ); + + $this->assertInstanceOf( DateTime::class, $test ); + $this->assertEquals( '2005-05-05 10:15:00', $test->format( 'Y-m-d H:i:s' ) ); + $this->assertEquals( 'Europe/Amsterdam', $test->getTimezone()->getName() ); + + $date = new DateTime('15-07-2014 18:30:00.123456', new \DateTimeZone( 'UTC' ) ); + + $test = DateTime::create_from_interface( $date ); + + $this->assertInstanceOf( DateTime::class, $test ); + $this->assertEquals( '2014-07-15 18:30:00.123456', $test->format( 'Y-m-d H:i:s.u' ) ); + $this->assertEquals( 'UTC', $test->getTimezone()->getName() ); + } + + /** + * Test create mutable from interface. + */ + public function test_create_immutable_from_interface() { + $date = new \DateTime( '2005-05-05 10:15:00', new \DateTimeZone( 'Europe/Amsterdam' ) ); + + $test = DateTimeImmutable::create_from_interface( $date ); + + $this->assertInstanceOf( DateTimeImmutable::class, $test ); + $this->assertEquals( '2005-05-05 10:15:00', $test->format( 'Y-m-d H:i:s' ) ); + $this->assertEquals( 'Europe/Amsterdam', $test->getTimezone()->getName() ); + + $date = new DateTime('15-07-2014 18:30:00.123456', new \DateTimeZone( 'UTC' ) ); + + $test = DateTimeImmutable::create_from_interface( $date ); + + $this->assertInstanceOf( DateTimeImmutable::class, $test ); + $this->assertEquals( '2014-07-15 18:30:00.123456', $test->format( 'Y-m-d H:i:s.u' ) ); + $this->assertEquals( 'UTC', $test->getTimezone()->getName() ); + } } From c04f8148f7eac82cffcbb4138c04439b041ee9c0 Mon Sep 17 00:00:00 2001 From: Remco Tolsma <869674+remcotolsma@users.noreply.github.com> Date: Thu, 4 Nov 2021 13:02:16 +0100 Subject: [PATCH 3/8] Improve phpdoc. --- src/DateTime.php | 2 +- src/DateTimeImmutable.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DateTime.php b/src/DateTime.php index 17c3581..5fb14dd 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -51,7 +51,7 @@ public static function createFromInterface( \DateTimeInterface $object ) { * * @link https://www.php.net/manual/en/datetime.createfrominterface.php * @link https://php.watch/versions/8.0/datetime-immutable-createfrominterface - * @param \DateTime $object The mutable DateTime object that you want to convert to an immutable version. + * @param \DateTimeInterface $object The mutable DateTime object that you want to convert to an immutable version. * @return self */ public static function create_from_interface( \DateTimeInterface $object ) { diff --git a/src/DateTimeImmutable.php b/src/DateTimeImmutable.php index f88a619..c213d88 100644 --- a/src/DateTimeImmutable.php +++ b/src/DateTimeImmutable.php @@ -51,7 +51,7 @@ public static function createFromInterface( \DateTimeInterface $object ) { * * @link https://www.php.net/manual/en/datetime.createfrominterface.php * @link https://php.watch/versions/8.0/datetime-immutable-createfrominterface - * @param \DateTime $object The mutable DateTime object that you want to convert to an immutable version. + * @param \DateTimeInterface $object The mutable DateTime object that you want to convert to an immutable version. * @return self */ public static function create_from_interface( \DateTimeInterface $object ) { From ad3a2952843a35a675b7d06eb439abaef3e0949c Mon Sep 17 00:00:00 2001 From: Remco Tolsma <869674+remcotolsma@users.noreply.github.com> Date: Thu, 4 Nov 2021 14:43:27 +0100 Subject: [PATCH 4/8] No longer override `createFromImmutable` and `createFromInterface`. --- src/DateTime.php | 24 ------------------------ src/DateTimeImmutable.php | 24 ------------------------ 2 files changed, 48 deletions(-) diff --git a/src/DateTime.php b/src/DateTime.php index 5fb14dd..9b424bc 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -22,30 +22,6 @@ class DateTime extends \DateTime implements DateTimeInterface { use DateTimeTrait; - /** - * Overrides upstream method to correct returned instance type to the inheriting one. - * - * {@inheritdoc} - * - * @param \DateTimeImmutable $object Object. - * @return self - */ - public static function createFromImmutable( $object ) { - return self::create_from_interface( $object ); - } - - /** - * Overrides upstream method to correct returned instance type to the inheriting one. - * - * {@inheritdoc} - * - * @param \DateTime $object Object. - * @return self - */ - public static function createFromInterface( \DateTimeInterface $object ) { - return self::create_from_interface( $object ); - } - /** * Create from interface. * diff --git a/src/DateTimeImmutable.php b/src/DateTimeImmutable.php index c213d88..b5d3d75 100644 --- a/src/DateTimeImmutable.php +++ b/src/DateTimeImmutable.php @@ -22,30 +22,6 @@ class DateTimeImmutable extends \DateTimeImmutable implements DateTimeInterface { use DateTimeTrait; - /** - * Overrides upstream method to correct returned instance type to the inheriting one. - * - * {@inheritdoc} - * - * @param \DateTime $object Object. - * @return self - */ - public static function createFromMutable( $object ) { - return self::create_from_interface( $object ); - } - - /** - * Overrides upstream method to correct returned instance type to the inheriting one. - * - * {@inheritdoc} - * - * @param \DateTime $object Object. - * @return self - */ - public static function createFromInterface( \DateTimeInterface $object ) { - return self::create_from_interface( $object ); - } - /** * Create from interface. * From 9d75f034a6571d3f664340aaa7d12d94094acc17 Mon Sep 17 00:00:00 2001 From: Remco Tolsma <869674+remcotolsma@users.noreply.github.com> Date: Thu, 4 Nov 2021 14:45:04 +0100 Subject: [PATCH 5/8] DRY. --- src/DateTime.php | 12 ------------ src/DateTimeImmutable.php | 12 ------------ src/DateTimeTrait.php | 12 ++++++++++++ 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/DateTime.php b/src/DateTime.php index 9b424bc..41cd39f 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -21,16 +21,4 @@ */ class DateTime extends \DateTime implements DateTimeInterface { use DateTimeTrait; - - /** - * Create from interface. - * - * @link https://www.php.net/manual/en/datetime.createfrominterface.php - * @link https://php.watch/versions/8.0/datetime-immutable-createfrominterface - * @param \DateTimeInterface $object The mutable DateTime object that you want to convert to an immutable version. - * @return self - */ - public static function create_from_interface( \DateTimeInterface $object ) { - return new self( $object->format('Y-m-d H:i:s.u'), $object->getTimezone() ); - } } diff --git a/src/DateTimeImmutable.php b/src/DateTimeImmutable.php index b5d3d75..86bf5d8 100644 --- a/src/DateTimeImmutable.php +++ b/src/DateTimeImmutable.php @@ -21,16 +21,4 @@ */ class DateTimeImmutable extends \DateTimeImmutable implements DateTimeInterface { use DateTimeTrait; - - /** - * Create from interface. - * - * @link https://www.php.net/manual/en/datetime.createfrominterface.php - * @link https://php.watch/versions/8.0/datetime-immutable-createfrominterface - * @param \DateTimeInterface $object The mutable DateTime object that you want to convert to an immutable version. - * @return self - */ - public static function create_from_interface( \DateTimeInterface $object ) { - return new self( $object->format('Y-m-d H:i:s.u'), $object->getTimezone() ); - } } diff --git a/src/DateTimeTrait.php b/src/DateTimeTrait.php index 3824a79..18860a8 100644 --- a/src/DateTimeTrait.php +++ b/src/DateTimeTrait.php @@ -293,4 +293,16 @@ public static function create_from_format( $format, $time, \DateTimeZone $timezo return $wp_date_time; } + + /** + * Create from interface. + * + * @link https://www.php.net/manual/en/datetime.createfrominterface.php + * @link https://php.watch/versions/8.0/datetime-immutable-createfrominterface + * @param \DateTimeInterface $object The mutable DateTime object that you want to convert to an immutable version. + * @return self + */ + public static function create_from_interface( \DateTimeInterface $object ) { + return new self( $object->format('Y-m-d H:i:s.u'), $object->getTimezone() ); + } } From f0698a8724e4dc77e1f978d1fad2e41e93fcd4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reu=CC=88el=20van=20der=20Steege?= Date: Thu, 18 Nov 2021 15:53:57 +0100 Subject: [PATCH 6/8] Update libraries. --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 71b4511..e0a7d47 100644 --- a/composer.json +++ b/composer.json @@ -29,10 +29,11 @@ "phpmd/phpmd": "^2.9", "phpunit/phpunit": "^5.7 || ^6.0", "pronamic/wp-coding-standards": "^1.0", - "roots/wordpress": "^5.6", + "roots/wordpress": "^5.8", "squizlabs/php_codesniffer": "^3.5", "vlucas/phpdotenv": "^2.0", - "wp-phpunit/wp-phpunit": "^5.6" + "wp-phpunit/wp-phpunit": "^5.8", + "yoast/phpunit-polyfills": "^1.0" }, "scripts": { "coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover build/logs/clover.xml --coverage-text", From 8062a704f934f56fd12229c7de00dfd6a7120713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reu=CC=88el=20van=20der=20Steege?= Date: Mon, 3 Jan 2022 14:05:56 +0100 Subject: [PATCH 7/8] Happy 2022. --- pronamic-datetime.php | 2 +- src/DateTime.php | 2 +- src/DateTimeImmutable.php | 2 +- src/DateTimeInterface.php | 2 +- src/DateTimeTrait.php | 2 +- src/DateTimeZone.php | 2 +- tests/bootstrap.php | 2 +- tests/phpstan/bootstrap.php | 2 +- tests/src/DateTimeTest.php | 2 +- tests/src/DateTimeZoneTest.php | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pronamic-datetime.php b/pronamic-datetime.php index c9f42fa..3999e2f 100644 --- a/pronamic-datetime.php +++ b/pronamic-datetime.php @@ -18,7 +18,7 @@ * GitHub URI: https://github.com/pronamic/wp-datetime * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\Pay */ diff --git a/src/DateTime.php b/src/DateTime.php index 41cd39f..46b9b35 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -3,7 +3,7 @@ * Date time * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\DateTime * @see https://github.com/woocommerce/woocommerce/blob/3.3.4/includes/class-wc-datetime.php diff --git a/src/DateTimeImmutable.php b/src/DateTimeImmutable.php index 86bf5d8..e9bb9c2 100644 --- a/src/DateTimeImmutable.php +++ b/src/DateTimeImmutable.php @@ -3,7 +3,7 @@ * Date time immutable * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\DateTime * @see https://github.com/woocommerce/woocommerce/blob/3.3.4/includes/class-wc-datetime.php diff --git a/src/DateTimeInterface.php b/src/DateTimeInterface.php index b1a1db9..f3ff736 100644 --- a/src/DateTimeInterface.php +++ b/src/DateTimeInterface.php @@ -3,7 +3,7 @@ * Date time interface * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\DateTime * @see https://github.com/woocommerce/woocommerce/blob/3.3.4/includes/class-wc-datetime.php diff --git a/src/DateTimeTrait.php b/src/DateTimeTrait.php index 18860a8..0d7f0d1 100644 --- a/src/DateTimeTrait.php +++ b/src/DateTimeTrait.php @@ -3,7 +3,7 @@ * Date time trait * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\DateTime * @see https://github.com/woocommerce/woocommerce/blob/3.3.4/includes/class-wc-datetime.php diff --git a/src/DateTimeZone.php b/src/DateTimeZone.php index 989fb6b..a62c6a7 100644 --- a/src/DateTimeZone.php +++ b/src/DateTimeZone.php @@ -3,7 +3,7 @@ * Date time zone * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\DateTime * @see https://github.com/woocommerce/woocommerce/blob/3.3.4/includes/class-wc-datetime.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e6182c1..bb102f6 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -3,7 +3,7 @@ * Bootstrap tests * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\DateTime */ diff --git a/tests/phpstan/bootstrap.php b/tests/phpstan/bootstrap.php index 6fcfb66..cff4f4d 100644 --- a/tests/phpstan/bootstrap.php +++ b/tests/phpstan/bootstrap.php @@ -3,7 +3,7 @@ * Definitions for PHPStan. * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\Pay */ diff --git a/tests/src/DateTimeTest.php b/tests/src/DateTimeTest.php index 356c54b..32e3a43 100644 --- a/tests/src/DateTimeTest.php +++ b/tests/src/DateTimeTest.php @@ -3,7 +3,7 @@ * DateTime * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\DateTime */ diff --git a/tests/src/DateTimeZoneTest.php b/tests/src/DateTimeZoneTest.php index 67eb288..b49f426 100644 --- a/tests/src/DateTimeZoneTest.php +++ b/tests/src/DateTimeZoneTest.php @@ -3,7 +3,7 @@ * DateTime * * @author Pronamic - * @copyright 2005-2021 Pronamic + * @copyright 2005-2022 Pronamic * @license GPL-3.0-or-later * @package Pronamic\WordPress\DateTime */ From 417723d2f9d9147f22ed418d0785f4d45a01b653 Mon Sep 17 00:00:00 2001 From: Remco Tolsma <869674+remcotolsma@users.noreply.github.com> Date: Mon, 10 Jan 2022 15:27:06 +0100 Subject: [PATCH 8/8] Update version number to 2.0.0. --- CHANGELOG.md | 11 ++++++++++- package.json | 2 +- pronamic-datetime.php | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f517e7..5f621bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ This projects adheres to [Semantic Versioning](http://semver.org/) and [Keep a C ## [Unreleased][unreleased] - +## [2.0.0] - 2022-01-10 +### Added +- Added `DateTimeTrait::create_from_interface`. + +### Removed +- Removed `DateTime::create_from_mutable`. +- Removed `DateTimeImmutable::create_from_mutable`. + ## [1.2.2] - 2021-08-26 - Added the character `p` to the date format characters list which was added in PHP 8. @@ -41,7 +49,8 @@ This projects adheres to [Semantic Versioning](http://semver.org/) and [Keep a C ## 1.0.0 - First release. -[unreleased]: https://github.com/pronamic/wp-datetime/compare/1.2.2...HEAD +[unreleased]: https://github.com/pronamic/wp-datetime/compare/2.0.0...HEAD +[2.0.0]: https://github.com/pronamic/wp-datetime/compare/1.2.2...2.0.0 [1.2.2]: https://github.com/pronamic/wp-datetime/compare/1.2.1...1.2.2 [1.2.1]: https://github.com/pronamic/wp-datetime/compare/1.2.0...1.2.1 [1.2.0]: https://github.com/pronamic/wp-datetime/compare/1.1.1...1.2.0 diff --git a/package.json b/package.json index 398f1ef..7ef885b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wp-datetime", - "version": "1.2.2", + "version": "2.0.0", "description": "WordPress DateTime library.", "repository": { "type": "git", diff --git a/pronamic-datetime.php b/pronamic-datetime.php index 3999e2f..830dfcb 100644 --- a/pronamic-datetime.php +++ b/pronamic-datetime.php @@ -4,7 +4,7 @@ * Plugin URI: https://www.pronamic.eu/plugins/pronamic-datetime/ * Description: WordPress DateTime library. * - * Version: 1.2.0 + * Version: 2.0.0 * Requires at least: 4.7 * * Author: Pronamic