summaryrefslogtreecommitdiffstats
path: root/jquery-ui-timepicker-addon.js
diff options
context:
space:
mode:
authorTrent Richardson <trentdrichardson@gmail.com>2012-10-25 14:55:52 -0400
committerTrent Richardson <trentdrichardson@gmail.com>2012-10-25 14:55:52 -0400
commitb7aa216515b0bc4200daa6cdac16c86163e6990e (patch)
treedac3981b055decf414027b4e5f48ff2949c65a23 /jquery-ui-timepicker-addon.js
parent465c1ab37613beeca89d68a9bd11e58528d30c91 (diff)
downloadjQuery-Timepicker-Addon-b7aa216515b0bc4200daa6cdac16c86163e6990e.zip
jQuery-Timepicker-Addon-b7aa216515b0bc4200daa6cdac16c86163e6990e.tar.gz
jQuery-Timepicker-Addon-b7aa216515b0bc4200daa6cdac16c86163e6990e.tar.bz2
Strict time parsing
Diffstat (limited to 'jquery-ui-timepicker-addon.js')
-rw-r--r--jquery-ui-timepicker-addon.js191
1 files changed, 110 insertions, 81 deletions
diff --git a/jquery-ui-timepicker-addon.js b/jquery-ui-timepicker-addon.js
index 89dfd5d..9ea06a9 100644
--- a/jquery-ui-timepicker-addon.js
+++ b/jquery-ui-timepicker-addon.js
@@ -2,7 +2,7 @@
* jQuery timepicker addon
* By: Trent Richardson [http://trentrichardson.com]
* Version 1.1.0-dev
- * Last Modified: 10/09/2012
+ * Last Modified: 10/25/2012
*
* Copyright 2012 Trent Richardson
* You may use this project under MIT or GPL licenses.
@@ -104,7 +104,8 @@
addSliderAccess: false,
sliderAccessArgs: null,
controlType: 'slider',
- defaultValue: null
+ defaultValue: null,
+ strict: true
};
$.extend(this._defaults, this.regional['']);
}
@@ -1023,98 +1024,126 @@
var o = extendRemove(extendRemove({}, $.timepicker._defaults), options || {});
- var regstr = '^' + timeFormat.toString()
- .replace(/([hH]{1,2}|mm?|ss?|[tT]{1,2}|[lz]|'.*?')/g, function (match) {
- switch (match.charAt(0).toLowerCase()) {
- case 'h': return '(\\d?\\d)';
- case 'm': return '(\\d?\\d)';
- case 's': return '(\\d?\\d)';
- case 'l': return '(\\d?\\d?\\d)';
- case 'z': return '(z|[-+]\\d\\d:?\\d\\d|\\S+)?';
- case 't': return getPatternAmpm(o.amNames, o.pmNames);
- default: // literal escaped in quotes
- return '(' + match.replace(/\'/g, "").replace(/(\.|\$|\^|\\|\/|\(|\)|\[|\]|\?|\+|\*)/g, function (m) { return "\\" + m; }) + ')?';
- }
- })
- .replace(/\s/g, '\\s?') +
- o.timeSuffix + '$',
- order = getFormatPositions(timeFormat),
- ampm = '',
- treg;
-
- treg = timeString.match(new RegExp(regstr, 'i'));
-
- var resTime = {
- hour: 0,
- minute: 0,
- second: 0,
- millisec: 0
- };
+ var strictParse = function(f, s, o){
+ var regstr = '^' + f.toString()
+ .replace(/([hH]{1,2}|mm?|ss?|[tT]{1,2}|[lz]|'.*?')/g, function (match) {
+ switch (match.charAt(0).toLowerCase()) {
+ case 'h': return '(\\d?\\d)';
+ case 'm': return '(\\d?\\d)';
+ case 's': return '(\\d?\\d)';
+ case 'l': return '(\\d?\\d?\\d)';
+ case 'z': return '(z|[-+]\\d\\d:?\\d\\d|\\S+)?';
+ case 't': return getPatternAmpm(o.amNames, o.pmNames);
+ default: // literal escaped in quotes
+ return '(' + match.replace(/\'/g, "").replace(/(\.|\$|\^|\\|\/|\(|\)|\[|\]|\?|\+|\*)/g, function (m) { return "\\" + m; }) + ')?';
+ }
+ })
+ .replace(/\s/g, '\\s?') +
+ o.timeSuffix + '$',
+ order = getFormatPositions(f),
+ ampm = '',
+ treg;
+
+ treg = s.match(new RegExp(regstr, 'i'));
+
+ var resTime = {
+ hour: 0,
+ minute: 0,
+ second: 0,
+ millisec: 0
+ };
- if (treg) {
- if (order.t !== -1) {
- if (treg[order.t] === undefined || treg[order.t].length === 0) {
- ampm = '';
- resTime.ampm = '';
- } else {
- ampm = $.inArray(treg[order.t].toUpperCase(), o.amNames) !== -1 ? 'AM' : 'PM';
- resTime.ampm = o[ampm == 'AM' ? 'amNames' : 'pmNames'][0];
+ if (treg) {
+ if (order.t !== -1) {
+ if (treg[order.t] === undefined || treg[order.t].length === 0) {
+ ampm = '';
+ resTime.ampm = '';
+ } else {
+ ampm = $.inArray(treg[order.t].toUpperCase(), o.amNames) !== -1 ? 'AM' : 'PM';
+ resTime.ampm = o[ampm == 'AM' ? 'amNames' : 'pmNames'][0];
+ }
}
- }
- if (order.h !== -1) {
- if (ampm == 'AM' && treg[order.h] == '12') {
- resTime.hour = 0; // 12am = 0 hour
- } else {
- if (ampm == 'PM' && treg[order.h] != '12') {
- resTime.hour = parseInt(treg[order.h], 10) + 12; // 12pm = 12 hour, any other pm = hour + 12
+ if (order.h !== -1) {
+ if (ampm == 'AM' && treg[order.h] == '12') {
+ resTime.hour = 0; // 12am = 0 hour
} else {
- resTime.hour = Number(treg[order.h]);
+ if (ampm == 'PM' && treg[order.h] != '12') {
+ resTime.hour = parseInt(treg[order.h], 10) + 12; // 12pm = 12 hour, any other pm = hour + 12
+ } else {
+ resTime.hour = Number(treg[order.h]);
+ }
}
}
- }
- if (order.m !== -1) {
- resTime.minute = Number(treg[order.m]);
- }
- if (order.s !== -1) {
- resTime.second = Number(treg[order.s]);
- }
- if (order.l !== -1) {
- resTime.millisec = Number(treg[order.l]);
- }
- if (order.z !== -1 && treg[order.z] !== undefined) {
- var tz = treg[order.z].toUpperCase();
- switch (tz.length) {
- case 1:
- // Z
- tz = o.timezoneIso8601 ? 'Z' : '+0000';
- break;
- case 5:
- // +hhmm
- if (o.timezoneIso8601) {
- tz = tz.substring(1) == '0000' ? 'Z' : tz.substring(0, 3) + ':' + tz.substring(3);
- }
- break;
- case 6:
- // +hh:mm
- if (!o.timezoneIso8601) {
- tz = tz == 'Z' || tz.substring(1) == '00:00' ? '+0000' : tz.replace(/:/, '');
- } else {
- if (tz.substring(1) == '00:00') {
- tz = 'Z';
+ if (order.m !== -1) {
+ resTime.minute = Number(treg[order.m]);
+ }
+ if (order.s !== -1) {
+ resTime.second = Number(treg[order.s]);
+ }
+ if (order.l !== -1) {
+ resTime.millisec = Number(treg[order.l]);
+ }
+ if (order.z !== -1 && treg[order.z] !== undefined) {
+ var tz = treg[order.z].toUpperCase();
+ switch (tz.length) {
+ case 1:
+ // Z
+ tz = o.timezoneIso8601 ? 'Z' : '+0000';
+ break;
+ case 5:
+ // +hhmm
+ if (o.timezoneIso8601) {
+ tz = tz.substring(1) == '0000' ? 'Z' : tz.substring(0, 3) + ':' + tz.substring(3);
+ }
+ break;
+ case 6:
+ // +hh:mm
+ if (!o.timezoneIso8601) {
+ tz = tz == 'Z' || tz.substring(1) == '00:00' ? '+0000' : tz.replace(/:/, '');
+ } else {
+ if (tz.substring(1) == '00:00') {
+ tz = 'Z';
+ }
}
+ break;
}
- break;
+ resTime.timezone = tz;
}
- resTime.timezone = tz;
- }
- return resTime;
- }
+ return resTime;
+ }
+ return false;
+ };// end strictParse
- return false;
+ var looseParse = function(f,s,o){
+ try{
+ var d = new Date('2012-01-01 '+ s);
+ return {
+ hour: d.getHours(),
+ minutes: d.getMinutes(),
+ seconds: d.getSeconds(),
+ millisec: d.getMilliseconds(),
+ timezone: d.getTimezoneOffset()
+ };
+ }
+ catch(err){
+ try{
+ return strictParse(f,s,o);
+ }
+ catch(err2){
+ $.datepicker.log("Unable to parse \ntimeString: "+ s +"\ntimeFormat: "+ f);
+ }
+ }
+ return false;
+ }; // end looseParse
+
+ if(o.strict !== undefined && o.strict === false){
+ return looseParse(timeFormat, timeString, o);
+ }
+ return strictParse(timeFormat, timeString, o);
};
/*