PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.2.0
Brevo – Email, SMS, Web Push, Chat, and more. v3.2.0
2.9.13 2.9.14 2.9.15 2.9.16 2.9.17 2.9.18 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.2 3.1.20 3.1.21 3.1.22 3.1.23 3.1.24 3.1.25 3.1.26 3.1.27 3.1.28 3.1.29 3.1.3 3.1.30 3.1.31 3.1.32 3.1.33 3.1.34 3.1.35 3.1.36 3.1.37 3.1.38 3.1.39 3.1.4 3.1.40 3.1.41 3.1.42 3.1.43 3.1.44 3.1.45 3.1.46 3.1.47 3.1.48 3.1.49 3.1.5 3.1.50 3.1.51 3.1.52 3.1.53 3.1.54 3.1.55 3.1.56 3.1.57 3.1.58 3.1.59 3.1.6 3.1.60 3.1.61 3.1.62 3.1.63 3.1.64 3.1.65 3.1.66 3.1.67 3.1.68 3.1.69 3.1.7 3.1.70 3.1.71 3.1.72 3.1.73 3.1.74 3.1.75 3.1.76 3.1.77 3.1.78 3.1.79 3.1.8 3.1.80 3.1.81 3.1.82 3.1.83 3.1.84 3.1.85 3.1.86 3.1.87 3.1.88 3.1.89 3.1.9 3.1.90 3.1.91 3.1.92 3.1.93 3.1.94 3.1.95 3.1.96 3.1.97 3.1.98 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 trunk 1.0 1.5 2.0.8 2.9.10 2.9.11 2.9.12
mailin / wonderpush-php-lib / lib / Util / TimeUnit.php
mailin / wonderpush-php-lib / lib / Util Last commit date
ArrayUtil.php 1 year ago DefaultLogger.php 1 year ago JsonSerializable.php 1 year ago Logger.php 1 year ago StringUtil.php 1 year ago TimeUnit.php 1 year ago TimeUtil.php 1 year ago TimeValue.php 1 year ago UrlUtil.php 1 year ago
TimeUnit.php
174 lines
1 <?php
2
3 namespace WonderPush\Util;
4
5 if (count(get_included_files()) === 1) { http_response_code(403); exit(); } // Prevent direct access
6
7 /**
8 * Time units and conversion.
9 */
10 class TimeUnit {
11
12 // Constants are defined by the multiple of seconds they represent
13 // or the negative power of ten they are expressed in.
14
15 /**
16 * Nanoseconds unit.
17 */
18 const NANOSECONDS = -9; // 10⁻⁹ seconds
19
20 /**
21 * Microseconds unit.
22 */
23 const MICROSECONDS = -6; // 10⁻⁶ seconds
24
25 /**
26 * Milliseconds unit.
27 */
28 const MILLISECONDS = -3; // 10⁻³ seconds
29
30 /**
31 * Seconds unit.
32 */
33 const SECONDS = 1; // 1 second exactly
34
35 /**
36 * Minutes unit.
37 */
38 const MINUTES = 60; // 60 seconds
39
40 /**
41 * Hours unit.
42 */
43 const HOURS = 3600; // 60 minutes
44
45 /**
46 * Days unit.
47 */
48 const DAYS = 86400; // 24 hours
49
50 /**
51 * Weeks unit.
52 */
53 const WEEKS = 604800; // 7 days
54
55 // other typical constants (month, year) don't convert to a constant number of seconds
56
57 /**
58 * Asserts the given unit is valid.
59 * @param mixed $unit
60 */
61 public static function assertValidUnit($unit) {
62 assert(is_int($unit));
63 }
64
65 private static $units = array(
66 self::NANOSECONDS,
67 self::MICROSECONDS,
68 self::MILLISECONDS,
69 self::SECONDS,
70 self::MINUTES,
71 self::HOURS,
72 self::DAYS,
73 self::WEEKS
74 );
75
76 private static $unitsLabels = array( // (preferred label first) (full label last)
77 self::NANOSECONDS => array('ns', 'nanos', 'nanosecond', 'nanoseconds'),
78 self::MICROSECONDS => array('us', 'micros', 'microsecond', 'microseconds'),
79 self::MILLISECONDS => array('ms', 'millis', 'millisecond', 'milliseconds'),
80 self::SECONDS => array('s', 'sec', 'secs', 'second', 'seconds'),
81 self::MINUTES => array('m', 'min', 'minute', 'minutes'),
82 self::HOURS => array('h', 'hr', 'hour', 'hours'),
83 self::DAYS => array('d', 'day', 'days'),
84 self::WEEKS => array('w', 'week', 'weeks')
85 );
86
87 private static $labelsToUnits; // lazily initialized
88
89 /**
90 * Returns a list of predefined units.
91 * @return integer[]
92 */
93 public static function getUnits() {
94 return self::$units;
95 }
96
97 /**
98 * Returns the labels of a given unit, or of all units.
99 * @param integer|null $unit
100 * @return string|null|string[]
101 */
102 public static function getUnitLabels($unit = null) {
103 if (null === $unit) {
104 return self::$unitsLabels;
105 }
106 self::assertValidUnit($unit);
107 return ArrayUtil::getIfSet(self::$unitsLabels, $unit);
108 }
109
110 /**
111 * Returns an mapping of labels to associated unit.
112 * @return integer[]
113 */
114 public static function getLabelsToUnits() {
115 if (null === self::$labelsToUnits) {
116 $map = array();
117 foreach (self::$unitsLabels as $unit => $labels) {
118 foreach ($labels as $label) {
119 assert(!isset($map[$label]));
120 $map[$label] = $unit;
121 }
122 }
123 self::$labelsToUnits = $map;
124 }
125 return self::$labelsToUnits;
126 }
127
128 /**
129 * Returns the unit associated to the given label.
130 * @param string $label
131 * @return integer|null
132 */
133 public static function labelToUnit($label) {
134 return ArrayUtil::getIfSet(self::getLabelsToUnits(), $label);
135 }
136
137 /**
138 * Converts a value from a given unit to another.
139 * @param integer|float $value
140 * @param integer $fromUnit
141 * @param integer $toUnit
142 * @return integer|float
143 */
144 public static function convert($value, $fromUnit, $toUnit) {
145 self::assertValidUnit($fromUnit);
146 self::assertValidUnit($toUnit);
147 assert(is_numeric($value));
148
149 if ($fromUnit === $toUnit) {
150 return $value;
151 }
152
153 // Normalize 1 second to 10⁰ seconds
154 if ($fromUnit === 1) {
155 $fromUnit = 0;
156 }
157 if ($toUnit === 1) {
158 $toUnit = 0;
159 }
160
161 if ($fromUnit <= 0) {
162 if ($toUnit <= 0) {
163 return $value * pow(10, $fromUnit - $toUnit);
164 }
165 return $value * pow(10, $fromUnit) / $toUnit;
166 }
167 if ($toUnit <= 0) {
168 return $value * $fromUnit / pow(10, $toUnit);
169 }
170 return $value * ((float)$fromUnit / $toUnit);
171 }
172
173 }
174