Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public static TimeResult HandleDigit(DateTimeExtra <TimeType> extra)
        {
            var timeResult = new TimeResult
            {
                Hour   = MatchToValue(extra.NamedEntity[Constants.HourGroupName].Value),
                Minute = MatchToValue(extra.NamedEntity[Constants.MinuteGroupName].Value),
                Second = MatchToValue(extra.NamedEntity[Constants.SecondGroupName].Value)
            };

            return(timeResult);
        }
Exemplo n.º 3
0
        public static TimeResult HandleDigit(DateTimeExtra <TimeType> extra)
        {
            var timeResult = new TimeResult
            {
                Hour   = MatchToValue(extra.NamedEntity["hour"].Value),
                Minute = MatchToValue(extra.NamedEntity["min"].Value),
                Second = MatchToValue(extra.NamedEntity["sec"].Value)
            };

            return(timeResult);
        }
Exemplo n.º 4
0
        public static TimeResult HandleChinese(DateTimeExtra <TimeType> extra)
        {
            var hour    = MatchToValue(extra.NamedEntity["hour"].Value);
            var quarter = MatchToValue(extra.NamedEntity["quarter"].Value);
            var minute  = MatchToValue(extra.NamedEntity["min"].Value);
            var second  = MatchToValue(extra.NamedEntity["sec"].Value);

            minute = !string.IsNullOrEmpty(extra.NamedEntity["half"].Value) ? 30 : quarter != -1 ? quarter * 15 : minute;
            return(new TimeResult
            {
                Hour = hour,
                Minute = minute,
                Second = second
            });
        }
Exemplo n.º 5
0
        public static TimeResult HandleLess(DateTimeExtra <TimeType> extra)
        {
            var hour    = MatchToValue(extra.NamedEntity["hour"].Value);
            var quarter = MatchToValue(extra.NamedEntity["quarter"].Value);
            var minute  = !string.IsNullOrEmpty(extra.NamedEntity["half"].Value) ? 30 : quarter != -1 ? quarter * 15 : 0;
            var second  = MatchToValue(extra.NamedEntity["sec"].Value);
            var less    = MatchToValue(extra.NamedEntity["min"].Value);
            var all     = hour * 60 + minute - less;

            if (all < 0)
            {
                all += 1440;
            }
            return(new TimeResult
            {
                Hour = all / 60,
                Minute = all % 60,
                Second = second
            });
        }
Exemplo n.º 6
0
        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 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
            };

            //右侧没有Desc,左侧有
            if (rightResult.LowBound == -1 && leftResult.LowBound != -1 && rightResult.Hour <= leftResult.LowBound)
            {
                rightResult.Hour += 12;
            }
            int day   = refTime.Day,
                month = refTime.Month,
                year  = refTime.Year;
            //判断右侧是否比小测的小,如果比左侧的小,则增加一天
            int hour   = leftResult.Hour > 0 ? leftResult.Hour : 0,
                min    = leftResult.Minute > 0 ? leftResult.Minute : 0,
                second = leftResult.Second > 0 ? leftResult.Second : 0;

            var leftTime = new DateObject(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 = new DateObject(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);
        }