MongoDate.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. */
  36. public function __construct($sec = 0, $usec = 0)
  37. {
  38. if (func_num_args() == 0) {
  39. $time = microtime(true);
  40. $sec = floor($time);
  41. $usec = ($time - $sec) * 1000000.0;
  42. } elseif ($sec instanceof UTCDateTime) {
  43. $msecString = (string) $sec;
  44. $sec = (int) substr($msecString, 0, -3);
  45. $usec = ((int) substr($msecString, -3)) * 1000;
  46. }
  47. $this->sec = $sec;
  48. $this->usec = $this->truncateMicroSeconds($usec);
  49. }
  50. /**
  51. * Returns a string representation of this date
  52. * @return string
  53. */
  54. public function __toString()
  55. {
  56. return (string) sprintf('%.8f', $this->truncateMicroSeconds($this->usec) / 1000000) . ' ' . $this->sec;
  57. }
  58. /**
  59. * Converts this MongoDate to the new BSON UTCDateTime type
  60. *
  61. * @return UTCDateTime
  62. * @internal This method is not part of the ext-mongo API
  63. */
  64. public function toBSONType()
  65. {
  66. $milliSeconds = ($this->sec * 1000) + ($this->truncateMicroSeconds($this->usec) / 1000);
  67. return new UTCDateTime($milliSeconds);
  68. }
  69. /**
  70. * Returns a DateTime object representing this date
  71. * @link http://php.net/manual/en/mongodate.todatetime.php
  72. * @return DateTime
  73. */
  74. public function toDateTime()
  75. {
  76. $datetime = new \DateTime();
  77. $datetime->setTimestamp($this->sec);
  78. $microSeconds = $this->truncateMicroSeconds($this->usec);
  79. if ($microSeconds > 0) {
  80. $datetime = \DateTime::createFromFormat('Y-m-d H:i:s.u', $datetime->format('Y-m-d H:i:s') . '.' . $microSeconds);
  81. }
  82. return $datetime;
  83. }
  84. /**
  85. * @param int $usec
  86. * @return int
  87. */
  88. private function truncateMicroSeconds($usec)
  89. {
  90. return (int) floor($usec / 1000) * 1000;
  91. }
  92. }