diff options
author | Trent Richardson <trentdrichardson@gmail.com> | 2012-10-25 14:55:52 -0400 |
---|---|---|
committer | Trent Richardson <trentdrichardson@gmail.com> | 2012-10-25 14:55:52 -0400 |
commit | b7aa216515b0bc4200daa6cdac16c86163e6990e (patch) | |
tree | dac3981b055decf414027b4e5f48ff2949c65a23 | |
parent | 465c1ab37613beeca89d68a9bd11e58528d30c91 (diff) | |
download | jQuery-Timepicker-Addon-b7aa216515b0bc4200daa6cdac16c86163e6990e.zip jQuery-Timepicker-Addon-b7aa216515b0bc4200daa6cdac16c86163e6990e.tar.gz jQuery-Timepicker-Addon-b7aa216515b0bc4200daa6cdac16c86163e6990e.tar.bz2 |
Strict time parsing
-rw-r--r-- | index.html | 7 | ||||
-rw-r--r-- | jquery-ui-timepicker-addon.js | 191 |
2 files changed, 115 insertions, 83 deletions
@@ -124,7 +124,7 @@ <h3>Version</h3> <p>Version 1.1.0</p> - <p>Last updated on 10/09/2012</p> + <p>Last updated on 10/25/2012</p> <p>jQuery Timepicker Addon is currently available for use in all personal or commercial projects under both MIT and GPL licenses. This means that you can choose the license that best suits your project, and use it accordingly. </p> <p><a href="http://trentrichardson.com/Impromptu/GPL-LICENSE.txt" title="GPL License">GPL License</a></p> <p><a href="http://trentrichardson.com/Impromptu/MIT-LICENSE.txt" title="MIT License">MIT License</a></p> @@ -344,6 +344,9 @@ <dt>maxDateTime</dt> <dd><em>Default: null</em> - Date object of the maximum datetime allowed. Also Available as maxDate.</dd> + + <dt>strict</dt> + <dd><em>Default: true</em> - Requires the time to exactly match the timeFormat when parsing. False will attempt to allow Javascript Date() to parse the time string.</dd> </dl> </div> @@ -356,7 +359,7 @@ <h2>Formatting Your Time</h2> - <p>The default format is "HH:mm". To use 12 hour time use something similar to: "hh:mm tt".</p> + <p>The default format is "HH:mm". To use 12 hour time use something similar to: "hh:mm tt". When both "t" and lower case "h" are present in the timeFormat, 12 hour time will be used.</p> <dl class="defs"> <dt>H</dt><dd>Hour with no leading 0 (24 hour)</dd> 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);
};
/*
|