| 1 |
// $Id: timezone.js,v 1.6 2009/04/27 20:19:35 webchick Exp $
|
| 2 |
(function ($) {
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Set the client's system time zone as default values of form fields.
|
| 6 |
*/
|
| 7 |
Drupal.behaviors.setTimezone = {
|
| 8 |
attach: function (context, settings) {
|
| 9 |
$('select.timezone-detect', context).once('timezone', function () {
|
| 10 |
var dateString = Date();
|
| 11 |
// In some client environments, date strings include a time zone
|
| 12 |
// abbreviation, between 3 and 5 letters enclosed in parentheses,
|
| 13 |
// which can be interpreted by PHP.
|
| 14 |
var matches = dateString.match(/\(([A-Z]{3,5})\)/);
|
| 15 |
var abbreviation = matches ? matches[1] : 0;
|
| 16 |
|
| 17 |
// For all other client environments, the abbreviation is set to "0"
|
| 18 |
// and the current offset from UTC and daylight saving time status are
|
| 19 |
// used to guess the time zone.
|
| 20 |
var dateNow = new Date();
|
| 21 |
var offsetNow = dateNow.getTimezoneOffset() * -60;
|
| 22 |
|
| 23 |
// Use January 1 and July 1 as test dates for determining daylight
|
| 24 |
// saving time status by comparing their offsets.
|
| 25 |
var dateJan = new Date(dateNow.getFullYear(), 0, 1, 12, 0, 0, 0);
|
| 26 |
var dateJul = new Date(dateNow.getFullYear(), 6, 1, 12, 0, 0, 0);
|
| 27 |
var offsetJan = dateJan.getTimezoneOffset() * -60;
|
| 28 |
var offsetJul = dateJul.getTimezoneOffset() * -60;
|
| 29 |
|
| 30 |
var isDaylightSavingTime;
|
| 31 |
// If the offset from UTC is identical on January 1 and July 1,
|
| 32 |
// assume daylight saving time is not used in this time zone.
|
| 33 |
if (offsetJan == offsetJul) {
|
| 34 |
isDaylightSavingTime = '';
|
| 35 |
}
|
| 36 |
// If the maximum annual offset is equivalent to the current offset,
|
| 37 |
// assume daylight saving time is in effect.
|
| 38 |
else if (Math.max(offsetJan, offsetJul) == offsetNow) {
|
| 39 |
isDaylightSavingTime = 1;
|
| 40 |
}
|
| 41 |
// Otherwise, assume daylight saving time is not in effect.
|
| 42 |
else {
|
| 43 |
isDaylightSavingTime = 0;
|
| 44 |
}
|
| 45 |
|
| 46 |
// Submit request to the system/timezone callback and set the form field
|
| 47 |
// to the response time zone. The client date is passed to the callback
|
| 48 |
// for debugging purposes. Submit a synchronous request to avoid database
|
| 49 |
// errors associated with concurrent requests during install.
|
| 50 |
var path = 'system/timezone/' + abbreviation + '/' + offsetNow + '/' + isDaylightSavingTime;
|
| 51 |
var element = this;
|
| 52 |
$.ajax({
|
| 53 |
async: false,
|
| 54 |
url: settings.basePath,
|
| 55 |
data: { q: path, date: dateString },
|
| 56 |
dataType: 'json',
|
| 57 |
success: function (data) {
|
| 58 |
if (data) {
|
| 59 |
$(element).val(data);
|
| 60 |
}
|
| 61 |
}
|
| 62 |
});
|
| 63 |
});
|
| 64 |
}
|
| 65 |
};
|
| 66 |
|
| 67 |
})(jQuery);
|