Skip to content

Support localized AM/PM markers #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 19, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 69 additions & 41 deletions jquery-ui-timepicker-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function Timepicker() {
currentText: 'Now',
closeText: 'Done',
ampm: false,
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
timeFormat: 'hh:mm tt',
timeSuffix: '',
timeOnlyTitle: 'Choose Time',
Expand Down Expand Up @@ -158,6 +160,8 @@ $.extend(Timepicker.prototype, {
},
timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
});
tp_inst.amNames = $.map(tp_inst._defaults.amNames, function(val) { return val.toUpperCase() });
tp_inst.pmNames = $.map(tp_inst._defaults.pmNames, function(val) { return val.toUpperCase() });

if (tp_inst._defaults.timezoneList === null) {
var timezoneList = [];
Expand Down Expand Up @@ -225,10 +229,11 @@ $.extend(Timepicker.prototype, {
.replace(/m{1,2}/ig, '(\\d?\\d)')
.replace(/s{1,2}/ig, '(\\d?\\d)')
.replace(/l{1}/ig, '(\\d?\\d?\\d)')
.replace(/t{1,2}/ig, '(am|pm|a|p)?')
.replace(/t{1,2}/ig, this._getPatternAmpm())
.replace(/z{1}/ig, '(z|[-+]\\d\\d:?\\d\\d)?')
.replace(/\s/g, '\\s?') + this._defaults.timeSuffix + '$',
order = this._getFormatPositions(),
ampm = '',
treg;

if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);
Expand All @@ -245,15 +250,20 @@ $.extend(Timepicker.prototype, {
treg = timeString.match(new RegExp(regstr, 'i'));

if (treg) {
if (order.t !== -1)
this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ?
'' :
(treg[order.t].charAt(0).toUpperCase() == 'A') ? 'AM' : 'PM').toUpperCase();
if (order.t !== -1) {
if (treg[order.t] === undefined || treg[order.t].length === 0) {
ampm = '';
this.ampm = '';
} else {
ampm = $.inArray(treg[order.t].toUpperCase(), this.amNames) !== -1 ? 'AM' : 'PM';
this.ampm = this._defaults[ampm == 'AM' ? 'amNames' : 'pmNames'][0];
}
}

if (order.h !== -1) {
if (this.ampm == 'AM' && treg[order.h] == '12')
if (ampm == 'AM' && treg[order.h] == '12')
this.hour = 0; // 12am = 0 hour
else if (this.ampm == 'PM' && treg[order.h] != '12')
else if (ampm == 'PM' && treg[order.h] != '12')
this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0); // 12pm = 12 hour, any other pm = hour + 12
else this.hour = Number(treg[order.h]);
}
Expand Down Expand Up @@ -291,6 +301,20 @@ $.extend(Timepicker.prototype, {
return false;
},

//########################################################################
// pattern for standard and localized AM/PM markers
//########################################################################
_getPatternAmpm: function() {
var markers = [];
o = this._defaults;
if (o.amNames)
$.merge(markers, o.amNames);
if (o.pmNames)
$.merge(markers, o.pmNames);
markers = $.map(markers, function(val) { return val.replace(/[.*+?|()\[\]{}\\]/g, '\\$&') });
return '(' + markers.join('|') + ')?';
},

//########################################################################
// figure out position of time elements.. cause js cant do named captures
//########################################################################
Expand Down Expand Up @@ -734,7 +758,8 @@ $.extend(Timepicker.prototype, {
minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
second = (this.second_slider) ? this.second_slider.slider('value') : false,
millisec = (this.millisec_slider) ? this.millisec_slider.slider('value') : false,
timezone = (this.timezone_select) ? this.timezone_select.val() : false;
timezone = (this.timezone_select) ? this.timezone_select.val() : false,
o = this._defaults;

if (typeof(hour) == 'object') hour = false;
if (typeof(minute) == 'object') minute = false;
Expand All @@ -747,11 +772,15 @@ $.extend(Timepicker.prototype, {
if (second !== false) second = parseInt(second,10);
if (millisec !== false) millisec = parseInt(millisec,10);

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

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

if (hasChanged) {

Expand All @@ -765,10 +794,10 @@ $.extend(Timepicker.prototype, {

this._limitMinMaxDateTime(this.inst, true);
}
if (this._defaults.ampm) this.ampm = ampm;
if (o.ampm) this.ampm = ampm;

this._formatTime();
if (this.$timeObj) this.$timeObj.text(this.formattedTime + this._defaults.timeSuffix);
if (this.$timeObj) this.$timeObj.text(this.formattedTime + o.timeSuffix);
this.timeDefined = true;
if (hasChanged) this._updateDateTime();
},
Expand All @@ -791,38 +820,35 @@ $.extend(Timepicker.prototype, {
_formatTime: function(time, format, ampm) {
if (ampm == undefined) ampm = this._defaults.ampm;
time = time || { hour: this.hour, minute: this.minute, second: this.second, millisec: this.millisec, ampm: this.ampm, timezone: this.timezone };
var tmptime = format || this._defaults.timeFormat.toString();
var tmptime = (format || this._defaults.timeFormat).toString();

var hour = parseInt(time.hour, 10);
if (ampm) {
var hour12 = ((time.ampm == 'AM') ? (time.hour) : (time.hour % 12));
hour12 = (Number(hour12) === 0) ? 12 : hour12;
tmptime = tmptime.toString()
.replace(/hh/g, ((hour12 < 10) ? '0' : '') + hour12)
.replace(/h/g, hour12)
.replace(/mm/g, ((time.minute < 10) ? '0' : '') + time.minute)
.replace(/m/g, time.minute)
.replace(/ss/g, ((time.second < 10) ? '0' : '') + time.second)
.replace(/s/g, time.second)
.replace(/l/g, ((time.millisec < 10) ? '00' : (time.millisec < 100) ? '0': '') + time.millisec)
.replace(/TT/g, time.ampm.toUpperCase())
.replace(/Tt/g, time.ampm.toUpperCase())
.replace(/tT/g, time.ampm.toLowerCase())
.replace(/tt/g, time.ampm.toLowerCase())
.replace(/T/g, time.ampm.charAt(0).toUpperCase())
.replace(/t/g, time.ampm.charAt(0).toLowerCase())
.replace(/z/g, time.timezone);
} else {
tmptime = tmptime.toString()
.replace(/hh/g, ((time.hour < 10) ? '0' : '') + time.hour)
.replace(/h/g, time.hour)
.replace(/mm/g, ((time.minute < 10) ? '0' : '') + time.minute)
.replace(/m/g, time.minute)
.replace(/ss/g, ((time.second < 10) ? '0' : '') + time.second)
.replace(/s/g, time.second)
.replace(/l/g, ((time.millisec < 10) ? '00' : (time.millisec < 100) ? '0': '') + time.millisec)
.replace(/z/g, time.timezone);
tmptime = $.trim(tmptime.replace(/t/gi, ''));
if (!$.inArray(time.ampm.toUpperCase(), this.amNames) !== -1)
hour = hour % 12;
if (hour === 0)
hour = 12;
}
tmptime = tmptime.replace(/(?:hh?|mm?|ss?|[tT]{1,2}|[lz])/g, function(match) {
switch (match.toLowerCase()) {
case 'hh': return ('0' + hour).slice(-2);
case 'h': return hour;
case 'mm': return ('0' + time.minute).slice(-2);
case 'm': return time.minute;
case 'ss': return ('0' + time.second).slice(-2);
case 's': return time.second;
case 'l': return ('00' + time.millisec).slice(-3);
case 'z': return time.timezone;
case 't': case 'tt':
if (ampm) {
var _ampm = time.ampm;
if (match.length == 1)
_ampm = _ampm.charAt(0);
return match.charAt(0) == 'T' ? _ampm.toUpperCase() : _ampm.toLowerCase();
}
return '';
}
});

if (arguments.length) return tmptime;
else this.formattedTime = tmptime;
Expand Down Expand Up @@ -978,6 +1004,8 @@ $.datepicker._doKeyPress = function(event) {
tp_inst._defaults.separator +
tp_inst._defaults.timeSuffix +
(tp_inst._defaults.showTimezone ? tp_inst._defaults.timezoneList.join('') : '') +
(tp_inst._defaults.amNames.join('')) +
(tp_inst._defaults.pmNames.join('')) +
dateChars,
chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode);
return event.ctrlKey || (chr < ' ' || !dateChars || datetimeChars.indexOf(chr) > -1);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-ca.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Ara',
closeText: 'Tancar',
timeFormat: 'hh:mm',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['ca']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-cs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Nyní',
closeText: 'Zavřít',
timeFormat: 'h:m',
amNames: ['dop.', 'AM', 'A'],
pmNames: ['odp.', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['cs']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-de.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Jetzt',
closeText: 'Fertig',
timeFormat: 'hh:mm tt',
amNames: ['vorm.', 'AM', 'A'],
pmNames: ['nachm.', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['de']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-el.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Τώρα',
closeText: 'Κλείσιμο',
timeFormat: 'hh:mm',
amNames: ['π.μ.', 'AM', 'A'],
pmNames: ['μ.μ.', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['el']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-es.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Ahora',
closeText: 'Cerrar',
timeFormat: 'hh:mm',
amNames: ['a.m.', 'AM', 'A'],
pmNames: ['p.m.', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['es']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-et.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Praegu',
closeText: 'Valmis',
timeFormat: 'hh:mm tt',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['et']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-fi.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Nyt',
closeText: 'Sulje',
timeFormat: 'hh:mm',
amNames: ['ap.', 'AM', 'A'],
pmNames: ['ip.', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['fi']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Maintenant',
closeText: 'Terminé',
timeFormat: 'hh:mm',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['fr']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-he.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: "עכשיו",
closeText:"סגור",
timeFormat: "hh:mm tt",
amNames: ['לפנה"צ', 'AM', 'A'],
pmNames: ['אחה"צ', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional["he"]);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-hu.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Most',
closeText: 'Kész',
timeFormat: 'hh:mm tt',
amNames: ['de.', 'AM', 'A'],
pmNames: ['du.', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['hu']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Sekarang',
closeText: 'OK',
timeFormat: 'hh:mm tt',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['id']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-it.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Adesso',
closeText: 'Chiudi',
timeFormat: 'hh:mm',
amNames: ['m.', 'AM', 'A'],
pmNames: ['p.', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['it']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-ja.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: '現時刻',
closeText: '閉じる',
timeFormat: 'hh:mm tt',
amNames: ['午前', 'AM', 'A'],
pmNames: ['午後', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['ja']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-lt.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Dabar',
closeText: 'Uždaryti',
timeFormat: 'hh:mm',
amNames: ['priešpiet', 'AM', 'A'],
pmNames: ['popiet', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['lt']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Vandaag',
closeText: 'Sluiten',
timeFormat: 'hh:mm tt',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['nl']);
Expand Down
4 changes: 3 additions & 1 deletion localization/jquery-ui-timepicker-pl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* German translation for the jQuery Timepicker Addon */
/* Polish translation for the jQuery Timepicker Addon */
/* Written by Michał Pena */
(function($) {
$.timepicker.regional['pl'] = {
Expand All @@ -12,6 +12,8 @@
currentText: 'Teraz',
closeText: 'Gotowe',
timeFormat: 'hh:mm tt',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['pl']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-ro.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Acum',
closeText: 'Închide',
timeFormat: 'hh:mm',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['ro']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-ru.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Теперь',
closeText: 'Закрыть',
timeFormat: 'hh:mm tt',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['ru']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-tr.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Şu an',
closeText: 'Tamam',
timeFormat: 'hh:mm',
amNames: ['AM', 'A'],
pmNames: ['PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['tr']);
Expand Down
2 changes: 2 additions & 0 deletions localization/jquery-ui-timepicker-vi.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
currentText: 'Hiện thời',
closeText: 'Đóng'
timeFormat: 'h:m',
amNames: ['SA', 'AM', 'A'],
pmNames: ['CH', 'PM', 'P'],
ampm: false
};
$.timepicker.setDefaults($.timepicker.regional['vi']);
Expand Down