SOAP
[ class tree: SOAP ] [ index: SOAP ] [ all elements ]

Source for file duration.php

Documentation is available at duration.php

  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.  */
  22.  
  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.  */
  79. {
  80.     // format PnYnMnDTnHnMnS
  81.     function unix_to_duration($seconds)
  82.     {
  83.         return SOAP_Type_duration::getduration($seconds);
  84.     }
  85.  
  86.     function mod($a$b&$d&$r)
  87.     {
  88.         $d floor($a $b);
  89.         $r $a $b;
  90.     }
  91.  
  92.     function getduration($seconds)
  93.     {
  94.         $neg '';
  95.         if ($seconds < 0{
  96.             $neg '-';
  97.             $seconds $seconds * -1;
  98.         }
  99.  
  100.         $_mi = 60;
  101.         $_h $_mi * 60;
  102.         $_d $_h * 24;
  103.         // XXX how do we properly handle month and year values?
  104.         $_m $_d * 30;
  105.         $_y $_d * 365;
  106.  
  107.         SOAP_Type_duration::mod($seconds$_y$y$seconds);
  108.         SOAP_Type_duration::mod($seconds$_m$m$seconds);
  109.         SOAP_Type_duration::mod($seconds$_d$d$seconds);
  110.         SOAP_Type_duration::mod($seconds$_h$h$seconds);
  111.         SOAP_Type_duration::mod($seconds$_mi$mi$s);
  112.  
  113.         $duration $neg.'P';
  114.         if ($y$duration .= $y.'Y';
  115.         if ($m$duration .= $m.'M';
  116.         if ($d$duration .= $d.'D';
  117.         if ($h || $mi || $s$duration .='T';
  118.         if ($h$duration .= $h.'H';
  119.         if ($mi$duration .= $mi.'M';
  120.         if ($s$duration .= $s.'S';
  121.         if ($duration == 'P' || $duration == '-P'$duration 'PT0S';
  122.  
  123.         return $duration;
  124.     }
  125.  
  126.     function mkduration($n$Y$Mo$D$H$Mi$S)
  127.     {
  128.         $_mi = 60;
  129.         $_h $_mi * 60;
  130.         $_d $_h * 24;
  131.         // XXX how do we properly handle month and year values?
  132.         $_m $_d * 30;
  133.         $_y $_d * 365;
  134.  
  135.         $sec $Y $_y $Mo $_m $D $_d $H $_h $Mi $_mi $S;
  136.         if ($n == '-'$sec $sec * -1;
  137.  
  138.         return $sec;
  139.     }
  140.  
  141.     function duration_to_unix($duration)
  142.     {
  143.         if (ereg('(-)?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T?([0-9]+H)?([0-9]+M)?([0-9]+S)?'$duration$regs)) {
  144.             return SOAP_Type_duration::mkduration($regs[1]$regs[2]$regs[3]$regs[4]$regs[5]$regs[6]$regs[7]);
  145.         }
  146.         return false;
  147.     }
  148.  
  149.     function is_duration($duration)
  150.     {
  151.         return ereg('(-)?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T?([0-9]+H)?([0-9]+M)?([0-9]+S)?'$duration$regs);
  152.     }
  153.  
  154.     function _test($time)
  155.     {
  156.         if (SOAP_Type_duration::is_duration($time)) {
  157.             $t SOAP_Type_duration::duration_to_unix($time);
  158.             echo "Duration: $time is ".$t." seconds\n";
  159.         else {
  160.             $t SOAP_Type_duration::unix_to_duration($time);
  161.             echo "Seconds: $time is ".$t." duration\n";
  162.         }
  163.         return $t;
  164.     }
  165.  
  166.     function add($d1$d2)
  167.     {
  168.         $s1 SOAP_Type_duration::duration_to_unix($d1);
  169.         $s2 SOAP_Type_duration::duration_to_unix($d2);
  170.         return SOAP_Type_duration::unix_to_duration($s1 $s2);
  171.     }
  172.  
  173.     function subtract($d1$d2)
  174.     {
  175.         $s1 SOAP_Type_duration::duration_to_unix($d1);
  176.         $s2 SOAP_Type_duration::duration_to_unix($d2);
  177.         return SOAP_Type_duration::unix_to_duration($s1 $s2);
  178.     }
  179.  
  180. }
  181.  
  182. /* Tests. */
  183. $t SOAP_Type_duration::_test('P1Y2M3DT10H30M');
  184. SOAP_Type_duration::_test($t);
  185. $t SOAP_Type_duration::_test('-P120D');
  186. SOAP_Type_duration::_test($t);
  187.  
  188. /* Duration since 1970. */
  189. $t SOAP_Type_duration::_test(time());
  190. SOAP_Type_duration::_test($t);
  191.  
  192. echo 'Add should be PT0S: ' SOAP_Type_duration::add('-P120D','P4M'"\n";
  193. echo 'Subtract should be PT0S: ' SOAP_Type_duration::subtract('P120D','P4M'"\n";

Documentation generated on Mon, 04 Aug 2008 20:00:18 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.