Beispiel #1
0
 private static TimeSpan ConvertResult(DurationVisitor visitor)
 {
     return(TimeSpan.Zero
            .Add(TimeSpan.FromDays(visitor.Weeks * 7))
            .Add(TimeSpan.FromDays(visitor.Days))
            .Add(TimeSpan.FromHours(visitor.Hours))
            .Add(TimeSpan.FromMinutes(visitor.Minutes))
            .Add(TimeSpan.FromSeconds(visitor.Seconds)));
 }
Beispiel #2
0
        public DurationVisitor Negate()
        {
            var result = new DurationVisitor();

            result.days          = -this.Days;
            result.hours         = -this.Hours;
            result.inTimeSection = this.inTimeSection;
            result.minutes       = -this.Minutes;
            result.seconds       = -this.Seconds;
            return(result);
        }
Beispiel #3
0
        public static DurationVisitor Parse(string duration)
        {
            var visitor = new DurationVisitor(duration);

            visitor.Visit();
            if (visitor.isValid)
            {
                return(visitor);
            }
            throw new FormatException($"无法解析duration:{duration}");
        }
Beispiel #4
0
        public DurationHelper(string expressions)
        {
            IList <string> expression = new List <string>();

            if (expressions != null)
            {
                expression = (expressions.Split('/'));
            }
            //datatypeFactory = DatatypeFactory.newInstance();

            if (expression.Count > 3 || expression.Count == 0)
            {
                throw Log.CannotParseDuration(expressions);
            }
            if (expression[0].StartsWith("R"))
            {
                isRepeat   = true;
                times      = expression[0].Length == 1 ? int.MaxValue : int.Parse(expression[0].Substring(1));
                expression = expression.Skip(1).ToList();
            }

            if (IsDuration(expression[0]))
            {
                period = DurationVisitor.Parse(expression[0]);
                end    = expression.Count == 1 ? (DateTime?)null : DateTime.Parse(expression[1]);
            }
            else
            {
                start = DateTime.Parse(expression[0]);
                if (IsDuration(expression[1]))
                {
                    period = DurationVisitor.Parse(expression[0]);
                }
                else
                {
                    end    = DateTime.Parse(expression[1]);
                    period = DurationVisitor.FromTimeSpan(((DateTime)end) - ((DateTime)start));// datatypeFactory.newDuration(end.GetValueOrDefault().Ticks - start.Ticks);
                }
            }
            if (start == null && end == null)
            {
                start = ClockUtil.CurrentTime;
            }
        }