Skip to content

Commit b7aa216

Browse files
Strict time parsing
1 parent 465c1ab commit b7aa216

File tree

2 files changed

+115
-83
lines changed

2 files changed

+115
-83
lines changed

index.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ <h3>Requirements</h3>
124124
<h3>Version</h3>
125125
<p>Version 1.1.0</p>
126126

127-
<p>Last updated on 10/09/2012</p>
127+
<p>Last updated on 10/25/2012</p>
128128
<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>
129129
<p><a href="http://trentrichardson.com/Impromptu/GPL-LICENSE.txt" title="GPL License">GPL License</a></p>
130130
<p><a href="http://trentrichardson.com/Impromptu/MIT-LICENSE.txt" title="MIT License">MIT License</a></p>
@@ -344,6 +344,9 @@ <h3>Other Options</h3>
344344

345345
<dt>maxDateTime</dt>
346346
<dd><em>Default: null</em> - Date object of the maximum datetime allowed. Also Available as maxDate.</dd>
347+
348+
<dt>strict</dt>
349+
<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>
347350
</dl>
348351

349352
</div>
@@ -356,7 +359,7 @@ <h3>Other Options</h3>
356359

357360
<h2>Formatting Your Time</h2>
358361

359-
<p>The default format is "HH:mm". To use 12 hour time use something similar to: "hh:mm tt".</p>
362+
<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>
360363

361364
<dl class="defs">
362365
<dt>H</dt><dd>Hour with no leading 0 (24 hour)</dd>

jquery-ui-timepicker-addon.js

+110-81
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* jQuery timepicker addon
33
* By: Trent Richardson [http://trentrichardson.com]
44
* Version 1.1.0-dev
5-
* Last Modified: 10/09/2012
5+
* Last Modified: 10/25/2012
66
*
77
* Copyright 2012 Trent Richardson
88
* You may use this project under MIT or GPL licenses.
@@ -104,7 +104,8 @@
104104
addSliderAccess: false,
105105
sliderAccessArgs: null,
106106
controlType: 'slider',
107-
defaultValue: null
107+
defaultValue: null,
108+
strict: true
108109
};
109110
$.extend(this._defaults, this.regional['']);
110111
}
@@ -1023,98 +1024,126 @@
10231024

10241025
var o = extendRemove(extendRemove({}, $.timepicker._defaults), options || {});
10251026

1026-
var regstr = '^' + timeFormat.toString()
1027-
.replace(/([hH]{1,2}|mm?|ss?|[tT]{1,2}|[lz]|'.*?')/g, function (match) {
1028-
switch (match.charAt(0).toLowerCase()) {
1029-
case 'h': return '(\\d?\\d)';
1030-
case 'm': return '(\\d?\\d)';
1031-
case 's': return '(\\d?\\d)';
1032-
case 'l': return '(\\d?\\d?\\d)';
1033-
case 'z': return '(z|[-+]\\d\\d:?\\d\\d|\\S+)?';
1034-
case 't': return getPatternAmpm(o.amNames, o.pmNames);
1035-
default: // literal escaped in quotes
1036-
return '(' + match.replace(/\'/g, "").replace(/(\.|\$|\^|\\|\/|\(|\)|\[|\]|\?|\+|\*)/g, function (m) { return "\\" + m; }) + ')?';
1037-
}
1038-
})
1039-
.replace(/\s/g, '\\s?') +
1040-
o.timeSuffix + '$',
1041-
order = getFormatPositions(timeFormat),
1042-
ampm = '',
1043-
treg;
1044-
1045-
treg = timeString.match(new RegExp(regstr, 'i'));
1046-
1047-
var resTime = {
1048-
hour: 0,
1049-
minute: 0,
1050-
second: 0,
1051-
millisec: 0
1052-
};
1027+
var strictParse = function(f, s, o){
1028+
var regstr = '^' + f.toString()
1029+
.replace(/([hH]{1,2}|mm?|ss?|[tT]{1,2}|[lz]|'.*?')/g, function (match) {
1030+
switch (match.charAt(0).toLowerCase()) {
1031+
case 'h': return '(\\d?\\d)';
1032+
case 'm': return '(\\d?\\d)';
1033+
case 's': return '(\\d?\\d)';
1034+
case 'l': return '(\\d?\\d?\\d)';
1035+
case 'z': return '(z|[-+]\\d\\d:?\\d\\d|\\S+)?';
1036+
case 't': return getPatternAmpm(o.amNames, o.pmNames);
1037+
default: // literal escaped in quotes
1038+
return '(' + match.replace(/\'/g, "").replace(/(\.|\$|\^|\\|\/|\(|\)|\[|\]|\?|\+|\*)/g, function (m) { return "\\" + m; }) + ')?';
1039+
}
1040+
})
1041+
.replace(/\s/g, '\\s?') +
1042+
o.timeSuffix + '$',
1043+
order = getFormatPositions(f),
1044+
ampm = '',
1045+
treg;
1046+
1047+
treg = s.match(new RegExp(regstr, 'i'));
1048+
1049+
var resTime = {
1050+
hour: 0,
1051+
minute: 0,
1052+
second: 0,
1053+
millisec: 0
1054+
};
10531055

1054-
if (treg) {
1055-
if (order.t !== -1) {
1056-
if (treg[order.t] === undefined || treg[order.t].length === 0) {
1057-
ampm = '';
1058-
resTime.ampm = '';
1059-
} else {
1060-
ampm = $.inArray(treg[order.t].toUpperCase(), o.amNames) !== -1 ? 'AM' : 'PM';
1061-
resTime.ampm = o[ampm == 'AM' ? 'amNames' : 'pmNames'][0];
1056+
if (treg) {
1057+
if (order.t !== -1) {
1058+
if (treg[order.t] === undefined || treg[order.t].length === 0) {
1059+
ampm = '';
1060+
resTime.ampm = '';
1061+
} else {
1062+
ampm = $.inArray(treg[order.t].toUpperCase(), o.amNames) !== -1 ? 'AM' : 'PM';
1063+
resTime.ampm = o[ampm == 'AM' ? 'amNames' : 'pmNames'][0];
1064+
}
10621065
}
1063-
}
10641066

1065-
if (order.h !== -1) {
1066-
if (ampm == 'AM' && treg[order.h] == '12') {
1067-
resTime.hour = 0; // 12am = 0 hour
1068-
} else {
1069-
if (ampm == 'PM' && treg[order.h] != '12') {
1070-
resTime.hour = parseInt(treg[order.h], 10) + 12; // 12pm = 12 hour, any other pm = hour + 12
1067+
if (order.h !== -1) {
1068+
if (ampm == 'AM' && treg[order.h] == '12') {
1069+
resTime.hour = 0; // 12am = 0 hour
10711070
} else {
1072-
resTime.hour = Number(treg[order.h]);
1071+
if (ampm == 'PM' && treg[order.h] != '12') {
1072+
resTime.hour = parseInt(treg[order.h], 10) + 12; // 12pm = 12 hour, any other pm = hour + 12
1073+
} else {
1074+
resTime.hour = Number(treg[order.h]);
1075+
}
10731076
}
10741077
}
1075-
}
10761078

1077-
if (order.m !== -1) {
1078-
resTime.minute = Number(treg[order.m]);
1079-
}
1080-
if (order.s !== -1) {
1081-
resTime.second = Number(treg[order.s]);
1082-
}
1083-
if (order.l !== -1) {
1084-
resTime.millisec = Number(treg[order.l]);
1085-
}
1086-
if (order.z !== -1 && treg[order.z] !== undefined) {
1087-
var tz = treg[order.z].toUpperCase();
1088-
switch (tz.length) {
1089-
case 1:
1090-
// Z
1091-
tz = o.timezoneIso8601 ? 'Z' : '+0000';
1092-
break;
1093-
case 5:
1094-
// +hhmm
1095-
if (o.timezoneIso8601) {
1096-
tz = tz.substring(1) == '0000' ? 'Z' : tz.substring(0, 3) + ':' + tz.substring(3);
1097-
}
1098-
break;
1099-
case 6:
1100-
// +hh:mm
1101-
if (!o.timezoneIso8601) {
1102-
tz = tz == 'Z' || tz.substring(1) == '00:00' ? '+0000' : tz.replace(/:/, '');
1103-
} else {
1104-
if (tz.substring(1) == '00:00') {
1105-
tz = 'Z';
1079+
if (order.m !== -1) {
1080+
resTime.minute = Number(treg[order.m]);
1081+
}
1082+
if (order.s !== -1) {
1083+
resTime.second = Number(treg[order.s]);
1084+
}
1085+
if (order.l !== -1) {
1086+
resTime.millisec = Number(treg[order.l]);
1087+
}
1088+
if (order.z !== -1 && treg[order.z] !== undefined) {
1089+
var tz = treg[order.z].toUpperCase();
1090+
switch (tz.length) {
1091+
case 1:
1092+
// Z
1093+
tz = o.timezoneIso8601 ? 'Z' : '+0000';
1094+
break;
1095+
case 5:
1096+
// +hhmm
1097+
if (o.timezoneIso8601) {
1098+
tz = tz.substring(1) == '0000' ? 'Z' : tz.substring(0, 3) + ':' + tz.substring(3);
1099+
}
1100+
break;
1101+
case 6:
1102+
// +hh:mm
1103+
if (!o.timezoneIso8601) {
1104+
tz = tz == 'Z' || tz.substring(1) == '00:00' ? '+0000' : tz.replace(/:/, '');
1105+
} else {
1106+
if (tz.substring(1) == '00:00') {
1107+
tz = 'Z';
1108+
}
11061109
}
1110+
break;
11071111
}
1108-
break;
1112+
resTime.timezone = tz;
11091113
}
1110-
resTime.timezone = tz;
1111-
}
11121114

11131115

1114-
return resTime;
1115-
}
1116+
return resTime;
1117+
}
1118+
return false;
1119+
};// end strictParse
11161120

1117-
return false;
1121+
var looseParse = function(f,s,o){
1122+
try{
1123+
var d = new Date('2012-01-01 '+ s);
1124+
return {
1125+
hour: d.getHours(),
1126+
minutes: d.getMinutes(),
1127+
seconds: d.getSeconds(),
1128+
millisec: d.getMilliseconds(),
1129+
timezone: d.getTimezoneOffset()
1130+
};
1131+
}
1132+
catch(err){
1133+
try{
1134+
return strictParse(f,s,o);
1135+
}
1136+
catch(err2){
1137+
$.datepicker.log("Unable to parse \ntimeString: "+ s +"\ntimeFormat: "+ f);
1138+
}
1139+
}
1140+
return false;
1141+
}; // end looseParse
1142+
1143+
if(o.strict !== undefined && o.strict === false){
1144+
return looseParse(timeFormat, timeString, o);
1145+
}
1146+
return strictParse(timeFormat, timeString, o);
11181147
};
11191148

11201149
/*

0 commit comments

Comments
 (0)