Debian Codegraph @

lib/3rdParty/composer/illuminate/support/InteractsWithTime.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use Carbon\CarbonInterval;
6 use DateInterval;
7 use DateTimeInterface;
8
9 trait InteractsWithTime
10 {
11 /**
12 * Get the number of seconds until the given DateTime.
13 *
14 * @param \DateTimeInterface|\DateInterval|int $delay
15 * @return int
16 */
17 protected function secondsUntil($delay)
18 {
19 $delay = $this->parseDateInterval($delay);
20
21 return $delay instanceof DateTimeInterface
22 ? max(0, $delay->getTimestamp() - $this->currentTime())
23 : (int) $delay;
24 }
25
26 /**
27 * Get the "available at" UNIX timestamp.
28 *
29 * @param \DateTimeInterface|\DateInterval|int $delay
30 * @return int
31 */
32 protected function availableAt($delay = 0)
33 {
34 $delay = $this->parseDateInterval($delay);
35
36 return $delay instanceof DateTimeInterface
37 ? $delay->getTimestamp()
38 : Carbon::now()->addSeconds($delay)->getTimestamp();
39 }
40
41 /**
42 * If the given value is an interval, convert it to a DateTime instance.
43 *
44 * @param \DateTimeInterface|\DateInterval|int $delay
45 * @return \DateTimeInterface|int
46 */
47 protected function parseDateInterval($delay)
48 {
49 if ($delay instanceof DateInterval) {
50 $delay = Carbon::now()->add($delay);
51 }
52
53 return $delay;
54 }
55
56 /**
57 * Get the current system time as a UNIX timestamp.
58 *
59 * @return int
60 */
61 protected function currentTime()
62 {
63 return Carbon::now()->getTimestamp();
64 }
65
66 /**
67 * Given a start time, format the total run time for human readability.
68 *
69 * @param float $startTime
70 * @param float|null $endTime
71 * @return string
72 */
73 protected function runTimeForHumans($startTime, $endTime = null)
74 {
75 $endTime ??= microtime(true);
76
77 $runTime = ($endTime - $startTime) * 1000;
78
79 return $runTime > 1000
80 ? CarbonInterval::milliseconds($runTime)->cascade()->forHumans(short: true)
81 : number_format($runTime, 2).'ms';
82 }
83 }