Work Calendar (Calendars)

Description

Work Calendar is working calendar (or business calendar) algorithm for .Net framework, give a start time and duration(such as 10 hours or 30 minutes) then it calculate the end time skip the non-working time such as saturday, sunday and holidays.

Features

  1. Add duration to time, skip the non-working time
  2. Calculate duration, only contain working time
  3. Get nearest working time, skip the non-working time
  4. Define a work calendar in xml format

Package Content

Folder

  • src – source code and unit test.
  • bin – binary file.

How to define work calendar

Calendar is define in xml, it contain the three type day: normal day, work day and holiday.
Sample calendar:


    <?xml version="1.0" encoding="UTF-8"?>
    <business-calendar hoursperday="6.5">
        <monday    hours="9:00-12:00 "/>
        <tuesday   hours="9:00-12:00 and 12:30-17:00 and 19:00 - 20:00"/>
        <wednesday hours="9:00-12:00 and 12:30-17:00"/>
        <thursday  hours="9:00-12:00 and 12:30-17:00"/>
        <friday    hours="9:00-12:00 and 12:30-17:00"/>

        <workday period="2/28/2013" hours="9:00-11:00" />
        <workday period="3/2/2013" hours="9:00-12:00" />

        <holiday period="3/5/2013 - 3/7/2013"/>
        <holiday period="3/14/2013"/>
    </business-calendar>

How to use it

  • parse calendar xml to calendar object

    string calendarFilePath = @"calendar.xml";
    string calendarXml = "";
    using (StreamReader sr = new StreamReader(calendarFilePath))
    {
        calendarXml = sr.ReadToEnd();
    }
    CalendarParser parser = new CalendarParser();
    ICalendar calendar = parser.Parse(calendarXml);
    
  • Add duration to time, skip the non-working time

    DateTime startTime = DateTime.Now;
    long seconds = 3600;
    DateTime endTime = calendar.Add(startTime, seconds);
    
  • Calculate duration, only contain working time

    startTime = new DateTime(2013, 1, 1, 8, 30, 0);
    endTime = new DateTime(2013, 1, 5, 10, 30, 0);
    long duration = calendar.CalculateDuration(startTime, endTime);
    
  • get nearest working time, skip the non-working time

    startTime = new DateTime(2013, 1, 1, 8, 0, 0);
    DateTime workingTime = calendar.GetNearestWorkingTime(startTime);
    

Support

Once again, As I said at the beginning, I’d be glad to help you if you have any questions relating to this code. I’ll do my best to assist. Thanks so much!.

Download Work Calendar (Calendars)

Leave a Reply

Your email address will not be published. Required fields are marked *