Changeset 876

Show
Ignore:
Timestamp:
04/12/08 08:29:07 (8 months ago)
Author:
laurentj
Message:

ticket #425: added a jDuration object p=Florian Hatat
ticket #453: jDateTime::durationTo method did a bad calculation of the duration, p=Florian Hatat

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/manifests/jelix-lib.mn

    r873 r876  
    301301  jDatatype.class.php 
    302302  jDateTime.class.php 
     303  jDuration.class.php 
    303304  jFile.class.php 
    304305* jFilter.class.php 
  • trunk/build/manifests/jelix-lib.mn

    r873 r876  
    301301  jDatatype.class.php 
    302302  jDateTime.class.php 
     303  jDuration.class.php 
    303304  jFile.class.php 
    304305* jFilter.class.php 
  • trunk/build/manifests/jelix-lib.mn

    r873 r876  
    301301  jDatatype.class.php 
    302302  jDateTime.class.php 
     303  jDuration.class.php 
    303304  jFile.class.php 
    304305* jFilter.class.php 
  • trunk/build/manifests/jelix-lib.mn

    r873 r876  
    301301  jDatatype.class.php 
    302302  jDateTime.class.php 
     303  jDuration.class.php 
    303304  jFile.class.php 
    304305* jFilter.class.php 
  • trunk/build/manifests/testapp.mn

    r864 r876  
    163163  jtpl.plugins.html.php 
    164164  utils.jdatatype.html_cli.php 
     165  utils.jduration.html_cli.php 
    165166  utils.jfilter.html_cli.php 
    166167  utils.jlog.html_cli.php 
  • trunk/build/manifests/testapp.mn

    r864 r876  
    163163  jtpl.plugins.html.php 
    164164  utils.jdatatype.html_cli.php 
     165  utils.jduration.html_cli.php 
    165166  utils.jfilter.html_cli.php 
    166167  utils.jlog.html_cli.php 
  • trunk/build/manifests/testapp.mn

    r864 r876  
    163163  jtpl.plugins.html.php 
    164164  utils.jdatatype.html_cli.php 
     165  utils.jduration.html_cli.php 
    165166  utils.jfilter.html_cli.php 
    166167  utils.jlog.html_cli.php 
  • trunk/build/manifests/testapp.mn

    r864 r876  
    163163  jtpl.plugins.html.php 
    164164  utils.jdatatype.html_cli.php 
     165  utils.jduration.html_cli.php 
    165166  utils.jfilter.html_cli.php 
    166167  utils.jlog.html_cli.php 
  • trunk/lib/jelix/CREDITS

    r868 r876  
    9999Florian Hatat (aka flh) 
    100100 - improvements on jDateTime + RFC2822 support (#379) 
     101 - Fix bugs on duration calculation + new jDuration class (#425, #453) 
    101102 
    102103Nicolas Jeudy (aka njeudy) 
  • trunk/lib/jelix/CREDITS

    r868 r876  
    9999Florian Hatat (aka flh) 
    100100 - improvements on jDateTime + RFC2822 support (#379) 
     101 - Fix bugs on duration calculation + new jDuration class (#425, #453) 
    101102 
    102103Nicolas Jeudy (aka njeudy) 
  • trunk/lib/jelix/CREDITS

    r868 r876  
    9999Florian Hatat (aka flh) 
    100100 - improvements on jDateTime + RFC2822 support (#379) 
     101 - Fix bugs on duration calculation + new jDuration class (#425, #453) 
    101102 
    102103Nicolas Jeudy (aka njeudy) 
  • trunk/lib/jelix/CREDITS

    r868 r876  
    9999Florian Hatat (aka flh) 
    100100 - improvements on jDateTime + RFC2822 support (#379) 
     101 - Fix bugs on duration calculation + new jDuration class (#425, #453) 
    101102 
    102103Nicolas Jeudy (aka njeudy) 
  • trunk/lib/jelix/utils/jDateTime.class.php

    r739 r876  
    77* @contributor Loic Mathaud 
    88* @contributor Florian Hatat 
    9 * @copyright   2005-2006 Laurent Jouanneau 
     9* @copyright   2005-2008 Laurent Jouanneau 
    1010* @copyright   2007 Loic Mathaud 
    11 * @copyright   2007 Florian Hatat 
     11* @copyright   2007-2008 Florian Hatat 
    1212* @copyright   2001-2005 CopixTeam, GeraldCroes, Laurent Jouanneau 
    1313* 
     
    283283                    if($match[6] != '') $this->second = $match[6]; 
    284284                    if($match[8] != 'Z'){ 
    285                         $d = new jDateTime(0,0,0,$match[10],$match[11]); 
     285                        $d = new jDuration(array('hour'=>$match[10],'minute'=>$match[11])); 
    286286                        if($match[9] == '+') 
    287287                            $this->add($d); 
     
    331331                   # Adjust according to the timezone, so that the stored time  
    332332                   # corresponds to UTC. 
    333                    $tz = new jDateTime(0, 0, 0, intval($match['tzhour']), 
    334                        intval($match['tzminute']), 0); 
     333                   $tz = new jDuration(array('hour'=>intval($match['tzhour']), 
     334                       'minute'=>intval($match['tzminute']))); 
    335335                   if($match['tzsign'] == '+'){ 
    336336                       $this->sub($tz); 
     
    348348    /** 
    349349     * Add a duration to the date. 
    350      * You can specify the duration in a jDateTime object (which then is not a date/time) 
    351      * or give each value of the duration 
    352      * @param jDateTime/int $year the duration value or a year with 4 digits 
     350     * You can specify the duration in a jDuration object or give each value of 
     351     * the duration. 
     352     * @param jDuration/int $year the duration value or a year with 4 digits 
    353353     * @param int $month month with 2 digits 
    354354     * @param int $day day with 2 digits 
     
    358358     */ 
    359359    public function add($year, $month=0, $day=0, $hour=0, $minute=0, $second=0) { 
    360         if ($year instanceof jDateTime) { 
     360        if ($year instanceof jDuration) { 
    361361            $dt = $year; 
    362362        } else { 
    363             $dt = new jDateTime($year, $month, $day, $hour, $minute, $second); 
    364         } 
    365         $t = mktime ( $this->hour +  $dt->hour, $this->minute + $dt->minute, $this->second + $dt->second , 
    366              $this->month + $dt->month, $this->day + $dt->day, $this->year + $dt->year); 
     363            $dt = new jDuration(array("year" => $year, "month" => $month, 
     364                "day" => $day, "hour" => $hour, "minute" => $minute, 
     365                "second" => $second)); 
     366        } 
     367        $t = mktime($this->hour, $this->minute, $this->second + $dt->seconds, 
     368             $this->month + $dt->months, $this->day + $dt->days, $this->year); 
    367369 
    368370        $t = getdate ($t); 
     
    377379    /** 
    378380     * substract a <b>duration</b> to the date 
    379      * You can specify the duration in a jDateTime object (which then is not a date/time) 
    380      * or give each value of the duration 
    381      * @param jDateTime/int $year the duration value or a year with 4 digits 
     381     * You can specify the duration in a jDuration object or give each value of 
     382     * the duration. 
     383     * @param jDuration/int $year the duration value or a year with 4 digits 
    382384     * @param int $month month with 2 digits 
    383385     * @param int $day day with 2 digits 
     
    387389     */ 
    388390    public function sub($year, $month=0, $day=0, $hour=0, $minute=0, $second=0) { 
    389         if ($year instanceof jDateTime) { 
     391        if ($year instanceof jDuration) { 
    390392            $dt = $year; 
    391393        } else { 
    392             $dt = new jDateTime($year, $month, $day, $hour, $minute, $second); 
    393         } 
    394         $t = mktime ( $this->hour -  $dt->hour, $this->minute - $dt->minute, $this->second - $dt->second , 
    395              $this->month - $dt->month, $this->day - $dt->day, $this->year - $dt->year); 
    396  
    397         $t = getdate ($t); 
    398         $this->year = $t['year']; 
    399         $this->month = $t['mon']; 
    400         $this->day = $t['mday']; 
    401         $this->hour = $t['hours']; 
    402         $this->minute = $t['minutes']; 
    403         $this->second = $t['seconds']; 
     394            $dt = new jDuration(array("year" => $year, "month" => $month, 
     395                "day" => $day, "hour" => $hour, "minute" => $minute, 
     396                "second" => $second)); 
     397        } 
     398        $dt->mult(-1); 
     399        $this->add($dt); 
    404400    } 
    405401 
     
    407403     * to know the duration between two dates. 
    408404     * @param jDateTime $dt  the date on which a sub will be made with the date on the current object 
    409      * @return jDateTime a jDateTime object which will contains <b>a duration</b> (not a date) 
    410      */ 
    411     public function durationTo($dt){ 
    412         $t = mktime ( $dt->hour, $dt->minute,$dt->second , $dt->month, $dt->day, $dt->year ) 
    413             - mktime ( $this->hour, $this->minute,$this->second , $this->month, $this->day, $this->year ); 
    414         $t = getdate ($t); 
    415         return new jDateTime( $t['year']-1970,$t['mon']-1, $t['mday']-1, $t['hours']-1, $t['minutes'], $t['seconds']); 
     405     * @param bool $absolute  
     406     * @return jDuration a jDuration object 
     407     */ 
     408    public function durationTo($dt, $absolute=true){ 
     409        if($absolute){ 
     410            $t = mktime($dt->hour, $dt->minute, $dt->second, 
     411                $dt->month, $dt->day, $dt->year) 
     412                - mktime($this->hour, $this->minute, $this->second, 
     413                    $this->month, $this->day, $this->year); 
     414            return new jDuration($t); 
     415        } 
     416        else{ 
     417            return new jDuration(array( 
     418                "year" => $dt->year - $this->year, 
     419                "month" => $dt->month - $this->month, 
     420                "day" => $dt->day - $this-> day, 
     421                "hour" => $dt->hour - $this->hour, 
     422                "minute" => $dt->minute - $this->minute, 
     423                "second" => $dt->second - $this->second 
     424            )); 
     425        } 
    416426    } 
    417427 
     
    444454    } 
    445455} 
    446  
    447 ?> 
  • trunk/lib/jelix/utils/jDateTime.class.php

    r739 r876  
    77* @contributor Loic Mathaud 
    88* @contributor Florian Hatat 
    9 * @copyright   2005-2006 Laurent Jouanneau 
     9* @copyright   2005-2008 Laurent Jouanneau 
    1010* @copyright   2007 Loic Mathaud 
    11 * @copyright   2007 Florian Hatat 
     11* @copyright   2007-2008 Florian Hatat 
    1212* @copyright   2001-2005 CopixTeam, GeraldCroes, Laurent Jouanneau 
    1313* 
     
    283283                    if($match[6] != '') $this->second = $match[6]; 
    284284                    if($match[8] != 'Z'){ 
    285                         $d = new jDateTime(0,0,0,$match[10],$match[11]); 
     285                        $d = new jDuration(array('hour'=>$match[10],'minute'=>$match[11])); 
    286286                        if($match[9] == '+') 
    287287                            $this->add($d); 
     
    331331                   # Adjust according to the timezone, so that the stored time  
    332332                   # corresponds to UTC. 
    333                    $tz = new jDateTime(0, 0, 0, intval($match['tzhour']), 
    334                        intval($match['tzminute']), 0); 
     333                   $tz = new jDuration(array('hour'=>intval($match['tzhour']), 
     334                       'minute'=>intval($match['tzminute']))); 
    335335                   if($match['tzsign'] == '+'){ 
    336336                       $this->sub($tz); 
     
    348348    /** 
    349349     * Add a duration to the date. 
    350      * You can specify the duration in a jDateTime object (which then is not a date/time) 
    351      * or give each value of the duration 
    352      * @param jDateTime/int $year the duration value or a year with 4 digits 
     350     * You can specify the duration in a jDuration object or give each value of 
     351     * the duration. 
     352     * @param jDuration/int $year the duration value or a year with 4 digits 
    353353     * @param int $month month with 2 digits 
    354354     * @param int $day day with 2 digits 
     
    358358     */ 
    359359    public function add($year, $month=0, $day=0, $hour=0, $minute=0, $second=0) { 
    360         if ($year instanceof jDateTime) { 
     360        if ($year instanceof jDuration) { 
    361361            $dt = $year; 
    362362        } else { 
    363             $dt = new jDateTime($year, $month, $day, $hour, $minute, $second); 
    364         } 
    365         $t = mktime ( $this->hour +  $dt->hour, $this->minute + $dt->minute, $this->second + $dt->second , 
    366              $this->month + $dt->month, $this->day + $dt->day, $this->year + $dt->year); 
     363            $dt = new jDuration(array("year" => $year, "month" => $month, 
     364                "day" => $day, "hour" => $hour, "minute" => $minute, 
     365                "second" => $second)); 
     366        } 
     367        $t = mktime($this->hour, $this->minute, $this->second + $dt->seconds, 
     368             $this->month + $dt->months, $this->day + $dt->days, $this->year); 
    367369 
    368370        $t = getdate ($t); 
     
    377379    /** 
    378380     * substract a <b>duration</b> to the date 
    379      * You can specify the duration in a jDateTime object (which then is not a date/time) 
    380      * or give each value of the duration 
    381      * @param jDateTime/int $year the duration value or a year with 4 digits 
     381     * You can specify the duration in a jDuration object or give each value of 
     382     * the duration. 
     383     * @param jDuration/int $year the duration value or a year with 4 digits 
    382384     * @param int $month month with 2 digits 
    383385     * @param int $day day with 2 digits 
     
    387389     */ 
    388390    public function sub($year, $month=0, $day=0, $hour=0, $minute=0, $second=0) { 
    389         if ($year instanceof jDateTime) { 
     391        if ($year instanceof jDuration) { 
    390392            $dt = $year; 
    391393        } else { 
    392             $dt = new jDateTime($year, $month, $day, $hour, $minute, $second); 
    393         } 
    394         $t = mktime ( $this->hour -  $dt->hour, $this->minute - $dt->minute, $this->second - $dt->second , 
    395              $this->month - $dt->month, $this->day - $dt->day, $this->year - $dt->year); 
    396  
    397         $t = getdate ($t); 
    398         $this->year = $t['year']; 
    399         $this->month = $t['mon']; 
    400         $this->day = $t['mday']; 
    401         $this->hour = $t['hours']; 
    402         $this->minute = $t['minutes']; 
    403         $this->second = $t['seconds']; 
     394            $dt = new jDuration(array("year" => $year, "month" => $month, 
     395                "day" => $day, "hour" => $hour, "minute" => $minute, 
     396                "second" => $second)); 
     397        } 
     398        $dt->mult(-1); 
     399        $this->add($dt); 
    404400    } 
    405401 
     
    407403     * to know the duration between two dates. 
    408404     * @param jDateTime $dt  the date on which a sub will be made with the date on the current object 
    409      * @return jDateTime a jDateTime object which will contains <b>a duration</b> (not a date) 
    410      */ 
    411     public function durationTo($dt){ 
    412         $t = mktime ( $dt->hour, $dt->minute,$dt->second , $dt->month, $dt->day, $dt->year ) 
    413             - mktime ( $this->hour, $this->minute,$this->second , $this->month, $this->day, $this->year ); 
    414         $t = getdate ($t); 
    415         return new jDateTime( $t['year']-1970,$t['mon']-1, $t['mday']-1, $t['hours']-1, $t['minutes'], $t['seconds']); 
     405     * @param bool $absolute  
     406     * @return jDuration a jDuration object 
     407     */ 
     408    public function durationTo($dt, $absolute=true){ 
     409        if($absolute){ 
     410            $t = mktime($dt->hour, $dt->minute, $dt->second, 
     411                $dt->month, $dt->day, $dt->year) 
     412                - mktime($this->hour, $this->minute, $this->second, 
     413                    $this->month, $this->day, $this->year); 
     414            return new jDuration($t); 
     415        } 
     416        else{ 
     417            return new jDuration(array( 
     418                "year" => $dt->year - $this->year, 
     419                "month" => $dt->month - $this->month, 
     420                "day" => $dt->day - $this-> day, 
     421                "hour" => $dt->hour - $this->hour, 
     422                "minute" => $dt->minute - $this->minute, 
     423                "second" => $dt->second - $this->second 
     424            )); 
     425        } 
    416426    } 
    417427 
     
    444454    } 
    445455} 
    446  
    447 ?> 
  • trunk/lib/jelix/utils/jDateTime.class.php

    r739 r876  
    77* @contributor Loic Mathaud 
    88* @contributor Florian Hatat 
    9 * @copyright   2005-2006 Laurent Jouanneau 
     9* @copyright   2005-2008 Laurent Jouanneau 
    1010* @copyright   2007 Loic Mathaud 
    11 * @copyright   2007 Florian Hatat 
     11* @copyright   2007-2008 Florian Hatat 
    1212* @copyright   2001-2005 CopixTeam, GeraldCroes, Laurent Jouanneau 
    1313* 
     
    283283                    if($match[6] != '') $this->second = $match[6]; 
    284284                    if($match[8] != 'Z'){ 
    285                         $d = new jDateTime(0,0,0,$match[10],$match[11]); 
     285                        $d = new jDuration(array('hour'=>$match[10],'minute'=>$match[11])); 
    286286                        if($match[9] == '+') 
    287287                            $this->add($d); 
     
    331331                   # Adjust according to the timezone, so that the stored time  
    332332                   # corresponds to UTC. 
    333                    $tz = new jDateTime(0, 0, 0, intval($match['tzhour']), 
    334                        intval($match['tzminute']), 0); 
     333                   $tz = new jDuration(array('hour'=>intval($match['tzhour']), 
     334                       'minute'=>intval($match['tzminute']))); 
    335335                   if($match['tzsign'] == '+'){ 
    336336                       $this->sub($tz); 
     
    348348    /** 
    349349     * Add a duration to the date. 
    350      * You can specify the duration in a jDateTime object (which then is not a date/time) 
    351      * or give each value of the duration 
    352      * @param jDateTime/int $year the duration value or a year with 4 digits 
     350     * You can specify the duration in a jDuration object or give each value of 
     351     * the duration. 
     352     * @param jDuration/int $year the duration value or a year with 4 digits 
    353353     * @param int $month month with 2 digits 
    354354     * @param int $day day with 2 digits 
     
    358358     */ 
    359359    public function add($year, $month=0, $day=0, $hour=0, $minute=0, $second=0) { 
    360         if ($year instanceof jDateTime) { 
     360        if ($year instanceof jDuration) { 
    361361            $dt = $year; 
    362362        } else { 
    363             $dt = new jDateTime($year, $month, $day, $hour, $minute, $second); 
    364         } 
    365         $t = mktime ( $this->hour +  $dt->hour, $this->minute + $dt->minute, $this->second + $dt->second , 
    366              $this->month + $dt->month, $this->day + $dt->day, $this->year + $dt->year); 
     363            $dt = new jDuration(array("year" => $year, "month" => $month, 
     364                "day" => $day, "hour" => $hour, "minute" => $minute, 
     365                "second" => $second)); 
     366        } 
     367        $t = mktime($this->hour, $this->minute, $this->second + $dt->seconds, 
     368             $this->month + $dt->months, $this->day + $dt->days, $this->year); 
    367369 
    368370        $t = getdate ($t); 
     
    377379    /** 
    378380     * substract a <b>duration</b> to the date 
    379      * You can specify the duration in a jDateTime object (which then is not a date/time) 
    380      * or give each value of the duration 
    381      * @param jDateTime/int $year the duration value or a year with 4 digits 
     381     * You can specify the duration in a jDuration object or give each value of 
     382     * the duration. 
     383     * @param jDuration/int $year the duration value or a year with 4 digits 
    382384     * @param int $month month with 2 digits 
    383385     * @param int $day day with 2 digits 
     
    387389     */ 
    388390    public function sub($year, $month=0, $day=0, $hour=0, $minute=0, $second=0) { 
    389         if ($year instanceof jDateTime) { 
     391        if ($year instanceof jDuration) { 
    390392            $dt = $year; 
    391393        } else { 
    392             $dt = new jDateTime($year, $month, $day, $hour, $minute, $second); 
    393         } 
    394         $t = mktime ( $this->hour -  $dt->hour, $this->minute - $dt->minute, $this->second - $dt->second , 
    395              $this->month - $dt->month, $this->day - $dt->day, $this->year - $dt->year); 
    396  
    397         $t = getdate ($t); 
    398         $this->year = $t['year']; 
    399         $this->month = $t['mon']; 
    400         $this->day = $t['mday']; 
    401         $this->hour = $t['hours']; 
    402         $this->minute = $t['minutes']; 
    403         $this->second = $t['seconds']; 
     394            $dt = new jDuration(array("year" => $year, "month" => $month, 
     395                "day" => $day, "hour" => $hour, "minute" => $minute, 
     396                "second" => $second)); 
     397        } 
     398        $dt->mult(-1); 
     399        $this->add($dt); 
    404400    } 
    405401 
     
    407403     * to know the duration between two dates. 
    408404     * @param jDateTime $dt  the date on which a sub will be made with the date on the current object 
    409      * @return jDateTime a jDateTime object which will contains <b>a duration</b> (not a date) 
    410      */ 
    411     public function durationTo($dt){ 
    412         $t = mktime ( $dt->hour, $dt->minute,$dt->second , $dt->month, $dt->day, $dt->year ) 
    413             - mktime ( $this->hour, $this->minute,$this->second , $this->month, $this->day, $this->year ); 
    414         $t = getdate ($t); 
    415         return new jDateTime( $t['year']-1970,$t['mon']-1, $t['mday']-1, $t['hours']-1, $t['minutes'], $t['seconds']); 
     405     * @param bool $absolute  
     406     * @return jDuration a jDuration object 
     407     */ 
     408    public function durationTo($dt, $absolute=true){ 
     409        if($absolute){ 
     410            $t = mktime($dt->hour, $dt->minute, $dt->second, 
     411                $dt->month, $dt->day, $dt->year) 
     412                - mktime($this->hour, $this->minute, $this->second, 
     413                    $this->month, $this->day, $this->year); 
     414            return new jDuration($t); 
     415        } 
     416        else{ 
     417            return new jDuration(array( 
     418                "year" => $dt->year - $this->year, 
     419                "month" => $dt->month - $this->month, 
     420                "day" => $dt->day - $this-> day, 
     421                "hour" => $dt->hour - $this->hour, 
     422                "minute" => $dt->minute - $this->minute, 
     423                "second" => $dt->second - $this->second 
     424            )); 
     425        } 
    416426    } 
    417427 
     
    444454    } 
    445455} 
    446  
    447 ?> 
  • trunk/lib/jelix/utils/jDateTime.class.php

    r739 r876  
    77* @contributor Loic Mathaud 
    88* @contributor Florian Hatat 
    9 * @copyright   2005-2006 Laurent Jouanneau 
     9* @copyright   2005-2008 Laurent Jouanneau 
    1010* @copyright   2007 Loic Mathaud 
    11 * @copyright   2007 Florian Hatat 
     11* @copyright   2007-2008 Florian Hatat 
    1212* @copyright   2001-2005 CopixTeam, GeraldCroes, Laurent Jouanneau 
    1313* 
     
    283283                    if($match[6] != '') $this->second = $match[6]; 
    284284                    if($match[8] != 'Z'){ 
    285                         $d = new jDateTime(0,0,0,$match[10],$match[11]); 
     285                        $d = new jDuration(array('hour'=>$match[10],'minute'=>$match[11])); 
    286286                        if($match[9] == '+') 
    287287                            $this->add($d); 
     
    331331                   # Adjust according to the timezone, so that the stored time  
    332332                   # corresponds to UTC. 
    333                    $tz = new jDateTime(0, 0, 0, intval($match['tzhour']), 
    334                        intval($match['tzminute']), 0); 
     333                   $tz = new jDuration(array('hour'=>intval($match['tzhour']), 
     334                       'minute'=>intval($match['tzminute']))); 
    335335                   if($match['tzsign'] == '+'){ 
    336336                       $this->sub($tz); 
     
    348348    /** 
    349349     * Add a duration to the date. 
    350      * You can specify the duration in a jDateTime object (which then is not a date/time) 
    351      * or give each value of the duration 
    352      * @param jDateTime/int $year the duration value or a year with 4 digits 
     350     * You can specify the duration in a jDuration object or give each value of 
     351     * the duration. 
     352     * @param jDuration/int $year the duration value or a year with 4 digits 
    353353     * @param int $month month with 2 digits 
    354354     * @param int $day day with 2 digits 
     
    358358     */ 
    359359    public function add($year, $month=0, $day=0, $hour=0, $minute=0, $second=0) { 
    360         if ($year instanceof jDateTime) { 
     360        if ($year instanceof jDuration) { 
    361361            $dt = $year; 
    362362        } else { 
    363             $dt = new jDateTime($year, $month, $day, $hour, $minute, $second); 
    364         } 
    365         $t = mktime ( $this->hour +  $dt->hour, $this->minute + $dt->minute, $this->second + $dt->second , 
    366              $this->month + $dt->month, $this->day + $dt->day, $this->year + $dt->year); 
     363            $dt = new jDuration(array("year" => $year, "month" => $month, 
     364                "day" => $day, "hour" => $hour, "minute" => $minute, 
     365                "second" => $second)); 
     366        } 
     367        $t = mktime($this->hour, $this->minute, $this->second + $dt->seconds, 
     368             $this->month + $dt->months, $this->day + $dt->days, $this->year); 
    367369 
    368370        $t = getdate ($t); 
     
    377379    /** 
    378380     * substract a <b>duration</b> to the date 
    379      * You can specify the duration in a jDateTime object (which then is not a date/time) 
    380      * or give each value of the duration 
    381      * @param jDateTime/int $year the duration value or a year with 4 digits 
     381     * You can specify the duration in a jDuration object or give each value of 
     382     * the duration. 
     383     * @param jDuration/int $year the duration value or a year with 4 digits 
    382384     * @param int $month month with 2 digits 
    383385     * @param int $day day with 2 digits 
     
    387389     */ 
    388390    public function sub($year, $month=0, $day=0, $hour=0, $minute=0, $second=0) { 
    389         if ($year instanceof jDateTime) { 
     391        if ($year instanceof jDuration) { 
    390392            $dt = $year; 
    391393        } else { 
    392             $dt = new jDateTime($year, $month, $day, $hour, $minute, $second); 
    393         } 
    394         $t = mktime ( $this->hour -  $dt->hour, $this->minute - $dt->minute, $this->second - $dt->second , 
    395              $this->month - $dt->month, $this->day - $dt->day, $this->year - $dt->year); 
    396  
    397         $t = getdate ($t); 
    398         $this->year = $t['year']; 
    399         $this->month = $t['mon']; 
    400         $this->day = $t['mday']; 
    401         $this->hour = $t['hours']; 
    402         $this->minute = $t['minutes']; 
    403         $this->second = $t['seconds']; 
     394            $dt = new jDuration(array("year" => $year, "month" => $month, 
     395                "day" => $day, "hour" => $hour, "minute" => $minute, 
     396                "second" => $second)); 
     397        } 
     398        $dt->mult(-1); 
     399        $this->add($dt); 
    404400    } 
    405401 
     
    407403     * to know the duration between two dates. 
    408404     * @param jDateTime $dt  the date on which a sub will be made with the date on the current object 
    409      * @return jDateTime a jDateTime object which will contains <b>a duration</b> (not a date) 
    410      */ 
    411     public function durationTo($dt){ 
    412         $t = mktime ( $dt->hour, $dt->minute,$dt->second , $dt->month, $dt->day, $dt->year ) 
    413             - mktime ( $this->hour, $this->minute,$this->second , $this->month, $this->day, $this->year ); 
    414         $t = getdate ($t); 
    415         return new jDateTime( $t['year']-1970,$t['mon']-1, $t['mday']-1, $t['hours']-1, $t['minutes'], $t['seconds']); 
     405     * @param bool $absolute  
     406     * @return jDuration a jDuration object 
     407     */ 
     408    public function durationTo($dt, $absolute=true){ 
     409        if($absolute){ 
     410            $t = mktime($dt->hour, $dt->minute, $dt->second, 
     411                $dt->month, $dt->day, $dt->year) 
     412                - mktime($this->hour, $this->minute, $this->second, 
     413                    $this->month, $this->day, $this->year); 
     414            return new jDuration($t); 
     415        } 
     416        else{ 
     417            return new jDuration(array( 
     418                "year" => $dt->year - $this->year, 
     419                "month" => $dt->month - $this->month, 
     420                "day" => $dt->day - $this-> day, 
     421                "hour" => $dt->hour - $this->hour, 
     422                "minute" => $dt->minute - $this->minute, 
     423                "second" => $dt->second - $this->second 
     424            )); 
     425        } 
    416426    } 
    417427 
     
    444454    } 
    445455} 
    446  
    447 ?> 
Download in other formats: Unified Diff Zip Archive