Skip to content

Commit 950fcb9

Browse files
Update to timezone functionality, remove defaultTimezone and useLocalTimezone options
1 parent 7bbcabc commit 950fcb9

File tree

2 files changed

+66
-28
lines changed

2 files changed

+66
-28
lines changed

index.html

+5-4
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ <h3>Alt Field Options</h3>
208208
<h3>Timezone Options</h3>
209209
<dl class="defs">
210210

211-
<dt>useLocalTimezone</dt>
211+
<!-- <dt>useLocalTimezone</dt>
212212
<dd><em>Default: false</em> - Whether to default timezone to the browser's set timezone.</dd>
213213
214214
<dt>defaultTimezone</dt>
215-
<dd><em>Default: "+0000"</em> - If not set, the default timezone used.</dd>
215+
<dd><em>Default: "+0000"</em> - If not set, the default timezone used.</dd> -->
216216

217217
<dt>timezoneIso8601</dt>
218218
<dd><em>Default: false</em> - Whether to follow the ISO 8601 standard.</dd>
@@ -869,8 +869,9 @@ <h3 id="utility_examples">Utilities</h3>
869869
var ex13 = $('#utility_example_1');
870870

871871
ex13.datetimepicker({
872-
dateFormat: "D MM d, yy",
873-
separator: ' @ '
872+
timeFormat: 'hh:mm tt z',
873+
separator: ' @ ',
874+
showTimezone: true
874875
});
875876

876877
$('#utility_example_1_setdt').click(function(){

jquery-ui-timepicker-addon.js

+61-24
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@
7373
second: 0,
7474
millisec: 0,
7575
timezone: null,
76-
useLocalTimezone: false,
77-
defaultTimezone: "+0000",
7876
hourMin: 0,
7977
minuteMin: 0,
8078
secondMin: 0,
@@ -125,7 +123,6 @@
125123
second: 0,
126124
millisec: 0,
127125
timezone: null,
128-
defaultTimezone: "+0000",
129126
hourMinOriginal: null,
130127
minuteMinOriginal: null,
131128
secondMinOriginal: null,
@@ -158,8 +155,8 @@
158155
_newInst: function($input, o) {
159156
var tp_inst = new Timepicker(),
160157
inlineSettings = {},
161-
fns = {},
162-
overrides, i;
158+
fns = {},
159+
overrides, i;
163160

164161
for (var attrName in this._defaults) {
165162
if(this._defaults.hasOwnProperty(attrName)){
@@ -173,6 +170,7 @@
173170
}
174171
}
175172
}
173+
176174
overrides = {
177175
beforeShow: function (input, dp_inst) {
178176
if ($.isFunction(tp_inst._defaults.evnts.beforeShow)) {
@@ -200,6 +198,7 @@
200198
fns[i] = o[i] || null;
201199
}
202200
}
201+
203202
tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, overrides, {
204203
evnts:fns,
205204
timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
@@ -236,7 +235,7 @@
236235
tp_inst._defaults.timezoneList = timezoneList;
237236
}
238237

239-
tp_inst.timezone = tp_inst._defaults.timezone;
238+
tp_inst.timezone = tp_inst._defaults.timezone !== null? tp_inst._defaults.timezone : $.timepicker.timeZoneOffsetString((new Date()).getTimezoneOffset());
240239
tp_inst.hour = tp_inst._defaults.hour < tp_inst._defaults.hourMin? tp_inst._defaults.hourMin :
241240
tp_inst._defaults.hour > tp_inst._defaults.hourMax? tp_inst._defaults.hourMax : tp_inst._defaults.hour;
242241
tp_inst.minute = tp_inst._defaults.minute < tp_inst._defaults.minuteMin? tp_inst._defaults.minuteMin :
@@ -453,21 +452,20 @@
453452
}));
454453
if (typeof(this.timezone) != "undefined" && this.timezone !== null && this.timezone !== "") {
455454
var local_date = new Date(this.inst.selectedYear, this.inst.selectedMonth, this.inst.selectedDay, 12);
456-
var local_timezone = $.timepicker.timeZoneOffsetString(local_date);
455+
var local_timezone = $.timepicker.timeZoneOffsetString(local_date.getTimezoneOffset());
457456
if (local_timezone == this.timezone) {
458457
selectLocalTimeZone(tp_inst);
459458
} else {
460459
this.timezone_select.val(this.timezone);
461460
}
462461
} else {
463462
if (typeof(this.hour) != "undefined" && this.hour !== null && this.hour !== "") {
464-
this.timezone_select.val(o.defaultTimezone);
463+
this.timezone_select.val(o.timezone);
465464
} else {
466465
selectLocalTimeZone(tp_inst);
467466
}
468467
}
469468
this.timezone_select.change(function() {
470-
tp_inst._defaults.useLocalTimezone = false;
471469
tp_inst._onTimeChange();
472470
tp_inst._onSelectHandler();
473471
});
@@ -698,7 +696,7 @@
698696
// If the update was done using the sliders, update the input field.
699697
var hasChanged = (hour != this.hour || minute != this.minute || second != this.second || millisec != this.millisec
700698
|| (this.ampm.length > 0 && (hour < 12) != ($.inArray(this.ampm.toUpperCase(), this.amNames) !== -1))
701-
|| ((this.timezone === null && timezone != this.defaultTimezone) || (this.timezone !== null && timezone != this.timezone)));
699+
|| (this.timezone !== null && timezone != this.timezone));
702700

703701
if (hasChanged) {
704702

@@ -1176,7 +1174,7 @@
11761174
minute: d.getMinutes(),
11771175
second: d.getSeconds(),
11781176
millisec: d.getMilliseconds(),
1179-
timezone: $.timepicker.timeZoneOffsetString(d)
1177+
timezone: $.timepicker.timeZoneOffsetString(d.getTimezoneOffset())
11801178
};
11811179
}
11821180
catch(err){
@@ -1245,7 +1243,7 @@
12451243
case 'l':
12461244
return ('00' + time.millisec).slice(-3);
12471245
case 'z':
1248-
return time.timezone === null? options.defaultTimezone : time.timezone;
1246+
return time.timezone === null? options.timezone : time.timezone;
12491247
case 'T':
12501248
return ampmName.charAt(0).toUpperCase();
12511249
case 'TT':
@@ -1306,12 +1304,6 @@
13061304
var tp_inst = this._get(inst, 'timepicker');
13071305
if (tp_inst) {
13081306
tp_inst._addTimePicker(inst);
1309-
1310-
// if (tp_inst._defaults.useLocalTimezone) { //checks daylight saving with the new date.
1311-
// var date = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay, 12);
1312-
// selectLocalTimeZone(tp_inst, date);
1313-
// tp_inst._onTimeChange();
1314-
// }
13151307
}
13161308
}
13171309
};
@@ -1543,6 +1535,13 @@
15431535
var date = this._getDate(inst);
15441536
if (date && tp_inst._parseTime($(target).val(), tp_inst.timeOnly)) {
15451537
date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
1538+
1539+
// This is important if you are using the timezone option, javascript's Date
1540+
// object will only return the timezone offset for the current locale, so we
1541+
// adjust it accordingly. If not using timezone option this won't matter..
1542+
if(tp_inst.timezone != null){
1543+
date = $.timepicker.timeZoneAdjust(date, tp_inst.timezone);
1544+
}
15461545
}
15471546
return date;
15481547
}
@@ -1799,9 +1798,8 @@
17991798
*/
18001799
var selectLocalTimeZone = function(tp_inst, date) {
18011800
if (tp_inst && tp_inst.timezone_select) {
1802-
tp_inst._defaults.useLocalTimezone = true;
18031801
var now = typeof date !== 'undefined' ? date : new Date();
1804-
var tzoffset = $.timepicker.timeZoneOffsetString(now);
1802+
var tzoffset = $.timepicker.timeZoneOffsetString(now.getTimezoneOffset());
18051803
if (tp_inst._defaults.timezoneIso8601) {
18061804
tzoffset = tzoffset.substring(0, 3) + ':' + tzoffset.substring(3);
18071805
}
@@ -1817,13 +1815,52 @@
18171815
/**
18181816
* Get the timezone offset as string from a date object (eg '+0530' for UTC+5.5)
18191817
* @param date
1818+
* @param boolean if true formats in accordance to iso1806 "+12:45"
18201819
* @return string
18211820
*/
1822-
$.timepicker.timeZoneOffsetString = function(date) {
1823-
var off = date.getTimezoneOffset() * -1,
1821+
$.timepicker.timeZoneOffsetString = function(tzMinutes, iso1806) {
1822+
var off = tzMinutes * -1,
18241823
minutes = off % 60,
1825-
hours = (off - minutes) / 60;
1826-
return (off >= 0 ? '+' : '-') + ('0' + (hours * 101).toString()).slice(-2) + ('0' + (minutes * 101).toString()).slice(-2);
1824+
hours = (off - minutes) / 60,
1825+
iso = iso1806? ':':'',
1826+
tz = (off >= 0 ? '+' : '-') + ('0' + (hours * 101).toString()).slice(-2) + iso + ('0' + (minutes * 101).toString()).slice(-2);
1827+
1828+
if(tz == '+00:00'){
1829+
return 'Z';
1830+
}
1831+
return tz;
1832+
};
1833+
1834+
/**
1835+
* Get the number in minutes that represents a timezone string
1836+
* @param string formated like "+0500", "-1245"
1837+
* @return number
1838+
*/
1839+
$.timepicker.timeZoneOffsetNumber = function(tzString) {
1840+
tzString = tzString.replace(/(\:|z)/gi,''); // excuse any iso1806, end up with "+1245"
1841+
1842+
if(!/^(\-|\+)\d{4}$/.test(tzString)){
1843+
return 0;
1844+
}
1845+
return ((tzString.substr(0,1) =='-'? -1 : 1) * // plus or minus
1846+
((parseInt(tzString.substr(1,2),10)*60) + // hours (converted to minutes)
1847+
parseInt(tzString.substr(3,2),10))); // minutes
1848+
};
1849+
1850+
/**
1851+
* No way to set timezone in js Date, so we must adjust the minutes to compensate
1852+
* @param date
1853+
* @param string formated like "+0500", "-1245"
1854+
* @return date
1855+
*/
1856+
$.timepicker.timeZoneAdjust = function(date, toTimeZone) {
1857+
var currTz = date.getTimezoneOffset(),
1858+
toTz = $.timepicker.timeZoneOffsetNumber(toTimeZone)*-1,
1859+
diff = currTz - toTz; // difference in minutes
1860+
1861+
date.setMinutes(date.getMinutes()+diff);
1862+
1863+
return date;
18271864
};
18281865

18291866
/**

0 commit comments

Comments
 (0)