Skip to content

Commit 07888e3

Browse files
author
Qingyu Zhou
committed
Fix TimeZone conversion
The issue is caused by different assumptions about current Date object's timezone. Such assumption should not be made within timezoneAdjust as it does not know where the Date object comes from. For one case, if the Date object is passed in by 'getDate', the Date object has an implied timezone of current TP instance's timezone setting. However, timezoneAdjust would have incorrectly assumed that the Date object's actual timezone is held by date.getTimezoneOffset(). Another issue is that inside timezoneAdjust, the calculation for adjusting Date object is wrong. The minutes should first be adjusted to UTC timezone, by substracting offset; then the resulted minutes can be adjusted to toTimezone, by adding the offset.
1 parent 60b93e4 commit 07888e3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/jquery-ui-timepicker-addon.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1713,8 +1713,8 @@
17131713
if (!tp_inst.support.timezone && tp_inst._defaults.timezone === null) {
17141714
tp_inst.timezone = tp_date.getTimezoneOffset() * -1;
17151715
}
1716-
date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
1717-
tp_date = $.timepicker.timezoneAdjust(tp_date, tp_inst.timezone);
1716+
date = $.timepicker.timezoneAdjust(date, $.timepicker.timezoneOffsetString(-date.getTimezoneOffset()), tp_inst.timezone);
1717+
tp_date = $.timepicker.timezoneAdjust(tp_date, $.timepicker.timezoneOffsetString(-tp_date.getTimezoneOffset()), tp_inst.timezone);
17181718
}
17191719

17201720
this._updateDatepicker(inst);
@@ -1773,7 +1773,7 @@
17731773
if (!tp_inst.support.timezone && tp_inst._defaults.timezone === null) {
17741774
tp_inst.timezone = date.getTimezoneOffset() * -1;
17751775
}
1776-
date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
1776+
date = $.timepicker.timezoneAdjust(date, tp_inst.timezone, $.timepicker.timezoneOffsetString(-date.getTimezoneOffset()));
17771777
}
17781778
}
17791779
return date;
@@ -2102,13 +2102,15 @@
21022102
/**
21032103
* No way to set timezone in js Date, so we must adjust the minutes to compensate. (think setDate, getDate)
21042104
* @param {Date} date
2105+
* @param {string} fromTimezone formatted like "+0500", "-1245"
21052106
* @param {string} toTimezone formatted like "+0500", "-1245"
21062107
* @return {Date}
21072108
*/
2108-
$.timepicker.timezoneAdjust = function (date, toTimezone) {
2109+
$.timepicker.timezoneAdjust = function (date, fromTimezone, toTimezone) {
2110+
var fromTz = $.timepicker.timezoneOffsetNumber(fromTimezone);
21092111
var toTz = $.timepicker.timezoneOffsetNumber(toTimezone);
21102112
if (!isNaN(toTz)) {
2111-
date.setMinutes(date.getMinutes() + -date.getTimezoneOffset() - toTz);
2113+
date.setMinutes(date.getMinutes() + (-fromTz) - (-toTz));
21122114
}
21132115
return date;
21142116
};

0 commit comments

Comments
 (0)