- Timestamp:
- 2012/01/17 16:32:25 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_11-dev/data/module/SOAP/Type/duration.php
r21318 r21410 1 1 <?php 2 /** 3 * This file contains the code for the SOAP_Type_duration class. 4 * 5 * PHP versions 4 and 5 6 * 7 * LICENSE: This source file is subject to version 2.02 of the PHP license, 8 * that is bundled with this package in the file LICENSE, and is available at 9 * through the world-wide-web at http://www.php.net/license/2_02.txt. If you 10 * did not receive a copy of the PHP license and are unable to obtain it 11 * through the world-wide-web, please send a note to license@php.net so we can 12 * mail you a copy immediately. 13 * 14 * @category Web Services 15 * @package SOAP 16 * @author Shane Caraveo <Shane@Caraveo.com> Port to PEAR and more 17 * @author Jan Schneider <jan@horde.org> Maintenance 18 * @copyright 2003-2007 The PHP Group 19 * @license http://www.php.net/license/2_02.txt PHP License 2.02 20 * @link http://pear.php.net/package/SOAP 21 */ 2 /* 3 http://www.w3.org/TR/xmlschema-2/ 22 4 23 /** 24 * This is only an aproximation of duration, more work still to do. See the 25 * schema url for more info on duration. 26 * 27 * http://www.w3.org/TR/xmlschema-2/ 28 * 29 * [Definition:] duration represents a duration of time. The value space of 30 * duration is a six-dimensional space where the coordinates designate the 31 * Gregorian year, month, day, hour, minute, and second components 32 * defined in 5.5.3.2 of [ISO 8601], respectively. These components are 33 * ordered in their significance by their order of appearance i.e. as year, 34 * month, day, hour, minute, and second. 35 * 36 * 3.2.6.1 Lexical representation 37 * The lexical representation for duration is the [ISO 8601] extended 38 * format PnYn MnDTnH nMnS, where nY represents the number of 39 * years, nM the number of months, nD the number of days, 'T' is the 40 * date/time separator, nH the number of hours, nM the number of 41 * minutes and nS the number of seconds. The number of seconds 42 * can include decimal digits to arbitrary precision. 43 * 44 * The values of the Year, Month, Day, Hour and Minutes components 45 * are not restricted but allow an arbitrary integer. Similarly, the 46 * value of the Seconds component allows an arbitrary decimal. 47 * Thus, the lexical representation of duration does not follow the 48 * alternative format of 5.5.3.2.1 of [ISO 8601]. 49 * 50 * An optional preceding minus sign ('-') is allowed, to indicate a 51 * negative duration. If the sign is omitted a positive duration is 52 * indicated. See also ISO 8601 Date and Time Formats (D). 53 * 54 * For example, to indicate a duration of 1 year, 2 months, 3 days, 55 * 10 hours, and 30 minutes, one would write: P1Y2M3DT10H30M. 56 * One could also indicate a duration of minus 120 days as: -P120D. 57 * 58 * Reduced precision and truncated representations of this format 59 * are allowed provided they conform to the following: 60 * 61 * If the number of years, months, days, hours, minutes, or seconds 62 * in any expression equals zero, the number and its corresponding 63 * designator *may* be omitted. However, at least one number and 64 * its designator *must* be present. 65 * The seconds part *may* have a decimal fraction. 66 * The designator 'T' shall be absent if all of the time items are absent. 67 * The designator 'P' must always be present. 68 * For example, P1347Y, P1347M and P1Y2MT2H are all allowed; P0Y1347M 69 * and P0Y1347M0D are allowed. P-1347M is not allowed although -P1347M 70 * is allowed. P1Y2MT is not allowed. 71 * 72 * @access public 73 * @package SOAP 74 * @author Shane Caraveo <shane@php.net> Port to PEAR and more 75 * @author Jan Schneider <jan@horde.org> Maintenance 76 * @todo Figure out best aproximation for year and month conversion to 77 * seconds 78 */ 5 [Definition:] duration represents a duration of time. The value space of 6 duration is a six-dimensional space where the coordinates designate the 7 Gregorian year, month, day, hour, minute, and second components 8 defined in 5.5.3.2 of [ISO 8601], respectively. These components are 9 ordered in their significance by their order of appearance i.e. as year, 10 month, day, hour, minute, and second. 11 12 3.2.6.1 Lexical representation 13 The lexical representation for duration is the [ISO 8601] extended 14 format PnYn MnDTnH nMnS, where nY represents the number of 15 years, nM the number of months, nD the number of days, 'T' is the 16 date/time separator, nH the number of hours, nM the number of 17 minutes and nS the number of seconds. The number of seconds 18 can include decimal digits to arbitrary precision. 19 20 The values of the Year, Month, Day, Hour and Minutes components 21 are not restricted but allow an arbitrary integer. Similarly, the 22 value of the Seconds component allows an arbitrary decimal. 23 Thus, the lexical representation of duration does not follow the 24 alternative format of 5.5.3.2.1 of [ISO 8601]. 25 26 An optional preceding minus sign ('-') is allowed, to indicate a 27 negative duration. If the sign is omitted a positive duration is 28 indicated. See also ISO 8601 Date and Time Formats (D). 29 30 For example, to indicate a duration of 1 year, 2 months, 3 days, 31 10 hours, and 30 minutes, one would write: P1Y2M3DT10H30M. 32 One could also indicate a duration of minus 120 days as: -P120D. 33 34 Reduced precision and truncated representations of this format 35 are allowed provided they conform to the following: 36 37 If the number of years, months, days, hours, minutes, or seconds 38 in any expression equals zero, the number and its corresponding 39 designator *may* be omitted. However, at least one number and 40 its designator *must* be present. 41 The seconds part *may* have a decimal fraction. 42 The designator 'T' shall be absent if all of the time items are absent. 43 The designator 'P' must always be present. 44 For example, P1347Y, P1347M and P1Y2MT2H are all allowed; P0Y1347M 45 and P0Y1347M0D are allowed. P-1347M is not allowed although -P1347M 46 is allowed. P1Y2MT is not allowed. 47 48 */ 49 50 /* this is only an aproximation of duration, more work still to do. 51 see above schema url for more info on duration 52 53 TODO: figure out best aproximation for year and month conversion to seconds 54 */ 55 56 $ereg_duration = '(-)?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T?([0-9]+H)?([0-9]+M)?([0-9]+S)?'; 79 57 class SOAP_Type_duration 80 58 { 81 59 // format PnYnMnDTnHnMnS 82 function unix_to_duration($seconds) 83 { 60 function unix_to_duration($seconds) { 84 61 return SOAP_Type_duration::getduration($seconds); 85 62 } 86 87 function mod($a, $b, &$d, &$r) 88 { 89 $d = floor($a / $b); 63 64 function mod($a, $b, &$d, &$r) { 65 $d = floor( $a / $b ); 90 66 $r = $a % $b; 91 67 } 92 93 function getduration($seconds) 94 { 68 69 function getduration($seconds) { 95 70 $neg = ''; 96 71 if ($seconds < 0) { … … 98 73 $seconds = $seconds * -1; 99 74 } 100 75 101 76 $_mi = 60; 102 77 $_h = $_mi * 60; … … 111 86 SOAP_Type_duration::mod($seconds, $_h, $h, $seconds); 112 87 SOAP_Type_duration::mod($seconds, $_mi, $mi, $s); 113 88 114 89 $duration = $neg.'P'; 115 90 if ($y) $duration .= $y.'Y'; … … 121 96 if ($s) $duration .= $s.'S'; 122 97 if ($duration == 'P' || $duration == '-P') $duration = 'PT0S'; 123 124 98 return $duration; 125 99 } 126 127 function mkduration($n, $Y, $Mo, $D, $H, $Mi, $S) 128 { 100 101 function mkduration($n, $Y, $Mo, $D, $H, $Mi, $S) { 129 102 $_mi = 60; 130 103 $_h = $_mi * 60; … … 133 106 $_m = $_d * 30; 134 107 $_y = $_d * 365; 135 108 136 109 $sec = $Y * $_y + $Mo * $_m + $D * $_d + $H * $_h + $Mi * $_mi + $S; 137 110 if ($n == '-') $sec = $sec * -1; 138 139 111 return $sec; 140 112 } 141 142 function duration_to_unix($duration) 143 {144 if (ereg( '(-)?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T?([0-9]+H)?([0-9]+M)?([0-9]+S)?', $duration,$regs)) {113 114 function duration_to_unix($duration) { 115 global $ereg_duration; 116 if (ereg($ereg_duration,$duration,$regs)) { 145 117 return SOAP_Type_duration::mkduration($regs[1], $regs[2], $regs[3], $regs[4], $regs[5], $regs[6], $regs[7]); 146 118 } 147 return false;119 return FALSE; 148 120 } 149 150 function is_duration($duration) 151 {152 return ereg( '(-)?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T?([0-9]+H)?([0-9]+M)?([0-9]+S)?', $duration,$regs);121 122 function is_duration($duration) { 123 global $ereg_duration; 124 return ereg($ereg_duration,$duration,$regs); 153 125 } 154 155 function _test($time) 156 { 126 127 function _test($time) { 157 128 if (SOAP_Type_duration::is_duration($time)) { 158 129 $t = SOAP_Type_duration::duration_to_unix($time); … … 164 135 return $t; 165 136 } 166 167 function add($d1, $d2) 168 { 137 138 function add($d1, $d2) { 169 139 $s1 = SOAP_Type_duration::duration_to_unix($d1); 170 140 $s2 = SOAP_Type_duration::duration_to_unix($d2); 171 141 return SOAP_Type_duration::unix_to_duration($s1 + $s2); 172 142 } 173 174 function subtract($d1, $d2) 175 { 143 144 function subtract($d1, $d2) { 176 145 $s1 = SOAP_Type_duration::duration_to_unix($d1); 177 146 $s2 = SOAP_Type_duration::duration_to_unix($d2); … … 181 150 } 182 151 183 /* Tests. */ 152 /* tests */ 153 184 154 $t = SOAP_Type_duration::_test('P1Y2M3DT10H30M'); 185 155 SOAP_Type_duration::_test($t); … … 187 157 SOAP_Type_duration::_test($t); 188 158 189 / * Duration since 1970. */159 // duration since 1970 190 160 $t = SOAP_Type_duration::_test(time()); 191 161 SOAP_Type_duration::_test($t); 192 162 193 echo 'Add should be PT0S: ' . SOAP_Type_duration::add('-P120D','P4M') . "\n"; 194 echo 'Subtract should be PT0S: ' . SOAP_Type_duration::subtract('P120D','P4M') . "\n"; 163 print "Add should be PT0S: ".SOAP_Type_duration::add('-P120D','P4M')."\n"; 164 print "Subtract should be PT0S: ".SOAP_Type_duration::subtract('P120D','P4M')."\n"; 165 ?>
Note: See TracChangeset
for help on using the changeset viewer.