-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuration.class.php
More file actions
179 lines (141 loc) · 4.55 KB
/
Duration.class.php
File metadata and controls
179 lines (141 loc) · 4.55 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?PHP
/*
#LICENSE BEGIN
**********************************************************************
* OgerArch - Archaeological Database is released under the GNU General Public License (GPL) <http://www.gnu.org/licenses>
* Copyright (C) Gerhard Öttl <gerhard.oettl@ogersoft.at>
**********************************************************************
#LICENSE END
*/
/*
* Duration helper class to convert input to seconds
* and format seconds for output
*
* @format: H=hours, i=minutes, s=seconds
*/
class Duration {
protected static $format = 'H:i'; // fixed format for this application
protected static $delimChar = ":"; // delimiter for duration parts (input)
protected static $altDelimChar = ".,"; // alternative delimiter for duration parts
/**
* return formated duration
*/
public static function format($seconds, $format = null) {
if (!$seconds) {
return '';
}
// check for minus
if ($seconds < 0) {
$seconds = abs($seconds);
$minus = true;
}
// split into time peaces / units
$hours = (int) ($seconds / 60 / 60);
$seconds -= $hours * 60 * 60;
$minutes = (int) ($seconds / 60);
$seconds -= $minutes * 60;
/*
$days = (int) ($hours / 24)
$hours -= $days * 24;
*/
// fill format (overflow not used time parts to smaler units)
$str = ($format ?: static::$format);
if (strpos($str, "H") !== false) {
// only pad to ONE leading zero
$str = str_replace("H", str_pad($hours, 1, '0', STR_PAD_LEFT), $str);
} else {
$minutes += $hours * 60;
}
if (strpos($str, "i") !== false) {
$str = str_replace("i", str_pad($minutes, 2, '0', STR_PAD_LEFT), $str);
} else {
$seconds += $minutes * 60;
}
if (strpos($str, "s") !== false) {
$str = str_replace("s", str_pad($seconds, 2, '0', STR_PAD_LEFT), $str);
}
if ($minus) {
$str = "-" . $str;
}
return $str;
} // end of formated output
/**
* Formated duration.
* Short style without trailing components and without leading zeros.
*/
public static function formatBare($inStr, $format = null) {
$str = static::format($inStr, $format);
// remove trailing parts
$pattern = '/' . static::$delimChar . '0+$/';
while (preg_match($pattern, $str)) {
$str = preg_replace($pattern, '', $str);
}
// remove leading zeros
$pattern = '/^0+([1-9])/';
$str = preg_replace($pattern, '$1', $str);
return $str;
} // end of short format
/**
* parse input according to format
* ATTENTION: parsing from left to right (most significant first)
*/
public static function parse(&$inStrOri, $format = null) {
// trim and replace alternative delimiters
$inStr = $inStrOri;
$inStr = trim($inStr);
// UNDOCUMENTED HACK: (relays on next hack)
// if first char is a '=' eval the expression and use the result as hours decimals
// replace every non arithmetic chars to have *some* security
// replace european decimal sign
if (substr($inStr, 0, 1) == '=') {
$inStr = strtr($inStr, ',', '.');
$inStr = '#' . OgerFunc::evalMath(substr($inStr,1));
}
// ANOTHER UNDOCUMENTED HACK:
// if first char is a '#' it are hours (decimals are fractal of hours)
// replace european decimal sign
if (substr($inStr, 0, 1) == '#') {
$inStr = trim(substr($inStr, 1));
$inStr = strtr($inStr, ',', '.');
$seconds = $inStr * 60 * 60;
// reformat original input string
$inStrOri = self::format($seconds);
return $seconds;
}
// replace alternative delimiters
$inStr = strtr($inStr, static::$altDelimChar, str_repeat(static::$delimChar, strlen(static::$altDelimChar)));
// check for valid chars, allow leading minus sign
if (substr($inStr, 0, 1) == "-") {
$minus = true;
$inStr = substr($inStr, 1);
$inStr = trim($inStr);
}
// check for empty field and exit if empty
if ($inStr == '') {
return 0;
}
for ($i=0; $i < strlen($inStr); $i++) {
if (strpos("01234567890" . static::$delimChar, substr($inStr, $i, 1)) === false) {
throw new Exception(Oger::_("Ungültiges Zeichen in Zeitdauer: $inStrOri."));
}
} // end of check chars
// fill format multiplier array
$tmpFormat = ($format ?: static::$format);
$tmpFormat = str_replace("H", 60 * 60, $tmpFormat);
$tmpFormat = str_replace("i", 60, $tmpFormat);
$tmpFormat = str_replace("s", 1, $tmpFormat);
$multiply = explode(static::$delimChar, $tmpFormat);
$input = explode(static::$delimChar, $inStr);
$seconds = 0;
for ($i = 0; $i < count($input); $i++) {
$seconds += $input[$i] * $multiply[$i];
}
if ($minus) {
$seconds *= -1;
}
// reformat original input string
$inStrOri = self::format($seconds);
return $seconds;
}
} // end of class
?>