MongoDate.php 3.4 KB

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