Skip to content

Commit 6737b60

Browse files
committed
Support timezone designators in iso8601.
1 parent 04efa58 commit 6737b60

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ function Timepicker() {
7979
separator: ' ',
8080
altFieldTimeOnly: true,
8181
showTimepicker: true,
82-
timezoneList: ["-1100", "-1000", "-0900", "-0800", "-0700", "-0600",
83-
"-0500", "-0400", "-0300", "-0200", "-0100", "+0000",
84-
"+0100", "+0200", "+0300", "+0400", "+0500", "+0600",
85-
"+0700", "+0800", "+0900", "+1000", "+1100", "+1200"]
82+
timezoneIso8609: false,
83+
timezoneList: null
8684
};
8785
$.extend(this._defaults, this.regional['']);
8886
}
@@ -114,10 +112,7 @@ $.extend(Timepicker.prototype, {
114112
formattedDate: '',
115113
formattedTime: '',
116114
formattedDateTime: '',
117-
timezoneList: ["-1100", "-1000", "-0900", "-0800", "-0700", "-0600",
118-
"-0500", "-0400", "-0300", "-0200", "-0100", "+0000",
119-
"+0100", "+0200", "+0300", "+0400", "+0500", "+0600",
120-
"+0700", "+0800", "+0900", "+1000", "+1100", "+1200"],
115+
timezoneList: null,
121116

122117
/* Override the default settings for all instances of the time picker.
123118
@param settings object - the new settings to use as defaults (anonymous object)
@@ -164,6 +159,17 @@ $.extend(Timepicker.prototype, {
164159
timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
165160
});
166161

162+
if (tp_inst._defaults.timezoneList === null) {
163+
var timezoneList = [];
164+
for (var i = -11; i <= 12; i++)
165+
timezoneList.push((i >= 0 ? '+' : '-') + ('0' + Math.abs(i).toString()).slice(-2) + '00');
166+
if (tp_inst._defaults.timezoneIso8609)
167+
timezoneList = $.map(timezoneList, function(val) {
168+
return val == '+0000' ? 'Z' : (val.substring(0, 3) + ':' + val.substring(3));
169+
});
170+
tp_inst._defaults.timezoneList = timezoneList;
171+
}
172+
167173
tp_inst.hour = tp_inst._defaults.hour;
168174
tp_inst.minute = tp_inst._defaults.minute;
169175
tp_inst.second = tp_inst._defaults.second;
@@ -220,7 +226,7 @@ $.extend(Timepicker.prototype, {
220226
.replace(/s{1,2}/ig, '(\\d?\\d)')
221227
.replace(/l{1}/ig, '(\\d?\\d?\\d)')
222228
.replace(/t{1,2}/ig, '(am|pm|a|p)?')
223-
.replace(/z{1}/ig, '((\\+|-)\\d\\d\\d\\d)?')
229+
.replace(/z{1}/ig, '(z|[-+]\\d\\d:?\\d\\d)?')
224230
.replace(/\s/g, '\\s?') + this._defaults.timeSuffix + '$',
225231
order = this._getFormatPositions(),
226232
treg;
@@ -255,7 +261,29 @@ $.extend(Timepicker.prototype, {
255261
if (order.m !== -1) this.minute = Number(treg[order.m]);
256262
if (order.s !== -1) this.second = Number(treg[order.s]);
257263
if (order.l !== -1) this.millisec = Number(treg[order.l]);
258-
if (order.z !== -1) this.timezone = treg[order.z];
264+
if (order.z !== -1 && treg[order.z] !== undefined) {
265+
var tz = treg[order.z].toUpperCase();
266+
switch (tz.length) {
267+
case 1: // Z
268+
tz = this._defaults.timezoneIso8609 ? 'Z' : '+0000';
269+
break;
270+
case 5: // +hhmm
271+
if (this._defaults.timezoneIso8609)
272+
tz = tz.substring(1) == '0000'
273+
? 'Z'
274+
: tz.substring(0, 3) + ':' + tz.substring(3);
275+
break;
276+
case 6: // +hh:mm
277+
if (!this._defaults.timezoneIso8609)
278+
tz = tz == 'Z' || tz.substring(1) == '00:00'
279+
? '+0000'
280+
: tz.replace(/:/, '');
281+
else if (tz.substring(1) == '00:00')
282+
tz = 'Z';
283+
break;
284+
}
285+
this.timezone = tz;
286+
}
259287

260288
return true;
261289

0 commit comments

Comments
 (0)