1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
use Alcaeus\MongoDbAdapter\TypeInterface; |
17
|
|
|
use MongoDB\BSON\UTCDateTime; |
18
|
|
|
|
19
|
|
|
class MongoDate implements TypeInterface |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @link http://php.net/manual/en/class.mongodate.php#mongodate.props.sec |
23
|
|
|
* @var int $sec |
24
|
|
|
*/ |
25
|
|
|
public $sec; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @link http://php.net/manual/en/class.mongodate.php#mongodate.props.usec |
29
|
|
|
* @var int $usec |
30
|
|
|
*/ |
31
|
|
|
public $usec; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates a new date. If no parameters are given, the current time is used. |
35
|
|
|
* |
36
|
|
|
* @link http://php.net/manual/en/mongodate.construct.php |
37
|
|
|
* @param int $sec Number of seconds since January 1st, 1970 |
38
|
|
|
* @param int $usec Microseconds |
39
|
|
|
* @return MongoDate Returns this new date |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function __construct($sec = 0, $usec = 0) |
42
|
|
|
{ |
43
|
|
|
if (func_num_args() == 0) { |
44
|
|
|
$time = microtime(true); |
45
|
|
|
$sec = floor($time); |
46
|
|
|
$usec = ($time - $sec) * 1000000.0; |
47
|
|
|
} elseif ($sec instanceof UTCDateTime) { |
|
|
|
|
48
|
|
|
$msecString = (string) $sec; |
49
|
|
|
|
50
|
|
|
$sec = (int) substr($msecString, 0, -3); |
51
|
|
|
$usec = ((int) substr($msecString, -3)) * 1000; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->sec = $sec; |
|
|
|
|
55
|
|
|
$this->usec = $this->truncateMicroSeconds($usec); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns a string representation of this date |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function __toString() |
63
|
|
|
{ |
64
|
|
|
return (string) sprintf('%.8f', $this->truncateMicroSeconds($this->usec) / 1000000) . ' ' . $this->sec; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Converts this MongoDate to the new BSON UTCDateTime type |
69
|
|
|
* |
70
|
|
|
* @return UTCDateTime |
71
|
|
|
* @internal This method is not part of the ext-mongo API |
72
|
|
|
*/ |
73
|
|
|
public function toBSONType() |
74
|
|
|
{ |
75
|
|
|
$milliSeconds = ($this->sec * 1000) + ($this->truncateMicroSeconds($this->usec) / 1000); |
76
|
|
|
|
77
|
|
|
return new UTCDateTime($milliSeconds); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns a DateTime object representing this date |
82
|
|
|
* @link http://php.net/manual/en/mongodate.todatetime.php |
83
|
|
|
* @return DateTime |
84
|
|
|
*/ |
85
|
|
|
public function toDateTime() |
86
|
|
|
{ |
87
|
|
|
$datetime = new \DateTime(); |
88
|
|
|
$datetime->setTimestamp($this->sec); |
89
|
|
|
|
90
|
|
|
$microSeconds = $this->truncateMicroSeconds($this->usec); |
91
|
|
|
if ($microSeconds > 0) { |
92
|
|
|
$datetime = \DateTime::createFromFormat('Y-m-d H:i:s.u', $datetime->format('Y-m-d H:i:s') . '.' . $microSeconds); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $datetime; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param int $usec |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
private function truncateMicroSeconds($usec) |
103
|
|
|
{ |
104
|
|
|
return (int) floor($usec / 1000) * 1000; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.