1: <?php // BUILD: Remove line
2:
3: namespace intouch\ical;
4:
5: /**
6: * A class for calculating how many seconds a duration-string is
7: *
8: * @author Morten Fangel (C) 2008
9: * @author Michael Kahn (C) 2013
10: * @license http://creativecommons.org/licenses/by-sa/2.5/dk/deed.en_GB CC-BY-SA-DK
11: */
12:
13: class Duration {
14: protected $dur;
15:
16: /**
17: * Constructs a new intouch\ical\Duration from a duration-rule.
18: * The basic build-up of DURATIONs are:
19: * (["+"] / "-") "P" (dur-date / dur-date + "T" + dur-time / dur-time / dur-week)
20: * Is solved via a really fugly reg-exp with way to many ()'s..
21: *
22: * @param $duration string
23: */
24: public function __construct( $duration ) {
25:
26: $ts = 0;
27:
28: if (preg_match('/[\\+\\-]{0,1}P((\d+)W)?((\d+)D)?(T)?((\d+)H)?((\d+)M)?((\d+)S)?/', $duration, $matches) === 1) {
29: $results = array(
30: 'weeks'=> (int)@ $matches[2],
31: 'days'=> (int)@ $matches[4],
32: 'hours'=> (int)@ $matches[7],
33: 'minutes'=>(int)@ $matches[9],
34: 'seconds'=>(int)@ $matches[11]
35: );
36:
37: $ts += $results['seconds'];
38: $ts += 60 * $results['minutes'];
39: $ts += 60 * 60 * $results['hours'];
40: $ts += 24 * 60 * 60 * $results['days'];
41: $ts += 7 * 24 * 60 * 60 * $results['weeks'];
42: } else {
43: // Invalid duration!
44: }
45:
46: $dir = ($duration{0} == '-') ? -1 : 1;
47:
48: $this->dur = $dir * $ts;
49: }
50:
51: /**
52: * Returns the duration in seconds
53: * @return int
54: */
55: public function getDuration() {
56: return $this->dur;
57: }
58: }
59: