initial setup of framework code
[cacert-mgr.git] / manager / library / date / HumanReadableTime.php
1 <?php
2
3 /**
4 * class that provides methods to convert human readable time / interval length
5 * expressions into other formats
6 *
7 * @author markus
8 * $Id: HumanReadableTime.php 92 2010-03-10 11:43:15Z markus $
9 */
10
11 require_once(LIBRARY_PATH . '/date/exception.HumanReadableTimeException.php');
12
13 class HumanReadableTime {
14 /**
15 * normalize an HRT string, convert from HRT to seconds and then convert back to
16 * HRT
17 * @param string $hrt
18 * @param string $maxunit
19 * @return string
20 */
21 public static function NormalizeHRT($hrt, $maxunit = 'w') {
22 return self::Seconds2HR(self::HR2Seconds($hrt), $maxunit);
23 }
24
25 /**
26 * convert string / interger which contains an interval length to
27 * human readable format (1w2d7h)
28 *
29 * if $maxunit is set, it defines the biggest unit in output (i.e. $maxunit = 'h' will
30 * allow only hms)
31 *
32 * @param string|integer $seconds
33 * @param string $maxunit
34 * @return string
35 */
36 public static function Seconds2HR($seconds, $maxunit = 'w') {
37 $maxunit = trim(strtolower($maxunit));
38 $allowed = array('w' => 0, 'd' => 0, 'h' => 0, 'm' => 0, 's' => 0);
39 if (!in_array($maxunit, array_keys($allowed), true))
40 throw new HumanReadableTimeException('illegal value for maxunit: "' . $maxunit . '"');
41 foreach ($allowed as $key => $value) {
42 if ($maxunit == $key)
43 break;
44 unset($allowed[$key]);
45 }
46
47 $seconds = intval($seconds);
48 $hrt = '';
49 foreach ($allowed as $key => $value) {
50 switch ($key) {
51 case 'w':
52 $tmp = intval($seconds / (7*86400));
53 if ($tmp > 0)
54 $seconds %= (7*86400);
55 $allowed[$key] += $tmp;
56 break;
57 case 'd':
58 $tmp = intval($seconds / (86400));
59 if ($tmp > 0)
60 $seconds %= (86400);
61 $allowed[$key] += $tmp;
62 break;
63 case 'h':
64 $tmp = intval($seconds / (3600));
65 if ($tmp > 0)
66 $seconds %= (3600);
67 $allowed[$key] += $tmp;
68 break;
69 case 'm':
70 $tmp = intval($seconds / (60));
71 if ($tmp > 0)
72 $seconds %= (60);
73 $allowed[$key] += $tmp;
74 break;
75 case 's':
76 $allowed[$key] += $seconds;
77 break;
78 }
79 }
80
81 $hrt = '';
82 foreach ($allowed as $key => $value) {
83 if ($value > 0)
84 $hrt .= sprintf('%d%s', $value, $key);
85 }
86 return $hrt;
87 }
88
89 /**
90 * parse a string of 3h2m7s and return the number of seconds as integer
91 * add "s" to the end of the number if $addsecond is set to true
92 * @param string $hr
93 * @param boolean $addsecond
94 * @return integer|string
95 */
96 public static function HR2Seconds($hr, $addsecond = false) {
97 $hr = trim($hr);
98 if ($hr == '') {
99 if ($addsecond === true)
100 return '0s';
101 else
102 return 0;
103 }
104
105 $hr = strtolower($hr);
106
107 $matches = array();
108 if (preg_match_all('/([0-9]*)([wdhms])/', $hr, $matches, PREG_SET_ORDER) > 0) {
109 $interval = 0;
110 for ($i = 0; $i < count($matches); $i++) {
111 switch ($matches[$i][2]) {
112 case 'w':
113 $interval += $matches[$i][1] * 7 * 86400;
114 break;
115 case 'd':
116 $interval += $matches[$i][1] * 86400;
117 break;
118 case 'h':
119 $interval += $matches[$i][1] * 3600;
120 break;
121 case 'm':
122 $interval += $matches[$i][1] * 60;
123 break;
124 case 's':
125 $interval += $matches[$i][1];
126 break;
127 }
128 }
129 if ($addsecond === true)
130 return sprintf('%ds', $interval);
131 else
132 return $interval;
133 }
134
135 if ($addsecond === true)
136 return '0s';
137 else
138 return 0;
139 }
140 }