summaryrefslogtreecommitdiff
path: root/manager/library/date/HumanReadableTime.php
blob: 7c2a7c862487f7a4255cfc2fc7812817c57ed1c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

/**
 * class that provides methods to convert human readable time / interval length
 * expressions into other formats
 *
 * @author markus
 * $Id: HumanReadableTime.php 92 2010-03-10 11:43:15Z markus $
 */

require_once(LIBRARY_PATH . '/date/exception.HumanReadableTimeException.php');

class HumanReadableTime {
    /**
     * normalize an HRT string, convert from HRT to seconds and then convert back to
     * HRT
     * @param string $hrt
     * @param string $maxunit
     * @return string
     */
    public static function NormalizeHRT($hrt, $maxunit = 'w') {
        return self::Seconds2HR(self::HR2Seconds($hrt), $maxunit);
    }

    /**
     * convert string / interger which contains an interval length to
     * human readable format (1w2d7h)
     *
     * if $maxunit is set, it defines the biggest unit in output (i.e. $maxunit = 'h' will
     * allow only hms)
     *
     * @param string|integer $seconds
     * @param string $maxunit
     * @return string
     */
    public static function Seconds2HR($seconds, $maxunit = 'w') {
        $maxunit = trim(strtolower($maxunit));
        $allowed = array('w' => 0, 'd' => 0, 'h' => 0, 'm' => 0, 's' => 0);
        if (!in_array($maxunit, array_keys($allowed), true))
            throw new HumanReadableTimeException('illegal value for maxunit: "' . $maxunit . '"');
        foreach ($allowed as $key => $value) {
            if ($maxunit == $key)
                break;
            unset($allowed[$key]);
        }

        $seconds = intval($seconds);
        $hrt = '';
        foreach ($allowed as $key => $value) {
            switch ($key) {
                case 'w':
                    $tmp = intval($seconds / (7*86400));
                    if ($tmp > 0)
                        $seconds %= (7*86400);
                    $allowed[$key] += $tmp;
                    break;
                case 'd':
                    $tmp = intval($seconds / (86400));
                    if ($tmp > 0)
                        $seconds %= (86400);
                    $allowed[$key] += $tmp;
                    break;
                case 'h':
                    $tmp = intval($seconds / (3600));
                    if ($tmp > 0)
                        $seconds %= (3600);
                    $allowed[$key] += $tmp;
                    break;
                case 'm':
                    $tmp = intval($seconds / (60));
                    if ($tmp > 0)
                        $seconds %= (60);
                    $allowed[$key] += $tmp;
                    break;
                case 's':
                    $allowed[$key] += $seconds;
                    break;
            }
        }

        $hrt = '';
        foreach ($allowed as $key => $value) {
            if ($value > 0)
                $hrt .= sprintf('%d%s', $value, $key);
        }
        return $hrt;
    }

    /**
     * parse a string of 3h2m7s and return the number of seconds as integer
     * add "s" to the end of the number if $addsecond is set to true
     * @param string $hr
     * @param boolean $addsecond
     * @return integer|string
     */
    public static function HR2Seconds($hr, $addsecond = false) {
        $hr = trim($hr);
        if ($hr == '') {
            if ($addsecond === true)
                return '0s';
            else
                return 0;
        }

        $hr = strtolower($hr);

        $matches = array();
        if (preg_match_all('/([0-9]*)([wdhms])/', $hr, $matches, PREG_SET_ORDER) > 0) {
            $interval = 0;
            for ($i = 0; $i < count($matches); $i++) {
                switch ($matches[$i][2]) {
                    case 'w':
                        $interval += $matches[$i][1] * 7 * 86400;
                        break;
                    case 'd':
                        $interval += $matches[$i][1] * 86400;
                        break;
                    case 'h':
                        $interval += $matches[$i][1] * 3600;
                        break;
                    case 'm':
                        $interval += $matches[$i][1] * 60;
                        break;
                    case 's':
                        $interval += $matches[$i][1];
                        break;
                }
            }
            if ($addsecond === true)
                return sprintf('%ds', $interval);
            else
                return $interval;
        }

        if ($addsecond === true)
            return '0s';
        else
            return 0;
    }
}