changed git call from https to git readonly
[atutor.git] / mods / atutor_opencaps / opencaps / include / classes / time_class.php
1 <?php\r
2 /*\r
3  * OpenCaps\r
4  * http://opencaps.atrc.utoronto.ca\r
5  * \r
6  * Copyright 2009 Antonio Gamba Bari\r
7  * Adaptive Technology Resource Centre, University of Toronto\r
8  * \r
9  * Licensed under the Educational Community License (ECL), Version 2.0. \r
10  * You may not use this file except in compliance with this License.\r
11  * http://www.opensource.org/licenses/ecl2.php\r
12  * \r
13  */\r
14 \r
15 class time {\r
16         public $ms;\r
17         public $formatted;\r
18 \r
19         function __construct($t, $milli=true) {\r
20                 if ($milli) {\r
21                         $this->ms = $t;\r
22                         $this->formatted = $this->timeFormatted($t);\r
23                 } else {\r
24                         $this->ms = $this->timeMilli($t);\r
25                         $this->formatted = $t;\r
26                 }\r
27         }\r
28         \r
29         \r
30         /*\r
31          * Functions written by Antonio for the conversion service\r
32          */\r
33         \r
34         /**\r
35          * Converts milliseconds time mark into 00:00:00:000 format (QT native format)\r
36          *\r
37          * @param int $miliSecTime A time format in miliseconds (e.g. 26070) 1000 = 1 second\r
38          * @return String $qtTimeString A QT mark in 00:00:00:000 format\r
39          */\r
40         static public function timeFormatted($miliSecTime)\r
41         {\r
42                 $qtTimeString = '';\r
43                 //  milliseconds holder \r
44                 $myMiliSecTime = $miliSecTime;\r
45                 \r
46                 // basic constant knowledge, values in milliseconds\r
47                 $anHour = 3600000; // 60*60*1000\r
48                 $aMin = 60000; // 60*1000\r
49                 $aSec = 1000; // 1 * 1000\r
50                 \r
51                 // temp holders for to store equivalent  hh, mm, sec, milisec\r
52                 $myHours = 0;\r
53                 $myMins = 0;\r
54                 $mySec = 0;\r
55                                 //$myMsec = 0; /// not using it.!!! \r
56                 \r
57         // initialize timeArray\r
58                 $timeArray = array();\r
59                 $timeArray['hour'] = '';\r
60                 $timeArray['min'] = '';\r
61                 $timeArray['sec'] = '';\r
62                 $timeArray['msec'] = '';                \r
63                 \r
64                 // parsing millisecodns QT time format 00:00:00.000\r
65 \r
66                 // is time mark at least an hour?\r
67                 if($miliSecTime>=$anHour)\r
68                 {\r
69                         // get only the int value\r
70                         $myHours = intval($miliSecTime/$anHour);\r
71 \r
72                         // set the tot milliseconds left after removing number of hour(s) \r
73                         $myMiliSecTime -= ($myHours*$anHour);\r
74 \r
75                         // set the current hours add leading 0 if needed\r
76                         if ($myHours<10)\r
77                         {\r
78                                 $timeArray['hour'] = '0'.$myHours;\r
79                         } else {\r
80                                 $timeArray['hour'] = ''.$myHours; \r
81                         }\r
82                 } else {\r
83                         $timeArray['hour'] = '00';\r
84                 }\r
85                 \r
86                 // Is time mark at least a minute? or rather, how many minutes are left?\r
87                 if($myMiliSecTime>=$aMin-1)\r
88                 {\r
89                         // get only the int value\r
90                         $myMins = intval($myMiliSecTime/$aMin);\r
91                                                 \r
92                         // set the milliseconds left after removing total of minute(s) \r
93                         $myMiliSecTime -= ($myMins*$aMin);\r
94                         \r
95                         // set the current minutes and add leading 0 if needed\r
96                         if ($myMins<10)\r
97                         {\r
98                                 $timeArray['min'] = '0'.$myMins;\r
99                         } else {\r
100                                 $timeArray['min'] = ''. $myMins;\r
101                         }\r
102                 } else {\r
103                         $timeArray['min'] = '00';\r
104                 }\r
105                 \r
106                 // does it have seconds, or rather, how many seconds are left\r
107                 if($myMiliSecTime>=$aSec)\r
108                 {\r
109                         // get only the int value\r
110                         $mySec = intval($myMiliSecTime/$aSec);\r
111 \r
112                         // set the milliseconds left after removing total of seconds\r
113                         $myMiliSecTime -= ($mySec*$aSec);\r
114                          \r
115                         // set the current number of seconds in time array, and add leading 0 if needed\r
116                         if ($mySec<10)\r
117                         {\r
118                                 $timeArray['sec'] = '0'.$mySec;\r
119                         } else {\r
120                                 $timeArray['sec'] = ''.$mySec;\r
121                         }\r
122                 } else {\r
123                         $timeArray['sec'] = '00';\r
124                 }\r
125                 \r
126                 // here a fix for adding leading zeros to milliseconds (e.g. 1=001, 10=010)  \r
127                 if($myMiliSecTime>0)\r
128                 {\r
129                         $tempMilliSec = 0 + (0.001 * $myMiliSecTime);\r
130                         $tempMilliSecArray = explode('.',$tempMilliSec); // split using '.' as separator\r
131                         $myMiliSecTimeString = ''. $tempMilliSecArray[1]; // get only the decimal value as a string\r
132 \r
133                         // add one zero after the sting value if $myMiliSecTimeString has 2 character\r
134                         if (strlen($myMiliSecTimeString)==2)\r
135                         {\r
136                                 $myMiliSecTimeString .= '0';\r
137                         } \r
138                         // add two zeros after the sting value if $myMiliSecTimeString has 1 character\r
139                         else if (strlen($myMiliSecTimeString)==1)\r
140                         {\r
141                                 $myMiliSecTimeString .= '00';\r
142                         }\r
143                         \r
144                         // set  millisecodns  \r
145                         $timeArray['msec'] = $myMiliSecTimeString;\r
146                 } else {\r
147                         $timeArray['msec'] = '000'; // no milliseconds left\r
148                 } \r
149 \r
150                 // concatenate values\r
151                 $qtTimeString = ''.$timeArray['hour'].':'.$timeArray['min'].':'.$timeArray['sec'].'.'.$timeArray['msec'];\r
152                 \r
153                 return $qtTimeString;\r
154                 \r
155         } // end samiToQtTime()\r
156 \r
157         /**\r
158          * Converts QT time to milliseconds format (Accepted by SAMI 1.0 and other CCformats)\r
159          * @return int $samiTime Time in miliseconds format; 1000 = 1. sec\r
160          * @param String $qtTime QT time Format; (e.g. "00:01:10.280")\r
161          */\r
162         static public function timeMilli($qtTime)\r
163         {\r
164             // Known patterns: 1, 2, or 3 decimals for millisecond definition\r
165                 $pattern_time_000 = "\[([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3})\]";\r
166         $pattern_time_00 = "\[([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2})\]";\r
167         $pattern_time_0 = "\[([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{1})\]";\r
168         $pattern_selected = '';\r
169                 \r
170         // If pattern is 3 digit\r
171         if(preg_match('/'.$pattern_time_000.'/',$qtTime))\r
172             {\r
173                 $pattern_selected = $pattern_time_000;\r
174             }\r
175             // If pattern is 2 digit\r
176         if(preg_match('/'.$pattern_time_00.'/',$qtTime))\r
177             {\r
178                 $pattern_selected = $pattern_time_00;\r
179             }\r
180             // If pattern is 1 digit\r
181         if(preg_match('/'.$pattern_time_0.'/',$qtTime))\r
182             {\r
183                 $pattern_selected = $pattern_time_0;\r
184             }\r
185             \r
186             $t1 = 0; // hours (e.g. [01])\r
187             $t2 = 0; // minutes (e.g. [12])\r
188             $t3 = 0; // seconds (e.g. [01.123])\r
189             \r
190             $qtTimeParts = explode(':',$qtTime); // split QT time mark into an array\r
191             \r
192             $t1 += $qtTimeParts[0]; // adding hours\r
193             $t2 += $qtTimeParts[1]; // adding minutes\r
194             $t3 += $qtTimeParts[2]; // adding seconds and miliseconds\r
195 \r
196             // millisecond equivalents\r
197             $t1 *= 3600000; // 1 hour = 60*60*1000\r
198             $t2 *= 60000; // 1 minute = 60*1000\r
199             $t3 *= 1000; // 1 second = 1*1000 \r
200             \r
201             // get time in milliseconds\r
202             $samiTime = $t1 + $t2 + $t3;\r
203 \r
204             return $samiTime;\r
205         \r
206         }       \r
207 \r
208\r
209 ?>