Skip to content

Commit cb98a45

Browse files
committed
Support localized AM/PM markers.
1 parent 04efa58 commit cb98a45

19 files changed

+103
-41
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ function Timepicker() {
3232
currentText: 'Now',
3333
closeText: 'Done',
3434
ampm: false,
35+
amNames: ['AM', 'A'],
36+
pmNames: ['PM', 'P'],
3537
timeFormat: 'hh:mm tt',
3638
timeSuffix: '',
3739
timeOnlyTitle: 'Choose Time',
@@ -163,6 +165,8 @@ $.extend(Timepicker.prototype, {
163165
},
164166
timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
165167
});
168+
tp_inst.amNames = $.map(tp_inst._defaults.amNames, function(val) { return val.toUpperCase() });
169+
tp_inst.pmNames = $.map(tp_inst._defaults.pmNames, function(val) { return val.toUpperCase() });
166170

167171
tp_inst.hour = tp_inst._defaults.hour;
168172
tp_inst.minute = tp_inst._defaults.minute;
@@ -219,10 +223,11 @@ $.extend(Timepicker.prototype, {
219223
.replace(/m{1,2}/ig, '(\\d?\\d)')
220224
.replace(/s{1,2}/ig, '(\\d?\\d)')
221225
.replace(/l{1}/ig, '(\\d?\\d?\\d)')
222-
.replace(/t{1,2}/ig, '(am|pm|a|p)?')
226+
.replace(/t{1,2}/ig, this._getPatternAmpm())
223227
.replace(/z{1}/ig, '((\\+|-)\\d\\d\\d\\d)?')
224228
.replace(/\s/g, '\\s?') + this._defaults.timeSuffix + '$',
225229
order = this._getFormatPositions(),
230+
ampm = '',
226231
treg;
227232

228233
if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);
@@ -239,15 +244,20 @@ $.extend(Timepicker.prototype, {
239244
treg = timeString.match(new RegExp(regstr, 'i'));
240245

241246
if (treg) {
242-
if (order.t !== -1)
243-
this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ?
244-
'' :
245-
(treg[order.t].charAt(0).toUpperCase() == 'A') ? 'AM' : 'PM').toUpperCase();
247+
if (order.t !== -1) {
248+
if (treg[order.t] === undefined || treg[order.t].length === 0) {
249+
ampm = '';
250+
this.ampm = '';
251+
} else {
252+
ampm = $.inArray(treg[order.t].toUpperCase(), this.amNames) !== -1 ? 'AM' : 'PM';
253+
this.ampm = this._defaults[ampm == 'AM' ? 'amNames' : 'pmNames'][0];
254+
}
255+
}
246256

247257
if (order.h !== -1) {
248-
if (this.ampm == 'AM' && treg[order.h] == '12')
258+
if (ampm == 'AM' && treg[order.h] == '12')
249259
this.hour = 0; // 12am = 0 hour
250-
else if (this.ampm == 'PM' && treg[order.h] != '12')
260+
else if (ampm == 'PM' && treg[order.h] != '12')
251261
this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0); // 12pm = 12 hour, any other pm = hour + 12
252262
else this.hour = Number(treg[order.h]);
253263
}
@@ -263,6 +273,20 @@ $.extend(Timepicker.prototype, {
263273
return false;
264274
},
265275

276+
//########################################################################
277+
// pattern for standard and localized AM/PM markers
278+
//########################################################################
279+
_getPatternAmpm: function() {
280+
var markers = [];
281+
o = this._defaults;
282+
if (o.amNames)
283+
$.merge(markers, o.amNames);
284+
if (o.pmNames)
285+
$.merge(markers, o.pmNames);
286+
markers = $.map(markers, function(val) { return val.replace(/[.*+?|()\[\]{}\\]/g, '\\$&') });
287+
return '(' + markers.join('|') + ')?';
288+
},
289+
266290
//########################################################################
267291
// figure out position of time elements.. cause js cant do named captures
268292
//########################################################################
@@ -706,7 +730,8 @@ $.extend(Timepicker.prototype, {
706730
minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
707731
second = (this.second_slider) ? this.second_slider.slider('value') : false,
708732
millisec = (this.millisec_slider) ? this.millisec_slider.slider('value') : false,
709-
timezone = (this.timezone_select) ? this.timezone_select.val() : false;
733+
timezone = (this.timezone_select) ? this.timezone_select.val() : false,
734+
o = this._defaults;
710735

711736
if (typeof(hour) == 'object') hour = false;
712737
if (typeof(minute) == 'object') minute = false;
@@ -719,11 +744,15 @@ $.extend(Timepicker.prototype, {
719744
if (second !== false) second = parseInt(second,10);
720745
if (millisec !== false) millisec = parseInt(millisec,10);
721746

722-
var ampm = (hour < 12) ? 'AM' : 'PM';
747+
var ampm = o[hour < 12 ? 'amNames' : 'pmNames'][0];
723748

724749
// If the update was done in the input field, the input field should not be updated.
725750
// If the update was done using the sliders, update the input field.
726-
var hasChanged = (hour != this.hour || minute != this.minute || second != this.second || millisec != this.millisec || (this.ampm.length > 0 && this.ampm != ampm) || timezone != this.timezone);
751+
var hasChanged = (hour != this.hour || minute != this.minute
752+
|| second != this.second || millisec != this.millisec
753+
|| (this.ampm.length > 0
754+
&& (hour < 12) != ($.inArray(this.ampm.toUpperCase(), this.amNames) !== -1))
755+
|| timezone != this.timezone);
727756

728757
if (hasChanged) {
729758

@@ -737,10 +766,10 @@ $.extend(Timepicker.prototype, {
737766

738767
this._limitMinMaxDateTime(this.inst, true);
739768
}
740-
if (this._defaults.ampm) this.ampm = ampm;
769+
if (o.ampm) this.ampm = ampm;
741770

742771
this._formatTime();
743-
if (this.$timeObj) this.$timeObj.text(this.formattedTime + this._defaults.timeSuffix);
772+
if (this.$timeObj) this.$timeObj.text(this.formattedTime + o.timeSuffix);
744773
this.timeDefined = true;
745774
if (hasChanged) this._updateDateTime();
746775
},
@@ -763,38 +792,35 @@ $.extend(Timepicker.prototype, {
763792
_formatTime: function(time, format, ampm) {
764793
if (ampm == undefined) ampm = this._defaults.ampm;
765794
time = time || { hour: this.hour, minute: this.minute, second: this.second, millisec: this.millisec, ampm: this.ampm, timezone: this.timezone };
766-
var tmptime = format || this._defaults.timeFormat.toString();
795+
var tmptime = (format || this._defaults.timeFormat).toString();
767796

797+
var hour = parseInt(time.hour, 10);
768798
if (ampm) {
769-
var hour12 = ((time.ampm == 'AM') ? (time.hour) : (time.hour % 12));
770-
hour12 = (Number(hour12) === 0) ? 12 : hour12;
771-
tmptime = tmptime.toString()
772-
.replace(/hh/g, ((hour12 < 10) ? '0' : '') + hour12)
773-
.replace(/h/g, hour12)
774-
.replace(/mm/g, ((time.minute < 10) ? '0' : '') + time.minute)
775-
.replace(/m/g, time.minute)
776-
.replace(/ss/g, ((time.second < 10) ? '0' : '') + time.second)
777-
.replace(/s/g, time.second)
778-
.replace(/l/g, ((time.millisec < 10) ? '00' : (time.millisec < 100) ? '0': '') + time.millisec)
779-
.replace(/TT/g, time.ampm.toUpperCase())
780-
.replace(/Tt/g, time.ampm.toUpperCase())
781-
.replace(/tT/g, time.ampm.toLowerCase())
782-
.replace(/tt/g, time.ampm.toLowerCase())
783-
.replace(/T/g, time.ampm.charAt(0).toUpperCase())
784-
.replace(/t/g, time.ampm.charAt(0).toLowerCase())
785-
.replace(/z/g, time.timezone);
786-
} else {
787-
tmptime = tmptime.toString()
788-
.replace(/hh/g, ((time.hour < 10) ? '0' : '') + time.hour)
789-
.replace(/h/g, time.hour)
790-
.replace(/mm/g, ((time.minute < 10) ? '0' : '') + time.minute)
791-
.replace(/m/g, time.minute)
792-
.replace(/ss/g, ((time.second < 10) ? '0' : '') + time.second)
793-
.replace(/s/g, time.second)
794-
.replace(/l/g, ((time.millisec < 10) ? '00' : (time.millisec < 100) ? '0': '') + time.millisec)
795-
.replace(/z/g, time.timezone);
796-
tmptime = $.trim(tmptime.replace(/t/gi, ''));
799+
if (!$.inArray(time.ampm.toUpperCase(), this.amNames) !== -1)
800+
hour = hour % 12;
801+
if (hour === 0)
802+
hour = 12;
797803
}
804+
tmptime = tmptime.replace(/(?:hh?|mm?|ss?|[tT]{1,2}|[lz])/g, function(match) {
805+
switch (match.toLowerCase()) {
806+
case 'hh': return ('0' + hour).slice(-2);
807+
case 'h': return hour;
808+
case 'mm': return ('0' + time.minute).slice(-2);
809+
case 'm': return time.minute;
810+
case 'ss': return ('0' + time.second).slice(-2);
811+
case 's': return time.second;
812+
case 'l': return ('00' + time.millisec).slice(-3);
813+
case 'z': return time.timezone;
814+
case 't': case 'tt':
815+
if (ampm) {
816+
var _ampm = time.ampm;
817+
if (match.length == 1)
818+
_ampm = _ampm.charAt(0);
819+
return match.charAt(0) == 'T' ? _ampm.toUpperCase() : _ampm.toLowerCase();
820+
}
821+
return '';
822+
}
823+
});
798824

799825
if (arguments.length) return tmptime;
800826
else this.formattedTime = tmptime;

localization/jquery-ui-timepicker-ca.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Ara',
1313
closeText: 'Tancar',
1414
timeFormat: 'hh:mm',
15+
amNames: ['AM', 'A'],
16+
pmNames: ['PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['ca']);

localization/jquery-ui-timepicker-cs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Nyní',
1313
closeText: 'Zavřít',
1414
timeFormat: 'h:m',
15+
amNames: ['dop.', 'AM', 'A'],
16+
pmNames: ['odp.', 'PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['cs']);

localization/jquery-ui-timepicker-de.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Jetzt',
1313
closeText: 'Fertig',
1414
timeFormat: 'hh:mm tt',
15+
amNames: ['vorm.', 'AM', 'A'],
16+
pmNames: ['nachm.', 'PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['de']);

localization/jquery-ui-timepicker-el.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Τώρα',
1313
closeText: 'Κλείσιμο',
1414
timeFormat: 'hh:mm',
15+
amNames: ['π.μ.', 'AM', 'A'],
16+
pmNames: ['μ.μ.', 'PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['el']);

localization/jquery-ui-timepicker-es.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Ahora',
1313
closeText: 'Cerrar',
1414
timeFormat: 'hh:mm',
15+
amNames: ['a.m.', 'AM', 'A'],
16+
pmNames: ['p.m.', 'PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['es']);

localization/jquery-ui-timepicker-et.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Praegu',
1313
closeText: 'Valmis',
1414
timeFormat: 'hh:mm tt',
15+
amNames: ['AM', 'A'],
16+
pmNames: ['PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['et']);

localization/jquery-ui-timepicker-fr.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Maintenant',
1313
closeText: 'Terminé',
1414
timeFormat: 'hh:mm',
15+
amNames: ['AM', 'A'],
16+
pmNames: ['PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['fr']);

localization/jquery-ui-timepicker-he.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: "עכשיו",
1313
closeText:"סגור",
1414
timeFormat: "hh:mm tt",
15+
amNames: ['לפנה"צ', AM', 'A'],
16+
pmNames: ['אחה"צ', PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional["he"]);

localization/jquery-ui-timepicker-hu.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Most',
1313
closeText: 'Kész',
1414
timeFormat: 'hh:mm tt',
15+
amNames: ['de.', 'AM', 'A'],
16+
pmNames: ['du.', 'PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['hu']);

localization/jquery-ui-timepicker-id.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Sekarang',
1313
closeText: 'OK',
1414
timeFormat: 'hh:mm tt',
15+
amNames: ['AM', 'A'],
16+
pmNames: ['PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['id']);

localization/jquery-ui-timepicker-it.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Adesso',
1313
closeText: 'Chiudi',
1414
timeFormat: 'hh:mm',
15+
amNames: ['m.', 'AM', 'A'],
16+
pmNames: ['p.', 'PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['it']);

localization/jquery-ui-timepicker-ja.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: '現時刻',
1313
closeText: '閉じる',
1414
timeFormat: 'hh:mm tt',
15+
amNames: ['午前', 'AM', 'A'],
16+
pmNames: ['午後', 'PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['ja']);

localization/jquery-ui-timepicker-lt.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Dabar',
1313
closeText: 'Uždaryti',
1414
timeFormat: 'hh:mm',
15+
amNames: ['priešpiet', 'AM', 'A'],
16+
pmNames: ['popiet', 'PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['lt']);

localization/jquery-ui-timepicker-nl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Vandaag',
1313
closeText: 'Sluiten',
1414
timeFormat: 'hh:mm tt',
15+
amNames: ['AM', 'A'],
16+
pmNames: ['PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['nl']);

localization/jquery-ui-timepicker-ro.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Acum',
1313
closeText: 'Închide',
1414
timeFormat: 'hh:mm',
15+
amNames: ['AM', 'A'],
16+
pmNames: ['PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['ro']);

localization/jquery-ui-timepicker-ru.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Теперь',
1313
closeText: 'Закрыть',
1414
timeFormat: 'hh:mm tt',
15+
amNames: ['AM', 'A'],
16+
pmNames: ['PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['ru']);

localization/jquery-ui-timepicker-tr.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Şu an',
1313
closeText: 'Tamam',
1414
timeFormat: 'hh:mm',
15+
amNames: ['AM', 'A'],
16+
pmNames: ['PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['tr']);

localization/jquery-ui-timepicker-vi.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
currentText: 'Hiện thời',
1313
closeText: 'Đóng'
1414
timeFormat: 'h:m',
15+
amNames: ['SA', 'AM', 'A'],
16+
pmNames: ['CH', 'PM', 'P'],
1517
ampm: false
1618
};
1719
$.timepicker.setDefaults($.timepicker.regional['vi']);

0 commit comments

Comments
 (0)