ALED NOSTRAD ALED NOSTRAD - 2 months ago 18
C# Question

Date business handling Windows Service on C#

I have a requirement where I need to work on a date field, which everyday I need manage a sending and receive tape for processing backup, but there any special condition for operator, they nobody agree to work on holiday, so the requirement is some thing like this

I will run a services which checking business date every time, and need for operator to be send a drive of tape before on holiday or weekend (Saturday and Sunday):


  1. I had a table of master holiday, so I will looking for holiday on current table.

  2. If ex. today is Monday when service first beginning start. and tomorrow on Tuesday is holiday, so I must process all of the jobs of Tuesday on Monday.

  3. If ex. today is Monday when service first beginning start. And tomorrow on Tuesday and Wednesday is holiday, so I must process all of the jobs of Tuesday and Wednesday on Monday.

  4. If ex. today is Monday when service first beginning start. and tomorrow on Thursday is holiday, so I must process all of the jobs on Thursday on Wednesday.



A process tape falls on Saturday and Sunday, must be process on Friday if Friday not on holiday, if holiday then a process tape which fall on Friday until Sunday, will be process on Thursday, and still continue process like that if a tomorrow falls on holiday so must be process before tomorrow on date not on holiday.

A problem come when I first running of service Ex on Monday and Tuesday and Wednesday is holiday, how I know? Actually I didn't know, how we can to know if tomorrow is holiday to continue process.

I have tried a solution to the above problem by having multiple if and else cases, but just wondering if there is any generic and graceful way of doing it?

Answer

Actually I didn't know, how we can to know if tomorrow is holiday to continue process.

I am not sure I understand the issue, because you can test if tomorrow is a holiday the same way you test if today is. A partial list of holiday dates:

hList = new List<Holiday>();
hList.Add(new Holiday("Thanksgiving", new DateTime(2016, 11, 24)));
hList.Add(new Holiday("Black Friday", new DateTime(2016, 11, 25)));

hList.Add(new Holiday("May Day", new DateTime(2016, 05, 1)));
hList.Add(new Holiday("Memorial Day", new DateTime(2016, 5, 30)));
hList.Add(new Holiday("Cinco", new DateTime(2016, 5, 5)));

hList.Add(new Holiday("FooBar Day", new DateTime(2016, 5, 18)));
// like Carnival without the hangover:
hList.Add(new Holiday("Ziggy Festival", new DateTime(2016, 2, 9)));
hList.Add(new Holiday("Ziggy Festival", new DateTime(2016, 2, 10)));
hList.Add(new Holiday("Ziggy Festival", new DateTime(2016, 2, 11)));

These are the interesting ones of a larger list used. By accident most of these created a multi-day sequence of holiday/weekends. FooBar Day is a single day one.

A work list class to accumulate future days to do the work for:

public class WorkItem
{
    // on this day...
    public DateTime Date { get; set; }
    // do work for these days:
    public List<DateTime> Tasks { get; private set; }

    public WorkItem(DateTime d)
    {
        Date = d.Date;
        Tasks = new List<DateTime>();
    }

    public void AddDay(DateTime dt)
    {
        if (!Tasks.Contains(dt.Date))
            Tasks.Add(dt.Date);
    }
}

A list of testDates was every date 3 days before and after each holiday entry. Scanning and creating the work list:

private void CreateHolidayWorkList()
{

    WorkItem w;
    wList = new List<WorkItem>();

    foreach (DateTime dt in testDates)
    {
        // if today is Holiday, skip
        if (!IsHolidayOrWeekend(dt))
        {
            // is TOMORROW a holiday or weekend??
            //  AndAlso is it not alreay in the list
            if (IsHolidayOrWeekend(dt.AddDays(1)) && 
                    wList.FirstOrDefault(f=> f.Date.Date==dt.Date)==null)
            {
                // current To-Do date 
                w = new WorkItem(dt);
                // scan ahead up to 6 days to find consecutive
                // holiday/weekend dates
                for (int n = 1; n <= 6; n++)
                {
                    if (IsHolidayOrWeekend(dt.AddDays(n)))
                        w.AddDay(dt.AddDays(n));
                    else
                        break;
                }
                // add todo list to list
                wList.Add(w);
            }
        }
    }
}

private bool IsHolidayOrWeekend(DateTime dt)
{
    if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
        return true;

    return (hList.FirstOrDefault(q => q.Date == dt) != null);
}

Some of the results:

on 2016-02-08 Do work for:
-> 2016-02-09
-> 2016-02-10
-> 2016-02-11
on 2016-02-12 Do work for:
-> 2016-02-13
-> 2016-02-14
...
on 2016-05-04 Do work for:
-> 2016-05-05
on 2016-05-17 Do work for:
-> 2016-05-18
...
on 2016-11-10 Do work for:
-> 2016-11-11
-> 2016-11-12
-> 2016-11-13
on 2016-11-23 Do work for:
-> 2016-11-24
-> 2016-11-25
-> 2016-11-26
-> 2016-11-27

  • The work list doesnt include the last working day before
  • The 2/8 work list is for the 3 day Ziggy Festival
  • The 2/12 list is an "accidental" test date. It was included because it was within 3 days of the last day of Ziggy Festival. It is the standard Fri list for a weekend
  • 5/4 and 5/17 recognize a single day holiday
  • The last two just show very long holiday strings