Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
tao_helpers_Duration
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 timetoDuration
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 intervalToTime
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 durationToTime
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2013 -  (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22use oat\tao\helpers\DateIntervalMS;
23
24/**
25 * Helps you to manipulate durations.
26 *
27 * @author Bertrand Chevrier <bertrand@taotesting.com>
28 * @package tao
29 */
30class tao_helpers_Duration
31{
32    /**
33     * Converts a time string to an ISO8601 duration
34     * @param string $time as hh:mm:ss.micros
35     * @return string the ISO duration
36     */
37    public static function timetoDuration($time)
38    {
39        $duration = 'PT';
40
41        $regexp = "/^([0-9]{2}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,6})?$/";
42
43        if (preg_match($regexp, $time, $matches)) {
44            $duration .= intval($matches[1]) . 'H' . intval($matches[2]) . 'M';
45            $duration .= isset($matches[4])
46                ? intval($matches[3]) . $matches[4] . 'S'
47                : intval($matches[3]) . 'S';
48        } else {
49            $duration .= '0S';
50        }
51
52        return $duration;
53    }
54
55    /**
56     * Converts  an interval to a time
57     * @param DateInterval $interval
58     * @return string time hh:mm:ss
59     */
60    public static function intervalToTime(DateInterval $interval)
61    {
62        $time = null;
63
64        if (!is_null($interval)) {
65            $format = property_exists(get_class($interval), 'u') ? '%H:%I:%S.%U' : '%H:%I:%S';
66            $time = $interval->format($format);
67        }
68
69        return $time;
70    }
71
72    /**
73     * Converts a duration to a time
74     * @param string $duration the ISO duration
75     * @return string time hh:mm:ss.micros
76     */
77    public static function durationToTime($duration)
78    {
79        $time = null;
80
81        try {
82            $interval = preg_match('/(\.[0-9]{1,6}S)$/', $duration)
83                ? new DateIntervalMS($duration)
84                : new DateInterval($duration);
85            $time = self::intervalToTime($interval);
86        } catch (Exception $e) {
87            common_Logger::e($e->getMessage());
88        }
89
90        return $time;
91    }
92}