commit 6bd88303144b18319241f1b2e4c995a4e1f646f4 Author: sylvainlecoy 2011-04-24 18:26:18 Committer: sylvainlecoy 2011-04-24 18:26:18 Parent: 838ba8d8f200b9fb281845b8d3c772ad89af5b09 (Updated to date 7.x-2.0-alpha1.) Branches: master Issue #1137062 Date Range return 1 day too much. ------- htdocs/sites/all/modules/contribs/date/date_api/date_api.module ------- diff --git a/htdocs/sites/all/modules/contribs/date/date_api/date_api.module b/htdocs/sites/all/modules/contribs/date/date_api/date_api.module index a0e30e4..1255e3e 100644 --- a/htdocs/sites/all/modules/contribs/date/date_api/date_api.module +++ b/htdocs/sites/all/modules/contribs/date/date_api/date_api.module @@ -119,7 +119,7 @@ $date->setTimezone($tz); $time = $date->format(DATE_FORMAT_DATETIME); $format = DATE_FORMAT_DATETIME; - } + } } if (is_array($time)) { @@ -131,7 +131,7 @@ $this->dateOnly = TRUE; } $this->errors = $this->arrayErrors($time); - // Make this into an ISO date, + // Make this into an ISO date, // forcing a full ISO date even if some values are missing. $time = $this->toISO($time, TRUE); // We checked for errors already, skip the step of parsing the input values. @@ -149,7 +149,7 @@ if ($element == 'year') { return FALSE; } - } + } elseif (is_string($time)) { // PHP < 5.3 doesn't like the GMT- notation for parsing timezones. $time = str_replace("GMT-", "-", $time); @@ -327,7 +327,7 @@ // Special case for "now" if ($time == 'now') { $this->granularity = array('year', 'month', 'day', 'hour', 'minute', 'second'); - } + } else { // This PHP date_parse() method currently doesn't have resolution down to seconds, so if // there is some time, all will be set. @@ -451,7 +451,7 @@ $this->addGranularity('timezone'); } - // SetDate expects an integer value for the year, results can + // SetDate expects an integer value for the year, results can // be unexpected if we feed it something like '0100' or '0000'; $final_date['year'] = intval($final_date['year']); @@ -486,12 +486,12 @@ */ public function toArray($force = FALSE) { return array( - 'year' => $this->format('Y', $force), - 'month' => $this->format('n', $force), - 'day' => $this->format('j', $force), - 'hour' => intval($this->format('H', $force)), - 'minute' => intval($this->format('i', $force)), - 'second' => intval($this->format('s', $force)), + 'year' => $this->format('Y', $force), + 'month' => $this->format('n', $force), + 'day' => $this->format('j', $force), + 'hour' => intval($this->format('H', $force)), + 'minute' => intval($this->format('i', $force)), + 'second' => intval($this->format('s', $force)), 'timezone' => $this->format('e', $force), ); } @@ -507,7 +507,7 @@ $datetime = date_pad(intval($arr['year']), 4); if ($full || $arr['month'] !== '') { $datetime .= '-'. date_pad(intval($arr['month'])); - if ($full || $arr['day'] !== '') { + if ($full || $arr['day'] !== '') { $datetime .= '-'. date_pad(intval($arr['day'])); } } @@ -533,10 +533,10 @@ * An array of date parts or a datetime string with values to be forced into date. * @param $format * The format of the date. - * @param $default + * @param $default * 'current' - default to current day values. * 'first' - default to the first possible valid value. - */ + */ public function setFuzzyDate($date, $format = NULL, $default = 'first') { $comp = new DateObject($date, $this->getTimeZone()->getName(), $format); $arr = $comp->toArray(TRUE); @@ -570,15 +570,15 @@ break; case 'hour': $fallback = $default == 'first' ? 0 : $now->format('G'); - return !is_int($value) || $value < 0 || $value > 23 ? $fallback : $value; + return !is_int($value) || $value < 0 || $value > 23 ? $fallback : $value; break; case 'minute': $fallback = $default == 'first' ? 0 : $now->format('i'); - return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value; - break; + return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value; + break; case 'second': $fallback = $default == 'first' ? 0 : $now->format('s'); - return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value; + return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value; break; } } @@ -846,7 +846,7 @@ break; default: $context = 'day_abbr'; - break; + break; } foreach (date_week_days_untranslated() as $key => $day) { $weekdays[$key] = t(substr($day, 0, $length), array(), array('context' => $context)); @@ -1001,10 +1001,10 @@ */ function date_format_patterns($strict = FALSE) { return array( - 'd' => '\d{' . ($strict ? '2' : '1,2') . '}', - 'm' => '\d{' . ($strict ? '2' : '1,2') . '}', - 'h' => '\d{' . ($strict ? '2' : '1,2') . '}', - 'H' => '\d{' . ($strict ? '2' : '1,2') . '}', + 'd' => '\d{' . ($strict ? '2' : '1,2') . '}', + 'm' => '\d{' . ($strict ? '2' : '1,2') . '}', + 'h' => '\d{' . ($strict ? '2' : '1,2') . '}', + 'H' => '\d{' . ($strict ? '2' : '1,2') . '}', 'i' => '\d{' . ($strict ? '2' : '1,2') . '}', 's' => '\d{' . ($strict ? '2' : '1,2') . '}', 'j' => '\d{1,2}', 'N' => '\d', 'S' => '\w{2}', @@ -1029,11 +1029,11 @@ */ function date_granularity_names() { return array( - 'year' => t('Year', array(), array('context' => 'datetime')), - 'month' => t('Month', array(), array('context' => 'datetime')), + 'year' => t('Year', array(), array('context' => 'datetime')), + 'month' => t('Month', array(), array('context' => 'datetime')), 'day' => t('Day', array(), array('context' => 'datetime')), - 'hour' => t('Hour', array(), array('context' => 'datetime')), - 'minute' => t('Minute', array(), array('context' => 'datetime')), + 'hour' => t('Hour', array(), array('context' => 'datetime')), + 'minute' => t('Minute', array(), array('context' => 'datetime')), 'second' => t('Second', array(), array('context' => 'datetime')), ); } @@ -1046,7 +1046,7 @@ } /** - * Give a granularity $precision, return an array of + * Give a granularity $precision, return an array of * all the possible granularity elements. */ function date_granularity_array_from_precision($precision) { @@ -1096,7 +1096,7 @@ return substr($format, 0, 9); default: return $format; - } + } } /** @@ -1117,12 +1117,12 @@ $data = timezone_identifiers_list(); asort($data); foreach ($data as $delta => $zone) { - // Because many time zones exist in PHP only for backward - // compatibility reasons and should not be used, the list is + // Because many time zones exist in PHP only for backward + // compatibility reasons and should not be used, the list is // filtered by a regular expression. if (preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) { $zonenames[$zone] = $zone; - } + } } if (!empty($zonenames)) { @@ -1208,15 +1208,15 @@ break; case 'F': $datestring .= t($date->format('F'), array(), array('context' => 'month_name', 'langcode' => $langcode)); - break; + break; case 'M': $datestring .= t($date->format('M'), array(), array('context' => 'month_abbr', 'langcode' => $langcode)); - break; + break; case 'A': case 'a': $datestring .= t($date->format($c), array(), array('context' => 'ampm', 'langcode' => $langcode)); - break; - // The timezone name translations can use t(). + break; + // The timezone name translations can use t(). case 'e': case 'T': $datestring .= t($date->format($c)); @@ -1230,13 +1230,13 @@ break; case 'Z': $datestring .= date_offset_get($date); - break; + break; case '\\': $datestring .= $format[++$i]; break; case 'r': $datestring .= date_format_date($date, 'custom', 'D, d M Y H:i:s O', $langcode); - break; + break; default: if (strpos('BdcgGhHiIjLmnNosStTuUwWYyz', $c) !== FALSE) { $datestring .= $date->format($c); @@ -1265,7 +1265,7 @@ $interval = REQUEST_TIME - $date->format('U'); if ($interval > 0 ) { return t('!time ago', array('!time' => format_interval($interval, $granularity))); - } + } else { return format_interval(abs($interval), $granularity); } @@ -1440,7 +1440,7 @@ // move forwards to the last day of the week $max_date = clone($min_date); - date_modify($max_date, '+7 days'); + date_modify($max_date, '+6 days'); if (date_format($min_date, 'Y') != $year) { $min_date = new DateObject($year . '-01-01 00:00:00'); @@ -1448,7 +1448,7 @@ return array($min_date, $max_date); } -/** +/** * Start and end dates for an ISO week. */ function date_iso_week_range($week, $year) { @@ -1467,13 +1467,13 @@ // move forwards to the last day of the week $max_date = clone($min_date); - date_modify($max_date, '+7 days'); + date_modify($max_date, '+6 days'); return array($min_date, $max_date); } /** * The number of calendar weeks in a year. - * + * * PHP week functions return the ISO week, not the calendar week. * * @param int $year @@ -1487,7 +1487,7 @@ /** * The calendar week number for a date. - * + * * PHP week functions return the ISO week, not the calendar week. * * @param string $date, in the format Y-m-d @@ -1514,7 +1514,7 @@ $last_date = clone($date); date_modify($last_date, '-7 days'); $week = date_format($last_date, 'W') + 1; - } + } elseif ($date_year < intval($parts[0])) { $week = 0; } @@ -1589,7 +1589,7 @@ '\. ' => ' . ', '\,' => ',', ); - $format = strtr($format, $replace); + $format = strtr($format, $replace); // Get the 'T' out of ISO date formats that don't have // both date and time. @@ -1646,7 +1646,7 @@ // Trim any whitespace from the result. $format = trim($format); - // After removing the non-desired parts of the format, test if the only + // After removing the non-desired parts of the format, test if the only // things left are escaped, non-date, characters. If so, return nothing. if (!$test = trim(preg_replace('(\\\\\w{1})', '', $format))) { return ''; @@ -1757,7 +1757,7 @@ 'date_part_label_ampm' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)), 'date_part_label_timezone' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)), 'date_views_filter_form' => $base + array('template' => 'date-views-filter-form', 'render element' => 'form'), - 'date_calendar_day' => $base + array('variables' => array('date' => NULL)), + 'date_calendar_day' => $base + array('variables' => array('date' => NULL)), 'date_time_ago' => $base + array('variables' => array('start_date' => NULL, 'end_date' => NULL, 'interval' => NULL)) ); } @@ -1837,9 +1837,9 @@ } /** - * Split a string like -3:+3 or 2001:2010 into + * Split a string like -3:+3 or 2001:2010 into * an array of min and max years. - * + * * Center the range around the current year, if any, but expand it far * enough so it will pick up the year value in the field in case * the value in the field is outside the initial range. @@ -1906,7 +1906,7 @@ return $min . ':' . $max; } /** - * Temporary helper to re-create equivalent + * Temporary helper to re-create equivalent * of content_database_info. */ function date_api_database_info($field) { @@ -2028,21 +2028,21 @@ * for a field by name, no matter how deeply nested it is within * fieldgroups or multigroups, or to find the multiple value * sub-elements within a field element by name (i.e. 'value' or - * 'rrule'). You can also use this function to see if an item exists + * 'rrule'). You can also use this function to see if an item exists * in a form (the return will be an empty array if it does not exist). * * The function returns an array of results. A field will generally - * only exist once in a form but the function can also be used to + * only exist once in a form but the function can also be used to * locate all the 'value' elements within a multiple value field, * so the result is always returned as an array of values. * - * For example, for a field named field_custom, the following will - * pluck out the form elements for this field from the node form, + * For example, for a field named field_custom, the following will + * pluck out the form elements for this field from the node form, * no matter how deeply it is nested within fieldgroups or fieldsets: * * $elements = content_get_nested_elements($node_form, 'field_custom'); * - * You can prefix the function with '&' to retrieve the element by + * You can prefix the function with '&' to retrieve the element by * reference to alter it directly: * * $elements = &content_get_nested_elements($form, 'field_custom'); ---------- htdocs/sites/all/modules/contribs/date/tests/date_api.test --------- diff --git a/htdocs/sites/all/modules/contribs/date/tests/date_api.test b/htdocs/sites/all/modules/contribs/date/tests/date_api.test index 46bb005..d5d17b0 100644 --- a/htdocs/sites/all/modules/contribs/date/tests/date_api.test +++ b/htdocs/sites/all/modules/contribs/date/tests/date_api.test @@ -119,24 +119,24 @@ // Test week range with calendar weeks. variable_set('date_first_day', 0); variable_set('date_api_use_iso8601', FALSE); - $expected = '2008-01-27 to 2008-02-03'; - $result = date_week_range(5, 2008); + $expected = '2011-02-28 to 2011-03-06'; + $result = date_week_range(10, 2011); $value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE); $this->assertEqual($expected, $value, "Test calendar date_week_range(5, 2008): should be $expected, found $value."); - $expected = '2009-01-25 to 2009-02-01'; - $result = date_week_range(5, 2009); + $expected = '2011-11-28 to 2011-12-04'; + $result = date_week_range(49, 2011); $value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE); $this->assertEqual($expected, $value, "Test calendar date_week_range(5, 2009): should be $expected, found $value."); // And now with ISO weeks. variable_set('date_first_day', 1); variable_set('date_api_use_iso8601', TRUE); - $expected = '2008-01-28 to 2008-02-04'; - $result = date_week_range(5, 2008); + $expected = '2011-02-28 to 2011-03-06'; + $result = date_week_range(9, 2011); $value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE); $this->assertEqual($expected, $value, "Test ISO date_week_range(5, 2008): should be $expected, found $value."); - $expected = '2009-01-26 to 2009-02-02'; - $result = date_week_range(5, 2009); + $expected = '2011-11-28 to 2011-12-04'; + $result = date_week_range(48, 2011); $value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE); $this->assertEqual($expected, $value, "Test ISO date_week_range(5, 2009): should be $expected, found $value."); variable_set('date_api_use_iso8601', FALSE);