/// <summary> /// Evaluates a math expression of 2 time spans. /// </summary> /// <param name="node">The AST node the evaluation is a part of.</param> /// <param name="lhs">The time on the left hand side</param> /// <param name="rhs">The time on the right hand side</param> /// <param name="op">The math operator.</param> /// <returns></returns> public static LBool CompareDates(AstNode node, LDate lhs, LDate rhs, Operator op) { var left = lhs.Value; var right = rhs.Value; var result = false; if (op == Operator.LessThan) result = left < right; else if (op == Operator.LessThanEqual) result = left <= right; else if (op == Operator.MoreThan) result = left > right; else if (op == Operator.MoreThanEqual) result = left >= right; else if (op == Operator.EqualEqual) result = left == right; else if (op == Operator.NotEqual) result = left != right; return new LBool(result); }
/// <summary> /// Execute the continue. /// </summary> public object VisitDay(DayExpr expr) { var date = DateTime.Today; var dayOfweek = DayOfWeek.Monday; var isDayOfWeek = false; var isTimeSpecified = expr.Time != "0"; var dayName = expr.Name.ToLower(); // 1. Determine date/day if (dayName == "today") date = DateTime.Today; else if (dayName == "yesterday") date = DateTime.Today.AddDays(-1); else if (dayName == "tomorrow") date = DateTime.Today.AddDays(1); else if (dayName == "monday") { isDayOfWeek = true; dayOfweek = DayOfWeek.Monday; } else if (dayName == "tuesday") { isDayOfWeek = true; dayOfweek = DayOfWeek.Tuesday; } else if (dayName == "wednesday") { isDayOfWeek = true; dayOfweek = DayOfWeek.Wednesday; } else if (dayName == "thursday") { isDayOfWeek = true; dayOfweek = DayOfWeek.Thursday; } else if (dayName == "friday") { isDayOfWeek = true; dayOfweek = DayOfWeek.Friday; } else if (dayName == "saturday") { isDayOfWeek = true; dayOfweek = DayOfWeek.Saturday; } else if (dayName == "sunday") { isDayOfWeek = true; dayOfweek = DayOfWeek.Sunday; } // Case 1 - if (isDayOfWeek) { // Case 1a: day of week only if(!isTimeSpecified) return new LDayOfWeek(dayOfweek); // Case 1b: day of week ( with time ) var today = DateTime.Today; var count = 0; while (today.DayOfWeek != dayOfweek && count < 8) { today = today.AddDays(1); } } // 3. Finally - add the time to the day. if(expr.Time != "0") { var t = TimeSpan.Parse(expr.Time); date = date.AddMilliseconds(t.TotalMilliseconds); } var result = new LDate(date); return result; }
/// <summary> /// Evaluates a math expression of 2 time spans. /// </summary> /// <param name="node">The AST node the evaluation is a part of.</param> /// <param name="lhs">The time on the left hand side</param> /// <param name="rhs">The time on the right hand side</param> /// <param name="op">The math operator.</param> /// <returns></returns> public static LTime CalcDates(AstNode node, LDate lhs, LDate rhs, Operator op) { if (op != Operator.Subtract) throw ExceptionHelper.BuildRunTimeException(node, "Can only subtract dates"); var left = lhs.Value; var right = rhs.Value; var result = left - right; return new LTime(result); }