public IEnumerable <DateTime> GetHolidays(WorkdayDevice device, DateTime begin, DateTime end)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (string.IsNullOrEmpty(device.Holidays))
            {
                return(new List <DateTime>(0));
            }

            if (begin > end)
            {
                var temp = begin;
                begin = end;
                end   = temp;
            }

            var holidays = device.Holidays.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
            var result   = new List <DateTime>();
            var easter   = GetYears(begin, end).Select(GetEasterDate).ToList();

            foreach (var holiday in holidays)
            {
                if (_dateRegex.IsMatch(holiday))
                {
                    foreach (var year in GetYears(begin, end))
                    {
                        var      withYear = $"{year:D}{holiday}";
                        DateTime parsed;
                        if (DateTime.TryParseExact(withYear, "yyyyMMMd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out parsed) &&
                            IsInRange(parsed, begin, end))
                        {
                            result.Add(parsed.Date);
                        }
                    }
                }
                else
                {
                    var easterMatch = _easterRegex.Match(holiday);
                    if (easterMatch.Success)
                    {
                        var days = int.Parse(easterMatch.Groups["days"].Value);

                        foreach (var e in easter)
                        {
                            var ed = e.AddDays(days).Date;
                            if (IsInRange(ed, begin, end))
                            {
                                result.Add(ed);
                            }
                        }
                    }
                }
            }

            result.Sort();
            return(result);
        }
        public IEnumerable <DateTime> GetWorkdays(WorkdayDevice device, DateTime begin, DateTime end)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (begin > end)
            {
                var temp = begin;
                begin = end;
                end   = temp;
            }

            var holidays = GetHolidays(device, begin, end).ToList();
            var workdays = device.Workdays.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var day in GetDays(begin, end))
            {
                if (holidays.Contains(day.Date))
                {
                    // is holiday
                    continue;
                }

                if (!workdays.Contains(day.ToString("ddd", CultureInfo.InvariantCulture), StringComparer.OrdinalIgnoreCase))
                {
                    // is weekend
                    continue;
                }

                yield return(day.Date);
            }
        }
Exemplo n.º 3
0
        private void UpdateVariables(WorkdayDevice device)
        {
            var workdays = _calculator.GetWorkdays(device, DateTime.Today, DateTime.Today.AddDays(1)).ToList();
            var holidays = _calculator.GetHolidays(device, DateTime.Today, DateTime.Today.AddDays(1)).ToList();

            _messageQueue.Publish(new UpdateVariableMessage(Name, device.Id, "TodayIsWorkday", workdays.Contains(DateTime.Today)));
            _messageQueue.Publish(new UpdateVariableMessage(Name, device.Id, "TomorrowIsWorkday", workdays.Contains(DateTime.Today.AddDays(1))));

            _messageQueue.Publish(new UpdateVariableMessage(Name, device.Id, "TodayIsHoliday", holidays.Contains(DateTime.Today)));
            _messageQueue.Publish(new UpdateVariableMessage(Name, device.Id, "TomorrowIsHoliday", holidays.Contains(DateTime.Today.AddDays(1))));
        }
 public WorkdayScriptObject(WorkdayDevice device, IWorkdayCalculator calculator)
 {
     _device     = device;
     _calculator = calculator;
 }