2018-12-16 15:18:17 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Time.php
|
|
|
|
*
|
|
|
|
* -Description-
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-12-16 15:18:17 -06:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2018-12-16 15:18:17 -06:00
|
|
|
* @copyright 2018 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace LibreNMS\Util;
|
|
|
|
|
2023-03-10 14:50:56 +01:00
|
|
|
use Carbon\Carbon;
|
2022-12-15 15:52:53 -06:00
|
|
|
use Carbon\CarbonInterface;
|
|
|
|
use Carbon\CarbonInterval;
|
|
|
|
|
2018-12-16 15:18:17 -06:00
|
|
|
class Time
|
|
|
|
{
|
2022-12-15 15:52:53 -06:00
|
|
|
public static function legacyTimeSpecToSecs(string $description): int
|
2018-12-16 15:18:17 -06:00
|
|
|
{
|
|
|
|
$conversion = [
|
|
|
|
'now' => 0,
|
|
|
|
'onehour' => 3600,
|
|
|
|
'fourhour' => 14400,
|
|
|
|
'sixhour' => 21600,
|
|
|
|
'twelvehour' => 43200,
|
|
|
|
'day' => 86400,
|
|
|
|
'twoday' => 172800,
|
|
|
|
'week' => 604800,
|
|
|
|
'twoweek' => 1209600,
|
|
|
|
'month' => 2678400,
|
|
|
|
'twomonth' => 5356800,
|
|
|
|
'threemonth' => 8035200,
|
|
|
|
'year' => 31536000,
|
|
|
|
'twoyear' => 63072000,
|
|
|
|
];
|
|
|
|
|
2022-12-15 15:52:53 -06:00
|
|
|
return $conversion[$description] ?? 0;
|
2018-12-16 15:18:17 -06:00
|
|
|
}
|
2019-03-18 03:09:58 +01:00
|
|
|
|
2022-12-15 15:52:53 -06:00
|
|
|
/**
|
|
|
|
* Format seconds as a human readable interval. Negative seconds will say "ago".
|
2020-06-22 22:57:30 +02:00
|
|
|
*/
|
2022-12-15 15:52:53 -06:00
|
|
|
public static function formatInterval(?int $seconds, bool $short = false, ?int $parts = null): string
|
2020-06-22 22:57:30 +02:00
|
|
|
{
|
2022-12-15 15:52:53 -06:00
|
|
|
if ($seconds == 0) {
|
|
|
|
return '';
|
2020-06-22 22:57:30 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 15:52:53 -06:00
|
|
|
$parts = $parts ?? ($short ? 3 : -1);
|
2020-06-22 22:57:30 +02:00
|
|
|
|
2022-12-15 15:52:53 -06:00
|
|
|
try {
|
|
|
|
// handle negative seconds correctly
|
|
|
|
if ($seconds < 0) {
|
|
|
|
return CarbonInterval::seconds($seconds)->invert()->cascade()->forHumans([
|
|
|
|
'syntax' => CarbonInterface::DIFF_RELATIVE_TO_NOW,
|
|
|
|
'parts' => $parts,
|
|
|
|
'short' => $short,
|
|
|
|
]);
|
2020-06-22 22:57:30 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 15:52:53 -06:00
|
|
|
return CarbonInterval::seconds($seconds)->cascade()->forHumans([
|
|
|
|
'syntax' => CarbonInterface::DIFF_ABSOLUTE,
|
|
|
|
'parts' => $parts,
|
|
|
|
'short' => $short,
|
|
|
|
]);
|
|
|
|
} catch (\Exception) {
|
|
|
|
return '';
|
2020-06-22 22:57:30 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-28 08:06:29 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a time string into a timestamp including signed relative times using:
|
|
|
|
* m - month
|
|
|
|
* d - day
|
|
|
|
* h - hour
|
|
|
|
* y - year
|
|
|
|
*/
|
|
|
|
public static function parseAt(string|int $time): int
|
|
|
|
{
|
|
|
|
if (is_numeric($time)) {
|
|
|
|
return $time < 0 ? time() + $time : intval($time);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/^[+-]\d+[hdmy]$/', $time)) {
|
|
|
|
$units = [
|
|
|
|
'm' => 60,
|
|
|
|
'h' => 3600,
|
|
|
|
'd' => 86400,
|
|
|
|
'y' => 31557600,
|
|
|
|
];
|
|
|
|
$value = Number::cast(substr($time, 1, -1));
|
|
|
|
$unit = substr($time, -1);
|
|
|
|
|
|
|
|
$offset = ($time[0] == '-' ? -1 : 1) * $units[$unit] * $value;
|
|
|
|
|
|
|
|
return time() + $offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) strtotime($time);
|
|
|
|
}
|
2023-03-10 14:50:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a date and return the number of days from now
|
|
|
|
*/
|
|
|
|
public static function dateToDays(string|int $date): int
|
|
|
|
{
|
|
|
|
$carbon = new Carbon();
|
|
|
|
|
|
|
|
return $carbon->diffInDays($date, false);
|
|
|
|
}
|
2018-12-16 15:18:17 -06:00
|
|
|
}
|