Issues (2963)

LibreNMS/Util/Time.php (1 issue)

1
<?php
2
/**
3
 * Time.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace LibreNMS\Util;
27
28
class Time
29
{
30
    public static function legacyTimeSpecToSecs($description)
31
    {
32
        $conversion = [
33
            'now' => 0,
34
            'onehour' => 3600,
35
            'fourhour' => 14400,
36
            'sixhour' => 21600,
37
            'twelvehour' => 43200,
38
            'day' => 86400,
39
            'twoday' => 172800,
40
            'week' => 604800,
41
            'twoweek' => 1209600,
42
            'month' => 2678400,
43
            'twomonth' => 5356800,
44
            'threemonth' => 8035200,
45
            'year' => 31536000,
46
            'twoyear' => 63072000,
47
        ];
48
49
        return isset($conversion[$description]) ? $conversion[$description] : 0;
50
    }
51
52
    public static function formatInterval($interval, $format = 'long')
53
    {
54
        $result = '';
55
        $data = [
56
            'years' => 31536000,
57
            'days' => 86400,
58
            'hours' => 3600,
59
            'minutes' => 60,
60
            'seconds' => 1,
61
        ];
62
63
        foreach ($data as $k => $v) {
64
            if ($interval >= $v) {
65
                $diff = floor($interval / $v);
66
67
                $result .= " $diff";
68
                if ($format == 'short') {
69
                    $result .= substr($k, 0, 1);
70
                }
71
72
                if ($format != 'short' && $diff > 1) {
73
                    $result .= ' ' . $k;
74
                }
75
76
                if ($format != 'short' && $diff < 2) {
77
                    $result .= ' ' . substr($k, 0, -1);
78
                }
79
80
                $interval -= $v * $diff;
81
            }
82
        }
83
84
        return $result;
85
    }
86
87
    /*
88
     * @param integer seconds of a time period
0 ignored issues
show
The type LibreNMS\Util\seconds was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
89
     * @return string human readably time period
90
     */
91
    public static function humanTime($s)
92
    {
93
        $ret = [];
94
95
        if ($s >= 86400) {
96
            $d = floor($s / 86400);
97
            $s -= $d * 86400;
98
            if ($d == 1) {
99
                $ret[] = $d . ' day';
100
            } else {
101
                $ret[] = $d . ' days';
102
            }
103
        }
104
105
        if ($s >= 3600) {
106
            $h = floor($s / 3600);
107
            $s -= $h * 3600;
108
            if ($h == 1) {
109
                $ret[] = $h . ' hour';
110
            } else {
111
                $ret[] = $h . ' hours';
112
            }
113
        }
114
115
        if ($s >= 60) {
116
            $m = floor($s / 60);
117
            $s -= $m * 60;
118
            if ($m == 1) {
119
                $ret[] = $m . ' minute';
120
            } else {
121
                $ret[] = $m . ' minutes';
122
            }
123
        }
124
125
        if ($s > 0) {
126
            if ($s == 1) {
127
                $ret[] = $s . ' second';
128
            } else {
129
                $ret[] = $s . ' seconds';
130
            }
131
        }
132
133
        return implode(' ,', $ret);
134
    }
135
}
136