blob: 1562ec0a4fa8a774da0bf06c0a72ca9607ec869e [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace LdapRecord\Query;
4
5use Carbon\Carbon;
6use DateInterval;
7use DateTimeInterface;
8
9/**
10 * @author Taylor Otwell
11 *
12 * @see https://laravel.com
13 */
14trait InteractsWithTime
15{
16 /**
17 * Get the number of seconds until the given DateTime.
18 *
19 * @param DateTimeInterface|DateInterval|int $delay
20 *
21 * @return int
22 */
23 protected function secondsUntil($delay)
24 {
25 $delay = $this->parseDateInterval($delay);
26
27 return $delay instanceof DateTimeInterface
28 ? max(0, $delay->getTimestamp() - $this->currentTime())
29 : (int) $delay;
30 }
31
32 /**
33 * Get the "available at" UNIX timestamp.
34 *
35 * @param DateTimeInterface|DateInterval|int $delay
36 *
37 * @return int
38 */
39 protected function availableAt($delay = 0)
40 {
41 $delay = $this->parseDateInterval($delay);
42
43 return $delay instanceof DateTimeInterface
44 ? $delay->getTimestamp()
45 : Carbon::now()->addRealSeconds($delay)->getTimestamp();
46 }
47
48 /**
49 * If the given value is an interval, convert it to a DateTime instance.
50 *
51 * @param DateTimeInterface|DateInterval|int $delay
52 *
53 * @return DateTimeInterface|int
54 */
55 protected function parseDateInterval($delay)
56 {
57 if ($delay instanceof DateInterval) {
58 $delay = Carbon::now()->add($delay);
59 }
60
61 return $delay;
62 }
63
64 /**
65 * Get the current system time as a UNIX timestamp.
66 *
67 * @return int
68 */
69 protected function currentTime()
70 {
71 return Carbon::now()->getTimestamp();
72 }
73}