annotate core/misc/timezone.es6.js @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * Timezone detection.
Chris@0 4 */
Chris@0 5
Chris@0 6 (function ($, Drupal) {
Chris@0 7 /**
Chris@0 8 * Set the client's system time zone as default values of form fields.
Chris@0 9 *
Chris@0 10 * @type {Drupal~behavior}
Chris@0 11 */
Chris@0 12 Drupal.behaviors.setTimezone = {
Chris@0 13 attach(context, settings) {
Chris@0 14 const $timezone = $(context).find('.timezone-detect').once('timezone');
Chris@0 15 if ($timezone.length) {
Chris@0 16 const dateString = Date();
Chris@0 17 // In some client environments, date strings include a time zone
Chris@0 18 // abbreviation, between 3 and 5 letters enclosed in parentheses,
Chris@0 19 // which can be interpreted by PHP.
Chris@0 20 const matches = dateString.match(/\(([A-Z]{3,5})\)/);
Chris@0 21 const abbreviation = matches ? matches[1] : 0;
Chris@0 22
Chris@0 23 // For all other client environments, the abbreviation is set to "0"
Chris@0 24 // and the current offset from UTC and daylight saving time status are
Chris@0 25 // used to guess the time zone.
Chris@0 26 const dateNow = new Date();
Chris@0 27 const offsetNow = dateNow.getTimezoneOffset() * -60;
Chris@0 28
Chris@0 29 // Use January 1 and July 1 as test dates for determining daylight
Chris@0 30 // saving time status by comparing their offsets.
Chris@0 31 const dateJan = new Date(dateNow.getFullYear(), 0, 1, 12, 0, 0, 0);
Chris@0 32 const dateJul = new Date(dateNow.getFullYear(), 6, 1, 12, 0, 0, 0);
Chris@0 33 const offsetJan = dateJan.getTimezoneOffset() * -60;
Chris@0 34 const offsetJul = dateJul.getTimezoneOffset() * -60;
Chris@0 35
Chris@0 36 let isDaylightSavingTime;
Chris@0 37 // If the offset from UTC is identical on January 1 and July 1,
Chris@0 38 // assume daylight saving time is not used in this time zone.
Chris@0 39 if (offsetJan === offsetJul) {
Chris@0 40 isDaylightSavingTime = '';
Chris@0 41 }
Chris@0 42 // If the maximum annual offset is equivalent to the current offset,
Chris@0 43 // assume daylight saving time is in effect.
Chris@0 44 else if (Math.max(offsetJan, offsetJul) === offsetNow) {
Chris@0 45 isDaylightSavingTime = 1;
Chris@0 46 }
Chris@0 47 // Otherwise, assume daylight saving time is not in effect.
Chris@0 48 else {
Chris@0 49 isDaylightSavingTime = 0;
Chris@0 50 }
Chris@0 51
Chris@0 52 // Submit request to the system/timezone callback and set the form
Chris@0 53 // field to the response time zone. The client date is passed to the
Chris@0 54 // callback for debugging purposes. Submit a synchronous request to
Chris@0 55 // avoid database errors associated with concurrent requests
Chris@0 56 // during install.
Chris@0 57 const path = `system/timezone/${abbreviation}/${offsetNow}/${isDaylightSavingTime}`;
Chris@0 58 $.ajax({
Chris@0 59 async: false,
Chris@0 60 url: Drupal.url(path),
Chris@0 61 data: { date: dateString },
Chris@0 62 dataType: 'json',
Chris@0 63 success(data) {
Chris@0 64 if (data) {
Chris@0 65 $timezone.val(data);
Chris@0 66 }
Chris@0 67 },
Chris@0 68 });
Chris@0 69 }
Chris@0 70 },
Chris@0 71 };
Chris@0 72 }(jQuery, Drupal));