示例#1
0
 /**
  * Create a complete datetime value out of an
  * incomplete array of selected values.
  *
  * For example, array('year' => 2008, 'month' => 05) will fill
  * in the day, hour, minute and second with the earliest possible
  * values if type = 'min', the latest possible values if type = 'max',
  * and the current values if type = 'now' .
  */
 function complete_date($selected, $type = 'now')
 {
     $calendar = system_calendar();
     if (empty($selected)) {
         return '';
     }
     // Special case for weeks.
     if (array_key_exists('week', $selected)) {
         if (config('date_api.settings')->get('iso8601')) {
             $dates = date_iso_week_range($selected['week'], $selected['year']);
         } else {
             $dates = date_calendar_week_range($selected['week'], $selected['year']);
         }
         switch ($type) {
             case 'empty_now':
             case 'empty_min':
             case 'min':
                 return date_format($dates[0], 'Y-m-d H:i:s');
             case 'empty_max':
             case 'max':
                 return date_format($dates[1], 'Y-m-d H:i:s');
             default:
                 return;
         }
     }
     $compare = array_merge($this->part_info('empty_' . $type), $selected);
     // If this is a max date, make sure the last day of
     // the month is the right one for this date.
     if ($type == 'max') {
         $compare['day'] = $calendar->days_in_month($compare['year'], $compare['month']);
     }
     $value = '';
     $separators = $this->part_info('sep');
     foreach ($this->date_parts() as $key => $name) {
         $value .= $separators[$key] . (!empty($selected[$key]) ? $selected[$key] : $compare[$key]);
     }
     return $value;
 }
/**
 * Start and end dates for a calendar week, adjusted to use the
 * chosen first day of week for this site.
 */
function date_week_range($week, $year)
{
    if (variable_get('date_api_use_iso8601', FALSE)) {
        return date_iso_week_range($week, $year);
    }
    $min_date = new DateObject($year . '-01-01 00:00:00');
    $min_date->setTimezone(date_default_timezone_object());
    // move to the right week
    date_modify($min_date, '+' . strval(7 * ($week - 1)) . ' days');
    // move backwards to the first day of the week
    $first_day = variable_get('date_first_day', 1);
    $day_wday = date_format($min_date, 'w');
    date_modify($min_date, '-' . strval((7 + $day_wday - $first_day) % 7) . ' days');
    // move forwards to the last day of the week
    $max_date = clone $min_date;
    date_modify($max_date, '+7 days');
    if (date_format($min_date, 'Y') != $year) {
        $min_date = new DateObject($year . '-01-01 00:00:00');
    }
    return array($min_date, $max_date);
}
示例#3
0
 /**
  * @todo.
  */
 public function testDateAPI()
 {
     $calendar = system_calendar();
     // Test the order of the weeks days for a calendar that starts on Monday and
     // one that starts on Sunday.
     variable_set('date_first_day', 1);
     $expected = array(0 => t('Mon'), 1 => t('Tue'), 2 => t('Wed'), 3 => t('Thu'), 4 => t('Fri'), 5 => t('Sat'), 6 => t('Sun'));
     $days = $calendar->week_days_ordered($calendar->week_days_abbr(1));
     $this->assertEqual($expected, $days, 'Test that $calendar->week_days_ordered() array starts on Monday when the site first day is on Monday.');
     variable_set('date_first_day', 0);
     $expected = array(0 => t('Sun'), 1 => t('Mon'), 2 => t('Tue'), 3 => t('Wed'), 4 => t('Thu'), 5 => t('Fri'), 6 => t('Sat'));
     $days = $calendar->week_days_ordered($calendar->week_days_abbr(1));
     $this->assertEqual($expected, $days, 'Test that $calendar->week_days_ordered() array starts on Sunday when the site first day is on Sunday.');
     // Test days in February for a leap year and a non-leap year.
     $expected = 28;
     $date = new DrupalDateTime(array('year' => 2005, 'month' => 2));
     $value = $calendar->days_in_month($date);
     $this->assertEqual($expected, $value, "Test \$calendar->days_in_month(2, 2005): should be {$expected}, found {$value}.");
     $expected = 29;
     $date = new DrupalDateTime(array('year' => 2004, 'month' => 2));
     $value = $calendar->days_in_month($date);
     $this->assertEqual($expected, $value, "Test \$calendar->days_in_month(2, 2004): should be {$expected}, found {$value}.");
     // Test days in year for a leap year and a non-leap year.
     $expected = 365;
     $date = new DrupalDateTime('2005-06-01 00:00:00');
     $value = $calendar->days_in_year($date);
     $this->assertEqual($expected, $value, "Test \$calendar->days_in_year(2005-06-01): should be {$expected}, found {$value}.");
     $expected = 366;
     $date = new DrupalDateTime('2004-06-01 00:00:00');
     $value = $calendar->days_in_year($date);
     $this->assertEqual($expected, $value, "Test \$calendar->days_in_year(2004-06-01): should be {$expected}, found {$value}.");
     // Test ISO weeks for a leap year and a non-leap year.
     $expected = 52;
     $value = date_iso_weeks_in_year('2008-06-01 00:00:00');
     $this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2008-06-01): should be {$expected}, found {$value}.");
     $expected = 53;
     $value = date_iso_weeks_in_year('2009-06-01 00:00:00');
     $this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2009-06-01): should be {$expected}, found {$value}.");
     // Test day of week for March 1, the day after leap day.
     $expected = 6;
     $date = new DrupalDateTime('2008-03-01 00:00:00');
     $value = $calendar->day_of_week($date);
     $this->assertEqual($expected, $value, "Test \$calendar->day_of_week(2008-03-01): should be {$expected}, found {$value}.");
     $expected = 0;
     $date = new DrupalDateTime('2009-03-01 00:00:00');
     $value = $calendar->day_of_week($date);
     $this->assertEqual($expected, $value, "Test \$calendar->day_of_week(2009-03-01): should be {$expected}, found {$value}.");
     // Test day of week name for March 1, the day after leap day.
     $expected = 'Sat';
     $date = new DrupalDateTime('2008-03-01 00:00:00');
     $value = $calendar->day_of_week_name($date);
     $this->assertEqual($expected, $value, "Test \$calendar->day_of_week_name(2008-03-01): should be {$expected}, found {$value}.");
     $expected = 'Sun';
     $date = new DrupalDateTime('2009-03-01 00:00:00');
     $value = $calendar->day_of_week_name($date);
     $this->assertEqual($expected, $value, "Test \$calendar->day_of_week_name(2009-03-01): should be {$expected}, found {$value}.");
     // Test week range with calendar weeks.
     variable_set('date_first_day', 0);
     $expected = '2008-01-27 to 2008-02-03';
     $result = date_calendar_week_range(5, 2008);
     $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE);
     $this->assertEqual($expected, $value, "Test calendar date_calendar_week_range(5, 2008): should be {$expected}, found {$value}.");
     $expected = '2009-01-25 to 2009-02-01';
     $result = date_calendar_week_range(5, 2009);
     $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE);
     $this->assertEqual($expected, $value, "Test calendar date_calendar_week_range(5, 2009): should be {$expected}, found {$value}.");
     // And now with ISO weeks.
     variable_set('date_first_day', 1);
     $expected = '2008-01-28 to 2008-02-04';
     $result = date_iso_week_range(5, 2008);
     $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE);
     $this->assertEqual($expected, $value, "Test ISO date_iso_week_range(5, 2008): should be {$expected}, found {$value}.");
     $expected = '2009-01-26 to 2009-02-02';
     $result = date_iso_week_range(5, 2009);
     $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE);
     $this->assertEqual($expected, $value, "Test ISO date_iso_week_range(5, 2009): should be {$expected}, found {$value}.");
     config('date_api.settings')->set('iso8601', FALSE)->save();
     // Find calendar week for a date.
     variable_set('date_first_day', 0);
     $expected = '09';
     $value = date_calendar_week('2008-03-01');
     $this->assertEqual($expected, $value, "Test date_calendar_week(2008-03-01): should be {$expected}, found {$value}.");
     $expected = '10';
     $value = date_calendar_week('2009-03-01');
     $this->assertEqual($expected, $value, "Test date_calendar_week(2009-03-01): should be {$expected}, found {$value}.");
     // Test date ranges.
     $valid = array('-20:+20', '-1:+0', '-10:-5', '2000:2020', '-10:2010', '1980:-10', '1920:+20');
     $invalid = array('abc', 'abc:+20', '1920:+20a', '+-20:+-30', '12:12', '0:+20', '-20:0');
     foreach ($valid as $range) {
         $this->assertTrue(date_range_valid($range), "{$range} recognized as a valid date range.");
     }
     foreach ($invalid as $range) {
         $this->assertFalse(date_range_valid($range), "{$range} recognized as an invalid date range.");
     }
 }