Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 $ereg_duration = '(-)?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T?([0-9]+H)?([0-9]+M)?([0-9]+S)?';
00057 class ilBMFType_duration
00058 {
00059
00060 function unix_to_duration($seconds) {
00061 return ilBMFType_duration::getduration($seconds);
00062 }
00063
00064 function mod($a, $b, &$d, &$r) {
00065 $d = floor( $a / $b );
00066 $r = $a % $b;
00067 }
00068
00069 function getduration($seconds) {
00070 $neg = '';
00071 if ($seconds < 0) {
00072 $neg = '-';
00073 $seconds = $seconds * -1;
00074 }
00075
00076 $_mi = 60;
00077 $_h = $_mi * 60;
00078 $_d = $_h * 24;
00079
00080 $_m = $_d * 30;
00081 $_y = $_d * 365;
00082
00083 ilBMFType_duration::mod($seconds, $_y, $y, $seconds);
00084 ilBMFType_duration::mod($seconds, $_m, $m, $seconds);
00085 ilBMFType_duration::mod($seconds, $_d, $d, $seconds);
00086 ilBMFType_duration::mod($seconds, $_h, $h, $seconds);
00087 ilBMFType_duration::mod($seconds, $_mi, $mi, $s);
00088
00089 $duration = $neg.'P';
00090 if ($y) $duration .= $y.'Y';
00091 if ($m) $duration .= $m.'M';
00092 if ($d) $duration .= $d.'D';
00093 if ($h || $mi || $s) $duration .='T';
00094 if ($h) $duration .= $h.'H';
00095 if ($mi) $duration .= $mi.'M';
00096 if ($s) $duration .= $s.'S';
00097 if ($duration == 'P' || $duration == '-P') $duration = 'PT0S';
00098 return $duration;
00099 }
00100
00101 function mkduration($n, $Y, $Mo, $D, $H, $Mi, $S) {
00102 $_mi = 60;
00103 $_h = $_mi * 60;
00104 $_d = $_h * 24;
00105
00106 $_m = $_d * 30;
00107 $_y = $_d * 365;
00108
00109 $sec = $Y * $_y + $Mo * $_m + $D * $_d + $H * $_h + $Mi * $_mi + $S;
00110 if ($n == '-') $sec = $sec * -1;
00111 return $sec;
00112 }
00113
00114 function duration_to_unix($duration) {
00115 global $ereg_duration;
00116 if (ereg($ereg_duration,$duration,$regs)) {
00117 return ilBMFType_duration::mkduration($regs[1], $regs[2], $regs[3], $regs[4], $regs[5], $regs[6], $regs[7]);
00118 }
00119 return FALSE;
00120 }
00121
00122 function is_duration($duration) {
00123 global $ereg_duration;
00124 return ereg($ereg_duration,$duration,$regs);
00125 }
00126
00127 function _test($time) {
00128 if (ilBMFType_duration::is_duration($time)) {
00129 $t = ilBMFType_duration::duration_to_unix($time);
00130 echo "Duration: $time is ".$t." seconds\n";
00131 } else {
00132 $t = ilBMFType_duration::unix_to_duration($time);
00133 echo "Seconds: $time is ".$t." duration\n";
00134 }
00135 return $t;
00136 }
00137
00138 function add($d1, $d2) {
00139 $s1 = ilBMFType_duration::duration_to_unix($d1);
00140 $s2 = ilBMFType_duration::duration_to_unix($d2);
00141 return ilBMFType_duration::unix_to_duration($s1 + $s2);
00142 }
00143
00144 function subtract($d1, $d2) {
00145 $s1 = ilBMFType_duration::duration_to_unix($d1);
00146 $s2 = ilBMFType_duration::duration_to_unix($d2);
00147 return ilBMFType_duration::unix_to_duration($s1 - $s2);
00148 }
00149
00150 }
00151
00152
00153
00154 $t = ilBMFType_duration::_test('P1Y2M3DT10H30M');
00155 ilBMFType_duration::_test($t);
00156 $t = ilBMFType_duration::_test('-P120D');
00157 ilBMFType_duration::_test($t);
00158
00159
00160 $t = ilBMFType_duration::_test(time());
00161 ilBMFType_duration::_test($t);
00162
00163 print "Add should be PT0S: ".ilBMFType_duration::add('-P120D','P4M')."\n";
00164 print "Subtract should be PT0S: ".ilBMFType_duration::subtract('P120D','P4M')."\n";
00165 ?>