Projet

Général

Profil

Paste
Télécharger (2,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / misc / timezone.js @ 76597ebf

1
(function ($) {
2

    
3
/**
4
 * Set the client's system time zone as default values of form fields.
5
 */
6
Drupal.behaviors.setTimezone = {
7
  attach: function (context, settings) {
8
    $('select.timezone-detect', context).once('timezone', function () {
9
      var dateString = Date();
10
      // In some client environments, date strings include a time zone
11
      // abbreviation, between 3 and 5 letters enclosed in parentheses,
12
      // which can be interpreted by PHP.
13
      var matches = dateString.match(/\(([A-Z]{3,5})\)/);
14
      var abbreviation = matches ? matches[1] : 0;
15

    
16
      // For all other client environments, the abbreviation is set to "0"
17
      // and the current offset from UTC and daylight saving time status are
18
      // used to guess the time zone.
19
      var dateNow = new Date();
20
      var offsetNow = dateNow.getTimezoneOffset() * -60;
21

    
22
      // Use January 1 and July 1 as test dates for determining daylight
23
      // saving time status by comparing their offsets.
24
      var dateJan = new Date(dateNow.getFullYear(), 0, 1, 12, 0, 0, 0);
25
      var dateJul = new Date(dateNow.getFullYear(), 6, 1, 12, 0, 0, 0);
26
      var offsetJan = dateJan.getTimezoneOffset() * -60;
27
      var offsetJul = dateJul.getTimezoneOffset() * -60;
28

    
29
      var isDaylightSavingTime;
30
      // If the offset from UTC is identical on January 1 and July 1,
31
      // assume daylight saving time is not used in this time zone.
32
      if (offsetJan == offsetJul) {
33
        isDaylightSavingTime = '';
34
      }
35
      // If the maximum annual offset is equivalent to the current offset,
36
      // assume daylight saving time is in effect.
37
      else if (Math.max(offsetJan, offsetJul) == offsetNow) {
38
        isDaylightSavingTime = 1;
39
      }
40
      // Otherwise, assume daylight saving time is not in effect.
41
      else {
42
        isDaylightSavingTime = 0;
43
      }
44

    
45
      // Submit request to the system/timezone callback and set the form field
46
      // to the response time zone. The client date is passed to the callback
47
      // for debugging purposes. Submit a synchronous request to avoid database
48
      // errors associated with concurrent requests during install.
49
      var path = 'system/timezone/' + abbreviation + '/' + offsetNow + '/' + isDaylightSavingTime;
50
      var element = this;
51
      $.ajax({
52
        async: false,
53
        url: settings.basePath,
54
        data: { q: path, date: dateString },
55
        dataType: 'json',
56
        success: function (data) {
57
          if (data) {
58
            $(element).val(data);
59
          }
60
        }
61
      });
62
    });
63
  }
64
};
65

    
66
})(jQuery);