Skip to content

Commit b876a04

Browse files
Merge branch 'MAGETWO-53299' of github.com:magento-vanilla/magento2ce into PR
2 parents 6f44ada + 38c3b31 commit b876a04

File tree

3 files changed

+67
-28
lines changed

3 files changed

+67
-28
lines changed

app/code/Magento/Backend/view/adminhtml/templates/page/js/calendar.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ require([
5151
showHour: false,
5252
showMinute: false,
5353
serverTimezoneSeconds: <?php echo (int) $block->getStoreTimestamp(); ?>,
54+
serverTimezoneOffset: <?php echo (int) $block->getTimezoneOffsetSeconds(); ?>,
5455
yearRange: '<?php /* @escapeNotVerified */ echo $block->getYearRange() ?>'
5556
}
5657
});

dev/tests/js/JsTestDriver/testsuite/mage/calendar/calendar-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ CalendarTest.prototype.testWithServerTimezoneOffset = function() {
5858
assertEquals(true, currentDate.toString() === calendar.calendar('getTimezoneDate').toString());
5959
calendar.calendar('destroy');
6060
};
61+
CalendarTest.prototype.testWithServerTimezoneShift = function() {
62+
/*:DOC += <input type="text" id="calendar" /> */
63+
var serverTimezoneOffset = 43200,
64+
calendar = $('#calendar').calendar({serverTimezoneOffset: serverTimezoneOffset}),
65+
currentDate = new Date();
66+
67+
setTimeout(function () {
68+
currentDate.setTime(currentDate.getTime() + (serverTimezoneOffset + currentDate.getTimezoneOffset() * 60) * 1000);
69+
assertEquals(true, currentDate.toString() === calendar.calendar('getTimezoneDate').toString());
70+
calendar.calendar('destroy');
71+
}, 61000);
72+
};
6173
CalendarTest.prototype.testWithoutServerTimezoneOffset = function() {
6274
/*:DOC += <input type="text" id="calendar" /> */
6375
var calendar = $('#calendar').calendar(),

lib/web/mage/calendar.js

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,49 @@
2121
}(function ($) {
2222
'use strict';
2323

24-
var calendarBasePrototype;
24+
var calendarBasePrototype,
25+
datepickerPrototype = $.datepicker.constructor.prototype;
2526

2627
$.datepicker.markerClassName = '_has-datepicker';
2728

29+
/**
30+
* Extend JQuery date picker prototype with store local time methods
31+
*/
32+
$.extend(datepickerPrototype, {
33+
/**
34+
* Get date/time according to store settings.
35+
* We use serverTimezoneOffset (in seconds) instead of serverTimezoneSeconds
36+
* in order to have ability to know actual store time even if page hadn't been reloaded
37+
* @returns {Date}
38+
*/
39+
_getTimezoneDate: function (options) {
40+
// local time in ms
41+
var ms = Date.now();
42+
43+
options = options || $.calendarConfig || {};
44+
45+
// Adjust milliseconds according to store timezone offset,
46+
// mind the GMT zero offset
47+
if (typeof options.serverTimezoneOffset !== 'undefined') {
48+
// Make UTC time and add store timezone offset in seconds
49+
ms += new Date().getTimezoneOffset() * 60 * 1000 + options.serverTimezoneOffset * 1000;
50+
} else if (typeof options.serverTimezoneSeconds !== 'undefined') {
51+
//Set milliseconds according to client local timezone offset
52+
ms = (options.serverTimezoneSeconds + new Date().getTimezoneOffset() * 60) * 1000;
53+
}
54+
55+
return new Date(ms);
56+
},
57+
58+
/**
59+
* Set date/time according to store settings.
60+
* @param {String|Object} target - the target input field or division or span
61+
*/
62+
_setTimezoneDateDatepicker: function (target) {
63+
this._setDateDatepicker(target, this._getTimezoneDate());
64+
}
65+
});
66+
2867
/**
2968
* Widget calendar
3069
*/
@@ -72,8 +111,6 @@
72111
* Wrapper for overwrite jQuery UI datepicker function.
73112
*/
74113
_overwriteGenerateHtml: function () {
75-
var self = this;
76-
77114
/**
78115
* Overwrite jQuery UI datepicker function.
79116
* Reason: magento date could be set before calendar show
@@ -83,7 +120,7 @@
83120
* @return {String} html template
84121
*/
85122
$.datepicker.constructor.prototype._generateHTML = function (inst) {
86-
var today = self.getTimezoneDate(),
123+
var today = this._getTimezoneDate(),
87124
isRTL = this._get(inst, 'isRTL'),
88125
showButtonPanel = this._get(inst, 'showButtonPanel'),
89126
hideIfNoPrevNext = this._get(inst, 'hideIfNoPrevNext'),
@@ -316,33 +353,14 @@
316353
};
317354
},
318355

319-
/**
320-
* If server timezone is defined then take to account server timezone shift
321-
* @return {Object} date
322-
*/
323-
getTimezoneDate: function () {
324-
325-
// Get local time in ms
326-
var ms = Date.now();
327-
328-
// Use store timestamp based on store timezone settings
329-
if (typeof this.options.serverTimezoneSeconds !== 'undefined') {
330-
//Adjust milliseconds according to client local timezone offset
331-
ms = (this.options.serverTimezoneSeconds + new Date().getTimezoneOffset() * 60) * 1000;
332-
}
333-
334-
return new Date(ms);
335-
},
336-
337356
/**
338357
* Set current date if the date is not set
339358
* @protected
340359
* @param {Object} element
341360
*/
342361
_setCurrentDate: function (element) {
343362
if (!element.val()) {
344-
element[this._picker()]('setDate', this.getTimezoneDate())
345-
.val('');
363+
element[this._picker()]('setTimezoneDate').val('');
346364
}
347365
},
348366

@@ -356,6 +374,7 @@
356374
pickerButtonText = picker.next('.ui-datepicker-trigger')
357375
.find('img')
358376
.attr('title');
377+
359378
picker.next('.ui-datepicker-trigger')
360379
.addClass('v-middle')
361380
.text('') // Remove jQuery UI datepicker generated image
@@ -369,6 +388,15 @@
369388
_destroy: function () {
370389
this.element[this._picker()]('destroy');
371390
this._super();
391+
},
392+
393+
/**
394+
* Method is kept for backward compatibility and unit-tests acceptance
395+
* see \mage\calendar\calendar-test.js
396+
* @return {Object} date
397+
*/
398+
getTimezoneDate: function () {
399+
return datepickerPrototype._getTimezoneDate.call(this, this.options);
372400
}
373401
});
374402

@@ -521,10 +549,8 @@
521549
* @param {Object} el
522550
*/
523551
$.datepicker._gotoToday = function (el) {
524-
var pickerObject = $(el);
525-
526-
// Set date/time according to timezone offset
527-
pickerObject.datepicker( "setDate", pickerObject.calendar("getTimezoneDate") )
552+
//Set date/time according to timezone offset
553+
$(el).datepicker('setTimezoneDate')
528554
// To ensure that user can re-select date field without clicking outside it first.
529555
.blur();
530556
};

0 commit comments

Comments
 (0)