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 |