MongoDate.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. use Alcaeus\MongoDbAdapter\TypeInterface;
  16. use MongoDB\BSON\UTCDateTime;
  17. class MongoDate implements TypeInterface
  18. {
  19. /**
  20. * @link http://php.net/manual/en/class.mongodate.php#mongodate.props.sec
  21. * @var int $sec
  22. */
  23. public $sec;
  24. /**
  25. * @link http://php.net/manual/en/class.mongodate.php#mongodate.props.usec
  26. * @var int $usec
  27. */
  28. public $usec;
  29. /**
  30. * Creates a new date. If no parameters are given, the current time is used.
  31. *
  32. * @link http://php.net/manual/en/mongodate.construct.php
  33. * @param int $sec Number of seconds since January 1st, 1970
  34. * @param int $usec Microseconds
  35. * @return MongoDate Returns this new date
  36. */
  37. public function __construct($sec = 0, $usec = 0)
  38. {
  39. if (func_num_args() == 0) {
  40. $time = microtime(true);
  41. $sec = floor($time);
  42. $usec = ($time - $sec) * 1000000.0;
  43. } elseif ($sec instanceof UTCDateTime) {
  44. $msecString = (string) $sec;
  45. $sec = (int) substr($msecString, 0, -3);
  46. $usec = ((int) substr($msecString, -3)) * 1000;
  47. }
  48. $this->sec = $sec;
  49. $this->usec = $this->truncateMicroSeconds($usec);
  50. }
  51. /**
  52. * Returns a string representation of this date
  53. * @return string
  54. */
  55. public function __toString()
  56. {
  57. return (string) sprintf('%.8f', $this->truncateMicroSeconds($this->usec) / 1000000) . ' ' . $this->sec;
  58. }
  59. /**
  60. * Converts this MongoDate to the new BSON UTCDateTime type
  61. *
  62. * @return UTCDateTime
  63. * @internal This method is not part of the ext-mongo API
  64. */
  65. public function toBSONType()
  66. {
  67. $milliSeconds = ($this->sec * 1000) + ($this->truncateMicroSeconds($this->usec) / 1000);
  68. return new UTCDateTime($milliSeconds);
  69. }
  70. /**
  71. * Returns a DateTime object representing this date
  72. * @link http://php.net/manual/en/mongodate.todatetime.php
  73. * @return DateTime
  74. */
  75. public function toDateTime()
  76. {
  77. $datetime = new \DateTime();
  78. $datetime->setTimestamp($this->sec);
  79. $microSeconds = $this->truncateMicroSeconds($this->usec);
  80. if ($microSeconds > 0) {
  81. $datetime = \DateTime::createFromFormat('Y-m-d H:i:s.u', $datetime->format('Y-m-d H:i:s') . '.' . $microSeconds);
  82. }
  83. return $datetime;
  84. }
  85. /**
  86. * @param int $usec
  87. * @return int
  88. */
  89. private function truncateMicroSeconds($usec)
  90. {
  91. return (int) floor($usec / 1000) * 1000;
  92. }
  93. }