You are here

function PHP4Dates::testPHP4Timezone in Date 6

Same name and namespace in other branches
  1. 5.2 tests/date_timezone.test \PHP4Dates::testPHP4Timezone()

File

tests/date_timezone.test, line 10

Class

PHP4Dates
Test PHP 4 Timezone Conversion

Code

function testPHP4Timezone() {
  include_once './' . drupal_get_path('module', 'date_php4') . '/date_php4.inc';
  $date = date_create('2007-03-11 01:59:59');
  $this
    ->assertEqual($date->value, '2007-03-11 01:59:59', 'Create the a date 2007-03-11 01:59:59');
  $date = date_create('2007-03-11 01:59:59', timezone_open('America/Chicago'));
  $this
    ->assertEqual($date->timezone, 'America/Chicago', 'Create the timezone America/Chicago');
  $format = date_format($date, 'm/d/Y g:i a');
  $this
    ->assertEqual($format, '03/11/2007 1:59 am', 'Test date_format()');
  $date = date_create('2007-01-01', timezone_open('US/Central'));
  $result = date_format(date_modify($date, '+3 month'), 'Y-m-d');
  $this
    ->assertEqual('2007-04-01', $result, "Check date_modify('2007-01-01', '+3 month'), should be '2007-04-01'");
  $date = date_create('2007-01-01', timezone_open('US/Central'));
  $result = date_format(date_modify($date, '-1 month'), 'Y-m-d');
  $this
    ->assertEqual('2006-12-01', $result, "Check date_modify('2007-01-01', '-1 month'), should be '2006-12-01'");
  $date = date_create('2007-01-01', timezone_open('US/Central'));
  $result = date_format(date_modify($date, '-1 day'), 'Y-m-d');
  $this
    ->assertEqual('2006-12-31', $result, "Check date_modify('2007-01-01', '-1 day'), should be '2006-12-31'");
  $date = date_create('2005-02-28', timezone_open('US/Central'));
  $result = date_format(date_modify($date, '+1 day'), 'Y-m-d');
  $this
    ->assertEqual('2005-03-01', $result, "Check date_modify('2005-02-28', '+1 day'), should be '2005-03-01'");
  $date = date_create('2004-02-28', timezone_open('US/Central'));
  $result = date_format(date_modify($date, '+1 day'), 'Y-m-d');
  $this
    ->assertEqual('2004-02-29', $result, "Check date_modify('2004-02-28', '+1 day'), should be '2004-02-29'");
  $date = date_create('2004-03-01', timezone_open('US/Central'));
  $result = date_format(date_modify($date, '-1 day'), 'Y-m-d');
  $this
    ->assertEqual('2004-02-29', $result, "Check date_modify('2004-03-01', '-1 day'), should be '2004-02-29'");
  $date = date_create('2005-03-01', timezone_open('US/Central'));
  $result = date_format(date_modify($date, '-1 day'), 'Y-m-d');
  $this
    ->assertEqual('2005-02-28', $result, "Check date_modify('2005-03-01', '-1 day'), should be '2005-02-28'");
  $timezone = timezone_open('America/Chicago');
  $date = date_create('2007-03-11 01:59:59', $timezone);
  $this
    ->assertEqual(date_offset_get($date), -21600, 'The offset 1 minute before start of CDT, 2am on Mar 11, 2007 is -21600, returned ' . date_offset_get($date));
  $date = date_create('2007-03-11 02:00:00', $timezone);
  $this
    ->assertEqual(date_offset_get($date), -18000, 'New DST rules: The offset at start of CDT, 2am on Mar 11, 2007 is -18000, returned ' . date_offset_get($date));
  $date = date_create('2001-03-11 02:00:00', $timezone);
  $this
    ->assertEqual(date_offset_get($date), -21600, 'Old DST rules: The offset at 2am on Mar 11, 2001 is -21600, returned ' . date_offset_get($date));
  $date = date_create('1970-01-01 00:00:00', $timezone);
  $date = date_timezone_set($date, timezone_open('Asia/Tokyo'));
  $format = date_format($date, 'm/d/Y h:i a');
  $this
    ->assertEqual($format, '01/01/1970 03:00 pm', 'Convert date using date_timezone_set(), 1970-01-01 00:00:00 in America/Chicago becomes 01/01/1970 03:00 pm in Asia/Tokyo, returned ' . $format);

  // Test offsets for DST and non DST in various parts of the world.
  $date = '2007-02-01 00:00:00';
  $date2 = '2007-08-07 00:00:00';
  $vals = array(
    // This is tricky, Honolulu does not observe dst
    array(
      'val' => $date,
      'tz' => 'Pacific/Honolulu',
      'offset' => -36000,
    ),
    array(
      'val' => $date2,
      'tz' => 'Pacific/Honolulu',
      'offset' => -36000,
    ),
    array(
      'val' => $date,
      'tz' => 'America/Vancouver',
      'offset' => -28800,
    ),
    array(
      'val' => $date2,
      'tz' => 'America/Vancouver',
      'offset' => -25200,
    ),
    // Mexico changes to dst at different times in different states,
    // too complex to get this working in PHP 4.

    //array('val' => $date, 'tz' => 'America/Mexico_City', 'offset' => -21600),

    //array('val' => $date2, 'tz' => 'America/Mexico_City', 'offset' => -18000),
    array(
      'val' => $date,
      'tz' => 'America/New_York',
      'offset' => -18000,
    ),
    array(
      'val' => $date2,
      'tz' => 'America/New_York',
      'offset' => -14400,
    ),
    array(
      'val' => $date,
      'tz' => 'Europe/London',
      'offset' => 0,
    ),
    array(
      'val' => $date2,
      'tz' => 'Europe/London',
      'offset' => 3600,
    ),
    array(
      'val' => $date,
      'tz' => 'Europe/Brussels',
      'offset' => 3600,
    ),
    array(
      'val' => $date2,
      'tz' => 'Europe/Brussels',
      'offset' => 7200,
    ),
    array(
      'val' => $date,
      'tz' => 'Europe/Kiev',
      'offset' => 7200,
    ),
    array(
      'val' => $date2,
      'tz' => 'Europe/Kiev',
      'offset' => 10800,
    ),
    array(
      'val' => $date,
      'tz' => 'Asia/Calcutta',
      'offset' => 19800,
    ),
    array(
      'val' => $date2,
      'tz' => 'Asia/Calcutta',
      'offset' => 19800,
    ),
    array(
      'val' => $date,
      'tz' => 'Asia/Shanghai',
      'offset' => 28800,
    ),
    array(
      'val' => $date2,
      'tz' => 'Asia/Shanghai',
      'offset' => 28800,
    ),
    array(
      'val' => $date,
      'tz' => 'Asia/Tokyo',
      'offset' => 32400,
    ),
    array(
      'val' => $date2,
      'tz' => 'Asia/Tokyo',
      'offset' => 32400,
    ),
    // Southern hemisphere rules get reversed.
    array(
      'val' => $date,
      'tz' => 'Australia/Sydney',
      'offset' => 39600,
    ),
    array(
      'val' => $date2,
      'tz' => 'Australia/Sydney',
      'offset' => 36000,
    ),
  );
  foreach ($vals as $val) {
    $timezone = timezone_open($val['tz']);
    $date = date_create($val['val'], $timezone);
    $this
      ->assertEqual(date_offset_get($date), $val['offset'], "Testing date_create('" . $val['val'] . "', timezone_open('" . $val['tz'] . "')) offset should be " . $val['offset'] . ", result is " . date_offset_get($date));
  }
}