diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2008-11-21 04:33:28 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2008-11-21 04:33:28 +0000 |
commit | b4435decef6ebbfa2c5e9da23c2916fca3b04f57 (patch) | |
tree | 0f911f1481c32613565e315b3f363570fd0f1ef2 /misc/timezone.js | |
parent | 328982747d916971c6309d7f3b453c5b3ec4891b (diff) | |
download | brdo-b4435decef6ebbfa2c5e9da23c2916fca3b04f57.tar.gz brdo-b4435decef6ebbfa2c5e9da23c2916fca3b04f57.tar.bz2 |
#11077 follow-up: adding missing timezone.js file.
Diffstat (limited to 'misc/timezone.js')
-rw-r--r-- | misc/timezone.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/misc/timezone.js b/misc/timezone.js new file mode 100644 index 000000000..104c3b072 --- /dev/null +++ b/misc/timezone.js @@ -0,0 +1,64 @@ +// $Id$ + +/** + * Set the client's system time zone as default values of form fields. + */ +Drupal.behaviors.setTimezone = { + attach: function(context) { + $('select.timezone-detect:not(.timezone-processed)', context).addClass('timezone-processed').each(function() { + var dateString = Date(); + // In some client environments, date strings include a time zone + // abbreviation, between 3 and 5 letters enclosed in parentheses, + // which can be interpreted by PHP. + var matches = dateString.match(/\(([A-Z]{3,5})\)/); + var abbreviation = matches ? matches[1] : 0; + + // For all other client environments, the abbreviation is set to "0" + // and the current offset from UTC and daylight saving time status are + // used to guess the time zone. + var dateNow = new Date(); + var offsetNow = dateNow.getTimezoneOffset() * -60; + + // Use January 1 and July 1 as test dates for determining daylight + // saving time status by comparing their offsets. + var dateJan = new Date(dateNow.getFullYear(), 0, 1, 12, 0, 0, 0); + var dateJul = new Date(dateNow.getFullYear(), 6, 1, 12, 0, 0, 0); + var offsetJan = dateJan.getTimezoneOffset() * -60; + var offsetJul = dateJul.getTimezoneOffset() * -60; + + var isDaylightSavingTime; + // If the offset from UTC is identical on January 1 and July 1, + // assume daylight saving time is not used in this time zone. + if (offsetJan == offsetJul) { + isDaylightSavingTime = ''; + } + // If the maximum annual offset is equivalent to the current offset, + // assume daylight saving time is in effect. + else if (Math.max(offsetJan, offsetJul) == offsetNow) { + isDaylightSavingTime = 1; + } + // Otherwise, assume daylight saving time is not in effect. + else { + isDaylightSavingTime = 0; + } + + // Submit request to the system/timezone callback and set the form field + // to the response time zone. The client date is passed to the callback + // for debugging purposes. Submit a synchronous request to avoid database + // errors associated with concurrent requests during install. + var path = 'system/timezone/' + abbreviation + '/' + offsetNow + '/' + isDaylightSavingTime; + var element = this; + $.ajax({ + async: false, + url: Drupal.settings.basePath, + data: { q: path, date: dateString }, + dataType: 'json', + success: function (data) { + if (data) { + $(element).val(data); + } + }, + }); + }); + } +}; |