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)); }
public void Given_Winterthur() { var calculator = new WorkdayCalculator(); var winterthur = new WorkdayDevice { Workdays = "MON,TUE,WED,THU,FRI", Holidays = "JAN1,JAN2,EASTER+0,EASTER+1,EASTER-2,MAY1,EASTER+39,EASTER+49,EASTER+50,EASTER-41,AUG1,DEC25,DEC26" }; var begin = new DateTime(2017, 1, 1); var end = new DateTime(2017, 12, 31); var holidays = calculator.GetHolidays(winterthur, begin, end).ToList(); var workdays = calculator.GetWorkdays(winterthur, begin, end).ToList(); _output.WriteLine($"Holidays ({holidays.Count})"); foreach (var holiday in holidays) { _output.WriteLine(holiday.ToLongDateString()); } _output.WriteLine(""); _output.WriteLine($"Workdays ({workdays.Count})"); foreach (var workday in workdays) { _output.WriteLine(workday.ToLongDateString()); } }
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 endDate = System.DateTime.FromOADate(ArgToInt(functionArguments, 1)); var calculator = new WorkdayCalculator(); var result = calculator.CalculateNumberOfWorkdays(startDate, endDate); if (functionArguments.Length > 2) { result = calculator.ReduceWorkdaysWithHolidays(result, functionArguments[2]); } return(new CompileResult(result.NumberOfWorkdays, DataType.Integer)); }
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); var resultDate = System.DateTime.MinValue; var calculator = new WorkdayCalculator(); var result = calculator.CalculateWorkday(startDate, nWorkDays); if (functionArguments.Length > 2) { result = calculator.AdjustResultWithHolidays(result, functionArguments[2]); } return(CreateResult(result.EndDate.ToOADate(), DataType.Date)); }
public void Given_Winterthur_Fasnacht() { var calculator = new WorkdayCalculator(); var winterthur = new WorkdayDevice { Workdays = "MON,TUE,WED,THU,FRI", Holidays = "EASTER-41" }; var begin = new DateTime(2000, 1, 1); var end = new DateTime(2035, 12, 31); var holidays = calculator.GetHolidays(winterthur, begin, end).ToList(); _output.WriteLine($"Holidays"); foreach (var holiday in holidays) { _output.WriteLine(holiday.ToLongDateString()); } }
/// <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)); }
/// <summary> /// Execute returns the date based on the user's input. /// </summary> /// <param name="arguments">The user specified date, number of workdays, and optional dates of holidays</param> /// <param name="context">Not used, but needed for overriding the method.</param> /// <returns>The date based on the date given and the number of workdays and optional holidays.</returns> public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context) { System.DateTime output; var functionArguments = arguments as FunctionArgument[] ?? arguments.ToArray(); if (this.ArgumentsAreValid(arguments, 2, out eErrorType argumentError) == false) { return(new CompileResult(argumentError)); } var serialNumberCandidate = arguments.ElementAt(0).Value; var workDaysCandidate = arguments.ElementAt(1).Value; if (workDaysCandidate == null || serialNumberCandidate == null) { return(new CompileResult(eErrorType.NA)); } var serialNumberIsZero = (ConvertUtil.TryParseObjectToDecimal(serialNumberCandidate, out double parsedSerialNumber) && parsedSerialNumber < 1 && parsedSerialNumber >= 0); if (serialNumberIsZero || ConvertUtil.TryParseDateObject(serialNumberCandidate, out output, out eErrorType? error)) { if (serialNumberCandidate is int && this.ArgToInt(functionArguments, 1) < 0) { return(new CompileResult(eErrorType.Num)); } if (workDaysCandidate is string) { if (!ConvertUtil.TryParseDateString(workDaysCandidate.ToString(), out output)) { return(new CompileResult(eErrorType.Value)); } } var dateSerial = (int)this.ArgToDecimal(arguments, 0); if (dateSerial < 0) { return(new CompileResult(eErrorType.Num)); } if (serialNumberIsZero && this.ArgToInt(arguments, 1) < 0) { return(new CompileResult(eErrorType.Num)); } var startDate = System.DateTime.FromOADate(dateSerial); var workDateSerial = this.ArgToDecimal(arguments, 1); var resultDate = System.DateTime.MinValue; var calculator = new WorkdayCalculator(); var dateResult = calculator.CalculateWorkday(startDate, (int)workDateSerial); if (functionArguments.Length > 2) { var weekend = arguments.ElementAt(weekendIndex).Value; if (this.WeekendSpecified(functionArguments)) { if (weekend is int && ArgToInt(functionArguments, 2) <= 0) { return(new CompileResult(eErrorType.Num)); } calculator = this.GetCalculator(weekend); if (this.IsNumeric(weekend) && calculator == null) { return(new CompileResult(eErrorType.Num)); } else if (calculator == null) { return(new CompileResult(eErrorType.Value)); } dateResult = calculator.CalculateWorkday(startDate, (int)workDateSerial); } } if (this.HolidaysSpecified(functionArguments)) { var holidayCandidate = arguments.ElementAt(this.HolidayIndex).Value; bool isHolidayZero = (serialNumberCandidate is int holAsint && holAsint == 0); if (holidayCandidate is int holAsInt && holAsInt < 0) { return(new CompileResult(eErrorType.Num)); } if (holidayCandidate is string && !ConvertUtil.TryParseDateString(holidayCandidate, out output)) { return(new CompileResult(eErrorType.Value)); } dateResult = calculator.AdjustResultWithHolidays(dateResult, functionArguments[this.HolidayIndex]); } if (serialNumberIsZero) { return(CreateResult(dateResult.EndDate.ToOADate() - 1, DataType.Date)); } return(CreateResult(dateResult.EndDate.ToOADate(), DataType.Date)); } else { return(new CompileResult(error.Value)); } }