6 (function ($, Drupal) {
11 * Set the client's system time zone as default values of form fields.
13 * @type {Drupal~behavior}
15 Drupal.behaviors.setTimezone = {
16 attach: function (context, settings) {
17 var $timezone = $(context).find('.timezone-detect').once('timezone');
18 if ($timezone.length) {
19 var dateString = Date();
20 // In some client environments, date strings include a time zone
21 // abbreviation, between 3 and 5 letters enclosed in parentheses,
22 // which can be interpreted by PHP.
23 var matches = dateString.match(/\(([A-Z]{3,5})\)/);
24 var abbreviation = matches ? matches[1] : 0;
26 // For all other client environments, the abbreviation is set to "0"
27 // and the current offset from UTC and daylight saving time status are
28 // used to guess the time zone.
29 var dateNow = new Date();
30 var offsetNow = dateNow.getTimezoneOffset() * -60;
32 // Use January 1 and July 1 as test dates for determining daylight
33 // saving time status by comparing their offsets.
34 var dateJan = new Date(dateNow.getFullYear(), 0, 1, 12, 0, 0, 0);
35 var dateJul = new Date(dateNow.getFullYear(), 6, 1, 12, 0, 0, 0);
36 var offsetJan = dateJan.getTimezoneOffset() * -60;
37 var offsetJul = dateJul.getTimezoneOffset() * -60;
39 var isDaylightSavingTime;
40 // If the offset from UTC is identical on January 1 and July 1,
41 // assume daylight saving time is not used in this time zone.
42 if (offsetJan === offsetJul) {
43 isDaylightSavingTime = '';
45 // If the maximum annual offset is equivalent to the current offset,
46 // assume daylight saving time is in effect.
47 else if (Math.max(offsetJan, offsetJul) === offsetNow) {
48 isDaylightSavingTime = 1;
50 // Otherwise, assume daylight saving time is not in effect.
52 isDaylightSavingTime = 0;
55 // Submit request to the system/timezone callback and set the form
56 // field to the response time zone. The client date is passed to the
57 // callback for debugging purposes. Submit a synchronous request to
58 // avoid database errors associated with concurrent requests
60 var path = 'system/timezone/' + abbreviation + '/' + offsetNow + '/' + isDaylightSavingTime;
63 url: Drupal.url(path),
64 data: {date: dateString},
66 success: function (data) {