<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id: DateMath.class.php 11031 2007-09-02 20:09:38Z kiesel $ 
 */

  uses(
    'util.Date', 
    'util.DateUtil',
    'util.TimeZone',
    'util.TimeInterval'
  );

  /**
   * Date math functions
   *
   * @ext       ext/date
   * @test      xp://net.xp_framework.unittest.util.DateMathTest
   * @see       xp://util.Date
   * @purpose   Date calculations
   */
  class DateMath extends Object {

    /**
     * Diff two date objects. Only full units are returned.
     *
     * @param   util.Date date1
     * @param   util.Date date2
     * @param   util.TimeInterval interval
     * @return  int
     */
    public static function diff(TimeInterval $interval, Date $date1, Date $date2) {
    
      // Convert date2 to same timezone as date1, then "cut off" tz. To workaround
      // bug #45038, not just take the timezone of date1, but construct a new one which
      // will have a timezone ID - which is required for this kind of computation.
      $tz= new TimeZone(timezone_name_from_abbr(
        '', 
        $date1->getOffsetInSeconds(), 
        $date1->toString('I')
      ));

      // Now, convert both dates to the same time (actually we only need to convert the
      // second one, as the first will remain in the same timezone)
      $date2= $tz->translate($date2);
      
      // Then cut off timezone, by setting both to GMT
      $date1= DateUtil::setTimeZone($date1, new TimeZone('GMT'));
      $date2= DateUtil::setTimeZone($date2, new TimeZone('GMT'));
      
      switch ($interval) {
        case TimeInterval::$YEAR: {
          return -($date1->getYear()- $date2->getYear());
        }
        
        case TimeInterval::$MONTH: {<commit_msg>- Only convert dates to same timezone if they actually differ # Optimization and fix(!) for net.xp_framework.unittest.util.DateMathTest # ::diffMidnightToMidnight # ::hourDiff<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id: DateMath.class.php 11031 2007-09-02 20:09:38Z kiesel $ 
 */

  uses(
    'util.Date', 
    'util.DateUtil',
    'util.TimeZone',
    'util.TimeInterval'
  );

  /**
   * Date math functions
   *
   * @ext       ext/date
   * @test      xp://net.xp_framework.unittest.util.DateMathTest
   * @see       xp://util.Date
   * @purpose   Date calculations
   */
  class DateMath extends Object {

    /**
     * Diff two date objects. Only full units are returned.
     *
     * @param   util.Date date1
     * @param   util.Date date2
     * @param   util.TimeInterval interval
     * @return  int
     */
    public static function diff(TimeInterval $interval, Date $date1, Date $date2) {
      if ($date1->getOffsetInSeconds() != $date2->getOffsetInSeconds()) {
    
        // Convert date2 to same timezone as date1. To work around PHP bug #45038, 
        // not just take the timezone of date1, but construct a new one which will 
        // have a timezone ID - which is required for this kind of computation.
        $tz= new TimeZone(timezone_name_from_abbr(
          '', 
          $date1->getOffsetInSeconds(), 
          $date1->toString('I')
        ));

        // Now, convert both dates to the same time (actually we only need to convert the
        // second one, as the first will remain in the same timezone)
        $date2= $tz->translate($date2);
      }
      
      // Then cut off timezone, by setting both to GMT
      $date1= DateUtil::setTimeZone($date1, new TimeZone('GMT'));
      $date2= DateUtil::setTimeZone($date2, new TimeZone('GMT'));
      
      switch ($interval) {
        case TimeInterval::$YEAR: {
          return -($date1->getYear()- $date2->getYear());
        }
        
        case TimeInterval::$MONTH: {
{'fraction_non_alphanumeric': 0.11871657754010695, 'fraction_numerical': 0.022459893048128343, 'mean_word_length': 2.287346221441125, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2341762', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1129, 'n_words': 348}