public static string BuildTimex(TimeResult timeResult) { var build = new StringBuilder("T"); if (timeResult.Hour >= 0) { build.Append(timeResult.Hour.ToString("D2")); } if (timeResult.Minute >= 0) { build.Append(":" + timeResult.Minute.ToString("D2")); } if (timeResult.Second >= 0) { build.Append(":" + timeResult.Second.ToString("D2")); } return(build.ToString()); }
public static DateTimeResolutionResult Handle(IDateTimeParser timeParser, DateTimeExtra <PeriodType> extra, DateObject refTime) { //Left is a time var left = extra.NamedEntity["left"]; TimeResult leftResult, rightResult = null; // 下午四点十分到五点十分 if (extra.Type == PeriodType.FullTime) { var leftExtract = new ExtractResult { Start = left.Index, Length = left.Length, Text = left.Value, Type = Constants.SYS_DATETIME_TIME }; leftResult = timeParser.Parse(leftExtract, refTime).Data as TimeResult; } else { // 下午四到五点 leftResult = TimeFunctions.GetShortLeft(left.Value); } //Right is a time var right = extra.NamedEntity["right"]; var rightExtract = new ExtractResult { Start = right.Index, Length = right.Length, Text = right.Value, Type = Constants.SYS_DATETIME_TIME }; rightResult = timeParser.Parse(rightExtract, refTime).Data as TimeResult; var ret = new DateTimeResolutionResult() { Success = true }; //the right side doesn't contain desc while the left side does if (rightResult.LowBound == -1 && leftResult.LowBound != -1 && rightResult.Hour <= leftResult.LowBound) { rightResult.Hour += Constants.HalfDayHourCount; } int day = refTime.Day, month = refTime.Month, year = refTime.Year; //determine if the right side time is smaller than the left side, if yes, add one day int hour = leftResult.Hour > 0 ? leftResult.Hour : 0, min = leftResult.Minute > 0 ? leftResult.Minute : 0, second = leftResult.Second > 0 ? leftResult.Second : 0; var leftTime = DateObject.MinValue.SafeCreateFromValue(year, month, day, hour, min, second); hour = rightResult.Hour > 0 ? rightResult.Hour : 0; min = rightResult.Minute > 0 ? rightResult.Minute : 0; second = rightResult.Second > 0 ? rightResult.Second : 0; var rightTime = DateObject.MinValue.SafeCreateFromValue(year, month, day, hour, min, second); if (rightTime.Hour < leftTime.Hour) { rightTime = rightTime.AddDays(1); } ret.FutureValue = ret.PastValue = new Tuple <DateObject, DateObject>(leftTime, rightTime); var leftTimex = BuildTimex(leftResult); var rightTimex = BuildTimex(rightResult); ret.Timex = $"({leftTimex},{rightTimex},{BuildSpan(leftResult, rightResult)})"; return(ret); }
public static DateTimeResolutionResult PackTimeResult(DateTimeExtra <TimeType> extra, TimeResult timeResult, DateObject referenceTime) { //Find if there is a description var noDesc = true; var dayDesc = extra.NamedEntity["daydesc"]?.Value; if (!string.IsNullOrEmpty(dayDesc)) { AddDesc(timeResult, dayDesc); noDesc = false; } int hour = timeResult.Hour > 0 ? timeResult.Hour : 0, min = timeResult.Minute > 0 ? timeResult.Minute : 0, second = timeResult.Second > 0 ? timeResult.Second : 0, day = referenceTime.Day, month = referenceTime.Month, year = referenceTime.Year; var dtResult = new DateTimeResolutionResult(); var build = new StringBuilder("T"); if (timeResult.Hour >= 0) { build.Append(timeResult.Hour.ToString("D2")); } if (timeResult.Minute >= 0) { build.Append(":" + timeResult.Minute.ToString("D2")); } if (timeResult.Second >= 0) { build.Append(":" + timeResult.Second.ToString("D2")); } if (noDesc) { //build.Append("ampm"); dtResult.Comment = Constants.Comment_AmPm; } dtResult.Timex = build.ToString(); if (hour == 24) { hour = 0; } dtResult.FutureValue = dtResult.PastValue = DateObject.MinValue.SafeCreateFromValue(year, month, day, hour, min, second); dtResult.Success = true; return(dtResult); }