public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context) { var functionArguments = arguments as FunctionArgument[] ?? arguments.ToArray(); ValidateArguments(functionArguments, 2); var startDate = System.DateTime.FromOADate(ArgToInt(functionArguments, 0)); var nWorkDays = ArgToInt(functionArguments, 1); WorkdayCalculator calculator = new WorkdayCalculator(); var weekdayFactory = new HolidayWeekdaysFactory(); if (functionArguments.Length > 2) { var holidayArg = functionArguments[2].Value; if (Regex.IsMatch(holidayArg.ToString(), "^[01]{7}")) { calculator = new WorkdayCalculator(weekdayFactory.Create(holidayArg.ToString())); } else if (IsNumeric(holidayArg)) { var holidayCode = Convert.ToInt32(holidayArg); calculator = new WorkdayCalculator(weekdayFactory.Create(holidayCode)); } else { return(new CompileResult(eErrorType.Value)); } } var result = calculator.CalculateWorkday(startDate, nWorkDays); if (functionArguments.Length > 3) { result = calculator.AdjustResultWithHolidays(result, functionArguments[3]); } return(new CompileResult(result.EndDate.ToOADate(), DataType.Integer)); }
/// <summary> /// Execute returns the calculator with the given weekend days. /// </summary> /// <param name="weekend">The user specified weekend code that indicates the weekend</param> /// <returns>The calculator with set weekend days.</returns> protected virtual WorkdayCalculator GetCalculator(object weekend) { var calculator = new WorkdayCalculator(); var weekdayFactory = new HolidayWeekdaysFactory(); if (weekend == null) { int defaultWeekend = 1; calculator = new WorkdayCalculator(weekdayFactory.Create(defaultWeekend)); } else if (Regex.IsMatch(weekend.ToString(), "^[01]{7}")) { var weekendDayOfWeek = weekdayFactory.Create(weekend.ToString()); if (weekendDayOfWeek == null) { return(null); } calculator = new WorkdayCalculator(weekendDayOfWeek); } else if (this.IsNumeric(weekend)) { var holidayCode = Convert.ToInt32(weekend); var weekendDayOfWeek = weekdayFactory.Create(holidayCode); if (weekendDayOfWeek == null) { return(null); } calculator = new WorkdayCalculator(weekendDayOfWeek); } else { return(null); } return(calculator); }
/// <summary> /// Execute returns the number of whole workdays between two specified dates with optional specified weekends days. /// </summary> /// <param name="arguments">The user specified start date, end date, and optional number of days that are weekends.</param> /// <param name="context">Not used, but needed to override the method.</param> /// <returns>The number of workdays as an integer value.</returns> public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context) { var functionArguments = arguments as FunctionArgument[] ?? arguments.ToArray(); if (this.ArgumentsAreValid(arguments, 2, out eErrorType argumentError) == false) { return(new CompileResult(argumentError)); } var startDate = System.DateTime.FromOADate(ArgToInt(functionArguments, 0)); var endDate = System.DateTime.FromOADate(ArgToInt(functionArguments, 1)); WorkdayCalculator calculator = new WorkdayCalculator(); var weekdayFactory = new HolidayWeekdaysFactory(); if (functionArguments.Length > 2) { var holidayArg = functionArguments[2].Value; if (Regex.IsMatch(holidayArg.ToString(), "^[01]{7}")) { calculator = new WorkdayCalculator(weekdayFactory.Create(holidayArg.ToString())); } else if (IsNumeric(holidayArg)) { var holidayCode = Convert.ToInt32(holidayArg); calculator = new WorkdayCalculator(weekdayFactory.Create(holidayCode)); } else { return(new CompileResult(eErrorType.Value)); } } var result = calculator.CalculateNumberOfWorkdays(startDate, endDate); if (functionArguments.Length > 3) { result = calculator.ReduceWorkdaysWithHolidays(result, functionArguments[3]); } return(new CompileResult(result.NumberOfWorkdays, DataType.Integer)); }