开发者

C# method to retrieve a Work time slots based on few parameters

开发者 https://www.devze.com 2023-04-03 14:55 出处:网络
Could someone help me write a method in c# that would accept follo开发者_如何学Pythonwing parameters and provide a list of working times.

Could someone help me write a method in c# that would accept follo开发者_如何学Pythonwing parameters and provide a list of working times.

1.String workStartingTime

2.String workStopTime

3.int iBreaktime

4.int iWorkInterval

I did write a code that works but damn it's too un-maintainable and contrived. Sample output for executing with below parameter is provided :

workStartTime = "9:00 AM",

workStopTime = "6:00 PM",

ibreakTime = 10,

iWorkInterval = 60

Output:

9:00 AM - 10:00 AM

10:10 AM - 11:10 AM

11:20 AM - 12:20 PM

12:30 PM - 01:30 PM

hope you get the idea, for each interval leave a gap and span the thing until work ending time.

Note:

By the way the work starttime and endtime are stored in database like so 9:30 AM - 10:00 PM or 9:00 - 16:00 ie 12 or 24 Hour format.


You can use the CalendarPeriodCollector of the free Time Period Library for .NET.

The tool supports various filters, including working hours:

// ----------------------------------------------------------------------
public void CalendarPeriodCollectorSample()
{
  CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();
  filter.Months.Add( YearMonth.January ); // only Januaries
  filter.WeekDays.Add( DayOfWeek.Friday ); // only Fridays
  filter.CollectingHours.Add( new HourRange( 8, 18 ) ); // working hours

  CalendarTimeRange testPeriod =
    new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) );
  Console.WriteLine( "Calendar period collector of period: " + testPeriod );
  // > Calendar period collector of period:
  //            01.01.2010 00:00:00 - 30.12.2011 23:59:59 | 728.23:59

  CalendarPeriodCollector collector =
          new CalendarPeriodCollector( filter, testPeriod );
  collector.CollectHours();
  foreach ( ITimePeriod period in collector.Periods )
  {
    Console.WriteLine( "Period: " + period );
  }
  // > Period: 01.01.2010; 08:00 - 17:59 | 0.09:59
  // > Period: 08.01.2010; 08:00 - 17:59 | 0.09:59
  // > Period: 15.01.2010; 08:00 - 17:59 | 0.09:59
  // > Period: 22.01.2010; 08:00 - 17:59 | 0.09:59
  // > Period: 29.01.2010; 08:00 - 17:59 | 0.09:59
  // > Period: 07.01.2011; 08:00 - 17:59 | 0.09:59
  // > Period: 14.01.2011; 08:00 - 17:59 | 0.09:59
  // > Period: 21.01.2011; 08:00 - 17:59 | 0.09:59
  // > Period: 28.01.2011; 08:00 - 17:59 | 0.09:59
} // CalendarPeriodCollectorSample


void CreateTimes(DateTime start, DateTime end, int interval, TimeSpan work)
{
    DateTime currentStart = start;
    while(currentStart < end && currentStart.Add(work)<end)
    {
        Console.WriteLine(String.Format("{0} - {1}", currentStart, currentStart.Add(work)));
        currentStart.AddMinutes(interval);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号