diff --git a/clock.js b/clock.js index 854d1af..915e352 100755 --- a/clock.js +++ b/clock.js @@ -18,8 +18,7 @@ Drupal.behaviors.clockDisplay = { {long: Drupal.t('February'), short: Drupal.t('Feb')}, {long: Drupal.t('March'), short: Drupal.t('Mar')}, {long: Drupal.t('April'), short: Drupal.t('Apr')}, - // Use context if http://drupal.org/node/488496 gets in. - {long: Drupal.t('May'), short: Drupal.t('May')}, + {long: Drupal.t('May', {}, {context: 'Long month name'}), short: Drupal.t('May')}, {long: Drupal.t('June'), short: Drupal.t('Jun')}, {long: Drupal.t('July'), short: Drupal.t('Jul')}, {long: Drupal.t('August'), short: Drupal.t('Aug')}, @@ -40,63 +39,34 @@ Drupal.behaviors.clockDisplay = { {long: Drupal.t('Saturday'), short: Drupal.t('Sat')} ); - var allSettings = Drupal.settings['clock']; - var clocks = new Array(); - for (var i = 0; i < allSettings['names'].length; i++) { - var cid = allSettings['names'][i]; - var settings = allSettings['clocks'][cid]; - - // Initialize variables. - // A PHP date format string. - var dateFormat = settings['date_format']; - // Prepare the timezone object. - var timeZone = { - // The name of the timezone, e.g. 'Europe/London'. - name: settings['time_zone'], - // The time zone offset in seconds. - offsetSeconds: settings['offset_seconds'], - // Daylight Savings Time information. '1' for yes, '0' for no. - daylightSavingsTime: settings['daylight_savings_time'], - // The name of the offset, e.g. 'GMT'. - offsetName: settings['offset_name'], - }; + $.each(settings['clock'], function(cid, clock) { // Creates a JavaScript date object, by calculating the difference between // the target offset and the current offset and adding that to the current // time. // 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'. + // the visiting user, even if the time zone is set to 'Local'. var date = new Date(); // JavaScript returns the time zone offset reversely signed as PHP, // therefore we calculate the difference in the absolute values by adding // the two numbers. - if (!Drupal.settings['local']) { - date = new Date(date.getTime() + (timeZone.offsetSeconds/60 + date.getTimezoneOffset())*60000); - } - - clocks[cid] = { - date: date, - dateFormat: dateFormat, - timeZone: timeZone, + if (!clock.localTime) { + // date.getTimeZoneOffset() returns minutes, while date.getTime() + // returns milliseconds. + date = new Date(date.getTime() + (clock.timeZone.offsetSeconds/60 + date.getTimezoneOffset())*60000); } - } + clock.date = date; - var cid = allSettings['names'][0]; - clock = clocks[cid]; - $('span.' + cid).each(function () { - $(this).once(cid, function() { - window.setInterval(function () { - for (var i = 0; i < allSettings['names'].length; i++) { - cid = allSettings['names'][i]; - clock = clocks[cid]; + $('span.' + cid).each(function () { + $(this).once(cid, function() { + window.setInterval(function () { var timestamp = clock.date.getTime(); timestamp += 1000; clock.date = new Date(timestamp); - formattedDate = formatDate(clock.date, clock.dateFormat, clock.timeZone, monthNames, weekdayNames); + formattedDate = formatDate(clock, monthNames, weekdayNames); $('span.' + cid).text(formattedDate); - clocks[cid] = clock; - }; - }, 1000); + }, 1000); + }); }); }); @@ -108,17 +78,16 @@ Drupal.behaviors.clockDisplay = { /** * Formats a date into a PHP date string. * - * @param date - * A date object. - * @param dateFormat - * A string. See http://php.net/date for possible values. - * @param timezone - * An associative array containing timezone information. It should consist of - * following keys: - * - timezoneName: The name of the timezone, e.g. 'Europe/London'. - * - offsetSeconds: The timezone offset in seconds. - * - offsetName: The name of the offset, e.g. 'UTC'. - * - daylightSavingsTime: Whether or not the time is in daylight savings time. + * @param clock + * A clock object containing the following keys: + * - date: A JavaScript date object. + * - dateFormat: A string. See http://php.net/date for possible values. + * - timeZone: An associative array containing timezone information. It + * consists of the following keys: + * - timezoneName: The name of the timezone, e.g. 'Europe/London'. + * - offsetSeconds: The timezone offset in seconds. + * - offsetName: The name of the offset, e.g. 'UTC'. + * - daylightSavingsTime: Whether or not the time is in daylight savings time. * '1' if yes, '0' if not. * @param monthNames * An array of month names keyed by the numbers 1-12. Each value should in turn @@ -132,59 +101,48 @@ Drupal.behaviors.clockDisplay = { * @return * A formatted date string. */ -function formatDate(date, dateFormat, timeZone, monthNames, weekdayNames) { - var timezoneName = timeZone.name; - var offsetSeconds = timeZone.offsetSeconds; - var offsetName = timeZone.offsetName; - var daylightSavingsTime = timeZone.daylightSavingsTime; - - // Initialize the 'formattedDate' variable for later use. +function formatDate(clock, monthNames, weekdayNames) { var formattedDate = ''; // Prepare variables for the format conversion. // Year-related. - var year = date.getFullYear(); + var year = clock.date.getFullYear(); var yearShort = year % 100; // Calculate whether it is a leap year or not. Years that are multiples of 4 // are leap years, while multiples of 100 are not, but multiples of 400 are. - var year = date.getFullYear(); - if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) { - var leapYear = '1'; - } - else { - var leapYear = '0'; - } + var leapYear = ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) ? 1 : 0); // Month-related. - var month = date.getMonth(); + var month = clock.date.getMonth(); // JavaScript starts counting the months with 0 instead of 1. month++; var monthLeadingZero = ((month < 10) ? '0' + month : month); var monthLongName = monthNames[month].long; var monthShortName = monthNames[month].short; - var day = date.getDate(); + var day = clock.date.getDate(); var dayLeadingZero = ((day < 10) ? '0' + day : day); + var dayOfMonthAppend = ''; switch (day) { case 1: case 21: case 31: - var dayOfMonthAppend = Drupal.t('st'); + dayOfMonthAppend = Drupal.t('st'); break; case 2: case 22: - var dayOfMonthAppend = Drupal.t('nd'); + dayOfMonthAppend = Drupal.t('nd'); break; case 3: case 23: - var dayOfMonthAppend = Drupal.t('rd'); + dayOfMonthAppend = Drupal.t('rd'); break; default: - var dayOfMonthAppend = Drupal.t('th'); + dayOfMonthAppend = Drupal.t('th'); break; } - // Create an array containing month names and lengths. + // Create an array containing month lengths. var months = new Array( 0, // The 0 key is unneeded. 31, // January @@ -208,7 +166,7 @@ function formatDate(date, dateFormat, timeZone, monthNames, weekdayNames) { daysOfYear += day; // Week-related. - var weekday = date.getDay(); + var weekday = clock.date.getDay(); if (weekday == 0) { var weekNumber = Math.floor((daysOfYear - 7) / 7) + 1; } @@ -220,58 +178,35 @@ function formatDate(date, dateFormat, timeZone, monthNames, weekdayNames) { var weekdayShortName = weekdayNames[weekday].short; // Offset-related. + var offsetSeconds = clock.timeZone.offsetSeconds; + var offsetPrefix = '+'; if (offsetSeconds < 0) { - var offsetPrefix = '-'; - var offsetHours = Math.floor(parseInt(offsetSeconds.substr(1)) / 3600); - var offsetHoursLeadingZero = ((offsetHours < 10) ? '0' + offsetHours : offsetHours); - var offsetMinutes = (parseInt(offsetSeconds.substr(1)) / 60) % 60; - var offsetMinutesLeadingZero = ((offsetMinutes < 10) ? '0' + offsetMinutes : offsetMinutes); - } - else { - var offsetPrefix = '+'; - var offsetHours = Math.floor(offsetSeconds) / 3600; - var offsetHoursLeadingZero = ((offsetHours < 10) ? '0' + offsetHours : offsetHours); - var offsetMinutes = (offsetSeconds / 60) % 60; - var offsetMinutesLeadingZero = ((offsetMinutes < 10) ? '0' + offsetMinutes : offsetMinutes); + offsetPrefix = '-'; + offsetSeconds = parseInt(offsetSeconds.substr(1)); } + var offsetHours = Math.floor(offsetSeconds / 3600); + var offsetHoursLeadingZero = ((offsetHours < 10) ? '0' + offsetHours : offsetHours); + var offsetMinutes = (offsetSeconds / 60) % 60; // Hour-related. - var hours = date.getHours(); + var hours = clock.date.getHours(); var hoursLeadingZero = ((hours < 10) ? '0' + hours : hours); - if (hours == 0) { - var hoursTwelve = (hours + 12); - } - else if (hours > 12) { - var hoursTwelve = (hours - 12); - } - else { - var hoursTwelve = hours; - } + var hoursTwelve = (hours == 0 ? 12 : hours % 12); var hoursTwelveLeadingZero = ((hoursTwelve < 10) ? '0' + hoursTwelve : hoursTwelve); // Minute-related. - var minutes = date.getMinutes(); + var minutes = clock.date.getMinutes(); var minutesLeadingZero = ((minutes < 10) ? '0' + minutes : minutes); // Second-related. - var seconds = date.getSeconds(); + var seconds = clock.date.getSeconds(); var secondsLeadingZero = ((seconds < 10) ? '0' + seconds : seconds); var beats = Math.floor(((hours * 3600) + (minutes * 60) + seconds - parseInt(offsetSeconds) + 3600) / 86400 * 1000); - if (beats < 10) { - var beatsLeadingZero = ((beats < 10) ? '00' + beats : beats); - } - else if (beats < 100) { - var beatsLeadingZero = ((beats < 100) ? '0' + beats : beats); - } - else { - var beatsLeadingZero = beats; - } - var timestamp = date.getTime(); + var beatsLeadingZero = (beats < 10 ? '00' + beats : (beats < 100 ? '0' + beats : beats)); + var timestamp = clock.date.getTime(); // Turn the date format string into an array so each character has its own key. - var dateFormat = dateFormat.split(''); - - // Perform the date format conversion for a character. + var dateFormat = clock.dateFormat.split(''); for (i=0; i < dateFormat.length; i++) { switch (dateFormat[i]) { // If the escape character '\' is used, simply return the following character. @@ -421,7 +356,7 @@ function formatDate(date, dateFormat, timeZone, monthNames, weekdayNames) { formattedDate += yearShort; break; // Days of the year, '0' - '365'. - // Since PHP starts counting the days of the year at '0', we need subtract one. + // Since PHP starts counting the days of the year at 1, we need subtract one. case 'z': formattedDate += daysOfYear - 1; break; diff --git a/clock.theme.inc b/clock.theme.inc index f445c4b..c88122e 100755 --- a/clock.theme.inc +++ b/clock.theme.inc @@ -56,23 +56,21 @@ function theme_clock($variables) { // Pass the needed variables to JavaScript. $time_zone = clock_get_time_zone($clock); - $js = array( + $js_settings = array( 'clock-date-' . $cid => array( - 'time_zone' => $time_zone, - 'date_format' => clock_get_date_format($clock), - 'local' => ($clock->time_zone_type == 'local' ? TRUE : FALSE), - // Get the name of the offset, e.g. 'GMT'. - 'offset_name' => format_date(REQUEST_TIME, 'custom', 'T', $time_zone), - // Get the time zone offset in seconds. - 'offset_seconds' => format_date(REQUEST_TIME, 'custom', 'Z', $time_zone), - // Get Daylight Savings Time information. '1' for yes, '0' for no. - 'daylight_savings_time' => format_date(REQUEST_TIME, 'custom', 'I', $time_zone), + 'dateFormat' => clock_get_date_format($clock), + // Time zone information is not available via the JavaScript date + // function so we need to pass the information from PHP. + 'timeZone' => array( + 'name' => $time_zone, + 'offsetName' => format_date(REQUEST_TIME, 'custom', 'T'), + 'offsetSeconds' => format_date(REQUEST_TIME, 'custom', 'Z'), + 'daylightSavingsTime' => format_date(REQUEST_TIME, 'custom', 'I'), + ), + 'localTime' => ($clock->time_zone_type == 'local' ? TRUE : FALSE), ), ); - drupal_add_js(array('clock' => array( - 'names' => array('clock-date-' . $cid), - 'clocks' => $js, - )), 'setting'); + drupal_add_js(array('clock' => $js_settings), 'setting'); drupal_add_js(drupal_get_path('module', 'clock') . '/clock.js'); return $output;