Index: clock.js =================================================================== RCS file: /cvs/drupal/contributions/modules/clock/clock.js,v retrieving revision 1.13 diff -u -p -r1.13 clock.js --- clock.js 4 May 2010 16:58:57 -0000 1.13 +++ clock.js 27 May 2010 23:54:28 -0000 @@ -12,24 +12,26 @@ Drupal.behaviors.clockDisplay = { attach: function (context, settings) { // Gets the correct variables from PHP. + // Whether or not to update the clock continuously. + var update = Drupal.settings['update']; + // Whether or not to use the user's local time zone. + var local = Drupal.settings['local'] // Creates a JavaScript date object, from a specially formatted date string. - var date = new Date(Drupal.settings['time']); - // A PHP date format string. - var dateFormat = Drupal.settings['date_format']; + // Note that due to JavaScript's inferior time zone handling, time zone + // related date formatters will return the time zone of the Drupal site, not + // the visiting user if the time zone is set to 'Local'. + var date = Drupal.settings['local'] ? new Date() : new Date(Drupal.settings['time']); // The name of the timezone, e.g. 'Europe/London'. var timezoneName = Drupal.settings['timezone_name']; - // If time zone is set to 'Local' overwrite the date. - if (timezoneName == 'Local') { - var date = new Date(); - } // The name of the offset, e.g. 'GMT'. var offsetName = Drupal.settings['offset_name']; // The time zone offset in seconds. var offsetSeconds = Drupal.settings['offset_seconds']; // Daylight Savings Time information. '1' for yes, '0' for no. var daylightSavingsTime = Drupal.settings['daylight_savings_time']; - // Whether or not to update the clock continuously - var update = Drupal.settings['js']; + // A PHP date format string. + var dateFormat = Drupal.settings['date_format']; + // Create an array containing month names. var monthNames = new Array(); Index: clock.module =================================================================== RCS file: /cvs/drupal/contributions/modules/clock/clock.module,v retrieving revision 1.14 diff -u -p -r1.14 clock.module --- clock.module 4 May 2010 16:58:57 -0000 1.14 +++ clock.module 27 May 2010 23:54:28 -0000 @@ -1,52 +1,52 @@ ' . t('About') . ''; - $output .= '

' . t('Clock module allows the display of a clock on your site.') . '

'; - $output .= '

' . t('Uses') . '

'; - $output .= '
'; - $output .= '
' . t('Administering the clock') . '
'; - $output .= '
' . t('Since the clock is a block, it can be administered via its block settings page which is accessible from the block administration page. In addition to the usual block configuration options there are a number of options.', array('@clock-settings' => '/admin/build/block/configure/clock/clock', '@block-admin' => '/admin/build/block')) . '
'; - $output .= '
'; - $output .= '
' . t('Time zone') . '
'; - $output .= '
' . t('The time zone of the clock.') . '
'; - $output .= '
'; - $output .= '
' . t('Site time zone') . '
'; - $output .= '
' . t('The time zone that has been set on the date and time settings page.', array('@date-admin' => '/admin/settings/date-time')) . '
'; - $output .= '
' . t('User time zone') . '
'; - $output .= '
' . t('This option will only show up, if user-configurable time zones are enabled. The time zone the user has selected.') . '
'; - $output .= '
' . t('Local time zone') . '
'; - $output .= '
' . t('The local time zone on the computer of the visiting user (anonymous and authenticated). Since JavaScript is used to calculate this time, it falls back to the site time zone if the visitor has JavaScript disabled.'); - $output .= '
' . t('Custom time zone') . '
'; - $output .= '
' . t('A custom time zone that can be selected in the select box below the radio buttons.') . '
'; - $output .= '
'; - $output .= '
' . t('Date format') . '
'; - $output .= '
' . t('The date format that the clock is to be displayed in.') . '
'; - $output .= '
'; - $output .= '
' . t('Short date format') . '
'; - $output .= '
' . t('The short date format that has been set on the date format configuration page. The expected output (not necessarily in the correct timezone) is shown in parentheses.', array('@format-admin' => '/admin/settings/date-time/formats')). '
'; - $output .= '
' . t('Medium date format') . '
'; - $output .= '
' . t('The medium date format that has been set on the date format configuration page. The expected output (not necessarily in the correct timezone) is shown in parentheses.', array('@format-admin' => '/admin/settings/date-time/formats')). '
'; - $output .= '
' . t('Long date format') . '
'; - $output .= '
' . t('The long date format that has been set on the date format configuration page. The expected output (not necessarily in the correct timezone) is shown in parentheses.', array('@format-admin' => '/admin/settings/date-time/formats')). '
'; - $output .= '
' . t('Custom date format') . '
'; - $output .= '
' . t('This options will only show up, if there are custom date formats. A custom date format can be selected in the select box below the radio buttons.', array('@custom-formats' => '/admin/settings/date-time/formats/custom')) . '
'; - $output .= '
'; - $output .= '
' . t('JavaScript updating') . '
'; - $output .= '
' . t('Whether or not the clock should be updated continuously via JavaScript. This is especially useful if seconds are displayed. It only works if the visitor has JavaScript enabled.') . '
'; - $output .= '
'; - $output .= '
'; - return $output; - } -} - + +/** +* Implements hook_help(). +*/ +function clock_help($path, $arg) { + switch ($path) { + case 'admin/help#clock': + $output = ''; + $output .= '

' . t('About') . '

'; + $output .= '

' . t('Clock module allows the display of a clock on your site.') . '

'; + $output .= '

' . t('Uses') . '

'; + $output .= '
'; + $output .= '
' . t('Administering the clock') . '
'; + $output .= '
' . t('Since the clock is a block, it can be administered via its block settings page which is accessible from the block administration page. In addition to the usual block configuration options there are a number of options.', array('@clock-settings' => '/admin/build/block/configure/clock/clock', '@block-admin' => '/admin/build/block')) . '
'; + $output .= '
'; + $output .= '
' . t('Time zone') . '
'; + $output .= '
' . t('The time zone of the clock.') . '
'; + $output .= '
'; + $output .= '
' . t('Site time zone') . '
'; + $output .= '
' . t('The time zone that has been set on the date and time settings page.', array('@date-admin' => '/admin/settings/date-time')) . '
'; + $output .= '
' . t('User time zone') . '
'; + $output .= '
' . t('This option will only show up, if user-configurable time zones are enabled. The time zone the user has selected.') . '
'; + $output .= '
' . t('Local time zone') . '
'; + $output .= '
' . t('The local time zone on the computer of the visiting user (anonymous and authenticated). Since JavaScript is used to calculate this time, it falls back to the site time zone if the visitor has JavaScript disabled.'); + $output .= '
' . t('Custom time zone') . '
'; + $output .= '
' . t('A custom time zone that can be selected in the select box below the radio buttons.') . '
'; + $output .= '
'; + $output .= '
' . t('Date format') . '
'; + $output .= '
' . t('The date format that the clock is to be displayed in.') . '
'; + $output .= '
'; + $output .= '
' . t('Short date format') . '
'; + $output .= '
' . t('The short date format that has been set on the date format configuration page. The expected output (not necessarily in the correct timezone) is shown in parentheses.', array('@format-admin' => '/admin/settings/date-time/formats')). '
'; + $output .= '
' . t('Medium date format') . '
'; + $output .= '
' . t('The medium date format that has been set on the date format configuration page. The expected output (not necessarily in the correct timezone) is shown in parentheses.', array('@format-admin' => '/admin/settings/date-time/formats')). '
'; + $output .= '
' . t('Long date format') . '
'; + $output .= '
' . t('The long date format that has been set on the date format configuration page. The expected output (not necessarily in the correct timezone) is shown in parentheses.', array('@format-admin' => '/admin/settings/date-time/formats')). '
'; + $output .= '
' . t('Custom date format') . '
'; + $output .= '
' . t('This options will only show up, if there are custom date formats. A custom date format can be selected in the select box below the radio buttons.', array('@custom-formats' => '/admin/settings/date-time/formats/custom')) . '
'; + $output .= '
'; + $output .= '
' . t('JavaScript updating') . '
'; + $output .= '
' . t('Whether or not the clock should be updated continuously via JavaScript. This is especially useful if seconds are displayed. It only works if the visitor has JavaScript enabled.') . '
'; + $output .= '
'; + $output .= '
'; + return $output; + } +} + /** * Implements hook_block_info(). */ @@ -57,55 +57,55 @@ function clock_block_info() { 'cache' => DRUPAL_NO_CACHE, ); return $blocks; -} - -/** - * Implement hook_block_configure(). - */ -function clock_block_configure($delta = '') { - if ($delta == 'clock') { - +} + +/** + * Implement hook_block_configure(). + */ +function clock_block_configure($delta = '') { + if ($delta == 'clock') { + // Time zone options. $form['timezone'] = array( '#type' => 'radios', '#title' => t('Time zone settings'), - '#weight' => 1, - '#options' => array( + '#weight' => 1, + '#options' => array( '1' => t('Site time zone'), - '3' => t('Local time zone'), + '3' => t('Local time zone'), '4' => t('Custom time zone'), ), '#default_value' => variable_get('clock_timezone_type', '1'), - ); + ); // In case of user-configurable timezones show it as an option. if (variable_get('configurable_timezones', 1) == 1) { - $form['timezone']['#options'] = array( + $form['timezone']['#options'] = array( '1' => t('Site time zone'), - '2' => t('User time zone'), + '2' => t('User time zone'), '3' => t('Local time zone'), '4' => t('Custom time zone'), - ); + ); } $form['custom_timezone'] = array( '#type' => 'select', '#multiple' => FALSE, '#weight' => 2, '#default_value' => variable_get('clock_custom_timezone', 'UTC'), - '#options' => date_timezone_names(), - // Show the select list only if "Custom time zone" is selected. - '#states' => array( - 'visible' => array( - ':input[name="timezone"]' => array('value' => '4'), - ), + '#options' => date_timezone_names(), + // Show the select list only if "Custom time zone" is selected. + '#states' => array( + 'visible' => array( + ':input[name="timezone"]' => array('value' => '4'), + ), ), - ); - - // Date format options. - // Format the current time with each date type to display them in the form. - $date_types = array(); - foreach(system_get_date_types() as $date_type => $info) { - $date_types[$date_type] = $info['title'] . ' (' . date_format_date(date_now(clock_get_timezone()), 'custom', clock_get_date_format($date_type)) . ')'; - } + ); + + // Date format options. + // Format the current time with each date type to display them in the form. + $date_types = array(); + foreach(system_get_date_types() as $date_type => $info) { + $date_types[$date_type] = $info['title'] . ' (' . date_format_date(date_now(clock_get_timezone()), 'custom', clock_get_date_format($date_type)) . ')'; + } $form['date_format'] = array( '#type' => 'radios', '#title' => t('Date format settings'), @@ -113,39 +113,39 @@ function clock_block_configure($delta = '#default_value' => variable_get('clock_date_format_type', 'long'), '#options' => $date_types, ); - - // JavaScript options. + + // JavaScript options. $form['js'] = array( '#type' => 'checkbox', '#title' => t('Use JavaScript to continuously update the clock'), '#weight' => 5, '#default_value' => variable_get('clock_js', '1'), ); - - return $form; - } + + return $form; + } } - -/** - * Implements hook_block_save(). - */ -function clock_block_save($delta = '', $edit = array()) { + +/** + * Implements hook_block_save(). + */ +function clock_block_save($delta = '', $edit = array()) { if ($delta == 'clock') { variable_set('clock_timezone_type', $edit['timezone']); if ($edit['timezone'] == '4') { variable_set('clock_custom_timezone', $edit['custom_timezone']); } - variable_set('clock_date_format_type', $edit['date_format']); + variable_set('clock_date_format_type', $edit['date_format']); variable_set('clock_js', $edit['js']); - return; - } + return; + } } - -/** - * Implements hook_block_view(). + +/** + * Implements hook_block_view(). */ -function clock_block_view($delta = '') { - if ($delta == 'clock') { +function clock_block_view($delta = '') { + if ($delta == 'clock') { $block = array(); $block['subject'] = 'Clock'; $block['content'] = theme_clock(clock_get_timezone(), clock_get_date_format(), variable_get('clock_js', '1')); @@ -153,58 +153,59 @@ function clock_block_view($delta = '') { } } -/** - * Gets the correct timezone to display. - * - * @return - * The name of the timezone, NULL if the user's time zone is to be used or - * 'Local' is the user's local time is to be used. - */ -function clock_get_timezone() { - $clock_timezone_type = variable_get('clock_timezone_type', '1'); - switch ($clock_timezone_type) { - // Site time zone - case '1': - // If time zone is set to 'Local' and the user has JS disabled, show the - // site time zone. - case '3': - $clock_timezone = variable_get('date_default_timezone_name', 'UTC'); +/** + * Gets the correct timezone to display. + * + * @return + * The name of the timezone, NULL if the user's time zone is to be used or + * 'Local' is the user's local time is to be used. + */ +function clock_get_timezone() { + $type = variable_get('clock_timezone_type', '1'); + switch ($type) { + // Site time zone. + case '1': + $timezone = variable_get('date_default_timezone', 'UTC'); break; - // User time zone + // User time zone. case '2': - $clock_timezone = NULL; + $timezone = NULL; break; - // Custom time zone + // Local time zone. + case '3': + $timezone = 'Local'; + break; + // Custom time zone. case '4': - $clock_timezone = variable_get('clock_custom_timezone', 'UTC'); + $timezone = variable_get('clock_custom_timezone', 'UTC'); break; - } - return $clock_timezone; -} - -/** - * Gets the correct date format. - * - * @param $type - * (optional) The format type. - * - * @return - * If $type was specified, the date format of the specified format type. If - * $type is omitted, the date format of the format type used for the clock. - */ -function clock_get_date_format($type = FALSE) { - if (!$type) { - $type = variable_get('clock_date_format_type', 'long'); - } - $date_format = variable_get('date_format_' . $type, ''); - // If the date format has not been specifically set, load the default. - if (!$date_format) { - $date_formats = array_keys(system_get_date_formats($type)); + } + return $timezone; +} + +/** + * Gets the correct date format. + * + * @param $type + * (optional) The format type. + * + * @return + * If $type was specified, the date format of the specified format type. If + * $type is omitted, the date format of the format type used for the clock. + */ +function clock_get_date_format($type = FALSE) { + if (!$type) { + $type = variable_get('clock_date_format_type', 'long'); + } + $date_format = variable_get('date_format_' . $type, ''); + // If the date format has not been specifically set, load the default. + if (!$date_format) { + $date_formats = array_keys(system_get_date_formats($type)); $date_format = array_shift($date_formats); - } - return $date_format; -} - + } + return $date_format; +} + /** * Implements hook_theme. */ @@ -213,51 +214,58 @@ function clock_theme() { 'clock' => array( 'variables' => array( 'timezone' => 'UTC', - 'date_format' => 'short', + 'date_format' => 'short', 'js' => '1', ), ), ); -} - +} + /* * Provide a default implementation of theme_clock(). */ -function theme_clock($timezone, $date_format, $js) { - - if ($js == '1' || $timezone == 'Local') { - - // Load the jQuery Timers library and clock.js +function theme_clock($timezone, $date_format, $update) { + + if ($update == '1' || $timezone == 'Local') { + + if ($timezone == 'Local') { + $local = TRUE; + // Use the site time zone as a fallback for non-JavaScript users. + $timezone = variable_get('date_default_timezone', 'UTC'); + } + + // Load the jQuery Timers library and clock.js drupal_add_js(libraries_get_path('jquerytimers') . '/jquery.timers.js'); drupal_add_js(drupal_get_path('module', 'clock') . '/clock.js'); - - // Create variables that are needed for the JavaScript time calculation. - // Create a time string. - $time = date_format_date(date_now($timezone), $type = 'custom', $format = 'F j, Y H:i:s'); - // Get the time zone offset in seconds. - $offset_seconds = date_format_date(date_now($timezone), $type = 'custom', $format = 'Z'); - // Get Daylight Savings Time information. '1' for yes, '0' for no. - $daylight_savings_time = date_format_date(date_now($timezone), $type = 'custom', $format = 'I'); - // Get the name of the offset, e.g. 'GMT'. - $offset_name = date_format_date(date_now($timezone), $type = 'custom', $format = 'T'); - - // Pass the variables to JavaScript. - drupal_add_js(array( - 'time' => $time, - 'date_format' => $date_format, - 'timezone_name' => $timezone, - 'offset_name' => $offset_name, - 'offset_seconds' => $offset_seconds, - 'daylight_savings_time' => $daylight_savings_time, - 'js' => $js, - ), 'setting'); - } + // Create variables that are needed for the JavaScript time calculation. + // Create a time string. + $time = date_format_date(date_now($timezone), $type = 'custom', $format = 'F j, Y H:i:s'); + // Get the time zone offset in seconds. + $offset_seconds = date_format_date(date_now($timezone), $type = 'custom', $format = 'Z'); + // Get Daylight Savings Time information. '1' for yes, '0' for no. + $daylight_savings_time = date_format_date(date_now($timezone), $type = 'custom', $format = 'I'); + // Get the name of the offset, e.g. 'GMT'. + $offset_name = date_format_date(date_now($timezone), $type = 'custom', $format = 'T'); + + // Pass the variables to JavaScript. + drupal_add_js(array( + 'update' => $update, + 'local' => isset($local) ? TRUE : FALSE, + 'time' => $time, + 'timezone_name' => $timezone, + 'offset_name' => $offset_name, + 'offset_seconds' => $offset_seconds, + 'daylight_savings_time' => $daylight_savings_time, + 'date_format' => $date_format, + ), 'setting'); + + } // Create a date object with the correct timezone and format it with the correct date format. - $clock = date_format_date(date_now($timezone), 'custom', $date_format); + $clock = date_format_date(date_now($timezone), 'custom', $date_format); $output = '
' . $clock . '
'; - + return $output; -} +} Index: clock.test =================================================================== RCS file: /cvs/drupal/contributions/modules/clock/clock.test,v retrieving revision 1.6 diff -u -p -r1.6 clock.test --- clock.test 3 May 2010 17:56:53 -0000 1.6 +++ clock.test 27 May 2010 23:54:29 -0000 @@ -22,63 +22,59 @@ class ClockBlockTestCase extends DrupalW // Save the test user's uid to access the user edit page later. variable_set('test_user_id', $privileged_user->uid); $this->drupalLogin($privileged_user); - // Enable the clock block in the right sidebar. + // Enable the clock block in main content area in the Seven theme. $edit = array(); - $edit['clock_clock[region]'] = 'right'; - $this->drupalPost('admin/structure/block', $edit, 'Save blocks'); + $edit['clock_clock[region]'] = 'content'; + $this->drupalPost('admin/structure/block/list/seven', $edit, 'Save blocks'); } - public function testClockBlockDefault() { - $clock = date_format_date(date_now('UTC'), 'short'); + public function testClockBlock() { + $this->drupalGet('admin/config/regional/settings'); + // Test the default display of the clock. + $clock = date_format_date(date_now(variable_get('date_default_timezone', 0)), 'long'); $this->assertText($clock, t('Ensure that the clock is correctly displayed by default.')); $edit['timezone'] = '3'; $this->drupalPost('admin/structure/block/manage/clock/clock/configure', $edit, 'Save block'); $this->assertText($clock, t('Ensure that the clock falls back to the site time with Local time zone enabled and without JavaScript.')); - } - public function testClockBlockUserTimezone() { - // Set the user's time zone to something different from the site default. + // Test the user time zone. $edit = array(); - $edit['timezone_name'] = 'Pacific/Fiji'; + $edit['timezone'] = 'Pacific/Fiji'; $this->drupalPost('user/' . variable_get('test_user_id', '1') . '/edit', $edit, 'Save'); // Set the clock block to display the user time zone. $edit = array(); $edit['timezone'] = '2'; $this->drupalPost('admin/structure/block/manage/clock/clock/configure', $edit, 'Save block'); - $clock = date_format_date(date_now('Pacific/Fiji'), 'short'); + $clock = date_format_date(date_now('Pacific/Fiji'), 'long'); $this->assertText($clock, t('Ensure that the clock is correctly displayed in the user time zone.')); - } - public function testClockBlockCustomTimezone() { - // Set the clock block to display a custom time zone. + // Test a custom time zone. $edit = array(); $edit['timezone'] = '4'; - $edit['custom_timezone'] = 'Africa/Timbuktu'; + $edit['custom_timezone'] = 'Africa/Lubumbashi'; $this->drupalPost('admin/structure/block/manage/clock/clock/configure', $edit, 'Save block'); - $clock = date_format_date(date_now('Africa/Timbuktu'), 'short'); - $this->assert($clock, t('Ensure that the clock is correctly displayed in a custom date format.')); - } + $clock = date_format_date(date_now('Africa/Timbuktu'), 'long'); + $this->assertText($clock, t('Ensure that the clock is correctly displayed in a custom date format')); - public function testClockBlockDateFormat() { - // Configure a custom date format. + // Test the clock block in all format types. + // Configure a custom date format. We leave the 'r' formatter out, because + // there is a bug in Date API. See http://drupal.org/node/697144 $edit = array(); - $edit['date_format'] = 'aABcdDeFgGhHiIjlmMnNoOPrsStTuUwWYyzZ' + $edit['date_format'] = 'aAbBcCdDeEfFgGhHiIjJlLmMnNoOpPqQRsStTuUvVwWxXyYzZ'; $this->drupalPost('admin/config/regional/date-time/formats/add', $edit, 'Add format'); // Configure a custom format type with the custom date format. $edit = array(); $edit['date_type'] = $this->randomName(); - $edit['date_format'] = 'aABcdDeFgGhHiIjlmMnNoOPrsStTuUwWYyzZ' + $edit['machine_name'] = $this->randomName(); + $edit['date_format'] = 'aAbBcCdDeEfFgGhHiIjJlLmMnNoOpPqQRsStTuUvVwWxXyYzZ'; $this->drupalPost('admin/config/regional/date-time/types/add', $edit, 'Add date type'); - // Test the clock block in each configured format type. - foreach (system_get_date_types() as $date_type => $info) { - if (!$date_format = variable_get('date_format' . $type, '') { - $date_format = array_shift(system_get_date_formats($type)); - } + foreach (system_get_date_types() as $type => $info) { + $format = variable_get('date_format_' . $type, '') ? variable_get('date_format_' . $type, '') : key(system_get_date_formats($type)); $edit = array(); - $edit['date_format'] = $date_type; + $edit['date_format'] = $type; $this->drupalPost('admin/structure/block/manage/clock/clock/configure', $edit, 'Save block'); - $clock = date_format_date(date_now(), 'custom', $date_format); - $this->assert($clock, t('Ensure that the clock is correctly displayed in the @type date format type.', array('@type' => $info['title']))); + $clock = date_format_date(date_now('Africa/Lubumbashi'), 'custom', $format); + $this->assertText($clock, t('Ensure that the clock is correctly displayed in the @type date format type.', array('@type' => $info['title']))); } } @@ -95,7 +91,7 @@ class ClockConfigureTestCase extends Dru } public function setUp() { - parent::setUp('block' 'date_api', 'clock'); + parent::setUp('block', 'date_api', 'clock'); $privileged_user = $this->drupalCreateUser(array('administer blocks', 'administer site configuration')); $this->drupalLogin($privileged_user); } @@ -103,9 +99,7 @@ class ClockConfigureTestCase extends Dru public function testClockInterfaceUserTimezone() { $this->drupalGet('admin/structure/block/manage/clock/clock/configure'); $this->assertText(t('User time zone'), t('Make sure the "User time zone" setting is available when user time zones are enabled.')); - $edit = array(); - $edit['configurable_timezones'] = '0'; - $this->drupalPost('admin/config/regional/settings', $edit, 'Save configuration'); + variable_set('configurable_timezones', 0); $this->drupalGet('admin/structure/block/manage/clock/clock/configure'); $this->assertNoText(t('User time zone'), t('Make sure the "User time zone" setting is not available when user time zones are disabled.')); }