/quizbd-master-main/vendor/nesbot/carbon/src/Carbon/Traits/Timestamp.php

https://gitlab.com/IR31121994/quizbd-master · PHP · 198 lines · 74 code · 19 blank · 105 comment · 4 complexity · e9dd9b3aa490a41a583b01bcbddab8e4 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Carbon\Traits;
  11. /**
  12. * Trait Timestamp.
  13. */
  14. trait Timestamp
  15. {
  16. /**
  17. * Create a Carbon instance from a timestamp and set the timezone (use default one if not specified).
  18. *
  19. * Timestamp input can be given as int, float or a string containing one or more numbers.
  20. *
  21. * @param float|int|string $timestamp
  22. * @param \DateTimeZone|string|null $tz
  23. *
  24. * @return static
  25. */
  26. public static function createFromTimestamp($timestamp, $tz = null)
  27. {
  28. return static::createFromTimestampUTC($timestamp)->setTimezone($tz);
  29. }
  30. /**
  31. * Create a Carbon instance from an timestamp keeping the timezone to UTC.
  32. *
  33. * Timestamp input can be given as int, float or a string containing one or more numbers.
  34. *
  35. * @param float|int|string $timestamp
  36. *
  37. * @return static
  38. */
  39. public static function createFromTimestampUTC($timestamp)
  40. {
  41. [$integer, $decimal] = self::getIntegerAndDecimalParts($timestamp);
  42. $delta = floor($decimal / static::MICROSECONDS_PER_SECOND);
  43. $integer += $delta;
  44. $decimal -= $delta * static::MICROSECONDS_PER_SECOND;
  45. $decimal = str_pad((string) $decimal, 6, '0', STR_PAD_LEFT);
  46. return static::rawCreateFromFormat('U u', "$integer $decimal");
  47. }
  48. /**
  49. * Create a Carbon instance from a timestamp in milliseconds.
  50. *
  51. * Timestamp input can be given as int, float or a string containing one or more numbers.
  52. *
  53. * @param float|int|string $timestamp
  54. *
  55. * @return static
  56. */
  57. public static function createFromTimestampMsUTC($timestamp)
  58. {
  59. [$milliseconds, $microseconds] = self::getIntegerAndDecimalParts($timestamp, 3);
  60. $sign = $milliseconds < 0 || $milliseconds === 0.0 && $microseconds < 0 ? -1 : 1;
  61. $milliseconds = abs($milliseconds);
  62. $microseconds = $sign * abs($microseconds) + static::MICROSECONDS_PER_MILLISECOND * ($milliseconds % static::MILLISECONDS_PER_SECOND);
  63. $seconds = $sign * floor($milliseconds / static::MILLISECONDS_PER_SECOND);
  64. $delta = floor($microseconds / static::MICROSECONDS_PER_SECOND);
  65. $seconds += $delta;
  66. $microseconds -= $delta * static::MICROSECONDS_PER_SECOND;
  67. $microseconds = str_pad($microseconds, 6, '0', STR_PAD_LEFT);
  68. return static::rawCreateFromFormat('U u', "$seconds $microseconds");
  69. }
  70. /**
  71. * Create a Carbon instance from a timestamp in milliseconds.
  72. *
  73. * Timestamp input can be given as int, float or a string containing one or more numbers.
  74. *
  75. * @param float|int|string $timestamp
  76. * @param \DateTimeZone|string|null $tz
  77. *
  78. * @return static
  79. */
  80. public static function createFromTimestampMs($timestamp, $tz = null)
  81. {
  82. return static::createFromTimestampMsUTC($timestamp)
  83. ->setTimezone($tz);
  84. }
  85. /**
  86. * Set the instance's timestamp.
  87. *
  88. * Timestamp input can be given as int, float or a string containing one or more numbers.
  89. *
  90. * @param float|int|string $unixTimestamp
  91. *
  92. * @return static
  93. */
  94. public function timestamp($unixTimestamp)
  95. {
  96. return $this->setTimestamp($unixTimestamp);
  97. }
  98. /**
  99. * Returns a timestamp rounded with the given precision (6 by default).
  100. *
  101. * @example getPreciseTimestamp() 1532087464437474 (microsecond maximum precision)
  102. * @example getPreciseTimestamp(6) 1532087464437474
  103. * @example getPreciseTimestamp(5) 153208746443747 (1/100000 second precision)
  104. * @example getPreciseTimestamp(4) 15320874644375 (1/10000 second precision)
  105. * @example getPreciseTimestamp(3) 1532087464437 (millisecond precision)
  106. * @example getPreciseTimestamp(2) 153208746444 (1/100 second precision)
  107. * @example getPreciseTimestamp(1) 15320874644 (1/10 second precision)
  108. * @example getPreciseTimestamp(0) 1532087464 (second precision)
  109. * @example getPreciseTimestamp(-1) 153208746 (10 second precision)
  110. * @example getPreciseTimestamp(-2) 15320875 (100 second precision)
  111. *
  112. * @param int $precision
  113. *
  114. * @return float
  115. */
  116. public function getPreciseTimestamp($precision = 6)
  117. {
  118. return round($this->rawFormat('Uu') / pow(10, 6 - $precision));
  119. }
  120. /**
  121. * Returns the milliseconds timestamps used amongst other by Date javascript objects.
  122. *
  123. * @return float
  124. */
  125. public function valueOf()
  126. {
  127. return $this->getPreciseTimestamp(3);
  128. }
  129. /**
  130. * Returns the timestamp with millisecond precision.
  131. *
  132. * @return int
  133. */
  134. public function getTimestampMs()
  135. {
  136. return (int) $this->getPreciseTimestamp(3);
  137. }
  138. /**
  139. * @alias getTimestamp
  140. *
  141. * Returns the UNIX timestamp for the current date.
  142. *
  143. * @return int
  144. */
  145. public function unix()
  146. {
  147. return $this->getTimestamp();
  148. }
  149. /**
  150. * Return an array with integer part digits and decimals digits split from one or more positive numbers
  151. * (such as timestamps) as string with the given number of decimals (6 by default).
  152. *
  153. * By splitting integer and decimal, this method obtain a better precision than
  154. * number_format when the input is a string.
  155. *
  156. * @param float|int|string $numbers one or more numbers
  157. * @param int $decimals number of decimals precision (6 by default)
  158. *
  159. * @return array 0-index is integer part, 1-index is decimal part digits
  160. */
  161. private static function getIntegerAndDecimalParts($numbers, $decimals = 6)
  162. {
  163. if (\is_int($numbers) || \is_float($numbers)) {
  164. $numbers = number_format($numbers, $decimals, '.', '');
  165. }
  166. $sign = str_starts_with($numbers, '-') ? -1 : 1;
  167. $integer = 0;
  168. $decimal = 0;
  169. foreach (preg_split('`[^0-9.]+`', $numbers) as $chunk) {
  170. [$integerPart, $decimalPart] = explode('.', "$chunk.");
  171. $integer += (int) $integerPart;
  172. $decimal += (float) ("0.$decimalPart");
  173. }
  174. $overflow = floor($decimal);
  175. $integer += $overflow;
  176. $decimal -= $overflow;
  177. return [$sign * $integer, $decimal === 0.0 ? 0.0 : $sign * round($decimal * pow(10, $decimals))];
  178. }
  179. }