timezone.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal) {
  8. Drupal.behaviors.setTimezone = {
  9. attach: function attach(context, settings) {
  10. var $timezone = $(context).find('.timezone-detect').once('timezone');
  11. if ($timezone.length) {
  12. var dateString = Date();
  13. var matches = dateString.match(/\(([A-Z]{3,5})\)/);
  14. var abbreviation = matches ? matches[1] : 0;
  15. var dateNow = new Date();
  16. var offsetNow = dateNow.getTimezoneOffset() * -60;
  17. var dateJan = new Date(dateNow.getFullYear(), 0, 1, 12, 0, 0, 0);
  18. var dateJul = new Date(dateNow.getFullYear(), 6, 1, 12, 0, 0, 0);
  19. var offsetJan = dateJan.getTimezoneOffset() * -60;
  20. var offsetJul = dateJul.getTimezoneOffset() * -60;
  21. var isDaylightSavingTime = void 0;
  22. if (offsetJan === offsetJul) {
  23. isDaylightSavingTime = '';
  24. } else if (Math.max(offsetJan, offsetJul) === offsetNow) {
  25. isDaylightSavingTime = 1;
  26. } else {
  27. isDaylightSavingTime = 0;
  28. }
  29. var path = 'system/timezone/' + abbreviation + '/' + offsetNow + '/' + isDaylightSavingTime;
  30. $.ajax({
  31. async: false,
  32. url: Drupal.url(path),
  33. data: { date: dateString },
  34. dataType: 'json',
  35. success: function success(data) {
  36. if (data) {
  37. $timezone.val(data);
  38. }
  39. }
  40. });
  41. }
  42. }
  43. };
  44. })(jQuery, Drupal);