Date Parser Update
This commit is contained in:
parent
2046fdf342
commit
cd99027e70
|
|
@ -1,5 +1,6 @@
|
|||
import { DateExpressionLexer } from "../text/lexer/DateExpressionLexer";
|
||||
import { LexerQuery } from "../text/lexer/LexerQuery";
|
||||
import { DateMath } from "./DateMath";
|
||||
|
||||
export class DateHelper
|
||||
{
|
||||
|
|
@ -39,10 +40,12 @@ export class DateHelper
|
|||
{
|
||||
let query = LexerQuery.from( expression, new DateExpressionLexer() );
|
||||
|
||||
// console.log( "\n\n>>", expression, ">>>", query.tokens.map( tk => tk.isDoneOrError ? "" : ( "[" + tk.type + ":" + tk.match + "]" ) ).join( ", " ) );
|
||||
let now = query.find( le => le.isMatcher( DateExpressionLexer.Now ) );
|
||||
|
||||
if ( now )
|
||||
{
|
||||
// console.log( "is now" );
|
||||
return DateHelper.now();
|
||||
}
|
||||
|
||||
|
|
@ -54,41 +57,52 @@ export class DateHelper
|
|||
let seconds = hourMinutesSeconds.length == 3 ? hourMinutesSeconds[ 2 ] : 0;
|
||||
|
||||
let year = DateHelper.now().getFullYear();
|
||||
let month = DateHelper.now().getMonth();
|
||||
let month = DateHelper.now().getMonth() + 1;
|
||||
let day = DateHelper.now().getDate();
|
||||
|
||||
let date = query.find( le => /date/i.test( le.type ) );
|
||||
|
||||
if ( date )
|
||||
if ( ! date )
|
||||
{
|
||||
let seperator = /slash/i.test( date.type ) ? "/" : /dot/i.test( date.type ) ? "." : "-";
|
||||
let currentDate = DateHelper.createYMD( year, month, day, hours, minutes, seconds );
|
||||
|
||||
let values = date.match.replace( /(\/|\.|\-)$/, "" ).split( seperator ).map( s => parseInt( s ) );
|
||||
|
||||
if ( /reverse/i.test( date.type ) )
|
||||
if ( DateMath.isInThePast( currentDate ) )
|
||||
{
|
||||
year = values[ 0 ];
|
||||
month = values[ 1 ] - 1;
|
||||
day = values[ 2 ];
|
||||
currentDate = DateMath.addDays( currentDate, 1 );
|
||||
}
|
||||
else
|
||||
|
||||
return currentDate;
|
||||
}
|
||||
|
||||
|
||||
let seperator = /slash/i.test( date.type ) ? "/" : /dot/i.test( date.type ) ? "." : "-";
|
||||
|
||||
let values = date.match.replace( /(\/|\.|\-)$/, "" ).split( seperator ).map( s => parseInt( s ) );
|
||||
|
||||
if ( /reverse/i.test( date.type ) )
|
||||
{
|
||||
year = values[ 0 ];
|
||||
month = values[ 1 ];
|
||||
day = values[ 2 ];
|
||||
}
|
||||
else
|
||||
{
|
||||
day = values[ 0 ];
|
||||
month = values[ 1 ];
|
||||
|
||||
if ( values.length == 3 )
|
||||
{
|
||||
day = values[ 0 ];
|
||||
month = values[ 1 ] - 1;
|
||||
year = values[ 2 ];
|
||||
|
||||
if ( values.length == 3 )
|
||||
if ( year < 100 )
|
||||
{
|
||||
year = values[ 2 ];
|
||||
|
||||
if ( year < 100 )
|
||||
{
|
||||
year += 2000;
|
||||
}
|
||||
year += 2000;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return DateHelper.createYMD( year, month, day, hours, minutes, seconds );
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -279,6 +279,6 @@ export class LexerQuery
|
|||
matcher = new LamdaExpression<LexerEvent>( matcherOrPredicate as (l:LexerEvent)=>boolean );
|
||||
}
|
||||
|
||||
return this.searchItem( 0, true, matcher );
|
||||
return this.searchItem( -1, true, matcher );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue