Skip to content

Commit 9c5de29

Browse files
committed
Added timezone selection.
1 parent 03d141a commit 9c5de29

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

jquery-ui-timepicker-addon.js

+49-11
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ function Timepicker() {
3737
timeText: 'Time',
3838
hourText: 'Hour',
3939
minuteText: 'Minute',
40-
secondText: 'Second'
40+
secondText: 'Second',
41+
timezoneText: 'Time Zone'
4142
};
4243
this._defaults = { // Global defaults for all the datetime picker instances
4344
showButtonPanel: true,
4445
timeOnly: false,
4546
showHour: true,
4647
showMinute: true,
4748
showSecond: false,
49+
showTimezone: false,
4850
showTime: true,
4951
stepHour: 0.05,
5052
stepMinute: 0.05,
@@ -66,7 +68,11 @@ function Timepicker() {
6668
alwaysSetTime: true,
6769
separator: ' ',
6870
altFieldTimeOnly: true,
69-
showTimepicker: true
71+
showTimepicker: true,
72+
timezoneList: ["-1100", "-1000", "-0900", "-0800", "-0700", "-0600",
73+
"-0500", "-0400", "-0300", "-0200", "-0100", "+0000",
74+
"+0100", "+0200", "+0300", "+0400", "+0500", "+0600",
75+
"+0700", "+0800", "+0900", "+1000", "+1100", "+1200"]
7076
};
7177
$.extend(this._defaults, this.regional['']);
7278
}
@@ -79,9 +85,11 @@ $.extend(Timepicker.prototype, {
7985
hour_slider: null,
8086
minute_slider: null,
8187
second_slider: null,
88+
timezone_select: null,
8289
hour: 0,
8390
minute: 0,
8491
second: 0,
92+
timezone: '+0000',
8593
hourMinOriginal: null,
8694
minuteMinOriginal: null,
8795
secondMinOriginal: null,
@@ -92,6 +100,10 @@ $.extend(Timepicker.prototype, {
92100
formattedDate: '',
93101
formattedTime: '',
94102
formattedDateTime: '',
103+
timezoneList: ["-1100", "-1000", "-0900", "-0800", "-0700", "-0600",
104+
"-0500", "-0400", "-0300", "-0200", "-0100", "+0000",
105+
"+0100", "+0200", "+0300", "+0400", "+0500", "+0600",
106+
"+0700", "+0800", "+0900", "+1000", "+1100", "+1200"],
95107

96108
/* Override the default settings for all instances of the time picker.
97109
@param settings object - the new settings to use as defaults (anonymous object)
@@ -112,6 +124,7 @@ $.extend(Timepicker.prototype, {
112124
tp_inst.minute = tp_inst._defaults.minute;
113125
tp_inst.second = tp_inst._defaults.second;
114126
tp_inst.ampm = '';
127+
tp_inst.timezone = tp_inst._defaults.timezone;
115128
tp_inst.$input = $input;
116129

117130

@@ -185,6 +198,7 @@ $.extend(Timepicker.prototype, {
185198
.replace(/m{1,2}/ig, '(\\d?\\d)')
186199
.replace(/s{1,2}/ig, '(\\d?\\d)')
187200
.replace(/t{1,2}/ig, '(am|pm|a|p)?')
201+
.replace(/z{1}/ig, '((\\+|-)\\d\\d\\d\\d)?')
188202
.replace(/\s/g, '\\s?') + '$',
189203
order = this._getFormatPositions(),
190204
treg;
@@ -197,7 +211,7 @@ $.extend(Timepicker.prototype, {
197211
var dp_dateFormat = $.datepicker._get(this.inst, 'dateFormat');
198212
regstr = '.{' + dp_dateFormat.length + ',}' + this._defaults.separator + regstr;
199213
}
200-
214+
201215
treg = timeString.match(new RegExp(regstr, 'i'));
202216

203217
if (treg) {
@@ -216,6 +230,7 @@ $.extend(Timepicker.prototype, {
216230

217231
if (order.m !== -1) this.minute = Number(treg[order.m]);
218232
if (order.s !== -1) this.second = Number(treg[order.s]);
233+
if (order.z !== -1) this.timezone = treg[order.z];
219234

220235
return true;
221236

@@ -227,8 +242,8 @@ $.extend(Timepicker.prototype, {
227242
// figure out position of time elements.. cause js cant do named captures
228243
//########################################################################
229244
_getFormatPositions: function() {
230-
var finds = this._defaults.timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|t{1,2})/g),
231-
orders = { h: -1, m: -1, s: -1, t: -1 };
245+
var finds = this._defaults.timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|t{1,2}|z)/g),
246+
orders = { h: -1, m: -1, s: -1, t: -1, z: -1 };
232247

233248
if (finds)
234249
for (var i = 0; i < finds.length; i++)
@@ -328,6 +343,11 @@ $.extend(Timepicker.prototype, {
328343
'</dd>';
329344
} else html += '<dd class="ui_tpicker_second" id="ui_tpicker_second_' + dp_id + '"' +
330345
((o.showSecond) ? '' : noDisplay) + '></dd>';
346+
347+
html += '<dt class="ui_tpicker_timezone_label" id="ui_tpicker_timezone_label_' + dp_id + '"' +
348+
((o.showTimezone) ? '' : noDisplay) + '>' + o.timezoneText + '</dt>';
349+
html += '<dd class="ui_tpicker_timezone" id="ui_tpicker_timezone_' + dp_id + '"' +
350+
((o.showTimezone) ? '' : noDisplay) + '></dd>';
331351

332352
html += '</dl></div>';
333353
$tp = $(html);
@@ -379,6 +399,20 @@ $.extend(Timepicker.prototype, {
379399
tp_inst._onTimeChange();
380400
}
381401
});
402+
403+
404+
this.timezone_select = $tp.find('#ui_tpicker_timezone_'+ dp_id).append('<select></select>').find("select");
405+
$.fn.append.apply(this.timezone_select,
406+
$.map(o.timezoneList, function(val, idx) {
407+
return $("<option />")
408+
.val(typeof val == "object" ? val.value : val)
409+
.text(typeof val == "object" ? val.label : val);
410+
})
411+
);
412+
this.timezone_select.val(this.timezone);
413+
this.timezone_select.change(function() {
414+
tp_inst._onTimeChange();
415+
});
382416

383417
// Add grid functionality
384418
if (o.showHour && o.hourGrid > 0) {
@@ -548,8 +582,9 @@ $.extend(Timepicker.prototype, {
548582
_onTimeChange: function() {
549583
var hour = (this.hour_slider) ? this.hour_slider.slider('value') : false,
550584
minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
551-
second = (this.second_slider) ? this.second_slider.slider('value') : false;
552-
585+
second = (this.second_slider) ? this.second_slider.slider('value') : false,
586+
timezone = (this.timezone_select) ? this.timezone_select.val() : false
587+
553588
if (hour !== false) hour = parseInt(hour,10);
554589
if (minute !== false) minute = parseInt(minute,10);
555590
if (second !== false) second = parseInt(second,10);
@@ -558,13 +593,14 @@ $.extend(Timepicker.prototype, {
558593

559594
// If the update was done in the input field, the input field should not be updated.
560595
// If the update was done using the sliders, update the input field.
561-
var hasChanged = (hour != this.hour || minute != this.minute || second != this.second || (this.ampm.length > 0 && this.ampm != ampm));
596+
var hasChanged = (hour != this.hour || minute != this.minute || second != this.second || (this.ampm.length > 0 && this.ampm != ampm) || timezone != this.timezone);
562597

563598
if (hasChanged) {
564599

565600
if (hour !== false)this.hour = hour;
566601
if (minute !== false) this.minute = minute;
567602
if (second !== false) this.second = second;
603+
if (timezone !== false) this.timezone = timezone;
568604
}
569605
if (this._defaults.ampm) this.ampm = ampm;
570606

@@ -579,7 +615,7 @@ $.extend(Timepicker.prototype, {
579615
//########################################################################
580616
_formatTime: function(time, format, ampm) {
581617
if (ampm == undefined) ampm = this._defaults.ampm;
582-
time = time || { hour: this.hour, minute: this.minute, second: this.second, ampm: this.ampm };
618+
time = time || { hour: this.hour, minute: this.minute, second: this.second, ampm: this.ampm, timezone: this.timezone };
583619
var tmptime = format || this._defaults.timeFormat.toString();
584620

585621
if (ampm) {
@@ -595,15 +631,17 @@ $.extend(Timepicker.prototype, {
595631
.replace(/TT/g, time.ampm.toUpperCase())
596632
.replace(/tt/g, time.ampm.toLowerCase())
597633
.replace(/T/g, time.ampm.charAt(0).toUpperCase())
598-
.replace(/t/g, time.ampm.charAt(0).toLowerCase());
634+
.replace(/t/g, time.ampm.charAt(0).toLowerCase())
635+
.replace(/z/g, time.timezone);
599636
} else {
600637
tmptime = tmptime.toString()
601638
.replace(/hh/g, ((time.hour < 10) ? '0' : '') + time.hour)
602639
.replace(/h/g, time.hour)
603640
.replace(/mm/g, ((time.minute < 10) ? '0' : '') + time.minute)
604641
.replace(/m/g, time.minute)
605642
.replace(/ss/g, ((time.second < 10) ? '0' : '') + time.second)
606-
.replace(/s/g, time.second);
643+
.replace(/s/g, time.second)
644+
.replace(/z/g, time.timezone);
607645
tmptime = $.trim(tmptime.replace(/t/gi, ''));
608646
}
609647

0 commit comments

Comments
 (0)