Added new block on front page.
[yaffs-website] / web / core / misc / timezone.js
1 /**
2  * @file
3  * Timezone detection.
4  */
5
6 (function ($, Drupal) {
7
8   'use strict';
9
10   /**
11    * Set the client's system time zone as default values of form fields.
12    *
13    * @type {Drupal~behavior}
14    */
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;
25
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;
31
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;
38
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 = '';
44         }
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;
49         }
50         // Otherwise, assume daylight saving time is not in effect.
51         else {
52           isDaylightSavingTime = 0;
53         }
54
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
59         // during install.
60         var path = 'system/timezone/' + abbreviation + '/' + offsetNow + '/' + isDaylightSavingTime;
61         $.ajax({
62           async: false,
63           url: Drupal.url(path),
64           data: {date: dateString},
65           dataType: 'json',
66           success: function (data) {
67             if (data) {
68               $timezone.val(data);
69             }
70           }
71         });
72       }
73     }
74   };
75
76 })(jQuery, Drupal);