Skip to content

Commit

Permalink
Implement #13
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 24, 2016
1 parent f63ebb3 commit 63d15ba
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 169 deletions.
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
[![Code Coverage](https://img.shields.io/coveralls/ICanBoogie/DateTime.svg)](https://coveralls.io/r/ICanBoogie/DateTime)
[![Packagist](https://img.shields.io/packagist/dm/icanboogie/datetime.svg?maxAge=2592000)](https://packagist.org/packages/icanboogie/datetime)

This package extends the features of PHP [DateTime](http://www.php.net/manual/en/class.datetime.php)
This package extends the features of PHP
[DateTime](http://www.php.net/manual/en/class.datetime.php),
[DateTimeImmutable](http://www.php.net/manual/en/class.datetimeimmutable.php),
and [DateTimeZone](http://www.php.net/manual/en/class.datetimezone.php) classes to ease the
handling of times, time zones and time zone locations. Getting the UTC or local representation of
a time, formatting the time to a predefined format, accessing common properties such as day, month,
Expand Down Expand Up @@ -83,11 +85,11 @@ The implementation of the [DateTime][] class is vastly inspired by Ruby's

## DateTime is a value object

Starting from v2.0, date time instances are immutable [value
objects](https://en.wikipedia.org/wiki/Value_object). A class to create mutable date time instances
is available, but it's important to remember the difference with PHP implementation. Thus,
[ICanBoogie\DateTime][] extends [\DateTimeImmutable][] and [ICanBoogie\MutableDateTime][] extends
[\DateTime][].
Starting from v2.0, date time instances are immutable [value objects][]. A class to create mutable
date time instances is available, but it's important to remember the difference with PHP
implementation. Thus, [ICanBoogie\DateTime][] extends [\DateTimeImmutable][] and
[ICanBoogie\MutableDateTime][] extends [\DateTime][]. Alose [ICanBoogie\Contract\DateTime], which is
implemented by both [DateTime][] and [MutableDateTime][], extends [\DateTimeInterface][].

You can switch from immutable to mutable instances using the `mutable` and `immutable` properties,
or the static `from()` method:
Expand Down Expand Up @@ -485,16 +487,17 @@ The package is continuously tested by [Travis CI](http://about.travis-ci.org/).



[ICanBoogie/CLDR]: https://github.com/ICanBoogie/CLDR
[JsonSerializable interface]: http://php.net/manual/en/class.jsonserializable.php
[documentation]: http://api.icanboogie.org/datetime/latest/
[\DateTime]: http://php.net/manual/en/class.datetime.php
[\DateTimeImmutable]: http://php.net/manual/en/class.datetimeimmutable.php
[DateTime]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.DateTime.html
[ICanBoogie\DateTime]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.DateTime.html
[ICanBoogie\MutableDateTime]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.MutableDateTime.html
[MutableDateTime]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.MutableDateTime.html
[TimeZone]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.TimeZone.html
[TimeZoneLocation]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.TimeZoneLocation.html
[PropertyNotDefined]: http://api.icanboogie.org/common/1.2/class-ICanBoogie.PropertyNotDefined.html
[PropertyNotWritable]: http://api.icanboogie.org/common/1.2/class-ICanBoogie.PropertyNotWritable.html
[ICanBoogie/CLDR]: https://github.com/ICanBoogie/CLDR
[JsonSerializable interface]: http://php.net/manual/en/class.jsonserializable.php
[documentation]: http://api.icanboogie.org/datetime/latest/
[\DateTime]: http://php.net/manual/en/class.datetime.php
[\DateTimeImmutable]: http://php.net/manual/en/class.datetimeimmutable.php
[\DateTimeInterface]: http://php.net/manual/en/class.datetimeinterface.php
[DateTime]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.DateTime.html
[ICanBoogie\Contract\DateTime]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.Contract.DateTime.html
[ICanBoogie\DateTime]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.DateTime.html
[ICanBoogie\MutableDateTime]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.MutableDateTime.html
[MutableDateTime]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.MutableDateTime.html
[TimeZone]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.TimeZone.html
[TimeZoneLocation]: http://api.icanboogie.org/datetime/2.0/class-ICanBoogie.TimeZoneLocation.html
[value objects]: https://en.wikipedia.org/wiki/Value_object
149 changes: 149 additions & 0 deletions lib/Contract/DateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace ICanBoogie\Contract;

/**
* Interface for ICanBoogie's mutable and immutable DateTime.
*/
interface DateTime extends \DateTimeInterface
{
/**
* DB (example: 2013-02-03 20:59:03)
*
* @var string
*/
const DB = 'Y-m-d H:i:s';

/**
* Number (example: 20130203205903)
*
* @var string
*/
const NUMBER = 'YmdHis';

/**
* Date (example: 2013-02-03)
*
* @var string
*/
const DATE = 'Y-m-d';

/**
* Time (example: 20:59:03)
*
* @var string
*/
const TIME = 'H:i:s';

/**
* Creates a {@link DateTime} instance from a source.
*
* <pre>
* <?php
*
* use ICanBoogie\DateTime;
*
* DateTime::from(new \DateTime('2001-01-01 01:01:01', new \DateTimeZone('Europe/Paris')));
* DateTime::from('2001-01-01 01:01:01', 'Europe/Paris');
* DateTime::from('now');
* </pre>
*
* @param mixed $source
* @param mixed $timezone The time zone to use to create the time. The value is ignored if the
* source is an instance of {@link \DateTime}.
*
* @return static
*/
static public function from($source, $timezone = null);

/**
* Returns an instance representing an empty date ("0000-00-00").
*
* <pre>
* <?php
*
* use ICanBoogie\DateTime;
*
* $d = DateTime::none();
* $d->is_empty; // true
* $d->timezone->name; // "UTC"
*
* $d = DateTime::none('Asia/Tokyo');
* $d->is_empty; // true
* $d->timezone->name; // "Asia/Tokio"
* </pre>
*
* @param \DateTimeZone|string $timezone The time zone in which the empty date is created.
* Defaults to "UTC".
*
* @return DateTime
*/
static public function none($timezone = 'utc');

/**
* Returns an instance with the current local time and the local time zone.
*
* **Note:** Subsequent calls return equal times, event if they are minutes apart. _now_
* actually refers to the `REQUEST_TIME` or, if it is now available, to the first time
* the method was invoked.
*
* @return static
*/
static public function now();

/**
* Returns an instance with the current local time and the local time zone.
*
* **Note:** Subsequent calls may return different times.
*
* @return static
*/
static public function right_now();

/**
* Modifies the properties of the instance according to the options.
*
* The following properties can be updated: {@link $year}, {@link $month}, {@link $day},
* {@link $hour}, {@link $minute} and {@link $second}.
*
* Note: Values exceeding ranges are added to their parent values.
*
* <pre>
* <?php
*
* use ICanBoogie\DateTime;
*
* $time = new DateTime('now');
* $time->change([ 'year' => 2000, 'second' => 0 ]);
* </pre>
*
* @param array $options
* @param bool $cascade If `true`, time options (`hour`, `minute`, `second`) reset
* cascading, so if only the hour is passed, then minute and second is set to 0. If the hour
* and minute is passed, then second is set to 0.
*
* @return $this
*/
public function change(array $options, $cascade = false);

/**
* Returns a new instance with changes properties.
*
* @param array $options
* @param bool $cascade
*
* @return DateTime
*/
public function with(array $options, $cascade = false);

/**
* Returns a localized instance.
*
* @param string $locale
*
* @return mixed
*
* @throws \LogicException if the instance cannot be localized.
*/
public function localize($locale = 'en');
}
19 changes: 3 additions & 16 deletions lib/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace ICanBoogie;

use ICanBoogie\DateTime\AdditionalFormats;

/**
* Representation of a date and time.
*
Expand Down Expand Up @@ -102,7 +100,7 @@
*
* @see http://en.wikipedia.org/wiki/ISO_8601
*/
class DateTime extends \DateTimeImmutable implements \JsonSerializable, AdditionalFormats
class DateTime extends \DateTimeImmutable implements \JsonSerializable, Contract\DateTime
{
use DateTime\Shared;
use DateTime\Readers;
Expand Down Expand Up @@ -130,13 +128,7 @@ class DateTime extends \DateTimeImmutable implements \JsonSerializable, Addition
static public $localizer = null;

/**
* Returns an instance with the current local time and the local time zone.
*
* **Note:** Subsequent calls return equal times, event if they are minutes apart. _now_
* actually refers to the `REQUEST_TIME` or, if it is now available, to the first time
* the method was invoked.
*
* @return static
* @inheritdoc
*/
static public function now()
{
Expand All @@ -161,12 +153,7 @@ public function __set($property, $value)
}

/**
* Instantiate a new instance with changes properties.
*
* @param array $options
* @param bool $cascade
*
* @return DateTime
* @inheritdoc
*/
public function with(array $options, $cascade = false)
{
Expand Down
46 changes: 0 additions & 46 deletions lib/DateTime/AdditionalFormats.php

This file was deleted.

Loading

0 comments on commit 63d15ba

Please sign in to comment.