annotate core/misc/timezone.es6.js @ 19:fa3358dc1485 tip

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