Core: cache shortDate parser & time parser extracts time

This commit is contained in:
Rob Garrison 2015-11-01 19:39:18 -06:00
parent b2ae0d194d
commit dbcd306ff2
2 changed files with 43 additions and 19 deletions

View File

@ -2485,6 +2485,21 @@
// XXY covers MDY & DMY formats // XXY covers MDY & DMY formats
ts.regex.shortDateXXY = /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{4})/; ts.regex.shortDateXXY = /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{4})/;
ts.regex.shortDateYMD = /(\d{4})[\/\s](\d{1,2})[\/\s](\d{1,2})/; ts.regex.shortDateYMD = /(\d{4})[\/\s](\d{1,2})[\/\s](\d{1,2})/;
ts.convertFormat = function( dateString, format ) {
dateString = ( dateString || '' )
.replace( ts.regex.spaces, ' ' )
.replace( ts.regex.shortDateReplace, '/' );
if ( format === 'mmddyyyy' ) {
dateString = dateString.replace( ts.regex.shortDateXXY, '$3/$1/$2' );
} else if ( format === 'ddmmyyyy' ) {
dateString = dateString.replace( ts.regex.shortDateXXY, '$3/$2/$1' );
} else if ( format === 'yyyymmdd' ) {
dateString = dateString.replace( ts.regex.shortDateYMD, '$1/$2/$3' );
}
var date = new Date( dateString );
return date instanceof Date && isFinite( date ) ? date.getTime() : '';
};
ts.addParser({ ts.addParser({
id : 'shortDate', // 'mmddyyyy', 'ddmmyyyy' or 'yyyymmdd' id : 'shortDate', // 'mmddyyyy', 'ddmmyyyy' or 'yyyymmdd'
is : function( str ) { is : function( str ) {
@ -2493,37 +2508,45 @@
}, },
format : function( str, table, cell, cellIndex ) { format : function( str, table, cell, cellIndex ) {
if ( str ) { if ( str ) {
var date, d, var c = table.config,
c = table.config, $header = c.$headerIndexed[ cellIndex ],
ci = c.$headerIndexed[ cellIndex ], format = $header.length && $header.data( 'dateFormat' ) ||
format = ci.length && ci[ 0 ].dateFormat || ts.getData( $header, ts.getColumnData( table, c.headers, cellIndex ), 'dateFormat' ) ||
ts.getData( ci, ts.getColumnData( table, c.headers, cellIndex ), 'dateFormat' ) ||
c.dateFormat; c.dateFormat;
d = str.replace( ts.regex.spaces, ' ' ).replace( ts.regex.shortDateReplace, '/' ); // save format because getData can be slow...
if ( format === 'mmddyyyy' ) { if ( $header.length ) {
d = d.replace( ts.regex.shortDateXXY, '$3/$1/$2' ); $header.data( 'dateFormat', format );
} else if ( format === 'ddmmyyyy' ) {
d = d.replace( ts.regex.shortDateXXY, '$3/$2/$1' );
} else if ( format === 'yyyymmdd' ) {
d = d.replace( ts.regex.shortDateYMD, '$1/$2/$3' );
} }
date = new Date( d ); return ts.convertFormat( str, format ) || str;
return date instanceof Date && isFinite( date ) ? date.getTime() : str;
} }
return str; return str;
}, },
type : 'numeric' type : 'numeric'
}); });
ts.regex.timeTest = /^(([0-2]?\d:[0-5]\d)|([0-1]?\d:[0-5]\d\s?([AP]M)))$/i; // match 24 hour time & 12 hours time + am/pm - see http://regexr.com/3c3tk
ts.regex.timeTest = /^([1-9]|1[0-2]):([0-5]\d)(\s[AP]M)|((?:[01]\d|[2][0-4]):[0-5]\d)$/i;
ts.regex.timeMatch = /([1-9]|1[0-2]):([0-5]\d)(\s[AP]M)|((?:[01]\d|[2][0-4]):[0-5]\d)/i;
ts.addParser({ ts.addParser({
id : 'time', id : 'time',
is : function( str ) { is : function( str ) {
return ts.regex.timeTest.test( str ); return ts.regex.timeTest.test( str );
}, },
format : function( str, table ) { format : function( str, table ) {
var date = str ? new Date( '2000/01/01 ' + str.replace( ts.regex.dateReplace, '$1 $2' ) ) : str; // isolate time... ignore month, day and year
return date instanceof Date && isFinite( date ) ? date.getTime() : str; var temp,
timePart = ( str || '' ).match( ts.regex.timeMatch ),
orig = new Date( str ),
// no time component? default to 00:00 by leaving it out, but only if str is defined
time = str && ( timePart !== null ? timePart[ 0 ] : '00:00 AM' ),
date = time ? new Date( '2000/01/01 ' + time.replace( ts.regex.dateReplace, '$1 $2' ) ) : time;
if ( date instanceof Date && isFinite( date ) ) {
temp = orig instanceof Date && isFinite( orig ) ? orig.getTime() : 0;
// if original string was a valid date, add it to the decimal so the column sorts in some kind of order
// luckily new Date() ignores the decimals
return temp ? parseFloat( date.getTime() + '.' + orig.getTime() ) : date.getTime();
}
return str;
}, },
type : 'numeric' type : 'numeric'
}); });

View File

@ -505,15 +505,16 @@ jQuery(function($){
QUnit.test( 'testing parsers', function(assert) { QUnit.test( 'testing parsers', function(assert) {
assert.expect(parserTests); assert.expect(parserTests);
report(assert, sample1); report(assert, sample1);
var $t = $(th0);
c1.sortLocaleCompare = true; c1.sortLocaleCompare = true;
c1.ignoreCase = false; c1.ignoreCase = false;
c1.usNumberFormat = false; c1.usNumberFormat = false;
th0.dateFormat = c1.dateFormat = 'ddmmyyyy'; $t.data('dateFormat', 'ddmmyyyy');
report(assert, sample2); report(assert, sample2);
c1.usNumberFormat = true; c1.usNumberFormat = true;
th0.dateFormat = c1.dateFormat = 'yyyymmdd'; $t.data('dateFormat', 'yyyymmdd');
report(assert, sample3); report(assert, sample3);
// undocumented sortValue // undocumented sortValue