Chronos.php
4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?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\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 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 = [];
if (static::$testNow === null) {
return parent::__construct($time === null ? 'now' : $time, $tz);
}
$relative = static::hasRelativeKeywords($time);
if (!empty($time) && $time !== 'now' && !$relative) {
return parent::__construct($time, $tz);
}
$testInstance = static::getTestNow();
if ($relative) {
$testInstance = $testInstance->modify($time);
}
if ($tz !== $testInstance->getTimezone()) {
$testInstance = $testInstance->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}
$time = $testInstance->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;
}
}