Skip to content

Commit

Permalink
Fix handling "later"/"earlier" in relative time ranges (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
WujiaShi authored and tellarin committed Dec 3, 2018
1 parent ca47985 commit 630ce12
Show file tree
Hide file tree
Showing 6 changed files with 9,501 additions and 9,297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,10 @@ private DateTimeResolutionResult ParseSimpleCases(string text, DateObject refere
return ret;
}

private bool IsPresent(int swift)
{
return swift == 0;
}
private DateTimeResolutionResult ParseOneWordPeriod(string text, DateObject referenceDate)
{
var ret = new DateTimeResolutionResult();
Expand Down Expand Up @@ -688,6 +692,16 @@ private DateTimeResolutionResult ParseOneWordPeriod(string text, DateObject refe
ret.Mod = Constants.MID_MOD;
}

var swift = 0;
if (!string.IsNullOrEmpty(match.Groups["month"].Value))
{
swift = this.config.GetSwiftYear(trimmedText);
}
else
{
swift = this.config.GetSwiftDayOrMonth(trimmedText);
}

// Handle the abbreviation of DatePeriod, e.g., 'eoy(end of year)', the behavior of 'eoy' should be the same as 'end of year'
if (this.config.UnspecificEndOfRangeRegex.IsMatch(match.Value))
{
Expand All @@ -699,12 +713,18 @@ private DateTimeResolutionResult ParseOneWordPeriod(string text, DateObject refe
if (match.Groups["RelEarly"].Success)
{
earlierPrefix = true;
ret.Mod = null;
if (IsPresent(swift))
{
ret.Mod = null;
}
}
else if (match.Groups["RelLate"].Success)
{
laterPrefix = true;
ret.Mod = null;
if (IsPresent(swift))
{
ret.Mod = null;
}
}

var monthStr = match.Groups["month"].Value;
Expand All @@ -731,7 +751,7 @@ private DateTimeResolutionResult ParseOneWordPeriod(string text, DateObject refe

if (!string.IsNullOrEmpty(monthStr))
{
var swift = this.config.GetSwiftYear(trimmedText);
swift = this.config.GetSwiftYear(trimmedText);

month = this.config.MonthOfYear[monthStr.ToLower()];

Expand All @@ -757,7 +777,7 @@ private DateTimeResolutionResult ParseOneWordPeriod(string text, DateObject refe
}
else
{
var swift = this.config.GetSwiftDayOrMonth(trimmedText);
swift = this.config.GetSwiftDayOrMonth(trimmedText);

if (this.config.IsWeekOnly(trimmedText))
{
Expand Down Expand Up @@ -802,6 +822,11 @@ private DateTimeResolutionResult ParseOneWordPeriod(string text, DateObject refe
}
}

if (latePrefix && swift != 0)
{
ret.Mod = Constants.LATE_MOD;
}

ret.FutureValue =
ret.PastValue =
new Tuple<DateObject, DateObject>(beginDate, endDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ export class BaseDatePeriodParser implements IDateTimeParser {
return result;
}

private isPresent(swift: number): boolean{
return swift == 0;
}

protected parseOneWordPeriod(source: string, referenceDate: Date): DateTimeResolutionResult {
let result = new DateTimeResolutionResult();
let year = referenceDate.getFullYear();
Expand Down Expand Up @@ -537,6 +541,15 @@ export class BaseDatePeriodParser implements IDateTimeParser {
result.mod = Constants.MID_MOD;
}

let monthStr = match.groups('month').value;
let swift = 0;
if (!StringUtility.isNullOrEmpty(monthStr)){
swift = this.config.getSwiftYear(trimedText);
}
else{
swift = this.config.getSwiftDayOrMonth(trimedText);
}

if (RegExpUtility.isMatch(this.config.unspecificEndOfRangeRegex, match.value))
{
latePrefix = true;
Expand All @@ -547,18 +560,19 @@ export class BaseDatePeriodParser implements IDateTimeParser {
if (match.groups("RelEarly").value)
{
earlierPrefix = true;
result.mod = null;
if (this.isPresent(swift))
result.mod = null;
}

if (match.groups("RelLate").value)
{
laterPrefix = true;
result.mod = null;
if (this.isPresent(swift))
result.mod = null;
}

let monthStr = match.groups('month').value;
if (!StringUtility.isNullOrEmpty(monthStr)) {
let swift = this.config.getSwiftYear(trimedText);
swift = this.config.getSwiftYear(trimedText);
month = this.config.monthOfYear.get(monthStr) - 1;
if (swift >= -1) {
result.timex = `${DateTimeFormatUtil.toString(year + swift, 4)}-${DateTimeFormatUtil.toString(month + 1, 2)}`;
Expand All @@ -571,7 +585,7 @@ export class BaseDatePeriodParser implements IDateTimeParser {
if (month >= referenceDate.getMonth()) pastYear--;
}
} else {
let swift = this.config.getSwiftDayOrMonth(trimedText);
swift = this.config.getSwiftDayOrMonth(trimedText);
if (this.config.isWeekOnly(trimedText)) {
let monday = DateUtils.addDays(DateUtils.this(referenceDate, DayOfWeek.Monday), 7 * swift);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,9 @@ def _parse_simple_case(self, source: str, reference: datetime) -> DateTimeResolu
result.success = True
return result

def __Is_Present(self, swift):
return swift == 0

def _parse_one_word_period(self, source: str, reference: datetime) -> DateTimeResolutionResult:
result = DateTimeResolutionResult()
year = reference.year
Expand Down Expand Up @@ -758,17 +761,26 @@ def _parse_one_word_period(self, source: str, reference: datetime) -> DateTimeRe
trimmed_source = match.group('suffix')
result.mod = TimeTypeConstants.MID_MOD

swift = 0
month_str = RegExpUtility.get_group(match, 'month')
if month_str:
swift = self.config.get_swift_year(trimmed_source)
else:
swift = self.config.get_swift_day_or_month(trimmed_source)

if self.config.unspecific_end_of_range_regex is not None and self.config.unspecific_end_of_range_regex.match(match.string):
late_prefix = True
trimmed_source = match.string
result.mod = TimeTypeConstants.LATE_MOD

if RegExpUtility.get_group(match, 'RelEarly'):
early_prefix = True
result.mod = None
if self.__Is_Present(swift):
result.mod = None
elif RegExpUtility.get_group(match, 'RelLate'):
late_prefix = True
result.mod = None
if self.__Is_Present(swift):
result.mod = None

month_str = RegExpUtility.get_group(match, 'month')

Expand Down
Loading

0 comments on commit 630ce12

Please sign in to comment.