This commit is contained in:
2019-09-30 01:25:32 +00:00
parent 544813052e
commit cb51be5f72
371 changed files with 44076 additions and 1 deletions

10
vendor/cakephp/chronos/Dockerfile vendored Normal file
View File

@@ -0,0 +1,10 @@
# Basic docker based environment
# Necessary to trick dokku into building the documentation
# using dockerfile instead of herokuish
FROM php:7.3
WORKDIR /code
VOLUME ["/code"]
CMD [ '/bin/bash' ]

20
vendor/cakephp/chronos/LICENSE vendored Normal file
View File

@@ -0,0 +1,20 @@
Copyright (C) Brian Nesbitt
Copyright (C) Cake Software Foundation, Inc. (http://cakefoundation.org)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

147
vendor/cakephp/chronos/README.md vendored Normal file
View File

@@ -0,0 +1,147 @@
# CakePHP Chronos
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Build Status](https://img.shields.io/travis/cakephp/chronos/master.svg?style=flat-square)](https://travis-ci.org/cakephp/chronos)
[![Coverage Status](https://img.shields.io/coveralls/cakephp/chronos/master.svg?style=flat-square)](https://coveralls.io/r/cakephp/chronos?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/cakephp/chronos.svg?style=flat-square)](https://packagist.org/packages/cakephp/chronos)
Chronos aims to be a drop-in replacement for `nesbot/carbon`. It focuses on providing
immutable date/datetime objects. Immutable objects help ensure that datetime objects
aren't accidentally modified keeping data more predictable.
# Installation
Installing with composer:
```
$ composer require cakephp/chronos
```
You can then use Chronos:
```php
<?php
require 'vendor/autoload.php';
use Cake\Chronos\Chronos;
printf("Now: %s", Chronos::now());
```
# Differences with nesbot/carbon
The biggest and main difference is that `Chronos` extends `DateTimeImmutable` instead of `DateTime`.
Immutability for date values has proven to be a great way of avoiding bugs and reduce the amount of code,
since developers don't have to manually copy the instance every time they need a change.
Another important feature it offers is the `Date` class, which is used for representing dates without time (calendar dates).
Any time method called on this type of object is basically a no-op.
A minor but still noticeable difference is that `Chronos` has no external dependencies, it is completely standalone.
Finally, Chronos is faster than Carbon as it has been optimized for the creation of hundreds of instances with minimal
overhead.
Chronos also strives for HHVM compatibility, this library can be used safely with HHVM 3.11.
# Migrating from Carbon
First add `cakephp/chronos` to your `composer.json`:
```shell
php composer.phar require cakephp/chronos
```
By default Chronos includes a compatibility script that creates aliases for the
relevant Carbon classes. This will let most applications upgrade with very
little effort. If you'd like to permanently update your code, you will
need to update imports and typehints. Assuming `src` contains the files you
want to migrate, we could use the following to update files:
```
# Replace imports
find ./src -type f -exec sed -i '' 's/use Carbon\\CarbonInterval/use Cake\\Chronos\\ChronosInterval/g' {} \;
find ./src -type f -exec sed -i '' 's/use Carbon\\Carbon/use Cake\\Chronos\\Chronos/g' {} \;
# Replace typehints and extensions
find ./src -type f -exec sed -i '' 's/CarbonInterval/ChronosInterval/g' {} \;
find ./src -type f -exec sed -i '' 's/Carbon/Chronos/g' {} \;
```
At this point your code should mostly work as it did before. The biggest
difference is that Chronos instances are immutable.
## Immutable Object Changes
Immutable objects have a number of advantages:
1. Using immutable objects is always free of side-effects.
2. Dates and times don't accidentally change underneath other parts of your code.
With those benefits in mind, there are a few things you need to keep in mind
when modifying immutable objects:
```php
// This will lose modifications
$date = new Chronos('2015-10-21 16:29:00');
$date->modify('+2 hours');
// This will keep modifications
$date = new Chronos('2015-10-21 16:29:00');
$date = $date->modify('+2 hours');
```
## Getting Mutable Objects
In the case that you need a mutable instance you can get one:
```php
$time = new Chronos('2015-10-21 16:29:00');
$mutable = $time->toMutable();
$date = new Date('2015-10-21');
$mutable = $date->toMutable();
```
## Converting Mutable Objects into Immutable ones.
If you have a mutable object and want an immutable variant you can do the following:
```php
$time = new MutableDateTime('2015-10-21 16:29:00');
$fixed = $time->toImmutable();
$date = new MutableDate('2015-10-21');
$fixed = $date->toImmutable();
```
# Calendar Dates
PHP only offers datetime objects as part of the native extensions. Chronos
adds a number of conveniences to the traditional DateTime object and introduces
a `Date` object. `Date` instances offer compatibility with the `ChronosInterface`, but
have their time & timezone frozen to `00:00:00 UTC`. This makes them ideal when working with
calendar dates as the time components will always match.
```php
use Cake\Chronos\Date;
$today = new Date();
echo $today;
// Outputs '2015-10-21'
echo $today->modify('+3 hours');
// Outputs '2015-10-21'
```
Like instances of `Chronos`, `Date` objects are also *immutable*. The `MutableDate` class provides
a mutable variant of `Date`.
# Documentation
A more descriptive documentation can be found at [book.cakephp.org/chronos/1.x/en/](https://book.cakephp.org/chronos/1.x/en/).
# API Documentation
API documentation can be found on [api.cakephp.org/chronos](https://api.cakephp.org/chronos).

62
vendor/cakephp/chronos/composer.json vendored Normal file
View File

@@ -0,0 +1,62 @@
{
"name": "cakephp/chronos",
"type": "library",
"description": "A simple API extension for DateTime.",
"keywords": [
"date",
"time",
"DateTime"
],
"homepage": "http://cakephp.org",
"license": "MIT",
"authors": [
{
"name": "Brian Nesbitt",
"email": "brian@nesbot.com",
"homepage": "http://nesbot.com"
},
{
"name": "The CakePHP Team",
"homepage": "http://cakephp.org"
}
],
"support": {
"issues": "https://github.com/cakephp/chronos/issues",
"irc": "irc://irc.freenode.org/cakephp",
"source": "https://github.com/cakephp/chronos"
},
"require": {
"php": "^5.5.9|^7"
},
"require-dev": {
"phpunit/phpunit": "<6.0 || ^7.0",
"athletic/athletic": "~0.1",
"cakephp/cakephp-codesniffer": "^3.0",
"phpbench/phpbench": "@dev",
"phpstan/phpstan": "^0.6.4"
},
"autoload": {
"psr-4": {
"Cake\\Chronos\\": "src/"
},
"files": ["src/carbon_compat.php"]
},
"autoload-dev": {
"psr-4": {
"Cake\\Chronos\\Test\\": "tests/"
},
"files": ["tests/TestCase.php"]
},
"scripts": {
"check": [
"@test",
"@cs-check",
"@phpstan"
],
"test": "phpunit",
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"bench": "phpbench run",
"phpstan": "phpstan analyze -c phpstan.neon -l 3 src tests"
}
}

19
vendor/cakephp/chronos/docs.Dockerfile vendored Normal file
View File

@@ -0,0 +1,19 @@
# Generate the HTML output.
FROM markstory/cakephp-docs-builder as builder
RUN pip install git+https://github.com/sphinx-contrib/video.git@master
COPY docs /data/docs
RUN cd /data/docs-builder && \
# In the future repeat website for each version
make website LANGS="en fr ja pt" SOURCE=/data/docs DEST=/data/website/1.x
# Build a small nginx container with just the static site in it.
FROM nginx:1.15-alpine
COPY --from=builder /data/website /data/website
COPY --from=builder /data/docs-builder/nginx.conf /etc/nginx/conf.d/default.conf
# Move each version into place
RUN mv /data/website/1.x/html/ /usr/share/nginx/html/1.x

View File

View File

@@ -0,0 +1,46 @@
# Global configuration information used across all the
# translations of documentation.
#
# Import the base theme configuration
from cakephpsphinx.config.all import *
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The full version, including alpha/beta/rc tags.
release = '1.x'
# The search index version.
search_version = 'chronos-1'
# The marketing display name for the book.
version_name = ''
# Project name shown in the black header bar
project = 'Chronos'
# Other versions that display in the version picker menu.
version_list = [
{'name': '1.x', 'number': '/chronos/1.x', 'title': '1.x', 'current': True},
]
# Languages available.
languages = ['en', 'fr', 'ja', 'pt']
# The GitHub branch name for this version of the docs
# for edit links to point at.
branch = 'master'
# Current version being built
version = '1.x'
# Language in use for this directory.
language = 'en'
show_root_link = True
repository = 'cakephp/chronos'
source_path = 'docs/'

View File

@@ -0,0 +1,9 @@
import sys, os
# Append the top level directory of the docs, so we can import from the config dir.
sys.path.insert(0, os.path.abspath('..'))
# Pull in all the configuration options defined in the global config file..
from config.all import *
language = 'en'

View File

@@ -0,0 +1,7 @@
.. toctree::
:maxdepth: 2
:caption: CakePHP Chronos
/index
API <https://api.cakephp.org/chronos>

313
vendor/cakephp/chronos/docs/en/index.rst vendored Normal file
View File

@@ -0,0 +1,313 @@
Chronos
#######
Chronos provides a zero-dependency collection of extensions to the ``DateTime``
object. In addition to convenience methods, Chronos provides:
* ``Date`` objects for representing calendar dates.
* Immutable date and datetime objects.
* A pluggable translation system. Only English translations are included in the
library. However, ``cakephp/i18n`` can be used for full language support.
Installation
------------
To install Chronos, you should use ``composer``. From your
application's ROOT directory (where composer.json file is located) run the
following::
php composer.phar require cakephp/chronos "@stable"
Overview
--------
Chronos provides a number of extensions to the DateTime objects provided by PHP.
Chronos provides 5 classes that cover mutable and immutable date/time variants
and extensions to ``DateInterval``.
* ``Cake\Chronos\Chronos`` is an immutable *date and time* object.
* ``Cake\Chronos\Date`` is a immutable *date* object.
* ``Cake\Chronos\MutableDateTime`` is a mutable *date and time* object.
* ``Cake\Chronos\MutableDate`` is a mutable *date* object.
* ``Cake\Chronos\ChronosInterval`` is an extension to the ``DateInterval``
object.
Lastly, if you want to typehint against Chronos-provided date/time objects you
should use ``Cake\Chronos\ChronosInterface``. All of the date and time objects
implement this interface.
Creating Instances
------------------
There are many ways to get an instance of Chronos or Date. There are a number of
factory methods that work with different argument sets::
use Cake\Chronos\Chronos;
$now = Chronos::now();
$today = Chronos::today();
$yesterday = Chronos::yesterday();
$tomorrow = Chronos::tomorrow();
// Parse relative expressions
$date = Chronos::parse('+2 days, +3 hours');
// Date and time integer values.
$date = Chronos::create(2015, 12, 25, 4, 32, 58);
// Date or time integer values.
$date = Chronos::createFromDate(2015, 12, 25);
$date = Chronos::createFromTime(11, 45, 10);
// Parse formatted values.
$date = Chronos::createFromFormat('m/d/Y', '06/15/2015');
Working with Immutable Objects
------------------------------
If you've used PHP's ``DateTime`` objects, you're comfortable with *mutable*
objects. Chronos offers mutable objects, but it also provides *immutable*
objects. Immutable objects create copies of objects each time an object is
modified. Because modifier methods around datetimes are not always transparent,
data can be modified accidentally or without the developer knowing.
Immutable objects prevent accidental changes to
data, and make code free of order-based dependency issues. Immutability
does mean that you will need to remember to replace variables when using
modifiers::
// This code doesn't work with immutable objects
$time->addDay(1);
doSomething($time);
return $time;
// This works like you'd expect
$time = $time->addDay(1);
$time = doSomething($time);
return $time;
By capturing the return value of each modification your code will work as
expected. If you ever have an immutable object, and want to create a mutable
one, you can use ``toMutable()``::
$inplace = $time->toMutable();
Date Objects
------------
PHP only provides a single DateTime object. Representing calendar dates can be
a bit awkward with this class as it includes timezones, and time components that
don't really belong in the concept of a 'day'. Chronos provides a ``Date``
object that allows you to represent dates. The time and timezone for these
objects is always fixed to ``00:00:00 UTC`` and all formatting/difference
methods operate at the day resolution::
use Cake\Chronos\Date;
$today = Date::today();
// Changes to the time/timezone are ignored.
$today->modify('+1 hours');
// Outputs '2015-12-20'
echo $today;
Modifier Methods
----------------
Chronos objects provide modifier methods that let you modify the value in
a granular way::
// Set components of the datetime value.
$halloween = Date::create()
->year(2015)
->month(10)
->day(31)
->hour(20)
->minute(30);
You can also modify parts of a date relatively::
$future = Date::create()
->addYear(1)
->subMonth(2)
->addDays(15)
->addHours(20)
->subMinutes(2);
It is also possible to make big jumps to defined points in time::
$time = Chronos::create();
$time->startOfDay();
$time->endOfDay();
$time->startOfMonth();
$time->endOfMonth();
$time->startOfYear();
$time->endOfYear();
$time->startOfWeek();
$time->endOfWeek();
Or jump to specific days of the week::
$time->next(ChronosInterface::TUESDAY);
$time->previous(ChronosInterface::MONDAY);
When modifying dates/times across :abbr:`DST (Daylight Savings Time)` transitions
your operations may gain/lose an additional hours resulting in hour values that
don't add up. You can avoid these issues by first changing your timezone to
``UTC``, modifying the time::
// Additional hour gained.
$time = new Chronos('2014-03-30 00:00:00', 'Europe/London');
debug($time->modify('+24 hours')); // 2014-03-31 01:00:00
// First switch to UTC, and modify
$time = $time->setTimezone('UTC')
->modify('+24 hours');
Once you are done modifying the time you can add the original timezone to get
the localized time.
Comparison Methods
------------------
Once you have 2 instances of Chronos date/time objects you can compare them in
a variety of ways::
// Full suite of comparators exist
// ne, gt, lt, lte.
$first->eq($second);
$first->gte($second);
// See if the current object is between two others.
$now->between($start, $end);
// Find which argument is closest or farthest.
$now->closest($june, $november);
$now->farthest($june, $november);
You can also inquire about where a given value falls on the calendar::
$now->isToday();
$now->isYesterday();
$now->isFuture();
$now->isPast();
// Check the day of the week
$now->isWeekend();
// All other weekday methods exist too.
$now->isMonday();
You can also find out if a value was within a relative time period::
$time->wasWithinLast('3 days');
$time->isWithinNext('3 hours');
Generating Differences
----------------------
In addition to comparing datetimes, calculating differences or deltas between
two values is a common task::
// Get a DateInterval representing the difference
$first->diff($second);
// Get difference as a count of specific units.
$first->diffInHours($second);
$first->diffInDays($second);
$first->diffInWeeks($second);
$first->diffInYears($second);
You can generate human readable differences suitable for use in a feed or
timeline::
// Difference from now.
echo $date->diffForHumans();
// Difference from another point in time.
echo $date->diffForHumans($other); // 1 hour ago;
Formatting Strings
------------------
Chronos provides a number of methods for displaying our outputting datetime
objects::
// Uses the format controlled by setToStringFormat()
echo $date;
// Different standard formats
echo $time->toAtomString(); // 1975-12-25T14:15:16-05:00
echo $time->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST
echo $time->toIso8601String(); // 1975-12-25T14:15:16-05:00
echo $time->toRfc822String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc850String(); // Thursday, 25-Dec-75 14:15:16 EST
echo $time->toRfc1036String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc1123String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc2822String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc3339String(); // 1975-12-25T14:15:16-05:00
echo $time->toRssString(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toW3cString(); // 1975-12-25T14:15:16-05:00
// Get the quarter/week
echo $time->toQuarter(); // 4
echo $time->toWeek(); // 52
// Generic formatting
echo $time->toTimeString(); // 14:15:16
echo $time->toDateString(); // 1975-12-25
echo $time->toDateTimeString(); // 1975-12-25 14:15:16
echo $time->toFormattedDateString(); // Dec 25, 1975
echo $time->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
Extracting Date Components
--------------------------
Getting parts of a date object can be done by directly accessing properties::
$time = new Chronos('2015-12-31 23:59:58');
$time->year; // 2015
$time->month; // 12
$time->day; // 31
$time->hour // 23
$time->minute // 59
$time->second // 58
Other properties that can be accessed are:
- timezone
- timezoneName
- micro
- dayOfWeek
- dayOfMonth
- dayOfYear
- daysInMonth
- timestamp
- quarter
Testing Aids
------------
When writing unit tests, it is helpful to fixate the current time. Chronos lets
you fix the current time for each class. As part of your test suite's bootstrap
process you can include the following::
Chronos::setTestNow(Chronos::now());
MutableDateTime::setTestNow(MutableDateTime::now());
Date::setTestNow(Date::now());
MutableDate::setTestNow(MutableDate::now());
This will fix the current time of all objects to be the point at which the test
suite started.
For example, if you fixate the ``Chronos`` to some moment in the past, any new
instance of ``Chronos`` created with ``now`` or a relative time string, will be
returned relative to the fixated time::
Chronos::setTestNow(new Chronos('1975-12-25 00:00:00'));
$time = new Chronos(); // 1975-12-25 00:00:00
$time = new Chronos('1 hour ago'); // 1975-12-24 23:00:00
To reset the fixation, simply call ``setTestNow()`` again with no parameter or
with ``null`` as a parameter.

View File

@@ -0,0 +1,9 @@
import sys, os
# Append the top level directory of the docs, so we can import from the config dir.
sys.path.insert(0, os.path.abspath('..'))
# Pull in all the configuration options defined in the global config file..
from config.all import *
language = 'fr'

View File

@@ -0,0 +1,7 @@
.. toctree::
:maxdepth: 2
:caption: CakePHP Chronos
/index
API <https://api.cakephp.org/chronos>

306
vendor/cakephp/chronos/docs/fr/index.rst vendored Normal file
View File

@@ -0,0 +1,306 @@
Chronos
#######
Chronos fournit une collection d'extensions sans aucune dépendance pour l'objet
``DateTime``. En plus de méthodes pratiques, Chronos fournit:
* Des objets ``Date`` pour représenter les dates du calendrier.
* Des objets immutables pour les dates et les datetimes.
* Un système de traduction intégrable. Seules les traductions anglaises sont
inclues dans la librairie. Cependant, ``cakephp/i18n`` peut être utilisé
pour un support complet d'autres langues.
Installation
------------
Pour installer Chronos, vous devez utiliser ``composer``. A partir du répertoire
ROOT de votre application (où le fichier composer.json se trouve) exécutez ce
qui suit::
php composer.phar require cakephp/chronos "@stable"
Vue d'Ensemble
--------------
Chronos fournit un certain nombre d'extensions pour les objets DateTime fournis
par PHP. Chronos fournit 5 classes qui gèrent les variantes mutables et
immutables de date/time et les extensions de ``DateInterval``.
* ``Cake\Chronos\Chronos`` est un objet de *date et time* immutable.
* ``Cake\Chronos\Date`` est un objet de *date* immutable.
* ``Cake\Chronos\MutableDateTime`` est un objet de *date et time* mutable.
* ``Cake\Chronos\MutableDate`` est un objet de *date* mutable.
* ``Cake\Chronos\ChronosInterval`` est une extension pour l'objet
``DateInterval``.
Enfin si vous voulez typer selon les objets date/time fournis par Chronos,
vous devez utiliser ``Cake\Chronos\ChronosInterface``. Tous les objets date et
time implémentent cette interface.
Créer des Instances
-------------------
Il y a plusieurs façons d'obtenir une instance de Chronos ou de Date. Il y a
un certain nombre de méthodes factory qui fonctionnent avec des ensembles
ayant des arguments différents::
use Cake\Chronos\Chronos;
$now = Chronos::now();
$today = Chronos::today();
$yesterday = Chronos::yesterday();
$tomorrow = Chronos::tomorrow();
// Parse les expressions relatives
$date = Chronos::parse('+2 days, +3 hours');
// Les valeurs d'entier de Date et time.
$date = Chronos::create(2015, 12, 25, 4, 32, 58);
// Les valeurs d'entier pour Date ou time.
$date = Chronos::createFromDate(2015, 12, 25);
$date = Chronos::createFromTime(11, 45, 10);
// Parse les valeurs formatées.
$date = Chronos::createFromFormat('m/d/Y', '06/15/2015');
Travailler avec les Objets Immutables
-------------------------------------
Si vous avez utilisé les objets ``DateTime`` de PHP, vous êtes à l'aise avec
les objets *mutable*. Chronos offre des objets mutables, mais elle fournit
également des objets *immutables*. Les objets Immutables créent des copies des
objets à chaque fois qu'un objet est modifié. Puisque les méthodes de
modification autour des datetimes ne sont pas toujours transparentes, les
données peuvent être modifiées accidentellement ou sans que le développeur ne
le sache. Les objets immutables évitent les changements accidentels des
données et permettent de s'affranchir de tout problème lié à l'ordre d'appel
des fonctions ou des dépendances. L'immutabilité signifie que vous devez vous
souvenir de remplacer les variables quand vous utilisez les modificateurs::
// Ce code ne fonctionne pas avec les objets immutables
$time->addDay(1);
doSomething($time);
return $time;
// Ceci fonctionne comme vous le souhaitez
$time = $time->addDay(1);
$time = doSomething($time);
return $time;
En capturant la valeur de retour pour chaque modification, votre code
fonctionnera comme souhaité. Si vous avez déjà créé un objet immutable, et que
vous souhaitez un objet mutable, vous pouvez utiliser ``toMutable()``::
$inplace = $time->toMutable();
Objets Date
-----------
PHP fournit seulement un unique objet DateTime. Représenter les dates de
calendrier peut être un peu gênant avec cette classe puisqu'elle inclut les
timezones, et les composants de time qui n'appartiennent pas vraiment
dans le concept d'un 'jour'. Chronos fournit un objet ``Date`` qui vous permet
de représenter les dates. Les time et timezone pour ces objets sont toujours
fixés à ``00:00:00 UTC`` et toutes les méthodes de formatage/différence
fonctionnent au niveau du jour::
use Cake\Chronos\Date;
$today = Date::today();
// Changements selon le time/timezone sont ignorés.
$today->modify('+1 hours');
// Affiche '2015-12-20'
echo $today;
Méthodes de Modification
------------------------
Les objets Chronos fournissent des méthodes de modification qui vous laissent
modifier la valeur d'une façon assez précise::
// Définit les composants de la valeur du datetime.
$halloween = Date::create()
->year(2015)
->month(10)
->day(31)
->hour(20)
->minute(30);
Vous pouvez aussi modifier les parties de la date de façon relative::
$future = Date::create()
->addYear(1)
->subMonth(2)
->addDays(15)
->addHours(20)
->subMinutes(2);
Il est également possible de faire des sauts vers des points définis dans le
temps::
$time = Chronos::create();
$time->startOfDay();
$time->endOfDay();
$time->startOfMonth();
$time->endOfMonth();
$time->startOfYear();
$time->endOfYear();
$time->startOfWeek();
$time->endOfWeek();
Ou de sauter à un jour spécifique de la semaine::
$time->next(ChronosInterface::TUESDAY);
$time->previous(ChronosInterface::MONDAY);
Méthodes de Comparaison
-----------------------
Une fois que vous avez 2 instances d'objets date/time de Chronos, vous pouvez
les comparer de plusieurs façons::
// Suite complète de comparateurs existante
// ne, gt, lt, lte.
$first->eq($second);
$first->gte($second);
// Regardez si l'objet courant est entre deux autres.
$now->between($start, $end);
// Trouver l'argument le plus proche ou le plus éloigné.
$now->closest($june, $november);
$now->farthest($june, $november);
Vous pouvez aussi vous renseigner sur le moment où une valeur donnée tombe dans
le calendrier::
$now->isToday();
$now->isYesterday();
$now->isFuture();
$now->isPast();
// Vérifie le jour de la semaine
$now->isWeekend();
// Toutes les autres méthodes des jours de la semaine existent aussi.
$now->isMonday();
Vous pouvez aussi trouver si une valeur était dans une période de temps relative::
$time->wasWithinLast('3 days');
$time->isWithinNext('3 hours');
Générer des Différences
-----------------------
En plus de comparer les datetimes, calculer les différences ou les deltas entre
des valeurs est une tâche courante::
// Récupère un DateInterval représentant la différence
$first->diff($second);
// Récupère la différence en tant que nombre d'unités spécifiques.
$first->diffInHours($second);
$first->diffInDays($second);
$first->diffInWeeks($second);
$first->diffInYears($second);
Vous pouvez générer des différences lisibles qui peuvent vous servir pour
l'utilisation d'un feed ou d'une timeline::
// Différence à partir de maintenant.
echo $date->diffForHumans();
// Différence à partir d'un autre point du temps.
echo $date->diffForHumans($other); // 1 hour ago;
Formater les Chaînes
--------------------
Chronos fournit un certain nombre de méthodes pour afficher nos sorties d'objets
datetime::
// Utilise le format contrôlé par setToStringFormat()
echo $date;
// Différents formats standards
echo $time->toAtomString(); // 1975-12-25T14:15:16-05:00
echo $time->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST
echo $time->toIso8601String(); // 1975-12-25T14:15:16-05:00
echo $time->toRfc822String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc850String(); // Thursday, 25-Dec-75 14:15:16 EST
echo $time->toRfc1036String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc1123String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc2822String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc3339String(); // 1975-12-25T14:15:16-05:00
echo $time->toRssString(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toW3cString(); // 1975-12-25T14:15:16-05:00
// Récupère le trimestre
echo $time->toQuarter(); // 4;
// Récupère la semaine
echo $time->toWeek(); // 52;
// Formatage générique
echo $time->toTimeString(); // 14:15:16
echo $time->toDateString(); // 1975-12-25
echo $time->toDateTimeString(); // 1975-12-25 14:15:16
echo $time->toFormattedDateString(); // Dec 25, 1975
echo $time->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
Extraire des Fragments de Date
------------------------------
Il est possible de récupérer des parties d'un objet date en accédant directement
à ses propriétés::
$time = new Chronos('2015-12-31 23:59:58');
$time->year; // 2015
$time->month; // 12
$time->day; // 31
$time->hour // 23
$time->minute // 59
$time->second // 58
Les autres propriétés accessibles sont:
- timezone
- timezoneName
- micro
- dayOfWeek
- dayOfMonth
- dayOfYear
- daysInMonth
- timestamp
- quarter
Aides aux Tests
---------------
Quand vous écrivez des tests unitaires, il peut être utile de fixer le *time*
courant. Chronos vous permet de fixer le time courant pour chaque classe.
Pour l'intégrer dans votre processus de démarrage (bootstrap) de suite de tests,
vous pouvez inclure ce qui suit::
Chronos::setTestNow(Chronos::now());
MutableDateTime::setTestNow(MutableDateTime::now());
Date::setTestNow(Date::now());
MutableDate::setTestNow(MutableDate::now());
Ceci va fixer le time courant de tous les objets selon le moment où la suite de
tests a démarré.
Par exemple, si vous fixez le ``Chronos`` à un moment du passé, chaque nouvelle
instance de ``Chronos`` créée avec ``now`` ou une chaine de temps relative, sera
retournée relativement à la date fixée::
Chronos::setTestNow(new Chronos('1975-12-25 00:00:00'));
$time = new Chronos(); // 1975-12-25 00:00:00
$time = new Chronos('1 hour ago'); // 1975-12-24 23:00:00
Pour réinitialiser la "fixation" du temps, appelez simplement ``setTestNow()``
sans paramètre ou avec ``null`` comme paramètre.

View File

@@ -0,0 +1,9 @@
import sys, os
# Append the top level directory of the docs, so we can import from the config dir.
sys.path.insert(0, os.path.abspath('..'))
# Pull in all the configuration options defined in the global config file..
from config.all import *
language = 'ja'

View File

@@ -0,0 +1,7 @@
.. toctree::
:maxdepth: 2
:caption: CakePHP Chronos
/index
API <https://api.cakephp.org/chronos>

304
vendor/cakephp/chronos/docs/ja/index.rst vendored Normal file
View File

@@ -0,0 +1,304 @@
Chronos
#######
Chronos (クロノス) は、 ``DateTime`` オブジェクトへの拡張の依存関係の無いコレクションを提供します。
便利なメソッドに加えて、Chronos は以下を提供します。
* カレンダー日付のための ``Date`` オブジェクト
* イミュータブルな日付と日時オブジェクト
* プラグインのような翻訳システム。ライブラリーは英語のみの翻訳を含んでいます。
しかし、全ての言語サポートのために、 ``cakephp/i18n`` を使うことができます。
インストール
------------
Chronos をインストールするためには、 ``composer`` を利用することができます。
アプリケーションの ROOT ディレクトリーcomposer.json ファイルのある場所)
で以下のように実行します。 ::
php composer.phar require cakephp/chronos "@stable"
概要
----
Chronos は PHP が提供する DateTime オブジェクトのいくつかの拡張を提供します。
Chronos は ``DateInterval`` の拡張機能および、ミュータブル(変更可能)と
イミュータブル(変更不可)な 日付/時刻 の派生系をカバーする5つのクラスを提供します。
* ``Cake\Chronos\Chronos`` はイミュータブルな *日付と時刻* オブジェクト。
* ``Cake\Chronos\Date`` はイミュータブルな *日付* オブジェクト。
* ``Cake\Chronos\MutableDateTime`` はミュータブルな *日付と時刻* オブジェクト。
* ``Cake\Chronos\MutableDate`` はミュータブルな *日付* オブジェクト。
* ``Cake\Chronos\ChronosInterval````DateInterval`` の拡張機能。
最後に、もしあなたが Chronos が提供する 日付/時刻 のオブジェクトに対して型宣言を行ないたい場合、
``Cake\Chronos\ChronosInterface`` を使用することができます。
全ての日付と時間のオブジェクトはこのインターフェイスを実装しています。
インスタンスの作成
------------------
Chronos または Date のインスタンスを取得するためには、多くの方法があります。
異なる引数セットで動作する多くのファクトリーメソッドがあります。 ::
use Cake\Chronos\Chronos;
$now = Chronos::now();
$today = Chronos::today();
$yesterday = Chronos::yesterday();
$tomorrow = Chronos::tomorrow();
// 相対式のパース
$date = Chronos::parse('+2 days, +3 hours');
// 日付と時間の整数値
$date = Chronos::create(2015, 12, 25, 4, 32, 58);
// 日付または時間の整数値
$date = Chronos::createFromDate(2015, 12, 25);
$date = Chronos::createFromTime(11, 45, 10);
// 整形した値にパース
$date = Chronos::createFromFormat('m/d/Y', '06/15/2015');
イミュータブルオブジェクトの動作
--------------------------------
もしあなたが、PHP の ``DateTime`` オブジェクトを使用したことがあるなら、
*ミュータブル* オブジェクトは簡単に使用できます。
Chronos はミュータブルオブジェクトを提供しますが、これは *イミュータブル* オブジェクトにもなります。
イミュータブルオブジェクトはオブジェクトが変更されるたびにオブジェクトのコピーを作ります。
なぜなら、日時周りの変更メソッドは必ずしも透明でないため、データが誤って、
または開発者が知らない内に変更してしまうからです。
イミュータブルオブジェクトはデータが誤って変更されることを防止し、
順序ベースの依存関係の問題の無いコードを作ります。
不変性は、変更時に忘れずに変数を置き換える必要があることを意味しています。 ::
// このコードはイミュータブルオブジェクトでは動作しません
$time->addDay(1);
doSomething($time);
return $time
// このコードは期待通りに動作します
$time = $time->addDay(1);
$time = doSomething($time);
return $time
各修正の戻り値をキャプチャーすることによって、コードは期待通りに動作します。
イミュータブルオブジェクトを持っていて、ミュータブルオブジェクトを作りたい場合、
``toMutable()`` が使用できます。 ::
$inplace = $time->toMutable();
日付オブジェクト
------------------
PHP は単純な DateTime オブジェクトだけを提供します。このクラスのカレンダー日付の表現で、
タイムゾーンおよび、本当に「日」の概念に属していないタイムコンポーネントを含むと、
少し厄介な可能性があります。
Chronos は日時表現のための ``Date`` オブジェクトを提供します。
これらのオブジェクトの時間とタイムゾーンは常に ``00:00:00 UTC`` に固定されており、
全ての書式/差分のメソッドは一日単位で動作します。 ::
use Cake\Chronos\Date;
$today = Date::today();
// 時間/タイムゾーンの変更は無視されます
$today->modify('+1 hours');
// 出力 '2015-12-20'
echo $today;
変更メソッド
------------
Chronos オブジェクトは細やかに値を変更できるメソッドを提供します。 ::
// 日時の値のコンポーネントを設定
$halloween = Date::create()
->year(2015)
->month(10)
->day(31)
->hour(20)
->minute(30);
また、日付部分を相対日付に変更することもできます。 ::
$future = Date::create()
->addYear(1)
->subMonth(2)
->addDays(15)
->addHours(20)
->subMinutes(2);
また、ある時間の中で、定義された時点に飛ぶことも可能です。 ::
$time = Chronos::create();
$time->startOfDay();
$time->endOfDay();
$time->startOfMonth();
$time->endOfMonth();
$time->startOfYear();
$time->endOfYear();
$time->startOfWeek();
$time->endOfWeek();
また、1週間中の特定の日にも飛べます。 ::
$time->next(ChronosInterface::TUESDAY);
$time->previous(ChronosInterface::MONDAY);
:abbr:`DST (夏時間)` の遷移の前後で日付/時間を変更すると、
あなたの操作で時間が増減するかもしれませんが、その結果、意図しない時間の値になります。
これらの問題を回避するには、最初にタイムゾーンを ``UTC`` に変更し、時間を変更します。 ::
// 余分な時間が追加されました
$time = new Chronos('2014-03-30 00:00:00', 'Europe/London');
debug($time->modify('+24 hours')); // 2014-03-31 01:00:00
// 最初に UTC に切り替え、そして更新
$time = $time->setTimezone('UTC')
->modify('+24 hours');
時間を変更すると、元のタイムゾーンを追加してローカライズされた時間を取得することができます。
比較メソッド
------------
Chronos の日付/時間オブジェクトの2つのインスタンスを様々な方法で比較することができます。 ::
// 比較のフルセットが存在します
// ne, gt, lt, lte.
$first->eq($second);
$first->gte($second);
// カレントオブジェクトが2つのオブジェクトの間にあるかどうかを確認します。
$now->between($start, $end);
// どちらの引数が最も近い (closest) か、または最も遠い (farthest) かを見つけます。
$now->closest($june, $november);
$now->farthest($june, $november);
また、与えられた値のカレンダーに当たる場所について問い合わせできます。 ::
$now->isToday();
$now->isYesterday();
$now->isFuture();
$now->isPast();
// 曜日をチェック
$now->isWeekend();
// 他の曜日のメソッドも全て存在します。
$now->isMonday();
また、値が相対的な期間内にあったかどうかを見つけることができます。 ::
$time->wasWithinLast('3 days');
$time->isWithinNext('3 hours');
差の生成
--------
日時比較に加えて、2つの値の差や変化の計算は一般的なタスクです。 ::
// 差をあらわす DateInterval を取得
$first->diff($second);
// 特定の単位での差を取得
$first->diffInHours($second);
$first->diffInDays($second);
$first->diffInWeeks($second);
$first->diffInYears($second);
フィードやタイムラインで使用するのに適した、人が読める形式の差を生成することができます。 ::
// 現在からの差
echo $date->diffForHumans();
// 別の時点からの差
echo $date->diffForHumans($other); // 1時間前;
フォーマットの設定
------------------
Chronos は、出力した日時オブジェクトを表示するための多くのメソッドを提供します。 ::
// setToStringFormat() が制御するフォーマットを使用します
echo $date;
// 別の標準フォーマット
echo $time->toAtomString(); // 1975-12-25T14:15:16-05:00
echo $time->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST
echo $time->toIso8601String(); // 1975-12-25T14:15:16-05:00
echo $time->toRfc822String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc850String(); // Thursday, 25-Dec-75 14:15:16 EST
echo $time->toRfc1036String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc1123String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc2822String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc3339String(); // 1975-12-25T14:15:16-05:00
echo $time->toRssString(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toW3cString(); // 1975-12-25T14:15:16-05:00
// クォーター/週数を取得
echo $time->toQuarter(); // 4;
echo $time->toWeek(); // 52
// 一般的なフォーマット
echo $time->toTimeString(); // 14:15:16
echo $time->toDateString(); // 1975-12-25
echo $time->toDateTimeString(); // 1975-12-25 14:15:16
echo $time->toFormattedDateString(); // Dec 25, 1975
echo $time->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
日付要素の抽出
--------------
日付オブジェクトのプロパティーに直接アクセスして要素を取得することができます。 ::
$time = new Chronos('2015-12-31 23:59:58');
$time->year; // 2015
$time->month; // 12
$time->day; // 31
$time->hour // 23
$time->minute // 59
$time->second // 58
以下のプロパティーにもアクセスできます。 :
- timezone
- timezoneName
- micro
- dayOfWeek
- dayOfMonth
- dayOfYear
- daysInMonth
- timestamp
- quarter
テストの支援
------------
単体テストを書いている時、現在時刻を固定すると便利です。Chronos は、
各クラスの現在時刻を修正することができます。
テストスイートの bootstrap 処理に以下を含めることができます。 ::
Chronos::setTestNow(Chronos::now());
MutableDateTime::setTestNow(MutableDateTime::now());
Date::setTestNow(Date::now());
MutableDate::setTestNow(MutableDate::now());
これでテストスイートが開始された時点で全てのオブジェクトの現在時刻を修正します。
例えば、 ``Chronos`` を過去のある瞬間に固定した場合、新たな ``Chronos``
のインスタンスが生成する ``now`` または相対時刻の文字列は、
固定された時刻の相対を返却します。 ::
Chronos::setTestNow(new Chronos('1975-12-25 00:00:00'));
$time = new Chronos(); // 1975-12-25 00:00:00
$time = new Chronos('1 hour ago'); // 1975-12-24 23:00:00
固定をリセットするには、 ``setTestNow()`` をパラメーター無し、または ``null`` を設定して
再び呼び出してください。

View File

@@ -0,0 +1,9 @@
import sys, os
# Append the top level directory of the docs, so we can import from the config dir.
sys.path.insert(0, os.path.abspath('..'))
# Pull in all the configuration options defined in the global config file..
from config.all import *
language = 'pt'

View File

@@ -0,0 +1,7 @@
.. toctree::
:maxdepth: 2
:caption: CakePHP Chronos
/index
API <https://api.cakephp.org/chronos>

285
vendor/cakephp/chronos/docs/pt/index.rst vendored Normal file
View File

@@ -0,0 +1,285 @@
Chronos
#######
O Chronos oferece uma coleção independente de extensões para lidar com o objeto
``DateTime``. Além de métodos de conveniência, o Chronos oferece:
* Objetos ``Date`` para representar datas de calendário.
* Objetos *date* e *datetime* imutáveis.
* Um sistema de tradução acoplável. Apenas traduções em inglês estão incluídas
na biblioteca. Todavia, ``cakephp/i18n`` pode ser usado para suporte completo
a idiomas.
Instalação
----------
Para instalar o Chronos, você deve usar o ``composer``. A partir do diretório
*ROOT* de sua aplicação (local onde o arquivo composer.json está localizado)
execute o seguinte comando::
php composer.phar require cakephp/chronos "@stable"
Visão geral
-----------
Chronos oferece extensões para lidar com objetos *DateTime* do PHP. 5 classes
cobrem variantes de data/hora mutáveis e imutáveis e uma extensão do objeto
``DateInterval``.
* ``Cake\Chronos\Chronos`` é um objeto *date & time* imutável.
* ``Cake\Chronos\Date`` é um objeto *date* imutável.
* ``Cake\Chronos\MutableDateTime`` é um objeto *date and time* mutável.
* ``Cake\Chronos\MutableDate`` é um objeto *date* mutável.
* ``Cake\Chronos\ChronosInterval`` é uma extensão do objeto ``DateInterval``.
Por último, se você quiser usar o *typehint* em objetos do Chronos, será preciso
usar a interface ``Cake\Chronos\ChronosInterface``. Todos os objetos de data e
hora implementam essa interface.
Criando instâncias
------------------
Existem várias maneiras de criar instâncias do Chronos ou mesmo, do objeto Date.
Um número considerável de métodos padrão que funcionam com conjuntos diferentes
de argumentos::
use Cake\Chronos\Chronos;
$now = Chronos::now();
$today = Chronos::today();
$yesterday = Chronos::yesterday();
$tomorrow = Chronos::tomorrow();
// Interpreta expressões relativas.
$date = Chronos::parse('+2 days, +3 hours');
// Valores inteiros de Date e Time.
$date = Chronos::create(2015, 12, 25, 4, 32, 58);
// Valores inteiros de Date ou Time.
$date = Chronos::createFromDate(2015, 12, 25);
$date = Chronos::createFromTime(11, 45, 10);
// Interpreta valores formatados.
$date = Chronos::createFromFormat('m/d/Y', '06/15/2015');
Trabalhando com objetos imutáveis
---------------------------------
Se você é familiarizado com os objetos ``DateTime`` do PHP, você se sentirá
confortável com objetos *mutáveis*. Além de objetos mutáveis o Chronos também
oferece objetos imutáveis que por sua vez criam cópias de objetos toda vez que
um objeto é modificado. Devido ao fato de que metodos modificadores relativos
a data e hora nem sempre serem transparentes, informações podem ser modificadas
acidentalmente ou sem que o desenvolvedor saiba. Objetos imutáveis previnem
essas alterações acidentais nos dados. Imutabilidade significa que você deverá
lembrar de substituir variáveis usando modificadores::
// Esse código não funciona com objetos imutáveis
$time->addDay(1);
doSomething($time);
return $time;
// Esse funciona como o esperado
$time = $time->addDay(1);
$time = doSomething($time);
return $time;
Ao capturar o valor de retorno de cada modificação, seu código funcionará como o
esperado. Se você tem um objeto imutável e quer criar um mutável a partir do
mesmo, use ``toMutable()``::
$inplace = $time->toMutable();
Objetos Date
------------
O PHP disponibiliza um único objeto DateTime. Representar datas de calendário
pode ser um pouco desconfortável por essa classe, uma vez que ela inclui
*timezones* e componentes de hora que realmente não se encaixam no conceito de
'dia'. O Chronos oferece um objeto ``Date`` para representar datas. A hora e a
zona desse objeto é sempre fixado em ``00:00:00 UTC`` e todos os métodos de
formatação/diferença operam sob a resolução de dia::
use Cake\Chronos\Date;
$today = Date::today();
// Mudanças na hora/timezone são ignoradas
$today->modify('+1 hours');
// Exibe '2016-08-15'
echo $today;
Métodos modificadores
---------------------
Objetos Chronos disponibilizam métodos que permitem a modificação de valores de
forma granular::
// Define componentes do valor datetime
$halloween = Date::create()
->year(2015)
->month(10)
->day(31)
->hour(20)
->minute(30);
Você também pode modificar partes da data relativamente::
$future = Date::create()
->addYear(1)
->subMonth(2)
->addDays(15)
->addHours(20)
->subMinutes(2);
Também é possível realizar grandes saltos para períodos definidos no tempo::
$time = Chronos::create();
$time->startOfDay();
$time->startOfMonth();
$time->endOfMonth();
$time->endOfYear();
$time->startOfWeek();
$time->endOfWeek();
Ou ainda para dias específicos da semana::
$time->next(ChronosInterface::TUESDAY);
$time->previous(ChronosInterface::MONDAY);
Métodos de comparação
---------------------
Uma vez que você possui 2 instâncias de objetos data/hora do Chronos, é possível
compará-los de várias maneiras::
// Coleção completa de comparadores
// ne, gt, lt, lte.
$first->eq($second);
$first->gte($second);
// Veja se o objeto atual está entre outros
$now->between($start, $end);
// Encontre qual argumento está mais perto ou mais longe
$now->closest($june, $november);
$now->farthest($june, $november);
Você também pode arguir sobre quando um determinado valor cai no calendário::
$now->isToday();
$now->isYesterday();
$now->isFuture();
$now->isPast();
// Verifica se o dia é no final de semana
$now->isWeekend();
// Todos os métodos para outros dias da semana existem também
$now->isMonday();
Você também pode verificar se um determinado valor está dentro de um período de
tempo relativo::
$time->wasWithinLast('3 days');
$time->isWithinNext('3 hours');
Gerando diferenças
------------------
Em adição à comparação de *datetimes*, calcular diferenças ou deltas entre
valores é uma tarefa simples::
// Recebe um DateInterval representando a diferença
$first->diff($second);
// Recebe a diferença como um contador de unidades específicas
$first->diffInHours($second);
$first->diffInDays($second);
$first->diffInWeeks($second);
$first->diffInYears($second);
Você pode gerar diferenças de fácil leitura para humanos para usar em um *feed*
ou *timeline*::
// Diferença em relação ao momento atual
echo $date->diffForHumans();
// Diferença em relação a outro período no tempo
echo $date->diffForHumans($other); // 1 hora atrás;
Formatando strings
------------------
O Chronos disponibiliza métodos para exibir nossos objetos *datetime*::
// Usa o formato controlado por setToStringFormat()
echo $date;
// Diferentes padrões de formato
echo $time->toAtomString(); // 1975-12-25T14:15:16-05:00
echo $time->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST
echo $time->toIso8601String(); // 1975-12-25T14:15:16-05:00
echo $time->toRfc822String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc850String(); // Thursday, 25-Dec-75 14:15:16 EST
echo $time->toRfc1036String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc1123String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc2822String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc3339String(); // 1975-12-25T14:15:16-05:00
echo $time->toRssString(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toW3cString(); // 1975-12-25T14:15:16-05:00
// Recebe o trimestre
echo $time->toQuarter(); // 4;
Extraindo componentes de data
-----------------------------
Podemos receber partes de um objeto *date* acessando propriedades::
$time = new Chronos('2015-12-31 23:59:58');
$time->year; // 2015
$time->month; // 12
$time->day; // 31
$time->hour // 23
$time->minute // 59
$time->second // 58
Outras propriedades que podem ser acessadas são:
- timezone
- timezoneName
- micro
- dayOfWeek
- dayOfMonth
- dayOfYear
- daysInMonth
- timestamp
- quarter
Auxílio para testes
-------------------
Ao escrever testes unitários, fixar a hora atual é bastante útil. O Chronos
lhe permite fixar a hora atual para cada classe. Como parte das suas ferramentas
de testes, você pode incluir o seguinte::
Chronos::setTestNow(Chronos::now());
MutableDateTime::setTestNow(MutableDateTime::now());
Date::setTestNow(Date::now());
MutableDate::setTestNow(MutableDate::now());
Isso irá corrigir a hora atual de todos os objetos para o momento em que o
processo de testes foi iniciado.
Por exemplo, se você fixar o ``Chronos`` em algum momento no passado, qualquer
nova instância do ``Chronos`` criada com ``now`` ou uma *string* de tempo
relativa, teremos um retorno referente ao tempo fixado::
Chronos::setTestNow(new Chronos('1975-12-25 00:00:00'));
$time = new Chronos(); // 1975-12-25 00:00:00
$time = new Chronos('1 hour ago'); // 1975-12-24 23:00:00

212
vendor/cakephp/chronos/src/Chronos.php vendored Normal file
View File

@@ -0,0 +1,212 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
use DateTimeImmutable;
use DateTimeZone;
/**
* An Immutable extension on the native DateTime object.
*
* Adds a number of convenience APIs methods and the ability
* to easily convert into a mutable object.
*
* @property-read int $year
* @property-read int $yearIso
* @property-read int $month
* @property-read int $day
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read DateTimeZone $timezone the current timezone
* @property-read DateTimeZone $tz alias of timezone
* @property-read int $micro
* @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday)
* @property-read int $dayOfYear 0 through 365
* @property-read int $weekOfMonth 1 through 5
* @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday
* @property-read int $daysInMonth number of days in the given month
* @property-read int $age does a diffInYears() with default parameters
* @property-read int $quarter the quarter of this instance, 1 - 4
* @property-read int $offset the timezone offset in seconds from UTC
* @property-read int $offsetHours the timezone offset in hours from UTC
* @property-read bool $dst daylight savings time indicator, true if DST, false otherwise
* @property-read bool $local checks if the timezone is local, true if local, false otherwise
* @property-read bool $utc checks if the timezone is UTC, true if UTC, false otherwise
* @property-read string $timezoneName
* @property-read string $tzName
*/
class Chronos extends DateTimeImmutable implements ChronosInterface
{
use Traits\ComparisonTrait;
use Traits\DifferenceTrait;
use Traits\FactoryTrait;
use Traits\FormattingTrait;
use Traits\MagicPropertyTrait;
use Traits\ModifierTrait;
use Traits\RelativeKeywordTrait;
use Traits\TimezoneTrait;
/**
* A test ChronosInterface instance to be returned when now instances are created
*
* There is a single test now for all date/time classes provided by Chronos.
* This aims to emulate stubbing out 'now' which is a single global fact.
*
* @var \Cake\Chronos\ChronosInterface
*/
protected static $testNow;
/**
* Format to use for __toString method when type juggling occurs.
*
* @var string
*/
protected static $toStringFormat = ChronosInterface::DEFAULT_TO_STRING_FORMAT;
/**
* Create a new Chronos instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* @param string|null $time Fixed or relative time
* @param \DateTimeZone|string|null $tz The timezone for the instance
*/
public function __construct($time = 'now', $tz = null)
{
if ($tz !== null) {
$tz = $tz instanceof DateTimeZone ? $tz : new DateTimeZone($tz);
}
static::$_lastErrors = [];
$testNow = static::getTestNow();
if ($testNow === null) {
parent::__construct($time === null ? 'now' : $time, $tz);
return;
}
$relative = static::hasRelativeKeywords($time);
if (!empty($time) && $time !== 'now' && !$relative) {
parent::__construct($time, $tz);
return;
}
$testNow = clone $testNow;
if ($relative) {
$testNow = $testNow->modify($time);
}
$relativetime = static::isTimeExpression($time);
if (!$relativetime && $tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}
$time = $testNow->format('Y-m-d H:i:s.u');
parent::__construct($time, $tz);
}
/**
* Create a new mutable instance from current immutable instance.
*
* @return \Cake\Chronos\MutableDateTime
*/
public function toMutable()
{
return MutableDateTime::instance($this);
}
/**
* Get a copy of the instance
*
* @return $this
*/
public function copy()
{
return $this;
}
/**
* Set a ChronosInterface instance (real or mock) to be returned when a "now"
* instance is created. The provided instance will be returned
* specifically under the following conditions:
* - A call to the static now() method, ex. ChronosInterface::now()
* - When a null (or blank string) is passed to the constructor or parse(), ex. new Chronos(null)
* - When the string "now" is passed to the constructor or parse(), ex. new Chronos('now')
* - When a string containing the desired time is passed to ChronosInterface::parse()
*
* Note the timezone parameter was left out of the examples above and
* has no affect as the mock value will be returned regardless of its value.
*
* To clear the test instance call this method using the default
* parameter of null.
*
* @param \Cake\Chronos\ChronosInterface|string|null $testNow The instance to use for all future instances.
* @return void
*/
public static function setTestNow($testNow = null)
{
static::$testNow = is_string($testNow) ? static::parse($testNow) : $testNow;
}
/**
* Get the ChronosInterface instance (real or mock) to be returned when a "now"
* instance is created.
*
* @return \Cake\Chronos\ChronosInterface The current instance used for testing
*/
public static function getTestNow()
{
return static::$testNow;
}
/**
* Determine if there is a valid test instance set. A valid test instance
* is anything that is not null.
*
* @return bool True if there is a test instance, otherwise false
*/
public static function hasTestNow()
{
return static::$testNow !== null;
}
/**
* Return properties for debugging.
*
* @return array
*/
public function __debugInfo()
{
// Conditionally add properties if state exists to avoid
// errors when using a debugger.
$vars = get_object_vars($this);
$properties = [
'hasFixedNow' => static::hasTestNow(),
];
if (isset($vars['date'])) {
$properties['time'] = $this->format('Y-m-d H:i:s.u');
}
if (isset($vars['timezone'])) {
$properties['timezone'] = $this->getTimezone()->getName();
}
return $properties;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,526 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
use DateInterval;
use InvalidArgumentException;
/**
* A simple API extension for DateInterval.
* The implementation provides helpers to handle weeks but only days are saved.
* Weeks are calculated based on the total days of the current instance.
*
* @property int $years Total years of the current interval.
* @property int $months Total months of the current interval.
* @property int $weeks Total weeks of the current interval calculated from the days.
* @property int $dayz Total days of the current interval (weeks * 7 + days).
* @property int $hours Total hours of the current interval.
* @property int $minutes Total minutes of the current interval.
* @property int $seconds Total seconds of the current interval.
*
* @property-read int $dayzExcludeWeeks Total days remaining in the final week of the current instance (days % 7).
* @property-read int $daysExcludeWeeks alias of dayzExcludeWeeks
*
* @method static ChronosInterval years($years = 1) Create instance specifying a number of years.
* @method static ChronosInterval year($years = 1) Alias for years
* @method static ChronosInterval months($months = 1) Create instance specifying a number of months.
* @method static ChronosInterval month($months = 1) Alias for months
* @method static ChronosInterval weeks($weeks = 1) Create instance specifying a number of weeks.
* @method static ChronosInterval week($weeks = 1) Alias for weeks
* @method static ChronosInterval days($days = 1) Create instance specifying a number of days.
* @method static ChronosInterval dayz($days = 1) Alias for days
* @method static ChronosInterval day($days = 1) Alias for days
* @method static ChronosInterval hours($hours = 1) Create instance specifying a number of hours.
* @method static ChronosInterval hour($hours = 1) Alias for hours
* @method static ChronosInterval minutes($minutes = 1) Create instance specifying a number of minutes.
* @method static ChronosInterval minute($minutes = 1) Alias for minutes
* @method static ChronosInterval seconds($seconds = 1) Create instance specifying a number of seconds.
* @method static ChronosInterval second($seconds = 1) Alias for seconds
*
* @method ChronosInterval years() years($years = 1) Set the years portion of the current interval.
* @method ChronosInterval year() year($years = 1) Alias for years.
* @method ChronosInterval months() months($months = 1) Set the months portion of the current interval.
* @method ChronosInterval month() month($months = 1) Alias for months.
* @method ChronosInterval weeks() weeks($weeks = 1) Set the weeks portion of the current interval. Will overwrite dayz value.
* @method ChronosInterval week() week($weeks = 1) Alias for weeks.
* @method ChronosInterval days() days($days = 1) Set the days portion of the current interval.
* @method ChronosInterval dayz() dayz($days = 1) Alias for days.
* @method ChronosInterval day() day($days = 1) Alias for days.
* @method ChronosInterval hours() hours($hours = 1) Set the hours portion of the current interval.
* @method ChronosInterval hour() hour($hours = 1) Alias for hours.
* @method ChronosInterval minutes() minutes($minutes = 1) Set the minutes portion of the current interval.
* @method ChronosInterval minute() minute($minutes = 1) Alias for minutes.
* @method ChronosInterval seconds() seconds($seconds = 1) Set the seconds portion of the current interval.
* @method ChronosInterval second() second($seconds = 1) Alias for seconds.
*/
class ChronosInterval extends DateInterval
{
/**
* Interval spec period designators
*/
const PERIOD_PREFIX = 'P';
const PERIOD_YEARS = 'Y';
const PERIOD_MONTHS = 'M';
const PERIOD_DAYS = 'D';
const PERIOD_TIME_PREFIX = 'T';
const PERIOD_HOURS = 'H';
const PERIOD_MINUTES = 'M';
const PERIOD_SECONDS = 'S';
/**
* Before PHP 5.4.20/5.5.4 instead of `false` days will be set to -99999 when the interval instance
* was created by DateTime:diff().
*/
const PHP_DAYS_FALSE = -99999;
/**
* Whether or not this object was created in HHVM
*
* @var bool
*/
protected $isHHVM = false;
/**
* Determine if the interval was created via DateTime:diff() or not.
*
* @param \DateInterval $interval The interval to check.
* @return bool
*/
protected static function wasCreatedFromDiff(DateInterval $interval)
{
return ($interval->days !== false && $interval->days !== static::PHP_DAYS_FALSE);
}
/**
* Create a new ChronosInterval instance.
*
* @param int|null $years The year to use.
* @param int|null $months The month to use.
* @param int|null $weeks The week to use.
* @param int|null $days The day to use.
* @param int|null $hours The hours to use.
* @param int|null $minutes The minutes to use.
* @param int|null $seconds The seconds to use.
*/
public function __construct($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null)
{
$this->isHHVM = defined('HHVM_VERSION');
$spec = static::PERIOD_PREFIX;
$spec .= $years > 0 ? $years . static::PERIOD_YEARS : '';
$spec .= $months > 0 ? $months . static::PERIOD_MONTHS : '';
$specDays = 0;
$specDays += $weeks > 0 ? $weeks * ChronosInterface::DAYS_PER_WEEK : 0;
$specDays += $days > 0 ? $days : 0;
$spec .= ($specDays > 0) ? $specDays . static::PERIOD_DAYS : '';
if ($spec === static::PERIOD_PREFIX) {
$spec .= '0' . static::PERIOD_YEARS;
}
if ($hours > 0 || $minutes > 0 || $seconds > 0) {
$spec .= static::PERIOD_TIME_PREFIX;
$spec .= $hours > 0 ? $hours . static::PERIOD_HOURS : '';
$spec .= $minutes > 0 ? $minutes . static::PERIOD_MINUTES : '';
$spec .= $seconds > 0 ? $seconds . static::PERIOD_SECONDS : '';
}
parent::__construct($spec);
}
/**
* Create a new ChronosInterval instance from specific values.
* This is an alias for the constructor that allows better fluent
* syntax as it allows you to do ChronosInterval::create(1)->fn() rather than
* (new ChronosInterval(1))->fn().
*
* @param int|null $years The year to use.
* @param int|null $months The month to use.
* @param int|null $weeks The week to use.
* @param int|null $days The day to use.
* @param int|null $hours The hours to use.
* @param int|null $minutes The minutes to use.
* @param int|null $seconds The seconds to use.
* @return static
*/
public static function create($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null)
{
return new static($years, $months, $weeks, $days, $hours, $minutes, $seconds);
}
/**
* Provide static helpers to create instances. Allows:
*
* ```
* ChronosInterval::years(3)
* // or
* ChronosInterval::month(1);
* ```
*
* Note: This is done using the magic method to allow static and instance methods to
* have the same names.
*
* @param string $name The property to configure. Accepts singular and plural forms.
* @param array $args Contains the value to use.
* @return static
*/
public static function __callStatic($name, $args)
{
$arg = count($args) === 0 ? 1 : $args[0];
switch ($name) {
case 'years':
case 'year':
return new static($arg);
case 'months':
case 'month':
return new static(null, $arg);
case 'weeks':
case 'week':
return new static(null, null, $arg);
case 'days':
case 'dayz':
case 'day':
return new static(null, null, null, $arg);
case 'hours':
case 'hour':
return new static(null, null, null, null, $arg);
case 'minutes':
case 'minute':
return new static(null, null, null, null, null, $arg);
case 'seconds':
case 'second':
return new static(null, null, null, null, null, null, $arg);
}
}
/**
* Create a ChronosInterval instance from a DateInterval one. Can not instance
* DateInterval objects created from DateTime::diff() as you can't externally
* set the $days field.
*
* @param \DateInterval $di The DateInterval instance to copy.
* @throws \InvalidArgumentException
* @return static
*/
public static function instance(DateInterval $di)
{
if (static::wasCreatedFromDiff($di)) {
throw new InvalidArgumentException(
"Can not instance a DateInterval object created from DateTime::diff()."
);
}
$instance = new static($di->y, $di->m, 0, $di->d, $di->h, $di->i, $di->s);
$instance->invert = $di->invert;
$instance->days = $di->days;
return $instance;
}
/**
* Get a part of the ChronosInterval object
*
* @param string $name The property to read.
* @throws \InvalidArgumentException
* @return int
*/
public function __get($name)
{
switch ($name) {
case 'years':
return $this->isHHVM ? parent::__get('y') : $this->y;
case 'months':
return $this->isHHVM ? parent::__get('m') : $this->m;
case 'dayz':
return $this->isHHVM ? parent::__get('d') : $this->d;
case 'hours':
return $this->isHHVM ? parent::__get('h') : $this->h;
case 'minutes':
return $this->isHHVM ? parent::__get('i') : $this->i;
case 'seconds':
return $this->isHHVM ? parent::__get('s') : $this->s;
case 'weeks':
return (int)floor(($this->isHHVM ? parent::__get('d') : $this->d) / ChronosInterface::DAYS_PER_WEEK);
case 'daysExcludeWeeks':
case 'dayzExcludeWeeks':
return $this->dayz % ChronosInterface::DAYS_PER_WEEK;
case 'days':
return $this->isHHVM ? parent::__get('days') : $this->days;
case 'y':
case 'm':
case 'd':
case 'h':
case 'i':
case 's':
case 'invert':
return parent::__get($name);
default:
throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name));
}
}
/**
* Set a part of the ChronosInterval object
*
* @param string $name The property to augment.
* @param int $val The value to change.
* @return void
* @throws \InvalidArgumentException
*/
public function __set($name, $val)
{
switch ($name) {
case 'years':
$this->isHHVM ? parent::__set('y', $val) : $this->y = $val;
break;
case 'months':
$this->isHHVM ? parent::__set('m', $val) : $this->m = $val;
break;
case 'weeks':
$val = $val * ChronosInterface::DAYS_PER_WEEK;
$this->isHHVM ? parent::__set('d', $val) : $this->d = $val;
break;
case 'dayz':
$this->isHHVM ? parent::__set('d', $val) : $this->d = $val;
break;
case 'hours':
$this->isHHVM ? parent::__set('h', $val) : $this->h = $val;
break;
case 'minutes':
$this->isHHVM ? parent::__set('i', $val) : $this->i = $val;
break;
case 'seconds':
$this->isHHVM ? parent::__set('s', $val) : $this->s = $val;
break;
case 'invert':
$this->isHHVM ? parent::__set('invert', $val) : $this->invert = $val;
break;
default:
if ($this->isHHVM) {
parent::__set($name, $val);
}
}
}
/**
* Allow setting of weeks and days to be cumulative.
*
* @param int $weeks Number of weeks to set
* @param int $days Number of days to set
* @return static
*/
public function weeksAndDays($weeks, $days)
{
$this->dayz = ($weeks * ChronosInterface::DAYS_PER_WEEK) + $days;
return $this;
}
/**
* Allow fluent calls on the setters... ChronosInterval::years(3)->months(5)->day().
*
* Note: This is done using the magic method to allow static and instance methods to
* have the same names.
*
* @param string $name The property name to augment. Accepts plural forms in addition
* to singular ones.
* @param array $args The value to set.
* @return static
*/
public function __call($name, $args)
{
$arg = count($args) === 0 ? 1 : $args[0];
switch ($name) {
case 'years':
case 'year':
$this->years = $arg;
break;
case 'months':
case 'month':
$this->months = $arg;
break;
case 'weeks':
case 'week':
$this->dayz = $arg * ChronosInterface::DAYS_PER_WEEK;
break;
case 'days':
case 'dayz':
case 'day':
$this->dayz = $arg;
break;
case 'hours':
case 'hour':
$this->hours = $arg;
break;
case 'minutes':
case 'minute':
$this->minutes = $arg;
break;
case 'seconds':
case 'second':
$this->seconds = $arg;
break;
}
return $this;
}
/**
* Add the passed interval to the current instance
*
* @param \DateInterval $interval The interval to add.
* @return static
*/
public function add(DateInterval $interval)
{
$sign = ($interval->invert === 1) ? -1 : 1;
if (static::wasCreatedFromDiff($interval)) {
$this->dayz = $this->dayz + ($interval->days * $sign);
} else {
$this->years = $this->years + ($interval->y * $sign);
$this->months = $this->months + ($interval->m * $sign);
$this->dayz = $this->dayz + ($interval->d * $sign);
$this->hours = $this->hours + ($interval->h * $sign);
$this->minutes = $this->minutes + ($interval->i * $sign);
$this->seconds = $this->seconds + ($interval->s * $sign);
}
return $this;
}
/**
* Returns the ISO 8601 interval string.
*
* @return string Interval as string representation
*/
public function __toString()
{
// equivalence
$oneMinuteInSeconds = 60;
$oneHourInSeconds = $oneMinuteInSeconds * 60;
$oneDayInSeconds = $oneHourInSeconds * 24;
$oneMonthInDays = 365 / 12;
$oneMonthInSeconds = $oneDayInSeconds * $oneMonthInDays;
$oneYearInSeconds = 12 * $oneMonthInSeconds;
// convert
$ySecs = $this->y * $oneYearInSeconds;
$mSecs = $this->m * $oneMonthInSeconds;
$dSecs = $this->d * $oneDayInSeconds;
$hSecs = $this->h * $oneHourInSeconds;
$iSecs = $this->i * $oneMinuteInSeconds;
$sSecs = $this->s;
$totalSecs = $ySecs + $mSecs + $dSecs + $hSecs + $iSecs + $sSecs;
$y = null;
$m = null;
$d = null;
$h = null;
$i = null;
// years
if ($totalSecs >= $oneYearInSeconds) {
$y = floor($totalSecs / $oneYearInSeconds);
$totalSecs = $totalSecs - $y * $oneYearInSeconds;
}
// months
if ($totalSecs >= $oneMonthInSeconds) {
$m = floor($totalSecs / $oneMonthInSeconds);
$totalSecs = $totalSecs - $m * $oneMonthInSeconds;
}
// days
if ($totalSecs >= $oneDayInSeconds) {
$d = floor($totalSecs / $oneDayInSeconds);
$totalSecs = $totalSecs - $d * $oneDayInSeconds;
}
// hours
if ($totalSecs >= $oneHourInSeconds) {
$h = floor($totalSecs / $oneHourInSeconds);
$totalSecs = $totalSecs - $h * $oneHourInSeconds;
}
// minutes
if ($totalSecs >= $oneMinuteInSeconds) {
$i = floor($totalSecs / $oneMinuteInSeconds);
$totalSecs = $totalSecs - $i * $oneMinuteInSeconds;
}
$s = $totalSecs;
$date = array_filter([
static::PERIOD_YEARS => $y,
static::PERIOD_MONTHS => $m,
static::PERIOD_DAYS => $d,
]);
$time = array_filter([
static::PERIOD_HOURS => $h,
static::PERIOD_MINUTES => $i,
static::PERIOD_SECONDS => $s,
]);
$specString = static::PERIOD_PREFIX;
foreach ($date as $key => $value) {
$specString .= $value . $key;
}
if (count($time) > 0) {
$specString .= static::PERIOD_TIME_PREFIX;
foreach ($time as $key => $value) {
$specString .= $value . $key;
}
}
if ($specString === static::PERIOD_PREFIX) {
return 'PT0S';
}
return $this->invert === 1 ? '-' . $specString : $specString;
}
}

150
vendor/cakephp/chronos/src/Date.php vendored Normal file
View File

@@ -0,0 +1,150 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
use DateTimeImmutable;
use DateTimeZone;
/**
* An immutable date object that converts all time components into 00:00:00.
*
* This class is useful when you want to represent a calendar date and ignore times.
* This means that timezone changes take no effect as a calendar date exists in all timezones
* in each respective date.
*
* @property-read int $year
* @property-read int $yearIso
* @property-read int $month
* @property-read int $day
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read DateTimeZone $timezone the current timezone
* @property-read DateTimeZone $tz alias of timezone
* @property-read int $micro
* @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday)
* @property-read int $dayOfYear 0 through 365
* @property-read int $weekOfMonth 1 through 5
* @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday
* @property-read int $daysInMonth number of days in the given month
* @property-read int $age does a diffInYears() with default parameters
* @property-read int $quarter the quarter of this instance, 1 - 4
* @property-read int $offset the timezone offset in seconds from UTC
* @property-read int $offsetHours the timezone offset in hours from UTC
* @property-read bool $dst daylight savings time indicator, true if DST, false otherwise
* @property-read bool $local checks if the timezone is local, true if local, false otherwise
* @property-read bool $utc checks if the timezone is UTC, true if UTC, false otherwise
* @property-read string $timezoneName
* @property-read string $tzName
*/
class Date extends DateTimeImmutable implements ChronosInterface
{
use Traits\ComparisonTrait;
use Traits\DifferenceTrait;
use Traits\FactoryTrait;
use Traits\FormattingTrait;
use Traits\FrozenTimeTrait;
use Traits\MagicPropertyTrait;
use Traits\ModifierTrait;
use Traits\TestingAidTrait;
/**
* Format to use for __toString method when type juggling occurs.
*
* @var string
*/
protected static $toStringFormat = 'Y-m-d';
/**
* Create a new Immutable Date instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* Date instances lack time components, however due to limitations in PHP's
* internal Datetime object the time will always be set to 00:00:00, and the
* timezone will always be UTC. Normalizing the timezone allows for
* subtraction/addition to have deterministic results.
*
* @param string|null $time Fixed or relative time
*/
public function __construct($time = 'now')
{
$tz = new DateTimeZone('UTC');
$testNow = Chronos::getTestNow();
if ($testNow === null) {
$time = $this->stripTime($time);
parent::__construct($time, $tz);
return;
}
$relative = static::hasRelativeKeywords($time);
if (!empty($time) && $time !== 'now' && !$relative) {
$time = $this->stripTime($time);
parent::__construct($time, $tz);
return;
}
$testNow = clone $testNow;
if ($relative) {
$time = $this->stripRelativeTime($time);
if (strlen($time) > 0) {
$testNow = $testNow->modify($time);
}
}
if ($tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}
$time = $testNow->format('Y-m-d 00:00:00');
parent::__construct($time, $tz);
}
/**
* Create a new mutable instance from current immutable instance.
*
* @return \Cake\Chronos\MutableDate
*/
public function toMutable()
{
return MutableDate::instance($this);
}
/**
* Return properties for debugging.
*
* @return array
*/
public function __debugInfo()
{
// Conditionally add properties if state exists to avoid
// errors when using a debugger.
$vars = get_object_vars($this);
$properties = [
'hasFixedNow' => static::hasTestNow(),
];
if (isset($vars['date'])) {
$properties['date'] = $this->format('Y-m-d');
}
return $properties;
}
}

View File

@@ -0,0 +1,103 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
/**
* Handles formatting differences in text.
*
* Provides a swappable component for other libraries to leverage.
* when localizing or customizing the difference output.
*/
class DifferenceFormatter
{
/**
* The text translator object
*
* @var \Cake\Chronos\Translator
*/
protected $translate;
/**
* Constructor.
*
* @param \Cake\Chronos\Translator|null $translate The text translator object.
*/
public function __construct($translate = null)
{
$this->translate = $translate ?: new Translator();
}
/**
* Get the difference in a human readable format.
*
* @param \Cake\Chronos\ChronosInterface $date The datetime to start with.
* @param \Cake\Chronos\ChronosInterface|null $other The datetime to compare against.
* @param bool $absolute removes time difference modifiers ago, after, etc
* @return string The difference between the two days in a human readable format
* @see \Cake\Chronos\ChronosInterface::diffForHumans
*/
public function diffForHumans(ChronosInterface $date, ChronosInterface $other = null, $absolute = false)
{
$isNow = $other === null;
if ($isNow) {
$other = $date->now($date->tz);
}
$diffInterval = $date->diff($other);
switch (true) {
case ($diffInterval->y > 0):
$unit = 'year';
$count = $diffInterval->y;
break;
case ($diffInterval->m > 0):
$unit = 'month';
$count = $diffInterval->m;
break;
case ($diffInterval->d > 0):
$unit = 'day';
$count = $diffInterval->d;
if ($count >= ChronosInterface::DAYS_PER_WEEK) {
$unit = 'week';
$count = (int)($count / ChronosInterface::DAYS_PER_WEEK);
}
break;
case ($diffInterval->h > 0):
$unit = 'hour';
$count = $diffInterval->h;
break;
case ($diffInterval->i > 0):
$unit = 'minute';
$count = $diffInterval->i;
break;
default:
$count = $diffInterval->s;
$unit = 'second';
break;
}
$time = $this->translate->plural($unit, $count, ['count' => $count]);
if ($absolute) {
return $time;
}
$isFuture = $diffInterval->invert === 1;
$transId = $isNow ? ($isFuture ? 'from_now' : 'ago') : ($isFuture ? 'after' : 'before');
// Some langs have special pluralization for past and future tense.
$tryKeyExists = $unit . '_' . $transId;
if ($this->translate->exists($tryKeyExists)) {
$time = $this->translate->plural($tryKeyExists, $count, ['count' => $count]);
}
return $this->translate->singular($transId, ['time' => $time]);
}
}

View File

@@ -0,0 +1,149 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
use DateTime;
use DateTimeZone;
/**
* A mutable date object that converts all time components into 00:00:00.
*
* This class is useful when you want to represent a calendar date and ignore times.
* This means that timezone changes take no effect as a calendar date exists in all timezones
* in each respective date.
*
* @property-read int $year
* @property-read int $yearIso
* @property-read int $month
* @property-read int $day
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read DateTimeZone $timezone the current timezone
* @property-read DateTimeZone $tz alias of timezone
* @property-read int $micro
* @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday)
* @property-read int $dayOfYear 0 through 365
* @property-read int $weekOfMonth 1 through 5
* @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday
* @property-read int $daysInMonth number of days in the given month
* @property-read int $age does a diffInYears() with default parameters
* @property-read int $quarter the quarter of this instance, 1 - 4
* @property-read int $offset the timezone offset in seconds from UTC
* @property-read int $offsetHours the timezone offset in hours from UTC
* @property-read bool $dst daylight savings time indicator, true if DST, false otherwise
* @property-read bool $local checks if the timezone is local, true if local, false otherwise
* @property-read bool $utc checks if the timezone is UTC, true if UTC, false otherwise
* @property-read string $timezoneName
* @property-read string $tzName
*/
class MutableDate extends DateTime implements ChronosInterface
{
use Traits\ComparisonTrait;
use Traits\DifferenceTrait;
use Traits\FactoryTrait;
use Traits\FormattingTrait;
use Traits\FrozenTimeTrait;
use Traits\MagicPropertyTrait;
use Traits\ModifierTrait;
use Traits\TestingAidTrait;
/**
* Format to use for __toString method when type juggling occurs.
*
* @var string
*/
protected static $toStringFormat = 'Y-m-d';
/**
* Create a new mutable Date instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* Date instances lack time components, however due to limitations in PHP's
* internal Datetime object the time will always be set to 00:00:00, and the
* timezone will always be UTC. Normalizing the timezone allows for
* subtraction/addition to have deterministic results.
*
* @param string|null $time Fixed or relative time
*/
public function __construct($time = 'now')
{
$tz = new DateTimeZone('UTC');
$testNow = Chronos::getTestNow();
if ($testNow === null) {
$time = $this->stripTime($time);
parent::__construct($time, $tz);
return;
}
$relative = static::hasRelativeKeywords($time);
if (!empty($time) && $time !== 'now' && !$relative) {
$time = $this->stripTime($time);
parent::__construct($time, $tz);
return;
}
$testNow = clone $testNow;
if ($relative) {
$time = $this->stripRelativeTime($time);
if (strlen($time) > 0) {
$testNow = $testNow->modify($time);
}
}
if ($tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}
$time = $testNow->format('Y-m-d 00:00:00');
parent::__construct($time, $tz);
}
/**
* Create a new immutable instance from current mutable instance.
*
* @return \Cake\Chronos\Date
*/
public function toImmutable()
{
return Date::instance($this);
}
/**
* Return properties for debugging.
*
* @return array
*/
public function __debugInfo()
{
// Conditionally add properties if state exists to avoid
// errors when using a debugger.
$vars = get_object_vars($this);
$properties = [
'hasFixedNow' => static::hasTestNow(),
];
if (isset($vars['date'])) {
$properties['date'] = $this->format('Y-m-d');
}
return $properties;
}
}

View File

@@ -0,0 +1,196 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
use DateTime;
use DateTimeZone;
use InvalidArgumentException;
/**
* A mutable datetime instance that implements the ChronosInterface.
*
* This object can be mutated in place using any setter method,
* or __set().
*
* @property-read int $year
* @property-read int $yearIso
* @property-read int $month
* @property-read int $day
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read DateTimeZone $timezone the current timezone
* @property-read DateTimeZone $tz alias of timezone
* @property-read int $micro
* @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday)
* @property-read int $dayOfYear 0 through 365
* @property-read int $weekOfMonth 1 through 5
* @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday
* @property-read int $daysInMonth number of days in the given month
* @property-read int $age does a diffInYears() with default parameters
* @property-read int $quarter the quarter of this instance, 1 - 4
* @property-read int $offset the timezone offset in seconds from UTC
* @property-read int $offsetHours the timezone offset in hours from UTC
* @property-read bool $dst daylight savings time indicator, true if DST, false otherwise
* @property-read bool $local checks if the timezone is local, true if local, false otherwise
* @property-read bool $utc checks if the timezone is UTC, true if UTC, false otherwise
* @property-read string $timezoneName
* @property-read string $tzName
*/
class MutableDateTime extends DateTime implements ChronosInterface
{
use Traits\ComparisonTrait;
use Traits\DifferenceTrait;
use Traits\FactoryTrait;
use Traits\FormattingTrait;
use Traits\MagicPropertyTrait;
use Traits\ModifierTrait;
use Traits\RelativeKeywordTrait;
use Traits\TestingAidTrait;
use Traits\TimezoneTrait;
/**
* Format to use for __toString method when type juggling occurs.
*
* @var string
*/
protected static $toStringFormat = ChronosInterface::DEFAULT_TO_STRING_FORMAT;
/**
* Create a new MutableDateTime instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* @param string|null $time Fixed or relative time
* @param \DateTimeZone|string|null $tz The timezone for the instance
*/
public function __construct($time = 'now', $tz = null)
{
if ($tz !== null) {
$tz = $tz instanceof DateTimeZone ? $tz : new DateTimeZone($tz);
}
$testNow = Chronos::getTestNow();
if ($testNow === null) {
parent::__construct($time === null ? 'now' : $time, $tz);
return;
}
$relative = static::hasRelativeKeywords($time);
if (!empty($time) && $time !== 'now' && !$relative) {
parent::__construct($time, $tz);
return;
}
$testNow = clone $testNow;
if ($relative) {
$testNow = $testNow->modify($time);
}
$relativetime = static::isTimeExpression($time);
if (!$relativetime && $tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}
$time = $testNow->format('Y-m-d H:i:s.u');
parent::__construct($time, $tz);
}
/**
* Create a new immutable instance from current mutable instance.
*
* @return Chronos
*/
public function toImmutable()
{
return Chronos::instance($this);
}
/**
* Set a part of the ChronosInterface object
*
* @param string $name The property to set.
* @param string|int|\DateTimeZone $value The value to set.
* @throws \InvalidArgumentException
* @return void
*/
public function __set($name, $value)
{
switch ($name) {
case 'year':
$this->year($value);
break;
case 'month':
$this->month($value);
break;
case 'day':
$this->day($value);
break;
case 'hour':
$this->hour($value);
break;
case 'minute':
$this->minute($value);
break;
case 'second':
$this->second($value);
break;
case 'timestamp':
$this->timestamp($value);
break;
case 'timezone':
case 'tz':
$this->timezone($value);
break;
default:
throw new InvalidArgumentException(sprintf("Unknown setter '%s'", $name));
}
}
/**
* Return properties for debugging.
*
* @return array
*/
public function __debugInfo()
{
// Conditionally add properties if state exists to avoid
// errors when using a debugger.
$vars = get_object_vars($this);
$properties = [
'hasFixedNow' => static::hasTestNow(),
];
if (isset($vars['date'])) {
$properties['time'] = $this->format('Y-m-d H:i:s.u');
}
if (isset($vars['timezone'])) {
$properties['timezone'] = $this->getTimezone()->getName();
}
return $properties;
}
}

View File

@@ -0,0 +1,499 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
use DateTime;
/**
* Provides various comparison operator methods for datetime objects.
*/
trait ComparisonTrait
{
use CopyTrait;
/**
* Days of weekend
*
* @var array
*/
protected static $weekendDays = [ChronosInterface::SATURDAY, ChronosInterface::SUNDAY];
/**
* Get weekend days
*
* @return array
*/
public static function getWeekendDays()
{
return static::$weekendDays;
}
/**
* Set weekend days
*
* @param array $days Which days are 'weekends'.
* @return void
*/
public static function setWeekendDays($days)
{
static::$weekendDays = $days;
}
/**
* Determines if the instance is equal to another
*
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function eq(ChronosInterface $dt)
{
return $this == $dt;
}
/**
* Determines if the instance is not equal to another
*
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function ne(ChronosInterface $dt)
{
return !$this->eq($dt);
}
/**
* Determines if the instance is greater (after) than another
*
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function gt(ChronosInterface $dt)
{
return $this > $dt;
}
/**
* Determines if the instance is greater (after) than or equal to another
*
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function gte(ChronosInterface $dt)
{
return $this >= $dt;
}
/**
* Determines if the instance is less (before) than another
*
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function lt(ChronosInterface $dt)
{
return $this < $dt;
}
/**
* Determines if the instance is less (before) or equal to another
*
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function lte(ChronosInterface $dt)
{
return $this <= $dt;
}
/**
* Determines if the instance is between two others
*
* @param \Cake\Chronos\ChronosInterface $dt1 The instance to compare with.
* @param \Cake\Chronos\ChronosInterface $dt2 The instance to compare with.
* @param bool $equal Indicates if a > and < comparison should be used or <= or >=
* @return bool
*/
public function between(ChronosInterface $dt1, ChronosInterface $dt2, $equal = true)
{
if ($dt1->gt($dt2)) {
$temp = $dt1;
$dt1 = $dt2;
$dt2 = $temp;
}
if ($equal) {
return $this->gte($dt1) && $this->lte($dt2);
}
return $this->gt($dt1) && $this->lt($dt2);
}
/**
* Get the closest date from the instance.
*
* @param \Cake\Chronos\ChronosInterface $dt1 The instance to compare with.
* @param \Cake\Chronos\ChronosInterface $dt2 The instance to compare with.
* @return \Cake\Chronos\ChronosInterface
*/
public function closest(ChronosInterface $dt1, ChronosInterface $dt2)
{
return $this->diffInSeconds($dt1) < $this->diffInSeconds($dt2) ? $dt1 : $dt2;
}
/**
* Get the farthest date from the instance.
*
* @param \Cake\Chronos\ChronosInterface $dt1 The instance to compare with.
* @param \Cake\Chronos\ChronosInterface $dt2 The instance to compare with.
* @return \Cake\Chronos\ChronosInterface
*/
public function farthest(ChronosInterface $dt1, ChronosInterface $dt2)
{
return $this->diffInSeconds($dt1) > $this->diffInSeconds($dt2) ? $dt1 : $dt2;
}
/**
* Get the minimum instance between a given instance (default now) and the current instance.
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to compare with.
* @return static
*/
public function min(ChronosInterface $dt = null)
{
$dt = ($dt === null) ? static::now($this->tz) : $dt;
return $this->lt($dt) ? $this : $dt;
}
/**
* Get the maximum instance between a given instance (default now) and the current instance.
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to compare with.
* @return static
*/
public function max(ChronosInterface $dt = null)
{
$dt = ($dt === null) ? static::now($this->tz) : $dt;
return $this->gt($dt) ? $this : $dt;
}
/**
* Determines if the instance is a weekday
*
* @return bool
*/
public function isWeekday()
{
return !$this->isWeekend();
}
/**
* Determines if the instance is a weekend day
*
* @return bool
*/
public function isWeekend()
{
return in_array($this->dayOfWeek, self::$weekendDays, true);
}
/**
* Determines if the instance is yesterday
*
* @return bool
*/
public function isYesterday()
{
return $this->toDateString() === static::yesterday($this->tz)->toDateString();
}
/**
* Determines if the instance is today
*
* @return bool
*/
public function isToday()
{
return $this->toDateString() === static::now($this->tz)->toDateString();
}
/**
* Determines if the instance is tomorrow
*
* @return bool
*/
public function isTomorrow()
{
return $this->toDateString() === static::tomorrow($this->tz)->toDateString();
}
/**
* Determines if the instance is within the next week
*
* @return bool
*/
public function isNextWeek()
{
return $this->format('W o') === static::now($this->tz)->addWeek()->format('W o');
}
/**
* Determines if the instance is within the last week
*
* @return bool
*/
public function isLastWeek()
{
return $this->format('W o') === static::now($this->tz)->subWeek()->format('W o');
}
/**
* Determines if the instance is within the next month
*
* @return bool
*/
public function isNextMonth()
{
return $this->format('m Y') === static::now($this->tz)->addMonth()->format('m Y');
}
/**
* Determines if the instance is within the last month
*
* @return bool
*/
public function isLastMonth()
{
return $this->format('m Y') === static::now($this->tz)->subMonth()->format('m Y');
}
/**
* Determines if the instance is within the next year
*
* @return bool
*/
public function isNextYear()
{
return $this->year === static::now($this->tz)->addYear()->year;
}
/**
* Determines if the instance is within the last year
*
* @return bool
*/
public function isLastYear()
{
return $this->year === static::now($this->tz)->subYear()->year;
}
/**
* Determines if the instance is in the future, ie. greater (after) than now
*
* @return bool
*/
public function isFuture()
{
return $this->gt(static::now($this->tz));
}
/**
* Determines if the instance is in the past, ie. less (before) than now
*
* @return bool
*/
public function isPast()
{
return $this->lt(static::now($this->tz));
}
/**
* Determines if the instance is a leap year
*
* @return bool
*/
public function isLeapYear()
{
return $this->format('L') === '1';
}
/**
* Checks if the passed in date is the same day as the instance current day.
*
* @param \Cake\Chronos\ChronosInterface $dt The instance to check against.
* @return bool
*/
public function isSameDay(ChronosInterface $dt)
{
return $this->toDateString() === $dt->toDateString();
}
/**
* Checks if this day is a Sunday.
*
* @return bool
*/
public function isSunday()
{
return $this->dayOfWeek === ChronosInterface::SUNDAY;
}
/**
* Checks if this day is a Monday.
*
* @return bool
*/
public function isMonday()
{
return $this->dayOfWeek === ChronosInterface::MONDAY;
}
/**
* Checks if this day is a Tuesday.
*
* @return bool
*/
public function isTuesday()
{
return $this->dayOfWeek === ChronosInterface::TUESDAY;
}
/**
* Checks if this day is a Wednesday.
*
* @return bool
*/
public function isWednesday()
{
return $this->dayOfWeek === ChronosInterface::WEDNESDAY;
}
/**
* Checks if this day is a Thursday.
*
* @return bool
*/
public function isThursday()
{
return $this->dayOfWeek === ChronosInterface::THURSDAY;
}
/**
* Checks if this day is a Friday.
*
* @return bool
*/
public function isFriday()
{
return $this->dayOfWeek === ChronosInterface::FRIDAY;
}
/**
* Checks if this day is a Saturday.
*
* @return bool
*/
public function isSaturday()
{
return $this->dayOfWeek === ChronosInterface::SATURDAY;
}
/**
* Returns true if this object represents a date within the current week
*
* @return bool
*/
public function isThisWeek()
{
return static::now($this->getTimezone())->format('W o') === $this->format('W o');
}
/**
* Returns true if this object represents a date within the current month
*
* @return bool
*/
public function isThisMonth()
{
return static::now($this->getTimezone())->format('m Y') === $this->format('m Y');
}
/**
* Returns true if this object represents a date within the current year
*
* @return bool
*/
public function isThisYear()
{
return static::now($this->getTimezone())->format('Y') === $this->format('Y');
}
/**
* Check if its the birthday. Compares the date/month values of the two dates.
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to compare with or null to use current day.
* @return static
*/
public function isBirthday(ChronosInterface $dt = null)
{
if ($dt === null) {
$dt = static::now($this->tz);
}
return $this->format('md') === $dt->format('md');
}
/**
* Returns true this instance happened within the specified interval
*
* @param string|int $timeInterval the numeric value with space then time type.
* Example of valid types: 6 hours, 2 days, 1 minute.
* @return bool
*/
public function wasWithinLast($timeInterval)
{
$now = new static();
$interval = $now->copy()->modify('-' . $timeInterval);
$thisTime = $this->format('U');
return $thisTime >= $interval->format('U') && $thisTime <= $now->format('U');
}
/**
* Returns true this instance will happen within the specified interval
*
* @param string|int $timeInterval the numeric value with space then time type.
* Example of valid types: 6 hours, 2 days, 1 minute.
* @return bool
*/
public function isWithinNext($timeInterval)
{
$now = new static();
$interval = $now->copy()->modify('+' . $timeInterval);
$thisTime = $this->format('U');
return $thisTime <= $interval->format('U') && $thisTime >= $now->format('U');
}
/**
* Check if instance of ChronosInterface is mutable.
*
* @return bool
*/
public function isMutable()
{
return $this instanceof DateTime;
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
/**
* Provides methods for copying datetime objects.
*
* Expects that implementing classes provide a static `instance()` method.
*/
trait CopyTrait
{
/**
* Get a copy of the instance
*
* @return static
*/
public function copy()
{
return static::instance($this);
}
}

View File

@@ -0,0 +1,302 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
use Cake\Chronos\ChronosInterval;
use Cake\Chronos\DifferenceFormatter;
use DatePeriod;
use DateTimeImmutable;
use DateTimeInterface;
/**
* Provides methods for getting differences between datetime objects.
*
* Expects that the implementing class implements:
*
* - static::now()
* - static::instance()
* - copy()
*/
trait DifferenceTrait
{
/**
* Instance of the diff formatting object.
*
* @var \Cake\Chronos\DifferenceFormatter
*/
protected static $diffFormatter;
/**
* Get the difference in years
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInYears(ChronosInterface $dt = null, $abs = true)
{
$dt = $dt === null ? static::now($this->tz) : $dt;
return (int)$this->diff($dt, $abs)->format('%r%y');
}
/**
* Get the difference in months
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInMonths(ChronosInterface $dt = null, $abs = true)
{
$dt = $dt === null ? static::now($this->tz) : $dt;
return $this->diffInYears($dt, $abs) * ChronosInterface::MONTHS_PER_YEAR + (int)$this->diff($dt, $abs)->format('%r%m');
}
/**
* Get the difference in weeks
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeeks(ChronosInterface $dt = null, $abs = true)
{
return (int)($this->diffInDays($dt, $abs) / ChronosInterface::DAYS_PER_WEEK);
}
/**
* Get the difference in days
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInDays(ChronosInterface $dt = null, $abs = true)
{
$dt = $dt === null ? static::now($this->tz) : $dt;
return (int)$this->diff($dt, $abs)->format('%r%a');
}
/**
* Get the difference in days using a filter callable
*
* @param callable $callback The callback to use for filtering.
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInDaysFiltered(callable $callback, ChronosInterface $dt = null, $abs = true)
{
return $this->diffFiltered(ChronosInterval::day(), $callback, $dt, $abs);
}
/**
* Get the difference in hours using a filter callable
*
* @param callable $callback The callback to use for filtering.
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInHoursFiltered(callable $callback, ChronosInterface $dt = null, $abs = true)
{
return $this->diffFiltered(ChronosInterval::hour(), $callback, $dt, $abs);
}
/**
* Get the difference by the given interval using a filter callable
*
* @param \Cake\Chronos\ChronosInterval $ci An interval to traverse by
* @param callable $callback The callback to use for filtering.
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffFiltered(ChronosInterval $ci, callable $callback, ChronosInterface $dt = null, $abs = true)
{
$start = $this;
$end = $dt === null ? static::now($this->tz) : $dt;
$inverse = false;
if (defined('HHVM_VERSION')) {
$start = new DateTimeImmutable($this->toIso8601String());
$end = new DateTimeImmutable($end->toIso8601String());
}
if ($end < $start) {
$start = $end;
$end = $this;
$inverse = true;
}
$period = new DatePeriod($start, $ci, $end);
$vals = array_filter(iterator_to_array($period), function (DateTimeInterface $date) use ($callback) {
return $callback(static::instance($date));
});
$diff = count($vals);
return $inverse && !$abs ? -$diff : $diff;
}
/**
* Get the difference in weekdays
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeekdays(ChronosInterface $dt = null, $abs = true)
{
return $this->diffInDaysFiltered(function (ChronosInterface $date) {
return $date->isWeekday();
}, $dt, $abs);
}
/**
* Get the difference in weekend days using a filter
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeekendDays(ChronosInterface $dt = null, $abs = true)
{
return $this->diffInDaysFiltered(function (ChronosInterface $date) {
return $date->isWeekend();
}, $dt, $abs);
}
/**
* Get the difference in hours
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInHours(ChronosInterface $dt = null, $abs = true)
{
return (int)($this->diffInSeconds($dt, $abs) / ChronosInterface::SECONDS_PER_MINUTE / ChronosInterface::MINUTES_PER_HOUR);
}
/**
* Get the difference in minutes
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInMinutes(ChronosInterface $dt = null, $abs = true)
{
return (int)($this->diffInSeconds($dt, $abs) / ChronosInterface::SECONDS_PER_MINUTE);
}
/**
* Get the difference in seconds
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInSeconds(ChronosInterface $dt = null, $abs = true)
{
$dt = ($dt === null) ? static::now($this->tz) : $dt;
$value = $dt->getTimestamp() - $this->getTimestamp();
return $abs ? abs($value) : $value;
}
/**
* The number of seconds since midnight.
*
* @return int
*/
public function secondsSinceMidnight()
{
return $this->diffInSeconds($this->copy()->startOfDay());
}
/**
* The number of seconds until 23:59:59.
*
* @return int
*/
public function secondsUntilEndOfDay()
{
return $this->diffInSeconds($this->copy()->endOfDay());
}
/**
* Convenience method for getting the remaining time from a given time.
*
* @param \DateTime|\DateTimeImmutable $datetime The date to get the remaining time from.
* @return \DateInterval|bool The DateInterval object representing the difference between the two dates or FALSE on failure.
*/
public static function fromNow($datetime)
{
$timeNow = new static();
return $timeNow->diff($datetime);
}
/**
* Get the difference in a human readable format.
*
* When comparing a value in the past to default now:
* 1 hour ago
* 5 months ago
*
* When comparing a value in the future to default now:
* 1 hour from now
* 5 months from now
*
* When comparing a value in the past to another value:
* 1 hour before
* 5 months before
*
* When comparing a value in the future to another value:
* 1 hour after
* 5 months after
*
* @param \Cake\Chronos\ChronosInterface|null $other The datetime to compare with.
* @param bool $absolute removes time difference modifiers ago, after, etc
* @return string
*/
public function diffForHumans(ChronosInterface $other = null, $absolute = false)
{
return static::diffFormatter()->diffForHumans($this, $other, $absolute);
}
/**
* Get the difference formatter instance or overwrite the current one.
*
* @param \Cake\Chronos\DifferenceFormatter|null $formatter The formatter instance when setting.
* @return \Cake\Chronos\DifferenceFormatter The formatter instance.
*/
public static function diffFormatter($formatter = null)
{
if ($formatter === null) {
if (static::$diffFormatter === null) {
static::$diffFormatter = new DifferenceFormatter();
}
return static::$diffFormatter;
}
return static::$diffFormatter = $formatter;
}
}

View File

@@ -0,0 +1,280 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
/**
* Provides a number of datetime related factory methods.
*/
trait FactoryTrait
{
/**
* Holds the last error generated by createFromFormat
*
* @var array
*/
protected static $_lastErrors = [];
/**
* Create a ChronosInterface instance from a DateTimeInterface one
*
* @param \DateTimeInterface $dt The datetime instance to convert.
* @return static
*/
public static function instance(DateTimeInterface $dt)
{
if ($dt instanceof static) {
return clone $dt;
}
return new static($dt->format('Y-m-d H:i:s.u'), $dt->getTimezone());
}
/**
* Create a ChronosInterface instance from a string. This is an alias for the
* constructor that allows better fluent syntax as it allows you to do
* ChronosInterface::parse('Monday next week')->fn() rather than
* (new Chronos('Monday next week'))->fn()
*
* @param string $time The strtotime compatible string to parse
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name.
* @return static
*/
public static function parse($time = 'now', $tz = null)
{
return new static($time, $tz);
}
/**
* Get a ChronosInterface instance for the current date and time
*
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name.
* @return static
*/
public static function now($tz = null)
{
return new static('now', $tz);
}
/**
* Create a ChronosInterface instance for today
*
* @param \DateTimeZone|string|null $tz The timezone to use.
* @return static
*/
public static function today($tz = null)
{
return new static('midnight', $tz);
}
/**
* Create a ChronosInterface instance for tomorrow
*
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function tomorrow($tz = null)
{
return new static('tomorrow, midnight', $tz);
}
/**
* Create a ChronosInterface instance for yesterday
*
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function yesterday($tz = null)
{
return new static('yesterday, midnight', $tz);
}
/**
* Create a ChronosInterface instance for the greatest supported date.
*
* @return \Cake\Chronos\ChronosInterface
*/
public static function maxValue()
{
return static::createFromTimestampUTC(PHP_INT_MAX);
}
/**
* Create a ChronosInterface instance for the lowest supported date.
*
* @return \Cake\Chronos\ChronosInterface
*/
public static function minValue()
{
$max = PHP_INT_SIZE === 4 ? PHP_INT_MAX : PHP_INT_MAX / 10;
return static::createFromTimestampUTC(~$max);
}
/**
* Create a new ChronosInterface instance from a specific date and time.
*
* If any of $year, $month or $day are set to null their now() values
* will be used.
*
* If $hour is null it will be set to its now() value and the default values
* for $minute and $second will be their now() values.
* If $hour is not null then the default values for $minute and $second
* will be 0.
*
* @param int|null $year The year to create an instance with.
* @param int|null $month The month to create an instance with.
* @param int|null $day The day to create an instance with.
* @param int|null $hour The hour to create an instance with.
* @param int|null $minute The minute to create an instance with.
* @param int|null $second The second to create an instance with.
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function create($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null)
{
$year = ($year === null) ? date('Y') : $year;
$month = ($month === null) ? date('n') : $month;
$day = ($day === null) ? date('j') : $day;
if ($hour === null) {
$hour = date('G');
$minute = ($minute === null) ? date('i') : $minute;
$second = ($second === null) ? date('s') : $second;
} else {
$minute = ($minute === null) ? 0 : $minute;
$second = ($second === null) ? 0 : $second;
}
$instance = static::createFromFormat('Y-n-j G:i:s', sprintf('%s-%s-%s %s:%02s:%02s', 0, $month, $day, $hour, $minute, $second), $tz);
return $instance->addYears($year);
}
/**
* Create a ChronosInterface instance from just a date. The time portion is set to now.
*
* @param int $year The year to create an instance with.
* @param int $month The month to create an instance with.
* @param int $day The day to create an instance with.
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromDate($year = null, $month = null, $day = null, $tz = null)
{
return static::create($year, $month, $day, null, null, null, $tz);
}
/**
* Create a ChronosInterface instance from just a time. The date portion is set to today.
*
* @param int|null $hour The hour to create an instance with.
* @param int|null $minute The minute to create an instance with.
* @param int|null $second The second to create an instance with.
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromTime($hour = null, $minute = null, $second = null, $tz = null)
{
return static::create(null, null, null, $hour, $minute, $second, $tz);
}
/**
* Create a ChronosInterface instance from a specific format
*
* @param string $format The date() compatible format string.
* @param string $time The formatted date string to interpret.
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
* @throws \InvalidArgumentException
*/
public static function createFromFormat($format, $time, $tz = null)
{
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
} else {
$dt = parent::createFromFormat($format, $time);
}
$errors = parent::getLastErrors();
if ($dt == false) {
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
}
$dt = static::instance($dt);
static::$_lastErrors = $errors;
return $dt;
}
/**
* Create a ChronosInterface instance from a timestamp
*
* @param int $timestamp The timestamp to create an instance from.
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromTimestamp($timestamp, $tz = null)
{
return static::now($tz)->setTimestamp($timestamp);
}
/**
* Create a ChronosInterface instance from an UTC timestamp
*
* @param int $timestamp The UTC timestamp to create an instance from.
* @return static
*/
public static function createFromTimestampUTC($timestamp)
{
return new static('@' . $timestamp);
}
/**
* Creates a DateTimeZone from a string or a DateTimeZone
*
* @param \DateTimeZone|string|null $object The value to convert.
* @return \DateTimeZone
* @throws \InvalidArgumentException
*/
protected static function safeCreateDateTimeZone($object)
{
if ($object === null) {
return new DateTimeZone(date_default_timezone_get());
}
if ($object instanceof DateTimeZone) {
return $object;
}
return new DateTimeZone($object);
}
/**
* Returns any errors or warnings that were found during the parsing
* of the last object created by this class.
*
* @return array
*/
public static function getLastErrors()
{
if (empty(static::$_lastErrors)) {
return parent::getLastErrors();
}
return static::$_lastErrors;
}
}

View File

@@ -0,0 +1,259 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
use DateTime;
/**
* Provides string formatting methods for datetime instances.
*
* Expects implementing classes to define static::$toStringFormat
*/
trait FormattingTrait
{
/**
* Reset the format used to the default when type juggling a ChronosInterface instance to a string
*
* @return void
*/
public static function resetToStringFormat()
{
static::setToStringFormat(ChronosInterface::DEFAULT_TO_STRING_FORMAT);
}
/**
* Set the default format used when type juggling a ChronosInterface instance to a string
*
* @param string $format The format to use in future __toString() calls.
* @return void
*/
public static function setToStringFormat($format)
{
static::$toStringFormat = $format;
}
/**
* Format the instance as a string using the set format
*
* @return string
*/
public function __toString()
{
return $this->format(static::$toStringFormat);
}
/**
* Format the instance as date
*
* @return string
*/
public function toDateString()
{
return $this->format('Y-m-d');
}
/**
* Format the instance as a readable date
*
* @return string
*/
public function toFormattedDateString()
{
return $this->format('M j, Y');
}
/**
* Format the instance as time
*
* @return string
*/
public function toTimeString()
{
return $this->format('H:i:s');
}
/**
* Format the instance as date and time
*
* @return string
*/
public function toDateTimeString()
{
return $this->format('Y-m-d H:i:s');
}
/**
* Format the instance with day, date and time
*
* @return string
*/
public function toDayDateTimeString()
{
return $this->format('D, M j, Y g:i A');
}
/**
* Format the instance as ATOM
*
* @return string
*/
public function toAtomString()
{
return $this->format(DateTime::ATOM);
}
/**
* Format the instance as COOKIE
*
* @return string
*/
public function toCookieString()
{
return $this->format(DateTime::COOKIE);
}
/**
* Format the instance as ISO8601
*
* @return string
*/
public function toIso8601String()
{
return $this->format(DateTime::ATOM);
}
/**
* Format the instance as RFC822
*
* @return string
*/
public function toRfc822String()
{
return $this->format(DateTime::RFC822);
}
/**
* Format the instance as RFC850
*
* @return string
*/
public function toRfc850String()
{
return $this->format(DateTime::RFC850);
}
/**
* Format the instance as RFC1036
*
* @return string
*/
public function toRfc1036String()
{
return $this->format(DateTime::RFC1036);
}
/**
* Format the instance as RFC1123
*
* @return string
*/
public function toRfc1123String()
{
return $this->format(DateTime::RFC1123);
}
/**
* Format the instance as RFC2822
*
* @return string
*/
public function toRfc2822String()
{
return $this->format(DateTime::RFC2822);
}
/**
* Format the instance as RFC3339
*
* @return string
*/
public function toRfc3339String()
{
return $this->format(DateTime::RFC3339);
}
/**
* Format the instance as RSS
*
* @return string
*/
public function toRssString()
{
return $this->format(DateTime::RSS);
}
/**
* Format the instance as W3C
*
* @return string
*/
public function toW3cString()
{
return $this->format(DateTime::W3C);
}
/**
* Returns a UNIX timestamp.
*
* @return string UNIX timestamp
*/
public function toUnixString()
{
return $this->format('U');
}
/**
* Returns the quarter
*
* @param bool $range Range.
* @return int|array 1, 2, 3, or 4 quarter of year or array if $range true
*/
public function toQuarter($range = false)
{
$quarter = ceil($this->format('m') / 3);
if ($range === false) {
return $quarter;
}
$year = $this->format('Y');
switch ($quarter) {
case 1:
return [$year . '-01-01', $year . '-03-31'];
case 2:
return [$year . '-04-01', $year . '-06-30'];
case 3:
return [$year . '-07-01', $year . '-09-30'];
case 4:
return [$year . '-10-01', $year . '-12-31'];
}
}
/**
* @return int
*/
public function toWeek()
{
return (int)$this->format('W');
}
}

View File

@@ -0,0 +1,186 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use DateTimeInterface;
/**
* A trait for freezing the time aspect of a DateTime.
*
* Used in making calendar date objects, both mutable and immutable.
*/
trait FrozenTimeTrait
{
use RelativeKeywordTrait;
/**
* Removes the time components from an input string.
*
* Used to ensure constructed objects always lack time.
*
* @param string|int $time The input time. Integer values will be assumed
* to be in UTC. The 'now' and '' values will use the current local time.
* @return string The date component of $time.
*/
protected function stripTime($time)
{
if (is_int($time) || ctype_digit($time)) {
return gmdate('Y-m-d 00:00:00', $time);
}
if ($time instanceof DateTimeInterface) {
$time = $time->format('Y-m-d 00:00:00');
}
if (substr($time, 0, 1) === '@') {
return gmdate('Y-m-d 00:00:00', substr($time, 1));
}
if ($time === null || $time === 'now' || $time === '') {
return date('Y-m-d 00:00:00');
}
if ($this->hasRelativeKeywords($time)) {
return date('Y-m-d 00:00:00', strtotime($time));
}
return preg_replace('/\d{1,2}:\d{1,2}:\d{1,2}(?:\.\d+)?/', '00:00:00', $time);
}
/**
* Remove time components from strtotime relative strings.
*
* @param string $time The input expression
* @return string The output expression with no time modifiers.
*/
protected function stripRelativeTime($time)
{
return preg_replace('/([-+]\s*\d+\s(?:minutes|seconds|hours|microseconds))/', '', $time);
}
/**
* Modify the time on the Date.
*
* This method ignores all inputs and forces all inputs to 0.
*
* @param int $hours The hours to set (ignored)
* @param int $minutes The minutes to set (ignored)
* @param int $seconds The seconds to set (ignored)
* @param int $microseconds The microseconds to set (ignored)
* @return static A modified Date instance.
*/
public function setTime($hours, $minutes, $seconds = null, $microseconds = null)
{
if (CHRONOS_SUPPORTS_MICROSECONDS) {
return parent::setTime(0, 0, 0, 0);
}
return parent::setTime(0, 0, 0);
}
/**
* Add an Interval to a Date
*
* Any changes to the time will be ignored and reset to 00:00:00
*
* @param \DateInterval $interval The interval to modify this date by.
* @return static A modified Date instance
*/
public function add($interval)
{
return parent::add($interval)->setTime(0, 0, 0);
}
/**
* Subtract an Interval from a Date.
*
* Any changes to the time will be ignored and reset to 00:00:00
*
* @param \DateInterval $interval The interval to modify this date by.
* @return static A modified Date instance
*/
public function sub($interval)
{
return parent::sub($interval)->setTime(0, 0, 0);
}
/**
* No-op method.
*
* Timezones have no effect on calendar dates.
*
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return $this
*/
public function timezone($value)
{
return $this;
}
/**
* No-op method.
*
* Timezones have no effect on calendar dates.
*
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return $this
*/
public function tz($value)
{
return $this;
}
/**
* No-op method.
*
* Timezones have no effect on calendar dates.
*
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return $this
*/
public function setTimezone($value)
{
return $this;
}
/**
* Set the timestamp value and get a new object back.
*
* This method will discard the time aspects of the timestamp
* and only apply the date portions
*
* @param int $value The timestamp value to set.
* @return static
*/
public function setTimestamp($value)
{
return parent::setTimestamp($value)->setTime(0, 0, 0);
}
/**
* Overloaded to ignore time changes.
*
* Changing any aspect of the time will be ignored, and the resulting object
* will have its time frozen to 00:00:00.
*
* @param string $relative The relative change to make.
* @return static A new date with the applied date changes.
*/
public function modify($relative)
{
if (preg_match('/hour|minute|second/', $relative)) {
return $this;
}
$new = parent::modify($relative);
if ($new->format('H:i:s') !== '00:00:00') {
return $new->setTime(0, 0, 0);
}
return $new;
}
}

View File

@@ -0,0 +1,131 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice. Provides various operator methods for datetime
* objects.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
use InvalidArgumentException;
/**
* Provides the magic methods that allow read access
* to magic properties.
*
* @property-read int $year
* @property-read int $yearIso
* @property-read int $month
* @property-read int $day
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $micro
* @property-read int $dayOfWeek
* @property-read int $dayOfYear
* @property-read int $weekOfYear
* @property-read int $daysInMonth
* @property-read int $timestamp
* @property-read int $weekOfMonth
* @property-read int $age
* @property-read int $quarter
* @property-read int $offset
* @property-read int $offsetHours
* @property-read boolean $dst
* @property-read boolean $local
* @property-read boolean $utc
* @property-read \DateTimeZone $timezone
* @property-read \DateTimeZone $tz
* @property-read string $timezoneName
* @property-read string $tzName
*/
trait MagicPropertyTrait
{
/**
* Get a part of the ChronosInterface object
*
* @param string $name The property name to read.
* @return string|int|\DateTimeZone The property value.
* @throws \InvalidArgumentException
*/
public function __get($name)
{
static $formats = [
'year' => 'Y',
'yearIso' => 'o',
'month' => 'n',
'day' => 'j',
'hour' => 'G',
'minute' => 'i',
'second' => 's',
'micro' => 'u',
'dayOfWeek' => 'N',
'dayOfYear' => 'z',
'weekOfYear' => 'W',
'daysInMonth' => 't',
'timestamp' => 'U',
];
switch (true) {
case isset($formats[$name]):
return (int)$this->format($formats[$name]);
case $name === 'weekOfMonth':
return (int)ceil($this->day / ChronosInterface::DAYS_PER_WEEK);
case $name === 'age':
return $this->diffInYears();
case $name === 'quarter':
return (int)ceil($this->month / 3);
case $name === 'offset':
return $this->getOffset();
case $name === 'offsetHours':
return $this->getOffset() / ChronosInterface::SECONDS_PER_MINUTE / ChronosInterface::MINUTES_PER_HOUR;
case $name === 'dst':
return $this->format('I') === '1';
case $name === 'local':
return $this->offset === $this->copy()->setTimezone(date_default_timezone_get())->offset;
case $name === 'utc':
return $this->offset === 0;
case $name === 'timezone' || $name === 'tz':
return $this->getTimezone();
case $name === 'timezoneName' || $name === 'tzName':
return $this->getTimezone()->getName();
default:
throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name));
}
}
/**
* Check if an attribute exists on the object
*
* @param string $name The property name to check.
* @return bool Whether or not the property exists.
*/
public function __isset($name)
{
try {
$this->__get($name);
} catch (InvalidArgumentException $e) {
return false;
}
return true;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,58 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice. Provides various operator methods for datetime
* objects.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
/**
* Provides methods for testing if strings contain relative keywords.
*/
trait RelativeKeywordTrait
{
protected static $relativePattern = '/this|next|last|tomorrow|yesterday|midnight|today|[+-]|first|last|ago/i';
/**
* Determine if there is just a time in the time string
*
* @param string $time The time string to check.
* @return bool true if there is a keyword, otherwise false
*/
private static function isTimeExpression($time)
{
// Just a time
if (preg_match('/^[0-2]?[0-9]:[0-5][0-9](?::[0-5][0-9])?$/', $time)) {
return true;
}
return false;
}
/**
* Determine if there is a relative keyword in the time string, this is to
* create dates relative to now for test instances. e.g.: next tuesday
*
* @param string $time The time string to check.
* @return bool true if there is a keyword, otherwise false
*/
public static function hasRelativeKeywords($time)
{
if (self::isTimeExpression($time)) {
return true;
}
// skip common format with a '-' in it
if (preg_match('/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/', $time) !== 1) {
return preg_match(static::$relativePattern, $time) > 0;
}
return false;
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice. Provides various operator methods for datetime
* objects.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\Chronos;
use Cake\Chronos\ChronosInterface;
/**
* Provides methods for setting a 'test' now. This lets you
* retrieve pre-determined times with now().
*/
trait TestingAidTrait
{
/**
* Set the test now used by Date and Time classes provided by Chronos
*
* @see \Cake\Chronos\Chronos::setTestNow()
* @param \Cake\Chronos\ChronosInterface|string|null $testNow The instance to use for all future instances.
* @return void
*/
public static function setTestNow($testNow = null)
{
Chronos::setTestNow($testNow);
}
/**
* Get the test instance stored in Chronos
*
* @see \Cake\Chronos\Chronos::getTestNow()
* @return static|null the current instance used for testing or null.
*/
public static function getTestNow()
{
return Chronos::getTestNow();
}
/**
* Get whether or not Chronos has a test instance set.
*
* @see \Cake\Chronos\Chronos::hasTestNow()
* @return bool True if there is a test instance, otherwise false
*/
public static function hasTestNow()
{
return Chronos::hasTestNow();
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice. Provides various operator methods for datetime
* objects.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
/**
* Methods for modifying/reading timezone data.
*/
trait TimezoneTrait
{
/**
* Alias for setTimezone()
*
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return static
*/
public function timezone($value)
{
return $this->setTimezone($value);
}
/**
* Alias for setTimezone()
*
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return static
*/
public function tz($value)
{
return $this->setTimezone($value);
}
/**
* Set the instance's timezone from a string or object
*
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return static
*/
public function setTimezone($value)
{
$date = parent::setTimezone(static::safeCreateDateTimeZone($value));
// https://bugs.php.net/bug.php?id=72338
// this is workaround for this bug
// Needed for PHP below 7.0 version
$date->getTimestamp();
return $date;
}
}

View File

@@ -0,0 +1,93 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
/**
* Basic english only 'translator' for diffForHumans()
*/
class Translator
{
/**
* Translation strings.
*
* @var array
*/
public static $strings = [
'year' => '1 year',
'year_plural' => '{count} years',
'month' => '1 month',
'month_plural' => '{count} months',
'week' => '1 week',
'week_plural' => '{count} weeks',
'day' => '1 day',
'day_plural' => '{count} days',
'hour' => '1 hour',
'hour_plural' => '{count} hours',
'minute' => '1 minute',
'minute_plural' => '{count} minutes',
'second' => '1 second',
'second_plural' => '{count} seconds',
'ago' => '{time} ago',
'from_now' => '{time} from now',
'after' => '{time} after',
'before' => '{time} before',
];
/**
* Check if a translation key exists.
*
* @param string $key The key to check.
* @return bool Whether or not the key exists.
*/
public function exists($key)
{
return isset(static::$strings[$key]);
}
/**
* Get a plural message.
*
* @param string $key The key to use.
* @param int $count The number of items in the translation.
* @param array $vars Additional context variables.
* @return string The translated message or ''.
*/
public function plural($key, $count, array $vars = [])
{
if ($count === 1) {
return $this->singular($key, $vars);
}
return $this->singular($key . '_plural', ['count' => $count] + $vars);
}
/**
* Get a singular message.
*
* @param string $key The key to use.
* @param array $vars Additional context variables.
* @return string The translated message or ''.
*/
public function singular($key, array $vars = [])
{
if (isset(static::$strings[$key])) {
$varKeys = array_keys($vars);
foreach ($varKeys as $i => $k) {
$varKeys[$i] = '{' . $k . '}';
}
return str_replace($varKeys, $vars, static::$strings[$key]);
}
return '';
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
define('CHRONOS_SUPPORTS_MICROSECONDS', version_compare(PHP_VERSION, '7.1.0', '>='));
if (!class_exists('Carbon\Carbon')) {
// Create class aliases for Carbon so applications
// can upgrade more easily.
class_alias('Cake\Chronos\Chronos', 'Carbon\MutableDateTime');
class_alias('Cake\Chronos\ChronosInterface', 'Carbon\CarbonInterface');
}