Calendar with repeating items
Hello I’m a trying to create a feature where an user an add an items that will be repeated on center intervals. But I am having some problems wrapping my head around the math, so I was hoping some of you guys have done something like this before.
It is based on the Google calendar, where an event can be repeated by 4 different methods.
- Daily
- Weekly
- Monthly
- Yearly
Then user then defines what type of repeating the user wants, daily are simple, it is just each day. Now weekly is more tricky, as the user can select 2 options
- Week interval (Repeats every other week, or third and so on)
- What day on the week (Monday, Thursday and so on)
Monthly and Yearly only have 1 option
- Month/Year interval
Beside this data do I have the following variables.
- Time (The time of day the item are to be added)
- StartsOn (The day the item shale start repeating)
- Occurrence (The number of times the item have been executed)
- LastRun (Last time the item was executed)
- NextRun (The next time the item is to be executed)
So I do not have to display future items, is the plan to loop though all items where the NextRun is equel to now, and after the item have been executed will the NextRun be calculated. I have tried myself but it seems to become overly complex, so I where hoping there was an already completed solution out there, or one close to, or simply just a hint to get it on track.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! The result, this have been tested and workes !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Itenso.TimePeriod;
namespace ShoppingList.Library.Objects
{
public class RepeatingItem
{
public int? Id { get; set; }
public int ListId { get; set; }
public string Item { get; set; }
public int Quantity { get; set; }
public RepeatsType Repeats { get; set; }
public string RepeatsVar { get; set; }
public TimeSpan Time { get; set; }
public DateTime StartsOn { get; set; }
public EndsType Ends { get; set; }
public string EndsVar { get; set; }
public int Occurrences { get; set; }
public DateTime? LastRun { get; set; }
public DateTime? NextRun { get; set; }
public enum RepeatsType
{
Daily = 0,
Weekly = 1,
Monthly = 2,
Yearly = 3,
}
public enum EndsType
{
Never = 0,
After = 1,
On = 2
}
public DateTime? CalculateNextRun()
{
DateTime? next = null;
if (Repeats == RepeatsType.Daily)
next = HandelDailyRepeating();
else if (Repeats == RepeatsType.Weekly)
next = HandelWeeklyRepeating();
else if (Repeats == RepeatsType.Monthly)
next = HandelMonthlyRepeating();
else if (Repeats == RepeatsType.Yearly)
next = HandelYearlyRepeating();
if (Ends != EndsType.Never && next != null)
{
if (Ends == EndsType.After)
{
if (Occurrences >= int.Parse(EndsVar))
next = null;
}
else if (Ends == EndsType.On)
{
if (next >= DateTime.Parse(EndsVar))
next = null;
}
}
return next;
}
private DateTime? HandelDailyRepeating()
{
DateTime last;
// If there where a last run no problem.
// but we are not sure that the time on
// start on are right.
if (LastRun != null)
last = GetDateTime((DateTime)LastRun, Time);
else
last = GetDateTime(StartsOn, Time);
DateTime next;
while (last < DateTime.UtcNow || last == LastRun)
{
last = last.AddDays(1);
}
return last;
}
private DateTime? HandelWeeklyRepeating()
{
DateTime last;
// If there where a last run no problem.
// but we are not sure that the time on
// start on are right.
if (LastRun != null)
last = GetDateTime((DateTime)LastRun, Time);
else
last = GetDateTime(StartsOn, Time);
string[] split = RepeatsVar.Split(';');
int recuringInterval = int.Parse(split[0]);
if (recuringInterval > 52)
recuringInterval = 52;
DayOfWeek[] weekDays = new DayOfWeek[split.Count() - 1];
for (int i = 1; i < split.Count(); i++)
{
weekDays[i-1] = (DayOfWeek)int.Parse(split[i]);
}
int days = 0;
bool validFound = false;
while (!validFound && days <= (7 * (recuringInterval + 1)))
{
if (last >= DateTime.UtcNow && IsWeekRecuringDay(StartsOn, last, recuringInterval, weekDays)
&& last != LastRun)
{
return last;
}
else
{
last = last.AddDays(1);
if(last > DateTime.UtcNow) days++;
}
}
return null;
}
private DateTime? HandelMonthlyRepeating()
{
DateTime last;
if (LastRun != null)
last = GetDateTime((DateTime)LastRun, Time);
else
last = GetDateTime(StartsOn, Time);
int recuringInterval = int.Parse(RepeatsVar);
int c = 0;
bool validFound = false;
while (!validFound && c <= (recuringInterval + 1))
{
if (last >= DateTime.UtcNow && IsMonthlyRecuringDay(StartsOn, last, recuringInterval)
&& last != LastRun)
{
return last;
}
else
{
last = last.AddMonths(1);
if (last > DateTime.UtcNow) c++;
}
}
return null;
}
private DateTime? HandelYearlyRepeating()
{
DateTime last;
// If there where a last run no problem.
// but we are not sure that the time on
// start on are right.
if (LastRun != null)
last = GetDateTime((DateTime)LastRun, Time);
else
last = GetDateTime(StartsOn, Time);
int recuringInterval = int.Parse(RepeatsVar);
int c = 0;
bool validFound = false;
while (!validFound && c <= (recuringInterval + 1))
{
if (last >= DateTime.UtcNow && IsYearlyRecuringDay(StartsOn, last, recuringInterval)
&& last != LastRun)
{
return last;
}
else
{
last = last.AddYears(1);
if (last > DateTime.UtcNow) c++;
}
}
return null;
}
public bool IsWeekRecuringDay(DateTime start, DateTime test, int recuringInterval, params DayOfWeek[] weekDays)
{
if (test < start || recuringInterval <= 0)
return false;
bool isValidDayOfWeek = false;
DayOfWeek testDayOfWeek = test.DayOfWeek;
// Is the given day a valid day
foreach (DayOfWeek weekDay in weekDays)
{
if (weekDay == testDayOfWeek)
{
isValidDayOfWeek = true;
break;
开发者_JS百科 }
}
// If the day is not in the list, no need to go further
if (!isValidDayOfWeek)
return false;
DateDiff dateDiff = new DateDiff(GetDateTime(start, new TimeSpan(0, 0, 0)), GetDateTime(test, new TimeSpan(0, 0, 0)));
return (dateDiff.Weeks % recuringInterval) == 0;
}
public bool IsMonthlyRecuringDay(DateTime start, DateTime test, int recuringInterval)
{
if (test < start || recuringInterval <= 0)
return false;
DateDiff dateDiff = new DateDiff(GetDateTime(start, new TimeSpan(0, 0, 0)), GetDateTime(test, new TimeSpan(0, 0, 0)));
return (dateDiff.Months % recuringInterval) == 0;
}
public bool IsYearlyRecuringDay(DateTime start, DateTime test, int recuringInterval)
{
if (test < start || recuringInterval <= 0)
return false;
DateDiff dateDiff = new DateDiff(GetDateTime(start, new TimeSpan(0, 0, 0)), GetDateTime(test, new TimeSpan(0, 0, 0)));
return (dateDiff.Years % recuringInterval) == 0;
}
private DateTime GetDateTime(DateTime d, TimeSpan t)
{
return new DateTime(d.Year, d.Month, d.Day, t.Hours, t.Minutes, t.Seconds); ;
}
private TimeSpan GetTimeSpanFromDateTime(DateTime s)
{
DateTime zero = new DateTime(s.Year, s.Month, s.Day, 0, 0, 0);
return s - zero;
}
}
}
One approach is to check the week difference between the test and start date.
The following sample uses the the DateDiff class of the Time Period Library for .NET to calculate the week differences:
// ----------------------------------------------------------------------
public bool IsWeekRecuringDay( DateTime start, DateTime test, int recuringInterval, params DayOfWeek[] weekDays )
{
if ( test < start || recuringInterval <= 0 )
{
return false;
}
bool isValidDayOfWeek = false;
DayOfWeek testDayOfWeek = test.DayOfWeek;
foreach ( DayOfWeek weekDay in weekDays )
{
if ( weekDay == testDayOfWeek )
{
isValidDayOfWeek = true;
break;
}
}
if ( !isValidDayOfWeek )
{
return false;
}
DateDiff dateDiff = new DateDiff( start, test );
return ( dateDiff.Weeks % recuringInterval ) == 0;
} // IsWeekRecuringDay
And here the usage:
// ----------------------------------------------------------------------
public void WeekRepeatSample()
{
DateTime start = new DateTime( 2011, 06, 1 );
DayOfWeek[] weekDays = new DayOfWeek[] { DayOfWeek.Monday, DayOfWeek.Thursday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };
Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 08 ), 2, weekDays ) ); // false
Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 11 ), 2, weekDays ) ); // false
Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 15 ), 2, weekDays ) ); // true
Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 18 ), 2, weekDays ) ); // false
Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 22 ), 2, weekDays ) ); // false
Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 25 ), 2, weekDays ) ); // false
Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 29 ), 2, weekDays ) ); // true
Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 07, 02 ), 2, weekDays ) ); // false
} // WeekRepeatSample
You can use Quartz.Net as your scheduling engine. It is very powerful and handles all your scheduling needs. It supports all sorts of things you might encounter later when implementing your own scheduler such as exception days (holidays, blackout days, etc.)
精彩评论