Skip to content

Commit a9a4550

Browse files
Merge pull request trentrichardson#350 from apepper/tz_autoselect
Autoselect "local" timezone.
2 parents 4584407 + 41e6a18 commit a9a4550

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

jquery-ui-timepicker-addon.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,13 @@ $.extend(Timepicker.prototype, {
449449
})
450450
);
451451
if (typeof this.timezone != "undefined" && this.timezone != null && this.timezone != "") {
452-
this.timezone_select.val(this.timezone);
452+
var local_date = new Date(this.inst.selectedYear, this.inst.selectedMonth, this.inst.selectedDay, 12);
453+
var local_timezone = timeZoneString(local_date);
454+
if (local_timezone == this.timezone) {
455+
selectLocalTimeZone(tp_inst);
456+
} else {
457+
this.timezone_select.val(this.timezone);
458+
}
453459
} else {
454460
if (typeof this.hour != "undefined" && this.hour != null && this.hour != "") {
455461
this.timezone_select.val(o.defaultTimezone);
@@ -1423,17 +1429,22 @@ var selectLocalTimeZone = function(tp_inst, date)
14231429
if (tp_inst && tp_inst.timezone_select) {
14241430
tp_inst._defaults.useLocalTimezone = true;
14251431
var now = typeof date !== 'undefined' ? date : new Date();
1426-
var tzoffset = now.getTimezoneOffset(); // If +0100, returns -60
1427-
var tzsign = tzoffset > 0 ? '-' : '+';
1428-
tzoffset = Math.abs(tzoffset);
1429-
var tzmin = tzoffset % 60;
1430-
tzoffset = tzsign + ('0' + (tzoffset - tzmin) / 60).slice(-2) + ('0' + tzmin).slice(-2);
1432+
var tzoffset = timeZoneString(now);
14311433
if (tp_inst._defaults.timezoneIso8601)
14321434
tzoffset = tzoffset.substring(0, 3) + ':' + tzoffset.substring(3);
14331435
tp_inst.timezone_select.val(tzoffset);
14341436
}
14351437
}
14361438

1439+
// Input: Date Object
1440+
// Output: String with timezone offset, e.g. '+0100'
1441+
var timeZoneString = function(date)
1442+
{
1443+
var off = date.getTimezoneOffset() * -10100 / 60;
1444+
var timezone = (off >= 0 ? '+' : '-') + Math.abs(off).toString().substr(1);
1445+
return timezone;
1446+
}
1447+
14371448
$.timepicker = new Timepicker(); // singleton instance
14381449
$.timepicker.version = "1.0.1";
14391450

0 commit comments

Comments
 (0)