Skip to content

Commit e9fb16a

Browse files
Fixes timeZone casing, setDate now considers timezone
1 parent 950fcb9 commit e9fb16a

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@
235235
tp_inst._defaults.timezoneList = timezoneList;
236236
}
237237

238-
tp_inst.timezone = tp_inst._defaults.timezone !== null? tp_inst._defaults.timezone : $.timepicker.timeZoneOffsetString((new Date()).getTimezoneOffset());
238+
tp_inst.timezone = tp_inst._defaults.timezone !== null? tp_inst._defaults.timezone : $.timepicker.timezoneOffsetString((new Date()).getTimezoneOffset());
239239
tp_inst.hour = tp_inst._defaults.hour < tp_inst._defaults.hourMin? tp_inst._defaults.hourMin :
240240
tp_inst._defaults.hour > tp_inst._defaults.hourMax? tp_inst._defaults.hourMax : tp_inst._defaults.hour;
241241
tp_inst.minute = tp_inst._defaults.minute < tp_inst._defaults.minuteMin? tp_inst._defaults.minuteMin :
@@ -452,17 +452,17 @@
452452
}));
453453
if (typeof(this.timezone) != "undefined" && this.timezone !== null && this.timezone !== "") {
454454
var local_date = new Date(this.inst.selectedYear, this.inst.selectedMonth, this.inst.selectedDay, 12);
455-
var local_timezone = $.timepicker.timeZoneOffsetString(local_date.getTimezoneOffset());
455+
var local_timezone = $.timepicker.timezoneOffsetString(local_date.getTimezoneOffset());
456456
if (local_timezone == this.timezone) {
457-
selectLocalTimeZone(tp_inst);
457+
selectLocalTimezone(tp_inst);
458458
} else {
459459
this.timezone_select.val(this.timezone);
460460
}
461461
} else {
462462
if (typeof(this.hour) != "undefined" && this.hour !== null && this.hour !== "") {
463463
this.timezone_select.val(o.timezone);
464464
} else {
465-
selectLocalTimeZone(tp_inst);
465+
selectLocalTimezone(tp_inst);
466466
}
467467
}
468468
this.timezone_select.change(function() {
@@ -1174,7 +1174,7 @@
11741174
minute: d.getMinutes(),
11751175
second: d.getSeconds(),
11761176
millisec: d.getMilliseconds(),
1177-
timezone: $.timepicker.timeZoneOffsetString(d.getTimezoneOffset())
1177+
timezone: $.timepicker.timezoneOffsetString(d.getTimezoneOffset())
11781178
};
11791179
}
11801180
catch(err){
@@ -1406,7 +1406,7 @@
14061406
$dp = inst.dpDiv;
14071407
this._base_gotoToday(id);
14081408
var tp_inst = this._get(inst, 'timepicker');
1409-
selectLocalTimeZone(tp_inst);
1409+
selectLocalTimezone(tp_inst);
14101410
var now = new Date();
14111411
this._setTime(inst, now);
14121412
$('.ui-datepicker-today', $dp).click();
@@ -1507,7 +1507,17 @@
15071507
return;
15081508
}
15091509

1510-
var tp_date = (date instanceof Date) ? new Date(date.getTime()) : date;
1510+
var tp_inst = this._get(inst, 'timepicker'),
1511+
tp_date = (date instanceof Date) ? new Date(date.getTime()) : date;
1512+
1513+
// This is important if you are using the timezone option, javascript's Date
1514+
// object will only return the timezone offset for the current locale, so we
1515+
// adjust it accordingly. If not using timezone option this won't matter..
1516+
// If a timezone is different in tp, keep the timezone as is
1517+
if(tp_inst && tp_inst.timezone != null){
1518+
date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
1519+
tp_date = $.timepicker.timezoneAdjust(tp_date, tp_inst.timezone);
1520+
}
15111521

15121522
this._updateDatepicker(inst);
15131523
this._base_setDateDatepicker.apply(this, arguments);
@@ -1540,7 +1550,7 @@
15401550
// object will only return the timezone offset for the current locale, so we
15411551
// adjust it accordingly. If not using timezone option this won't matter..
15421552
if(tp_inst.timezone != null){
1543-
date = $.timepicker.timeZoneAdjust(date, tp_inst.timezone);
1553+
date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
15441554
}
15451555
}
15461556
return date;
@@ -1796,10 +1806,10 @@
17961806
/*
17971807
* Internal function to set timezone_select to the local timezone
17981808
*/
1799-
var selectLocalTimeZone = function(tp_inst, date) {
1809+
var selectLocalTimezone = function(tp_inst, date) {
18001810
if (tp_inst && tp_inst.timezone_select) {
18011811
var now = typeof date !== 'undefined' ? date : new Date();
1802-
var tzoffset = $.timepicker.timeZoneOffsetString(now.getTimezoneOffset());
1812+
var tzoffset = $.timepicker.timezoneOffsetString(now.getTimezoneOffset());
18031813
if (tp_inst._defaults.timezoneIso8601) {
18041814
tzoffset = tzoffset.substring(0, 3) + ':' + tzoffset.substring(3);
18051815
}
@@ -1818,7 +1828,7 @@
18181828
* @param boolean if true formats in accordance to iso1806 "+12:45"
18191829
* @return string
18201830
*/
1821-
$.timepicker.timeZoneOffsetString = function(tzMinutes, iso1806) {
1831+
$.timepicker.timezoneOffsetString = function(tzMinutes, iso1806) {
18221832
var off = tzMinutes * -1,
18231833
minutes = off % 60,
18241834
hours = (off - minutes) / 60,
@@ -1836,7 +1846,7 @@
18361846
* @param string formated like "+0500", "-1245"
18371847
* @return number
18381848
*/
1839-
$.timepicker.timeZoneOffsetNumber = function(tzString) {
1849+
$.timepicker.timezoneOffsetNumber = function(tzString) {
18401850
tzString = tzString.replace(/(\:|z)/gi,''); // excuse any iso1806, end up with "+1245"
18411851

18421852
if(!/^(\-|\+)\d{4}$/.test(tzString)){
@@ -1848,14 +1858,14 @@
18481858
};
18491859

18501860
/**
1851-
* No way to set timezone in js Date, so we must adjust the minutes to compensate
1861+
* No way to set timezone in js Date, so we must adjust the minutes to compensate. (think setDate, getDate)
18521862
* @param date
18531863
* @param string formated like "+0500", "-1245"
18541864
* @return date
18551865
*/
1856-
$.timepicker.timeZoneAdjust = function(date, toTimeZone) {
1866+
$.timepicker.timezoneAdjust = function(date, toTimezone) {
18571867
var currTz = date.getTimezoneOffset(),
1858-
toTz = $.timepicker.timeZoneOffsetNumber(toTimeZone)*-1,
1868+
toTz = $.timepicker.timezoneOffsetNumber(toTimezone)*-1,
18591869
diff = currTz - toTz; // difference in minutes
18601870

18611871
date.setMinutes(date.getMinutes()+diff);

0 commit comments

Comments
 (0)