/laravel_tintuc/vendor/nesbot/carbon/src/Carbon/Traits/Timestamp.php

https://gitlab.com/nmhieucoder/laravel_tintuc · PHP · 186 lines · 69 code · 17 blank · 100 comment · 4 complexity · 2fd812a31b4b18cbd94d8df7dff59c87 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. return static::rawCreateFromFormat('U u', "$integer $decimal");
  46. }
  47. /**
  48. * Create a Carbon instance from a timestamp in milliseconds.
  49. *
  50. * Timestamp input can be given as int, float or a string containing one or more numbers.
  51. *
  52. * @param float|int|string $timestamp
  53. *
  54. * @return static
  55. */
  56. public static function createFromTimestampMsUTC($timestamp)
  57. {
  58. [$milliseconds, $microseconds] = self::getIntegerAndDecimalParts($timestamp, 3);
  59. $sign = $milliseconds < 0 || $milliseconds === 0.0 && $microseconds < 0 ? -1 : 1;
  60. $milliseconds = abs($milliseconds);
  61. $microseconds = $sign * abs($microseconds) + static::MICROSECONDS_PER_MILLISECOND * ($milliseconds % static::MILLISECONDS_PER_SECOND);
  62. $seconds = $sign * floor($milliseconds / static::MILLISECONDS_PER_SECOND);
  63. $delta = floor($microseconds / static::MICROSECONDS_PER_SECOND);
  64. $seconds += $delta;
  65. $microseconds -= $delta * static::MICROSECONDS_PER_SECOND;
  66. $microseconds = str_pad($microseconds, 6, '0', STR_PAD_LEFT);
  67. return static::rawCreateFromFormat('U u', "$seconds $microseconds");
  68. }
  69. /**
  70. * Create a Carbon instance from a timestamp in milliseconds.
  71. *
  72. * Timestamp input can be given as int, float or a string containing one or more numbers.
  73. *
  74. * @param float|int|string $timestamp
  75. * @param \DateTimeZone|string|null $tz
  76. *
  77. * @return static
  78. */
  79. public static function createFromTimestampMs($timestamp, $tz = null)
  80. {
  81. return static::createFromTimestampMsUTC($timestamp)
  82. ->setTimezone($tz);
  83. }
  84. /**
  85. * Set the instance's timestamp.
  86. *
  87. * Timestamp input can be given as int, float or a string containing one or more numbers.
  88. *
  89. * @param float|int|string $unixTimestamp
  90. *
  91. * @return static
  92. */
  93. public function timestamp($unixTimestamp)
  94. {
  95. return $this->setTimestamp($unixTimestamp);
  96. }
  97. /**
  98. * Returns a timestamp rounded with the given precision (6 by default).
  99. *
  100. * @example getPreciseTimestamp() 1532087464437474 (microsecond maximum precision)
  101. * @example getPreciseTimestamp(6) 1532087464437474
  102. * @example getPreciseTimestamp(5) 153208746443747 (1/100000 second precision)
  103. * @example getPreciseTimestamp(4) 15320874644375 (1/10000 second precision)
  104. * @example getPreciseTimestamp(3) 1532087464437 (millisecond precision)
  105. * @example getPreciseTimestamp(2) 153208746444 (1/100 second precision)
  106. * @example getPreciseTimestamp(1) 15320874644 (1/10 second precision)
  107. * @example getPreciseTimestamp(0) 1532087464 (second precision)
  108. * @example getPreciseTimestamp(-1) 153208746 (10 second precision)
  109. * @example getPreciseTimestamp(-2) 15320875 (100 second precision)
  110. *
  111. * @param int $precision
  112. *
  113. * @return float
  114. */
  115. public function getPreciseTimestamp($precision = 6)
  116. {
  117. return round($this->rawFormat('Uu') / pow(10, 6 - $precision));
  118. }
  119. /**
  120. * Returns the milliseconds timestamps used amongst other by Date javascript objects.
  121. *
  122. * @return float
  123. */
  124. public function valueOf()
  125. {
  126. return $this->getPreciseTimestamp(3);
  127. }
  128. /**
  129. * @alias getTimestamp
  130. *
  131. * Returns the UNIX timestamp for the current date.
  132. *
  133. * @return int
  134. */
  135. public function unix()
  136. {
  137. return $this->getTimestamp();
  138. }
  139. /**
  140. * Return an array with integer part digits and decimals digits split from one or more positive numbers
  141. * (such as timestamps) as string with the given number of decimals (6 by default).
  142. *
  143. * By splitting integer and decimal, this method obtain a better precision than
  144. * number_format when the input is a string.
  145. *
  146. * @param float|int|string $numbers one or more numbers
  147. * @param int $decimals number of decimals precision (6 by default)
  148. *
  149. * @return array 0-index is integer part, 1-index is decimal part digits
  150. */
  151. private static function getIntegerAndDecimalParts($numbers, $decimals = 6)
  152. {
  153. if (\is_int($numbers) || \is_float($numbers)) {
  154. $numbers = number_format($numbers, $decimals, '.', '');
  155. }
  156. $sign = substr($numbers, 0, 1) === '-' ? -1 : 1;
  157. $integer = 0;
  158. $decimal = 0;
  159. foreach (preg_split('`[^0-9.]+`', $numbers) as $chunk) {
  160. [$integerPart, $decimalPart] = explode('.', "$chunk.");
  161. $integer += \intval($integerPart);
  162. $decimal += \floatval("0.$decimalPart");
  163. }
  164. $overflow = floor($decimal);
  165. $integer += $overflow;
  166. $decimal -= $overflow;
  167. return [$sign * $integer, $decimal === 0.0 ? 0.0 : $sign * round($decimal * pow(10, $decimals))];
  168. }
  169. }