├── NuGet
├── RecurrenceCalculator.1.0.0.nupkg
├── RecurrenceCalculator.1.0.1.nupkg
├── RecurrenceCalculator.1.0.2.nupkg
└── RecurrenceCalculator.1.0.3.nupkg
├── RecurrenceCalculator
├── RecurrenceType.cs
├── RecurrenceCalculator.sln
├── RecurrenceCalculator.csproj
├── IRecurrence.cs
├── IWritableRecurrence.cs
├── Recurrence.cs
├── RecurreanceBuilder.cs
└── Calculator.cs
├── RecurrenceCalculator.Tests
├── AppointmentRecurrence.cs
├── RecurrenceCalculator.Tests.csproj
├── RecurrenceBuilderTests.cs
└── RecurrenceTests.cs
├── .github
└── workflows
│ ├── main.yml
│ └── build-publish.yml
├── .gitignore
├── README.md
└── LICENSE
/NuGet/RecurrenceCalculator.1.0.0.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SergeyBarskiy/RecurrenceCalculator/HEAD/NuGet/RecurrenceCalculator.1.0.0.nupkg
--------------------------------------------------------------------------------
/NuGet/RecurrenceCalculator.1.0.1.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SergeyBarskiy/RecurrenceCalculator/HEAD/NuGet/RecurrenceCalculator.1.0.1.nupkg
--------------------------------------------------------------------------------
/NuGet/RecurrenceCalculator.1.0.2.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SergeyBarskiy/RecurrenceCalculator/HEAD/NuGet/RecurrenceCalculator.1.0.2.nupkg
--------------------------------------------------------------------------------
/NuGet/RecurrenceCalculator.1.0.3.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SergeyBarskiy/RecurrenceCalculator/HEAD/NuGet/RecurrenceCalculator.1.0.3.nupkg
--------------------------------------------------------------------------------
/RecurrenceCalculator/RecurrenceType.cs:
--------------------------------------------------------------------------------
1 | namespace RecurrenceCalculator
2 | {
3 | ///
4 | /// Enum RecurrenceType
5 | /// Types of recurrence supported by the calculator
6 | ///
7 | public enum RecurrenceType
8 | {
9 | ///
10 | /// The daily
11 | ///
12 | Daily = 1,
13 | ///
14 | /// The weekly
15 | ///
16 | Weekly = 2,
17 | ///
18 | /// The monthly
19 | ///
20 | Monthly = 3,
21 | ///
22 | /// The N-th day of the month
23 | ///
24 | MonthNth = 4,
25 | ///
26 | /// The yearly
27 | ///
28 | Yearly = 5,
29 | ///
30 | /// The N-th day of the month of the year
31 | ///
32 | YearNth = 6
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/RecurrenceCalculator.Tests/AppointmentRecurrence.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 |
4 | namespace RecurrenceCalculator.Tests
5 | {
6 | public class AppointmentRecurrence : IRecurrence
7 | {
8 | public int DayOfMonth { get; set; }
9 | public bool Sunday { get; set; }
10 | public bool Monday { get; set; }
11 | public bool Tuesday { get; set; }
12 | public bool Wednesday { get; set; }
13 | public bool Thursday { get; set; }
14 | public bool Friday { get; set; }
15 | public bool Saturday { get; set; }
16 | public int Instance { get; set; }
17 | public int Interval { get; set; }
18 | public int MonthOfYear { get; set; }
19 | public int Occurrences { get; set; }
20 | public DateTime StartDate { get; set; }
21 | public RecurrenceType RecurrenceType { get; set; }
22 | public DateTime? EndDate { get; set; }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/RecurrenceCalculator.Tests/RecurrenceCalculator.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0
5 | false
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | all
14 | runtime; build; native; contentfiles; analyzers; buildtransitive
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on:
4 | pull_request:
5 | branches: [ master ]
6 | workflow_dispatch:
7 |
8 | jobs:
9 | build:
10 | env:
11 | BUILD_CONFIG: 'Release'
12 | SOLUTION: 'RecurrenceCalculator.sln'
13 |
14 | runs-on: ubuntu-latest
15 |
16 | steps:
17 | - uses: actions/checkout@v2
18 |
19 | - name: Setup NuGet
20 | uses: NuGet/setup-nuget@v1.0.5
21 | with:
22 | nuget-version: latest
23 |
24 | - name: Setup .NET
25 | uses: actions/setup-dotnet@v3
26 | with:
27 | dotnet-version: 7.0.*
28 |
29 | - name: Restore dependencies
30 | working-directory: ./RecurrenceCalculator
31 | run: dotnet restore $SOLUTION
32 |
33 | - name: Build
34 | working-directory: ./RecurrenceCalculator
35 | run: dotnet build $SOLUTION --configuration $BUILD_CONFIG --no-restore
36 |
37 | - name: Run tests
38 | working-directory: ./RecurrenceCalculator
39 | run: dotnet test $SOLUTION --configuration $BUILD_CONFIG --no-restore --no-build --verbosity normal
40 |
--------------------------------------------------------------------------------
/.github/workflows/build-publish.yml:
--------------------------------------------------------------------------------
1 | name: BuildAndPublish
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | paths-ignore:
7 | - '.github/**'
8 | workflow_dispatch:
9 |
10 | jobs:
11 | build:
12 | env:
13 | BUILD_CONFIG: 'Release'
14 | SOLUTION: 'RecurrenceCalculator.sln'
15 |
16 | runs-on: ubuntu-latest
17 |
18 | steps:
19 | - uses: actions/checkout@v2
20 |
21 | - name: Setup NuGet
22 | uses: NuGet/setup-nuget@v1.0.5
23 | with:
24 | nuget-version: latest
25 |
26 | - name: Setup .NET
27 | uses: actions/setup-dotnet@v3
28 | with:
29 | dotnet-version: 7.0.*
30 |
31 | - name: Restore dependencies
32 | working-directory: ./RecurrenceCalculator
33 | run: dotnet restore $SOLUTION
34 |
35 | - name: Build
36 | working-directory: ./RecurrenceCalculator
37 | run: dotnet build $SOLUTION --configuration $BUILD_CONFIG --no-restore
38 |
39 | - name: Run tests
40 | working-directory: ./RecurrenceCalculator
41 | run: dotnet test $SOLUTION --configuration $BUILD_CONFIG --no-restore --no-build --verbosity normal
42 |
43 | - name: Push to nuget
44 | working-directory: ./RecurrenceCalculator
45 | run: dotnet nuget push **/*.nupkg -s https://api.nuget.org/v3/index.json -k ${{secrets.NUGET_API_KEY}}
46 |
--------------------------------------------------------------------------------
/RecurrenceCalculator/RecurrenceCalculator.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.30501.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RecurrenceCalculator", "RecurrenceCalculator.csproj", "{3D6D608D-1825-4645-80F7-0954516FE0C3}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RecurrenceCalculator.Tests", "..\RecurrenceCalculator.Tests\RecurrenceCalculator.Tests.csproj", "{BF6079B8-0B5D-4AAA-BA49-9FA7E74B1078}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {3D6D608D-1825-4645-80F7-0954516FE0C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {3D6D608D-1825-4645-80F7-0954516FE0C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {3D6D608D-1825-4645-80F7-0954516FE0C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {3D6D608D-1825-4645-80F7-0954516FE0C3}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {BF6079B8-0B5D-4AAA-BA49-9FA7E74B1078}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {BF6079B8-0B5D-4AAA-BA49-9FA7E74B1078}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {BF6079B8-0B5D-4AAA-BA49-9FA7E74B1078}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {BF6079B8-0B5D-4AAA-BA49-9FA7E74B1078}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/RecurrenceCalculator/RecurrenceCalculator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;net6.0;net7.0
5 | true
6 | 3.0.2
7 | RecurrenceCalculator
8 | Sergey Barskiy
9 | Performs Outlook style recurrence calculations
10 | https://github.com/SergeyBarskiy/RecurrenceCalculator/
11 | https://github.com/SergeyBarskiy/RecurrenceCalculator/
12 | GIT
13 | Version 1 Update to .net 6, but still support netstandard 2.0
14 | Version 2 Adds support for different starting day of the week
15 | Version 3 adds support for .net 7, default recurrance class and recurrence bulder with fluent API
16 | Version 3.0.1 fix issue with monthly recurance when start date is greater than day scheduled
17 | @SergeyBarskiy
18 | Apache-2.0
19 | Outlook,recurrence
20 | README.md
21 | True
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | True
31 | \
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.sln.docstates
8 |
9 | # Build results
10 |
11 | [Dd]ebug/
12 | [Rr]elease/
13 | x64/
14 | build/
15 | [Bb]in/
16 | [Oo]bj/
17 |
18 | # MSTest test Results
19 | [Tt]est[Rr]esult*/
20 | [Bb]uild[Ll]og.*
21 |
22 | *_i.c
23 | *_p.c
24 | *.ilk
25 | *.meta
26 | *.obj
27 | *.pch
28 | *.pdb
29 | *.pgc
30 | *.pgd
31 | *.rsp
32 | *.sbr
33 | *.tlb
34 | *.tli
35 | *.tlh
36 | *.tmp
37 | *.tmp_proj
38 | *.log
39 | *.vspscc
40 | *.vssscc
41 | .builds
42 | *.pidb
43 | *.log
44 | *.scc
45 |
46 | # Visual C++ cache files
47 | ipch/
48 | *.aps
49 | *.ncb
50 | *.opensdf
51 | *.sdf
52 | *.cachefile
53 |
54 | # Visual Studio profiler
55 | *.psess
56 | *.vsp
57 | *.vspx
58 |
59 | # Guidance Automation Toolkit
60 | *.gpState
61 |
62 | # ReSharper is a .NET coding add-in
63 | _ReSharper*/
64 | *.[Rr]e[Ss]harper
65 |
66 | # TeamCity is a build add-in
67 | _TeamCity*
68 |
69 | # DotCover is a Code Coverage Tool
70 | *.dotCover
71 |
72 | # NCrunch
73 | *.ncrunch*
74 | .*crunch*.local.xml
75 |
76 | # Installshield output folder
77 | [Ee]xpress/
78 |
79 | # DocProject is a documentation generator add-in
80 | DocProject/buildhelp/
81 | DocProject/Help/*.HxT
82 | DocProject/Help/*.HxC
83 | DocProject/Help/*.hhc
84 | DocProject/Help/*.hhk
85 | DocProject/Help/*.hhp
86 | DocProject/Help/Html2
87 | DocProject/Help/html
88 |
89 | # Click-Once directory
90 | publish/
91 |
92 | # Publish Web Output
93 | #*.Publish.xml
94 | *.pubxml
95 |
96 | # NuGet Packages Directory
97 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line
98 | #packages/
99 |
100 | # Windows Azure Build Output
101 | csx
102 | *.build.csdef
103 |
104 | # Windows Store app package directory
105 | AppPackages/
106 |
107 | # Others
108 | sql/
109 | *.Cache
110 | ClientBin/
111 | [Ss]tyle[Cc]op.*
112 | ~$*
113 | *~
114 | *.dbmdl
115 | #*.[Pp]ublish.xml
116 | *.pfx
117 | *.publishsettings
118 |
119 | # RIA/Silverlight projects
120 | Generated_Code/
121 |
122 | # Backup & report files from converting an old project file to a newer
123 | # Visual Studio version. Backup files are not needed, because we have git ;-)
124 | _UpgradeReport_Files/
125 | Backup*/
126 | UpgradeLog*.XML
127 | UpgradeLog*.htm
128 |
129 | # SQL Server files
130 | App_Data/*.mdf
131 | App_Data/*.ldf
132 |
133 |
134 | #LightSwitch generated files
135 | GeneratedArtifacts/
136 | _Pvt_Extensions/
137 | ModelManifest.xml
138 |
139 | # =========================
140 | # Windows detritus
141 | # =========================
142 |
143 | # Windows image file caches
144 | Thumbs.db
145 | ehthumbs.db
146 |
147 | # Folder config file
148 | Desktop.ini
149 |
150 | # Recycle Bin used on file shares
151 | $RECYCLE.BIN/
152 |
153 | # Mac desktop service store files
154 | .DS_Store
155 | # javascript under app
156 | /Source/Zeus/Zeus.Web/app/*.map
157 | /Source/Zeus/Zeus.Web/app/*.js
158 | /Source/Zeus/Zeus.Web/app/**/**/*.map
159 | /Source/Zeus/Zeus.Web/app/**/**/*.js
160 | /Source/Zeus/Zeus.Web/app/**/*.map
161 | /Source/Zeus/Zeus.Web/app/**/*.js
162 | *.xml
163 | RecurrenceCalculator/.vs/
164 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Recurrence Calculator
2 | ====================
3 | Library written for .NET that perform Outlook style recurrence calculations.
4 | The key to usage is the `IRecurrence` interface that allows you to specify the recurrence pattern.
5 |
6 | I have used for the most part the interface that Outlook supports. You can find the details on MSDN site: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.recurrencepattern(v=office.15).aspx
7 | I made one change - I changed the DayOfWeekMask from flags based enumeration to just 7 flags - one for each day of the week. This will allow you to easily expose the same class at the user interface to allow the user to set flags for the days of the week. In order to support things like day, week day and weekend, you just set appropriate days flags. For example, set all the flags for the "day", Sunday and Saturday for the "weekend". I also decided not to support "no end date", so date is required or number of occurrences.
8 |
9 | It is very easy to use. Implement `IRecurrence` in any of your class, create an instance of the calculator, and call `Calculate` method. An exception will be thrown if your pattern is invalid. You can also call `Validate` manually. If you get an empty string, you are valid. Otherwise you will get the message telling you what is wrong with your pattern.
10 |
11 | The project includes tests that will tell you how to use the calculator.
12 | There is now support for default recurrence class and recurrence fluent API builder
13 |
14 | ```csharp
15 | private Calculator calendarUtility;
16 | private readonly DateTime startDate = new DateTime(2014, 1, 31, 16, 0, 0);
17 |
18 | [TestInitialize]
19 | public void Setup()
20 | {
21 | calendarUtility = new Calculator();
22 | }
23 |
24 | private AppointmentRecurrence CreateWeeklyRecurrence()
25 | {
26 | return new AppointmentRecurrence
27 | {
28 | RecurrenceType = RecurrenceType.Weekly,
29 | Interval = 2,
30 | Sunday = false,
31 | Monday = false,
32 | Tuesday = true,
33 | Wednesday = false,
34 | Thursday = true,
35 | Friday = false,
36 | Saturday = false,
37 | StartDate = startDate,
38 | Occurrences = 5
39 | };
40 | }
41 |
42 | [TestMethod]
43 | public void Should_Generate_Weekly_Recurrences_Without_End_Date()
44 | {
45 | var recurrence = CreateWeeklyRecurrence();
46 | var occurrences = calendarUtility.CalculateOccurrences(recurrence).ToList();
47 | Assert.AreEqual(5, occurrences.Count, "Should create 5 occurrences");
48 | Assert.AreEqual(new DateTime(2014, 2, 11).Add(startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
49 | Assert.AreEqual(new DateTime(2014, 2, 13).Add(startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
50 | Assert.AreEqual(new DateTime(2014, 2, 25).Add(startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
51 | Assert.AreEqual(new DateTime(2014, 2, 27).Add(startDate.TimeOfDay), occurrences[3], "Date 4 should be correct");
52 | Assert.AreEqual(new DateTime(2014, 3, 11).Add(startDate.TimeOfDay), occurrences[4], "Date 5 should be correct");
53 | }
54 | ```
55 |
56 | You can also use NuGet to install the package: https://www.nuget.org/packages/RecurrenceCalculator/
57 | Just type Install-Package RecurrenceCalculator in package manager console window.
58 |
--------------------------------------------------------------------------------
/RecurrenceCalculator.Tests/RecurrenceBuilderTests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using System;
3 | using System.Linq;
4 |
5 | namespace RecurrenceCalculator.Tests
6 | {
7 | [TestClass]
8 | public class RecurrenceBuilderTests
9 | {
10 | private readonly DateTime _startDate = new DateTime(2014, 1, 31, 16, 0, 0);
11 |
12 | [TestMethod]
13 | public void Should_Build_Recurrence()
14 | {
15 | var builder = new RecurrenceBuilder();
16 | var final = builder
17 | .Occurrences(1)
18 | .RecurrenceType(RecurrenceType.Daily)
19 | .Saturday()
20 | .Sunday()
21 | .Monday()
22 | .Tuesday()
23 | .Wednesday()
24 | .Thursday()
25 | .Friday()
26 | .Interval(2)
27 | .DayOfMonth(3)
28 | .MonthOfYear(4)
29 | .Instance(5)
30 | .StartDate(DateTime.Today)
31 | .EndDate(DateTime.Today.AddYears(1))
32 | .Build();
33 |
34 | Assert.IsTrue(final.Sunday, "Should set Sunday");
35 | Assert.IsTrue(final.Monday, "Should set Monday");
36 | Assert.IsTrue(final.Tuesday, "Should set Tuesday");
37 | Assert.IsTrue(final.Wednesday, "Should set Wednesday");
38 | Assert.IsTrue(final.Thursday, "Should set Thursday");
39 | Assert.IsTrue(final.Friday, "Should set Friday");
40 | Assert.IsTrue(final.Saturday, "Should set Saturday");
41 | Assert.AreEqual(RecurrenceType.Daily, final.RecurrenceType, "Should set RecurrenceType");
42 | Assert.AreEqual(1, final.Occurrences, "Should set Occurrences");
43 | Assert.AreEqual(3, final.DayOfMonth, "Should set DayOfMonth");
44 | Assert.AreEqual(4, final.MonthOfYear, "Should set MonthOfYear");
45 | Assert.AreEqual(5, final.Instance, "Should set Instance");
46 | Assert.AreEqual(DateTime.Today, final.StartDate, "Should set StartDate");
47 | Assert.AreEqual(DateTime.Today.AddYears(1), final.EndDate, "Should set EndDate");
48 | Assert.AreEqual(2, final.Interval, "Should set Interval");
49 | }
50 |
51 | [TestMethod]
52 | public void Should_Generate_Daily_Recurrences_With_End_Date()
53 | {
54 | var builder = new RecurrenceBuilder();
55 | var recurrence = builder
56 | .Occurrences(0)
57 | .RecurrenceType(RecurrenceType.Daily)
58 | .Saturday()
59 | .Sunday()
60 | .Monday()
61 | .Tuesday()
62 | .Wednesday()
63 | .Thursday()
64 | .Friday()
65 | .Interval(2)
66 | .StartDate(_startDate)
67 | .EndDate(new DateTime(2014, 2, 19))
68 | .Build();
69 | var occurrences = new Calculator().CalculateOccurrences(recurrence).ToList();
70 | Assert.AreEqual(10, occurrences.Count, "Should create 10 occurrences");
71 | for (int i = 0; i < occurrences.Count - 1; i++)
72 | {
73 | Assert.AreEqual(_startDate.AddDays(i * recurrence.Interval), occurrences[i], "Should have correct date");
74 | }
75 | }
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/RecurrenceCalculator/IRecurrence.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace RecurrenceCalculator
4 | {
5 | ///
6 | /// Interface IRecurrence
7 | /// Recurrence setup information
8 | ///
9 | public interface IRecurrence
10 | {
11 | ///
12 | /// Gets the day of month.
13 | ///
14 | /// The day of month.
15 | int DayOfMonth { get; }
16 |
17 | ///
18 | /// Gets a value indicating whether this event can occur on Sunday.
19 | ///
20 | /// true if event can occur on Sunday; otherwise, false.
21 | bool Sunday { get; }
22 |
23 | ///
24 | /// Gets a value indicating whether this event can occur on Monday.
25 | ///
26 | /// true if event can occur on Monday; otherwise, false.
27 | bool Monday { get; }
28 |
29 | ///
30 | /// Gets a value indicating whether this event can occur on Tuesday.
31 | ///
32 | /// true if event can occur on Tuesday; otherwise, false.
33 | bool Tuesday { get; }
34 |
35 | ///
36 | /// Gets a value indicating whether this event can occur on Wednesday.
37 | ///
38 | /// true if event can occur on Wednesday; otherwise, false.
39 | bool Wednesday { get; }
40 |
41 | ///
42 | /// Gets a value indicating whether this event can occur on Thursday.
43 | ///
44 | /// true if event can occur on Thursday; otherwise, false.
45 | bool Thursday { get; }
46 |
47 | ///
48 | /// Gets a value indicating whether this event can occur on Friday.
49 | ///
50 | /// true if event can occur on Friday; otherwise, false.
51 | bool Friday { get; }
52 |
53 | ///
54 | /// Gets a value indicating whether this event can occur on Saturday.
55 | ///
56 | /// true if event can occur on Saturday; otherwise, false.
57 | bool Saturday { get; }
58 |
59 | ///
60 | /// Gets the instance of the occurrence, such as 1st or 2nd. Set to 5 for the last instance
61 | ///
62 | /// The of the occurrence, such as 1st or 2nd. Set to 5 for the last instance.
63 | int Instance { get; }
64 | ///
65 | /// Gets the interval, such as every 2 days or every 3 years.
66 | ///
67 | /// The interval, such as every 2 days or every 3 years.
68 | int Interval { get; }
69 |
70 | ///
71 | /// Gets the month of year for yearly occurrences.
72 | ///
73 | /// The month of year.
74 | int MonthOfYear { get; }
75 |
76 | ///
77 | /// Gets the total number of occurrences to generate
78 | ///
79 | /// The total number of occurrences to generate.
80 | int Occurrences { get; }
81 |
82 | ///
83 | /// Gets the start date, including time which is appointment type.
84 | ///
85 | /// The start date, including time which is appointment type.
86 | DateTime StartDate { get; }
87 |
88 | ///
89 | /// Gets the type of the recurrence.
90 | ///
91 | /// The type of the recurrence.
92 | RecurrenceType RecurrenceType { get; }
93 |
94 | ///
95 | /// Gets the end date. Can be the last date of the event.
96 | /// Alternative to number of occurrences.
97 | ///
98 | /// The end date. Can be the last date of the event.
99 | /// AAlternative to number of occurrences..
100 | DateTime? EndDate { get; }
101 |
102 | }
103 | }
--------------------------------------------------------------------------------
/RecurrenceCalculator/IWritableRecurrence.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace RecurrenceCalculator
4 | {
5 | ///
6 | /// Interface IWritableRecurrence
7 | /// Recurrence setup information, but with writable properties
8 | ///
9 | public interface IWritableRecurrence : IRecurrence
10 | {
11 | ///
12 | /// Gets the day of month.
13 | ///
14 | /// The day of month.
15 | new int DayOfMonth { get; set; }
16 |
17 | ///
18 | /// Gets a value indicating whether this event can occur on Sunday.
19 | ///
20 | /// true if event can occur on Sunday; otherwise, false.
21 | new bool Sunday { get; set; }
22 |
23 | ///
24 | /// Gets a value indicating whether this event can occur on Monday.
25 | ///
26 | /// true if event can occur on Monday; otherwise, false.
27 | new bool Monday { get; set; }
28 |
29 | ///
30 | /// Gets a value indicating whether this event can occur on Tuesday.
31 | ///
32 | /// true if event can occur on Tuesday; otherwise, false.
33 | new bool Tuesday { get; set; }
34 |
35 | ///
36 | /// Gets a value indicating whether this event can occur on Wednesday.
37 | ///
38 | /// true if event can occur on Wednesday; otherwise, false.
39 | new bool Wednesday { get; set; }
40 |
41 | ///
42 | /// Gets a value indicating whether this event can occur on Thursday.
43 | ///
44 | /// true if event can occur on Thursday; otherwise, false.
45 | new bool Thursday { get; set; }
46 |
47 | ///
48 | /// Gets a value indicating whether this event can occur on Friday.
49 | ///
50 | /// true if event can occur on Friday; otherwise, false.
51 | new bool Friday { get; set; }
52 |
53 | ///
54 | /// Gets a value indicating whether this event can occur on Saturday.
55 | ///
56 | /// true if event can occur on Saturday; otherwise, false.
57 | new bool Saturday { get; set; }
58 |
59 | ///
60 | /// Gets the instance of the occurrence, such as 1st or 2nd. Set to 5 for the last instance
61 | ///
62 | /// The of the occurrence, such as 1st or 2nd. Set to 5 for the last instance.
63 | new int Instance { get; set; }
64 | ///
65 | /// Gets the interval, such as every 2 days or every 3 years.
66 | ///
67 | /// The interval, such as every 2 days or every 3 years.
68 | new int Interval { get; set; }
69 |
70 | ///
71 | /// Gets the month of year for yearly occurrences.
72 | ///
73 | /// The month of year.
74 | new int MonthOfYear { get; set; }
75 |
76 | ///
77 | /// Gets the total number of occurrences to generate
78 | ///
79 | /// The total number of occurrences to generate.
80 | new int Occurrences { get; set; }
81 |
82 | ///
83 | /// Gets the start date, including time which is appointment type.
84 | ///
85 | /// The start date, including time which is appointment type.
86 | new DateTime StartDate { get; set; }
87 |
88 | ///
89 | /// Gets the type of the recurrence.
90 | ///
91 | /// The type of the recurrence.
92 | new RecurrenceType RecurrenceType { get; set; }
93 |
94 | ///
95 | /// Gets the end date. Can be the last date of the event.
96 | /// AAlternative to number of occurrences.
97 | ///
98 | /// The end date. Can be the last date of the event.
99 | /// AAlternative to number of occurrences..
100 | new DateTime? EndDate { get; set; }
101 |
102 | }
103 | }
--------------------------------------------------------------------------------
/RecurrenceCalculator/Recurrence.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace RecurrenceCalculator
4 | {
5 | ///
6 | /// Default implementation of
7 | /// Recurrence setup information, but with writable properties
8 | ///
9 | public class Recurrence : IWritableRecurrence
10 | {
11 | ///
12 | /// Gets the day of month.
13 | ///
14 | /// The day of month.
15 | public int DayOfMonth { get; set; }
16 |
17 | ///
18 | /// Gets a value indicating whether this event can occur on Sunday.
19 | ///
20 | /// true if event can occur on Sunday; otherwise, false.
21 | public bool Sunday { get; set; }
22 |
23 | ///
24 | /// Gets a value indicating whether this event can occur on Monday.
25 | ///
26 | /// true if event can occur on Monday; otherwise, false.
27 | public bool Monday { get; set; }
28 |
29 | ///
30 | /// Gets a value indicating whether this event can occur on Tuesday.
31 | ///
32 | /// true if event can occur on Tuesday; otherwise, false.
33 | public bool Tuesday { get; set; }
34 |
35 | ///
36 | /// Gets a value indicating whether this event can occur on Wednesday.
37 | ///
38 | /// true if event can occur on Wednesday; otherwise, false.
39 | public bool Wednesday { get; set; }
40 |
41 | ///
42 | /// Gets a value indicating whether this event can occur on Thursday.
43 | ///
44 | /// true if event can occur on Thursday; otherwise, false.
45 | public bool Thursday { get; set; }
46 |
47 | ///
48 | /// Gets a value indicating whether this event can occur on Friday.
49 | ///
50 | /// true if event can occur on Friday; otherwise, false.
51 | public bool Friday { get; set; }
52 |
53 | ///
54 | /// Gets a value indicating whether this event can occur on Saturday.
55 | ///
56 | /// true if event can occur on Saturday; otherwise, false.
57 | public bool Saturday { get; set; }
58 |
59 | ///
60 | /// Gets the instance of the occurrence, such as 1st or 2nd. Set to 5 for the last instance
61 | ///
62 | /// The of the occurrence, such as 1st or 2nd. Set to 5 for the last instance.
63 | public int Instance { get; set; }
64 | ///
65 | /// Gets the interval, such as every 2 days or every 3 years.
66 | ///
67 | /// The interval, such as every 2 days or every 3 years.
68 | public int Interval { get; set; }
69 |
70 | ///
71 | /// Gets the month of year for yearly occurrences.
72 | ///
73 | /// The month of year.
74 | public int MonthOfYear { get; set; }
75 |
76 | ///
77 | /// Gets the total number of occurrences to generate
78 | ///
79 | /// The total number of occurrences to generate.
80 | public int Occurrences { get; set; }
81 |
82 | ///
83 | /// Gets the start date, including time which is appointment type.
84 | ///
85 | /// The start date, including time which is appointment type.
86 | public DateTime StartDate { get; set; }
87 |
88 | ///
89 | /// Gets the type of the recurrence.
90 | ///
91 | /// The type of the recurrence.
92 | public RecurrenceType RecurrenceType { get; set; }
93 |
94 | ///
95 | /// Gets the end date. Can be the last date of the event.
96 | /// AAlternative to number of occurrences.
97 | ///
98 | /// The end date. Can be the last date of the event.
99 | /// AAlternative to number of occurrences..
100 | public DateTime? EndDate { get; set; }
101 |
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/RecurrenceCalculator/RecurreanceBuilder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace RecurrenceCalculator
4 | {
5 | ///
6 | /// RecurrenceBuilder
7 | /// Allows to build recurrance configuration in a fluent API
8 | ///
9 | public class RecurrenceBuilder
10 | where T: IWritableRecurrence, new()
11 | {
12 | private readonly T _recurrence;
13 |
14 | ///
15 | /// New instance of
16 | ///
17 | public RecurrenceBuilder()
18 | {
19 | _recurrence = new T();
20 | }
21 |
22 | ///
23 | ///
24 | ///
25 | /// Day of the month
26 | ///
27 | public RecurrenceBuilder DayOfMonth(int dayOfMonth)
28 | {
29 | _recurrence.DayOfMonth = dayOfMonth;
30 | return this;
31 | }
32 |
33 | ///
34 | /// Gets a value indicating whether this event can occur on Sunday.
35 | ///
36 | /// True, if occurs on Sundays
37 | ///
38 | public RecurrenceBuilder Sunday(bool sunday = true)
39 | {
40 | _recurrence.Sunday = sunday;
41 | return this;
42 | }
43 |
44 | ///
45 | /// Gets a value indicating whether this event can occur on Monday.
46 | ///
47 | /// True, if occurs on Mondays
48 | ///
49 | public RecurrenceBuilder Monday(bool monday = true)
50 | {
51 | _recurrence.Monday = monday;
52 | return this;
53 | }
54 |
55 | ///
56 | /// Gets a value indicating whether this event can occur on Tuesday.
57 | ///
58 | /// True, if occurs on Tuesday
59 | ///
60 | public RecurrenceBuilder Tuesday(bool tuesday = true)
61 | {
62 | _recurrence.Tuesday = tuesday;
63 | return this;
64 | }
65 |
66 | ///
67 | /// Gets a value indicating whether this event can occur on Wednesday.
68 | ///
69 | /// True, if occurs on Wednesday
70 | ///
71 | public RecurrenceBuilder Wednesday(bool wednesday = true)
72 | {
73 | _recurrence.Wednesday = wednesday;
74 | return this;
75 | }
76 |
77 | ///
78 | /// Gets a value indicating whether this event can occur on Thursday.
79 | ///
80 | /// True, if occurs on Thursday
81 | ///
82 | public RecurrenceBuilder Thursday(bool thursday = true)
83 | {
84 | _recurrence.Thursday = thursday;
85 | return this;
86 | }
87 |
88 | ///
89 | /// Gets a value indicating whether this event can occur on Friday.
90 | ///
91 | /// True, if occurs on Friday
92 | ///
93 | public RecurrenceBuilder Friday(bool friday = true)
94 | {
95 | _recurrence.Friday = friday;
96 | return this;
97 | }
98 |
99 | ///
100 | /// Gets a value indicating whether this event can occur on Saturday.
101 | ///
102 | /// True, if occurs on Saturday
103 | ///
104 | public RecurrenceBuilder Saturday(bool saturday = true)
105 | {
106 | _recurrence.Saturday = saturday;
107 | return this;
108 | }
109 |
110 | ///
111 | /// The instance of the occurrence, such as 1st or 2nd. Set to 5 for the last instance
112 | ///
113 | /// The instance of the occurrence, such as 1st or 2nd. Set to 5 for the last instance
114 | ///
115 | public RecurrenceBuilder Instance(int instance)
116 | {
117 | _recurrence.Instance = instance;
118 | return this;
119 | }
120 |
121 | ///
122 | /// The interval, such as every 2 days or every 3 years.
123 | ///
124 | /// The interval, such as every 2 days or every 3 years.
125 | ///
126 | public RecurrenceBuilder Interval(int interval)
127 | {
128 | _recurrence.Interval = interval;
129 | return this;
130 | }
131 |
132 | ///
133 | /// The month of year for yearly occurrences.
134 | ///
135 | /// The month of year for yearly occurrences.
136 | ///
137 | public RecurrenceBuilder MonthOfYear(int monthOfYear)
138 | {
139 | _recurrence.MonthOfYear = monthOfYear;
140 | return this;
141 | }
142 |
143 | ///
144 | /// The total number of occurrences to generate
145 | ///
146 | /// The total number of occurrences to generate
147 | ///
148 | public RecurrenceBuilder Occurrences(int occurrences)
149 | {
150 | _recurrence.Occurrences = occurrences;
151 | return this;
152 | }
153 |
154 | ///
155 | /// The start date, including time which is appointment type.
156 | ///
157 | /// The start date, including time which is appointment type.
158 | ///
159 | public RecurrenceBuilder StartDate(DateTime startDate)
160 | {
161 | _recurrence.StartDate = startDate;
162 | return this;
163 | }
164 |
165 | ///
166 | /// The type of the recurrence.
167 | ///
168 | /// Rhe type of the recurrence.
169 | ///
170 | public RecurrenceBuilder RecurrenceType(RecurrenceType recurrenceType)
171 | {
172 | _recurrence.RecurrenceType = recurrenceType;
173 | return this;
174 | }
175 |
176 | ///
177 | /// The end date. Can be the last date of the event.
178 | /// Alternative to number of occurrences.
179 | ///
180 | /// The end date. Can be the last date of the event. Alternative to number of occurrences.
181 | ///
182 | public RecurrenceBuilder EndDate(DateTime endDate)
183 | {
184 | _recurrence.EndDate = endDate;
185 | return this;
186 | }
187 |
188 | ///
189 | /// Build and return the final recurrance instance
190 | ///
191 | /// Recurrance
192 | public T Build()
193 | {
194 | return _recurrence;
195 | }
196 | }
197 | }
198 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
--------------------------------------------------------------------------------
/RecurrenceCalculator/Calculator.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace RecurrenceCalculator
5 | {
6 |
7 | ///
8 | /// Class Calculator.
9 | /// Performs occurrence calculations based on recurrence pattern
10 | ///
11 | public class Calculator
12 | {
13 | ///
14 | /// New instance of
15 | /// First day of week will be set to Sunday
16 | ///
17 | public Calculator() : this(DayOfWeek.Sunday) { }
18 |
19 | ///
20 | /// New instance of
21 | /// First day of week will be set to Sunday
22 | ///
23 | /// First day of the week
24 | public Calculator(DayOfWeek firstDayOfWeek)
25 | {
26 | FirstDayOfWeek = firstDayOfWeek;
27 | }
28 |
29 | ///
30 | /// Configurable first day of the week as different cultures can use Monday or Sunday.
31 | /// Defaulted to Sunday but can be overridden by setting the property at runtime
32 | ///
33 | public DayOfWeek FirstDayOfWeek { get; private set; } = DayOfWeek.Sunday;
34 |
35 | ///
36 | /// Calculates the occurrences.
37 | ///
38 | /// The recurrence pattern.
39 | /// The dates for occurrences.
40 | /// Occurs when recurrence patter is invalid
41 | /// Occurs when type of recurrence is invalid
42 | public IEnumerable CalculateOccurrences(IRecurrence recurrence)
43 | {
44 | var validationResult = ValidateRecurrence(recurrence);
45 | if (!string.IsNullOrEmpty(validationResult))
46 | {
47 | throw new ArgumentException(validationResult);
48 | }
49 | switch (recurrence.RecurrenceType)
50 | {
51 | case RecurrenceType.Daily:
52 | return GetDailyRecurrenceOccurrences(recurrence);
53 | case RecurrenceType.Weekly:
54 | return GetWeeklyRecurrenceOccurrences(recurrence);
55 | case RecurrenceType.Monthly:
56 | return GetMonthlyRecurrenceOccurrences(recurrence);
57 | case RecurrenceType.MonthNth:
58 | return GetMonthNthRecurrenceOccurrences(recurrence);
59 | case RecurrenceType.Yearly:
60 | return GetYearlyRecurrenceOccurrences(recurrence);
61 | case RecurrenceType.YearNth:
62 | return GetYearNthRecurrenceOccurrences(recurrence);
63 | default:
64 | throw new ArgumentOutOfRangeException();
65 | }
66 |
67 | }
68 |
69 |
70 | ///
71 | /// Gets the daily recurrence occurrences.
72 | ///
73 | /// The recurrence pattern.
74 | /// The dates for occurrences.
75 | private IEnumerable GetDailyRecurrenceOccurrences(IRecurrence recurrence)
76 | {
77 | DateTime date = recurrence.StartDate.Date;
78 | while (!IsDayOfTheWeekMatched(date, recurrence))
79 | {
80 | date = date.AddDays(1);
81 | }
82 |
83 | int occurenceCounter = 0;
84 | while (!GeneratedEnoughOccurrences(occurenceCounter++, date, recurrence))
85 | {
86 | yield return date.Add(recurrence.StartDate.TimeOfDay);
87 |
88 | date = IsWeekdaySet(recurrence)
89 | ? (date.AddDays((date.DayOfWeek == DayOfWeek.Friday) ? 3 : 1))
90 | : date.AddDays(recurrence.Interval);
91 | }
92 | }
93 |
94 | ///
95 | /// Gets the weekly recurrence occurrences.
96 | ///
97 | /// The recurrence pattern.
98 | /// The dates for occurrences.
99 | private IEnumerable GetWeeklyRecurrenceOccurrences(IRecurrence recurrence)
100 | {
101 | DateTime date = recurrence.StartDate.Date;
102 | // find first day of the week - start of first week
103 | while (date.DayOfWeek != FirstDayOfWeek)
104 | {
105 | date = date.AddDays(-1);
106 | }
107 |
108 | int occurenceCounter = 0;
109 | while (!GeneratedEnoughOccurrences(occurenceCounter, date, recurrence))
110 | {
111 | // run through this week
112 | for (int day = 0; day < 7; ++day)
113 | {
114 | var tempDate = date.AddDays(day);
115 | if (GeneratedEnoughOccurrences(occurenceCounter, tempDate, recurrence))
116 | {
117 | break;
118 | }
119 |
120 | if (tempDate >= recurrence.StartDate.Date && IsDayOfTheWeekMatched(tempDate, recurrence))
121 | {
122 | yield return tempDate.Add(recurrence.StartDate.TimeOfDay);
123 | ++occurenceCounter;
124 | }
125 | }
126 | // advance to the next week based on number of weeks to skip
127 | date = date.AddDays(7 * recurrence.Interval);
128 | }
129 | }
130 |
131 | ///
132 | /// Gets the monthly recurrence occurrences.
133 | ///
134 | /// The recurrence pattern.
135 | /// The dates for occurrences.
136 | private IEnumerable GetMonthlyRecurrenceOccurrences(IRecurrence recurrence)
137 | {
138 | DateTime date = recurrence.StartDate.Date;
139 | if (date.Day > recurrence.DayOfMonth)
140 | {
141 | date = date.AddMonths(recurrence.Interval);
142 | date = date.AddDays(-1 * (date.Day - 1));
143 | }
144 |
145 | int occurenceCounter = 0;
146 | while (!GeneratedEnoughOccurrences(occurenceCounter++, date, recurrence))
147 | {
148 | if (date.Day < recurrence.DayOfMonth)
149 | {
150 | date = date.AddDays(((recurrence.DayOfMonth > DateTime.DaysInMonth(date.Year, date.Month))
151 | ? DateTime.DaysInMonth(date.Year, date.Month)
152 | : recurrence.DayOfMonth) - date.Day);
153 | }
154 |
155 | yield return date.Add(recurrence.StartDate.TimeOfDay);
156 | date = date.AddMonths(recurrence.Interval);
157 | }
158 | }
159 |
160 |
161 | ///
162 | /// Gets the month NTH recurrence occurrences.
163 | ///
164 | /// The recurrence pattern.
165 | /// The dates for occurrences.
166 | private IEnumerable GetMonthNthRecurrenceOccurrences(IRecurrence recurrence)
167 | {
168 | var date = GetMonthNthDate(recurrence, new DateTime(recurrence.StartDate.Year, recurrence.StartDate.Month, 1));
169 | if (date < recurrence.StartDate.Date)
170 | {
171 | date = GetMonthNthDate(recurrence,
172 | new DateTime(recurrence.StartDate.Year, recurrence.StartDate.Month, 1).AddMonths(recurrence.Interval));
173 | }
174 |
175 | int occurenceCounter = 0;
176 | while (!GeneratedEnoughOccurrences(occurenceCounter++, date, recurrence))
177 | {
178 | yield return date.Add(recurrence.StartDate.TimeOfDay);
179 | date = GetMonthNthDate(recurrence, new DateTime(date.Year, date.Month, 1).AddMonths(recurrence.Interval));
180 | }
181 | }
182 |
183 | ///
184 | /// Gets the yearly recurrence occurrences.
185 | ///
186 | /// The recurrence pattern.
187 | /// The dates for occurrences.
188 | private IEnumerable GetYearlyRecurrenceOccurrences(IRecurrence recurrence)
189 | {
190 | var lastDayOfMonth = DateTime.DaysInMonth(recurrence.StartDate.Year, recurrence.MonthOfYear);
191 | var date = new DateTime(recurrence.StartDate.Year, recurrence.MonthOfYear,
192 | lastDayOfMonth > recurrence.DayOfMonth ? recurrence.DayOfMonth : lastDayOfMonth);
193 | if (date < recurrence.StartDate.Date)
194 | {
195 | lastDayOfMonth = DateTime.DaysInMonth(recurrence.StartDate.Year + recurrence.Interval, recurrence.MonthOfYear);
196 | date = new DateTime(recurrence.StartDate.Year + recurrence.Interval, recurrence.MonthOfYear,
197 | lastDayOfMonth > recurrence.DayOfMonth ? recurrence.DayOfMonth : lastDayOfMonth);
198 | }
199 |
200 | int occurenceCounter = 0;
201 | while (!GeneratedEnoughOccurrences(occurenceCounter++, date, recurrence))
202 | {
203 | yield return date.Add(recurrence.StartDate.TimeOfDay);
204 |
205 | lastDayOfMonth = DateTime.DaysInMonth(date.Year + recurrence.Interval, recurrence.MonthOfYear);
206 | date = new DateTime(date.Year + recurrence.Interval, recurrence.MonthOfYear,
207 | lastDayOfMonth > recurrence.DayOfMonth ? recurrence.DayOfMonth : lastDayOfMonth);
208 | }
209 | }
210 |
211 |
212 |
213 | ///
214 | /// Gets the year NTH recurrence occurrences.
215 | ///
216 | /// The recurrence pattern.
217 | /// The dates for occurrences.
218 | private IEnumerable GetYearNthRecurrenceOccurrences(IRecurrence recurrence)
219 | {
220 | var date = GetMonthNthDate(recurrence, new DateTime(recurrence.StartDate.Year, recurrence.MonthOfYear, 1));
221 | if (date < recurrence.StartDate.Date)
222 | {
223 | date = GetMonthNthDate(recurrence, new DateTime(recurrence.StartDate.Year + recurrence.Interval, recurrence.MonthOfYear, 1));
224 | }
225 |
226 | int occurenceCounter = 0;
227 | while (!GeneratedEnoughOccurrences(occurenceCounter++, date, recurrence))
228 | {
229 | yield return date.Add(recurrence.StartDate.TimeOfDay);
230 | date = GetMonthNthDate(recurrence, new DateTime(date.Year + recurrence.Interval, recurrence.MonthOfYear, 1));
231 | }
232 | }
233 |
234 | #region Helper Methods
235 |
236 |
237 |
238 | ///
239 | /// Determines whether [is at least one day checked] [the specified recurrence].
240 | ///
241 | /// The recurrence pattern.
242 | /// true if [is at least one day checked] [the specified recurrence]; otherwise, false.
243 | private bool IsAtLeastOneDayChecked(IRecurrence recurrence)
244 | {
245 | return recurrence.Sunday ||
246 | recurrence.Monday ||
247 | recurrence.Tuesday ||
248 | recurrence.Wednesday ||
249 | recurrence.Thursday ||
250 | recurrence.Friday ||
251 | recurrence.Saturday;
252 | }
253 |
254 | ///
255 | /// Determines whether [is weekday set] [the specified recurrence].
256 | ///
257 | /// The recurrence pattern.
258 | /// true if [is weekday set] [the specified recurrence]; otherwise, false.
259 | private bool IsWeekdaySet(IRecurrence recurrence)
260 | {
261 | return !recurrence.Sunday &&
262 | recurrence.Monday &&
263 | recurrence.Tuesday &&
264 | recurrence.Wednesday &&
265 | recurrence.Thursday &&
266 | recurrence.Friday &&
267 | !recurrence.Saturday;
268 | }
269 |
270 | ///
271 | /// Determines whether [is weekend set] [the specified recurrence].
272 | ///
273 | /// The recurrence pattern.
274 | /// true if [is weekend set] [the specified recurrence]; otherwise, false.
275 | private bool IsWeekendSet(IRecurrence recurrence)
276 | {
277 | return recurrence.Sunday &&
278 | !recurrence.Monday &&
279 | !recurrence.Tuesday &&
280 | !recurrence.Wednesday &&
281 | !recurrence.Thursday &&
282 | !recurrence.Friday &&
283 | recurrence.Saturday;
284 | }
285 |
286 |
287 | ///
288 | /// Determines whether [is date matching recurrence day of week] [the specified recurrence].
289 | ///
290 | /// The recurrence pattern.
291 | /// The date.
292 | /// true if [is date matching recurrence day of week] [the specified recurrence]; otherwise, false.
293 | private bool IsDateMatchingRecurrenceDayOfWeek(IRecurrence recurrence, DateTime date)
294 | {
295 | return
296 | (IsWeekend(date) && IsWeekendSet(recurrence)) ||
297 | (!IsWeekend(date) && IsWeekdaySet(recurrence)) ||
298 | (!IsWeekdaySet(recurrence) && !IsWeekendSet(recurrence) && IsDayOfTheWeekMatched(date, recurrence));
299 |
300 | }
301 |
302 | ///
303 | /// Generateds the enough occurrences.
304 | ///
305 | /// The current count.
306 | /// The current date.
307 | /// The recurrence pattern.
308 | /// true if XXXX, false otherwise.
309 | private bool GeneratedEnoughOccurrences(int currentCount, DateTime currentDate, IRecurrence recurrence)
310 | {
311 | return
312 | (!recurrence.EndDate.HasValue && currentCount >= recurrence.Occurrences) ||
313 | (recurrence.EndDate.HasValue && currentDate.Date > recurrence.EndDate.Value.Date);
314 |
315 | }
316 |
317 | ///
318 | /// Determines whether [is day of the week matched] [the specified date].
319 | ///
320 | /// The date.
321 | /// The recurrence pattern.
322 | /// true if [is day of the week matched] [the specified date]; otherwise, false.
323 | private bool IsDayOfTheWeekMatched(DateTime date, IRecurrence recurrence)
324 | {
325 | return
326 | (date.DayOfWeek == DayOfWeek.Sunday && recurrence.Sunday) ||
327 | (date.DayOfWeek == DayOfWeek.Monday && recurrence.Monday) ||
328 | (date.DayOfWeek == DayOfWeek.Tuesday && recurrence.Tuesday) ||
329 | (date.DayOfWeek == DayOfWeek.Wednesday && recurrence.Wednesday) ||
330 | (date.DayOfWeek == DayOfWeek.Thursday && recurrence.Thursday) ||
331 | (date.DayOfWeek == DayOfWeek.Friday && recurrence.Friday) ||
332 | (date.DayOfWeek == DayOfWeek.Saturday && recurrence.Saturday);
333 | }
334 |
335 | ///
336 | /// Determines whether the specified date is weekend.
337 | ///
338 | /// The date.
339 | /// true if the specified date is weekend; otherwise, false.
340 | private bool IsWeekend(DateTime date)
341 | {
342 | return date.DayOfWeek == DayOfWeek.Sunday || date.DayOfWeek == DayOfWeek.Saturday;
343 | }
344 |
345 |
346 | ///
347 | /// Gets the month NTH date.
348 | ///
349 | /// The recurrence pattern.
350 | /// The start date.
351 | /// DateTime.
352 | private DateTime GetMonthNthDate(IRecurrence recurrence, DateTime startDate)
353 | {
354 | var date = new DateTime(startDate.Year, startDate.Month, 1);
355 | var instance = 1;
356 | if (recurrence.Instance > 4)
357 | {
358 | //last
359 | for (int day = DateTime.DaysInMonth(date.Year, date.Month); day >= 1; day--)
360 | {
361 | date = new DateTime(date.Year, date.Month, day);
362 | if (IsDateMatchingRecurrenceDayOfWeek(recurrence, date))
363 | {
364 | break;
365 | }
366 | }
367 |
368 | }
369 | else
370 | {
371 | //specific
372 | for (int day = 1; day <= DateTime.DaysInMonth(date.Year, date.Month); day++)
373 | {
374 | date = new DateTime(date.Year, date.Month, day);
375 | if (IsDateMatchingRecurrenceDayOfWeek(recurrence, date))
376 | {
377 | if (instance == recurrence.Instance)
378 | {
379 | break;
380 | }
381 | instance++;
382 | }
383 | }
384 | }
385 |
386 |
387 | return date;
388 |
389 | }
390 |
391 |
392 |
393 | #endregion
394 |
395 | ///
396 | /// Validates The recurrence pattern.
397 | ///
398 | /// The recurrence pattern.
399 | /// System.String.
400 | public string ValidateRecurrence(IRecurrence recurrence)
401 | {
402 | if (recurrence.Occurrences < 1 && recurrence.EndDate == null)
403 | {
404 | return "Number of occurrences or end date is required.";
405 | }
406 | if (recurrence.Occurrences > 0 && recurrence.EndDate.HasValue)
407 | {
408 | return "Cannot set both number of occurrences and end date.";
409 | }
410 | if (recurrence.EndDate.HasValue && recurrence.StartDate.Date > recurrence.EndDate.Value.Date)
411 | {
412 | return "Start date must be before end date.";
413 | }
414 |
415 | if (recurrence.RecurrenceType == RecurrenceType.Daily)
416 | {
417 | if (recurrence.Interval == 0 && !IsAtLeastOneDayChecked(recurrence))
418 | {
419 | return "Daily recurrence requires a day to occur on";
420 | }
421 | }
422 | else if (recurrence.RecurrenceType == RecurrenceType.Weekly)
423 | {
424 | if (recurrence.Interval < 1 || !IsAtLeastOneDayChecked(recurrence))
425 | {
426 | return "Weekly recurrence required an interval and day to occur on.";
427 | }
428 | }
429 | else if (recurrence.RecurrenceType == RecurrenceType.Monthly)
430 | {
431 | if (recurrence.Interval < 1 || recurrence.DayOfMonth < 1)
432 | {
433 | return "Monthly recurrence requires an interval and day of the month.";
434 | }
435 | }
436 | else if (recurrence.RecurrenceType == RecurrenceType.MonthNth)
437 | {
438 | if (recurrence.Interval < 1 || recurrence.Instance < 1 || recurrence.Instance > 5 || !IsAtLeastOneDayChecked(recurrence))
439 | {
440 | return "Monthly recurrence required an interval, instance and day of the week.";
441 | }
442 | }
443 | else if (recurrence.RecurrenceType == RecurrenceType.Yearly)
444 | {
445 | if (recurrence.Interval < 1 || recurrence.DayOfMonth < 1 || recurrence.MonthOfYear < 1 || recurrence.MonthOfYear > 12)
446 | {
447 | return "Yearly recurrence required an interval, day of the month and month of the year.";
448 | }
449 | }
450 | else if (recurrence.RecurrenceType == RecurrenceType.YearNth)
451 | {
452 | if (recurrence.Interval < 0 || recurrence.Instance < 1 || recurrence.MonthOfYear < 1 ||
453 | !IsAtLeastOneDayChecked(recurrence))
454 | {
455 | return "Yearly recurrence required an interval, instance, day of the week and month of the year.";
456 | }
457 | }
458 | return string.Empty;
459 | }
460 | }
461 | }
462 |
--------------------------------------------------------------------------------
/RecurrenceCalculator.Tests/RecurrenceTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Globalization;
3 | using System.Linq;
4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
5 |
6 | namespace RecurrenceCalculator.Tests
7 | {
8 |
9 | [TestClass]
10 | public class RecurrenceTests
11 | {
12 | private Calculator _calendarUtility;
13 | private readonly DateTime _startDate = new DateTime(2014, 1, 31, 16, 0, 0);
14 |
15 | [TestInitialize]
16 | public void Setup()
17 | {
18 | _calendarUtility = new Calculator();
19 | }
20 |
21 | [TestMethod]
22 | public void Should_Generate_Daily_Recurrences_Without_End_Date()
23 | {
24 | var recurrence = CreateDailyRecurrence();
25 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
26 | Assert.AreEqual(10, occurrences.Count, "Should create 10 occurrences");
27 | for (int i = 0; i < occurrences.Count - 1; i++)
28 | {
29 | Assert.AreEqual(_startDate.AddDays(i * recurrence.Interval), occurrences[i], "Should have correct date");
30 | }
31 | }
32 |
33 | [TestMethod]
34 | public void Should_Generate_Daily_Recurrences_With_End_Date()
35 | {
36 | var recurrence = CreateDailyRecurrence();
37 | recurrence.Occurrences = 0;
38 | recurrence.EndDate = new DateTime(2014, 2, 19);
39 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
40 | Assert.AreEqual(10, occurrences.Count, "Should create 10 occurrences");
41 | for (int i = 0; i < occurrences.Count - 1; i++)
42 | {
43 | Assert.AreEqual(_startDate.AddDays(i * recurrence.Interval), occurrences[i], "Should have correct date");
44 | }
45 | }
46 |
47 | [TestMethod]
48 | public void Should_Generate_Daily_Weekday_Recurrences_Without_End_Date()
49 | {
50 | var recurrence = CreateDailyWeekdayRecurrence();
51 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
52 | Assert.AreEqual(3, occurrences.Count, "Should create 10 occurrences");
53 | Assert.AreEqual(new DateTime(2014, 1, 31).Add(_startDate.TimeOfDay), occurrences[0], "First date should be correct");
54 | Assert.AreEqual(new DateTime(2014, 2, 3).Add(_startDate.TimeOfDay), occurrences[1], "Second date should be correct");
55 | Assert.AreEqual(new DateTime(2014, 2, 4).Add(_startDate.TimeOfDay), occurrences[2], "Third date should be correct");
56 | }
57 |
58 | [TestMethod]
59 | public void Should_Generate_Daily_Weekday_Recurrences_With_End_Date()
60 | {
61 | var recurrence = CreateDailyWeekdayRecurrence();
62 | recurrence.Occurrences = 0;
63 | recurrence.EndDate = new DateTime(2014, 2, 10);
64 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
65 | Assert.AreEqual(7, occurrences.Count, "Should create 7 occurrences");
66 | Assert.AreEqual(new DateTime(2014, 1, 31).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
67 | Assert.AreEqual(new DateTime(2014, 2, 3).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
68 | Assert.AreEqual(new DateTime(2014, 2, 4).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
69 | Assert.AreEqual(new DateTime(2014, 2, 5).Add(_startDate.TimeOfDay), occurrences[3], "Date 4 should be correct");
70 | Assert.AreEqual(new DateTime(2014, 2, 6).Add(_startDate.TimeOfDay), occurrences[4], "Date 5 should be correct");
71 | Assert.AreEqual(new DateTime(2014, 2, 7).Add(_startDate.TimeOfDay), occurrences[5], "Date 6 should be correct");
72 | Assert.AreEqual(new DateTime(2014, 2, 10).Add(_startDate.TimeOfDay), occurrences[6], "Date 7 should be correct");
73 | }
74 |
75 | [TestMethod]
76 | public void Should_Generate_Weekly_Recurrences_Without_End_Date()
77 | {
78 | var recurrence = CreateWeeklyRecurrence();
79 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
80 | Assert.AreEqual(5, occurrences.Count, "Should create 5 occurrences");
81 | Assert.AreEqual(new DateTime(2014, 2, 11).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
82 | Assert.AreEqual(new DateTime(2014, 2, 13).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
83 | Assert.AreEqual(new DateTime(2014, 2, 25).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
84 | Assert.AreEqual(new DateTime(2014, 2, 27).Add(_startDate.TimeOfDay), occurrences[3], "Date 4 should be correct");
85 | Assert.AreEqual(new DateTime(2014, 3, 11).Add(_startDate.TimeOfDay), occurrences[4], "Date 5 should be correct");
86 | }
87 |
88 | [TestMethod]
89 | public void Should_Generate_Weekly_Recurrences_With_End_Date()
90 | {
91 | var recurrence = CreateWeeklyRecurrence();
92 | recurrence.Occurrences = 0;
93 | recurrence.EndDate = new DateTime(2014, 3, 11);
94 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
95 | Assert.AreEqual(5, occurrences.Count, "Should create 5 occurrences");
96 | Assert.AreEqual(new DateTime(2014, 2, 11).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
97 | Assert.AreEqual(new DateTime(2014, 2, 13).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
98 | Assert.AreEqual(new DateTime(2014, 2, 25).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
99 | Assert.AreEqual(new DateTime(2014, 2, 27).Add(_startDate.TimeOfDay), occurrences[3], "Date 4 should be correct");
100 | Assert.AreEqual(new DateTime(2014, 3, 11).Add(_startDate.TimeOfDay), occurrences[4], "Date 5 should be correct");
101 | }
102 |
103 | [TestMethod]
104 | public void Should_Generate_Weekly_Recurrences_FirstDayOfWeek_Monday_FirstOccurrence_Is_At_First_Occurrence()
105 | {
106 | var recurrence = new AppointmentRecurrence
107 | {
108 | RecurrenceType = RecurrenceType.Weekly,
109 | Interval = 3,
110 | Sunday = true,
111 | Monday = false,
112 | Tuesday = false,
113 | Wednesday = false,
114 | Thursday = false,
115 | Friday = false,
116 | Saturday = false,
117 | StartDate = _startDate,
118 | Occurrences = 5
119 | };
120 | var specificCalendar = new Calculator(DayOfWeek.Monday);
121 | var occurrences = specificCalendar.CalculateOccurrences(recurrence).ToList();
122 | Assert.AreEqual(5, occurrences.Count, "Should create 5 occurrences");
123 | Assert.AreEqual(new DateTime(2014, 02, 02).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
124 | Assert.AreEqual(new DateTime(2014, 02, 23).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
125 | Assert.AreEqual(new DateTime(2014, 03, 16).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
126 | Assert.AreEqual(new DateTime(2014, 04, 06).Add(_startDate.TimeOfDay), occurrences[3], "Date 4 should be correct");
127 | Assert.AreEqual(new DateTime(2014, 04, 27).Add(_startDate.TimeOfDay), occurrences[4], "Date 5 should be correct");
128 | }
129 |
130 | [TestMethod]
131 | public void Should_Generate_Weekly_Recurrences_FirstDayOfWeek_Sunday_FirstOccurrence_Is_At_First_Interval()
132 | {
133 | var recurrence = new AppointmentRecurrence
134 | {
135 | RecurrenceType = RecurrenceType.Weekly,
136 | Interval = 3,
137 | Sunday = true,
138 | Monday = false,
139 | Tuesday = false,
140 | Wednesday = false,
141 | Thursday = false,
142 | Friday = false,
143 | Saturday = false,
144 | StartDate = _startDate,
145 | Occurrences = 5
146 | };
147 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
148 | Assert.AreEqual(5, occurrences.Count, "Should create 5 occurrences");
149 | Assert.AreEqual(new DateTime(2014, 02, 16).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
150 | Assert.AreEqual(new DateTime(2014, 03, 09).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
151 | Assert.AreEqual(new DateTime(2014, 03, 30).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
152 | Assert.AreEqual(new DateTime(2014, 04, 20).Add(_startDate.TimeOfDay), occurrences[3], "Date 4 should be correct");
153 | Assert.AreEqual(new DateTime(2014, 05, 11).Add(_startDate.TimeOfDay), occurrences[4], "Date 5 should be correct");
154 | }
155 |
156 | [TestMethod]
157 | public void Should_Generate_Monthly_Recurrences_Without_End_Date()
158 | {
159 | var recurrence = CreateMonthlyRecurrence();
160 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
161 | Assert.AreEqual(3, occurrences.Count, "Should create 3 occurrences");
162 | Assert.AreEqual(new DateTime(2014, 1, 31).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
163 | Assert.AreEqual(new DateTime(2014, 4, 30).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
164 | Assert.AreEqual(new DateTime(2014, 7, 31).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
165 | }
166 |
167 | [TestMethod]
168 | public void Should_Generate_Monthly_Recurrences_With_End_Date()
169 | {
170 | var recurrence = CreateMonthlyRecurrence();
171 | recurrence.Occurrences = 0;
172 | recurrence.EndDate = new DateTime(2014, 11, 11);
173 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
174 | Assert.AreEqual(4, occurrences.Count, "Should create 4 occurrences");
175 | Assert.AreEqual(new DateTime(2014, 1, 31).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
176 | Assert.AreEqual(new DateTime(2014, 4, 30).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
177 | Assert.AreEqual(new DateTime(2014, 7, 31).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
178 | Assert.AreEqual(new DateTime(2014, 10, 31).Add(_startDate.TimeOfDay), occurrences[3], "Date 4 should be correct");
179 | }
180 |
181 | [TestMethod]
182 | public void Should_Generate_MonthNth_Recurrences_Without_End_Date()
183 | {
184 | var recurrence = CreateMonthNthRecurrence();
185 | recurrence.Instance = 3;
186 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
187 | Assert.AreEqual(3, occurrences.Count, "Should create 3 occurrences");
188 | Assert.AreEqual(new DateTime(2014, 3, 18).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
189 | Assert.AreEqual(new DateTime(2014, 5, 20).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
190 | Assert.AreEqual(new DateTime(2014, 7, 15).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
191 | }
192 |
193 | [TestMethod]
194 | public void Should_Generate_MonthNth_Recurrences_Last_Instance_Without_End_Date()
195 | {
196 | var recurrence = CreateMonthNthRecurrence();
197 | recurrence.Instance = 5;
198 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
199 | Assert.AreEqual(3, occurrences.Count, "Should create 3 occurrences");
200 | Assert.AreEqual(new DateTime(2014, 3, 25).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
201 | Assert.AreEqual(new DateTime(2014, 5, 27).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
202 | Assert.AreEqual(new DateTime(2014, 7, 29).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
203 | }
204 |
205 | [TestMethod]
206 | public void Should_Generate_MonthNth_Recurrences_Last_Weekend_Without_End_Date()
207 | {
208 | var recurrence = CreateMonthNthRecurrence();
209 | recurrence.Tuesday = false;
210 | recurrence.Sunday = true;
211 | recurrence.Saturday = true;
212 | recurrence.Instance = 5;
213 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
214 | Assert.AreEqual(3, occurrences.Count, "Should create 3 occurrences");
215 | Assert.AreEqual(new DateTime(2014, 3, 30).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
216 | Assert.AreEqual(new DateTime(2014, 5, 31).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
217 | Assert.AreEqual(new DateTime(2014, 7, 27).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
218 | }
219 |
220 | [TestMethod]
221 | public void Should_Generate_MonthNth_Recurrences_Last_Weekday_Without_End_Date()
222 | {
223 | var recurrence = CreateMonthNthRecurrence();
224 | recurrence.Monday = true;
225 | recurrence.Wednesday = true;
226 | recurrence.Thursday = true;
227 | recurrence.Friday = true;
228 | recurrence.Instance = 5;
229 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
230 | Assert.AreEqual(3, occurrences.Count, "Should create 3 occurrences");
231 | Assert.AreEqual(new DateTime(2014, 1, 31).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
232 | Assert.AreEqual(new DateTime(2014, 3, 31).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
233 | Assert.AreEqual(new DateTime(2014, 5, 30).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
234 | }
235 |
236 | [TestMethod]
237 | public void Should_Generate_Yearly_Recurrences_With_End_Date()
238 | {
239 | var recurrence = CreateYearlyRecurrence();
240 | recurrence.Occurrences = 0;
241 | recurrence.EndDate = new DateTime(2020, 11, 11);
242 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
243 | Assert.AreEqual(3, occurrences.Count, "Should create 3 occurrences");
244 | Assert.AreEqual(new DateTime(2014, 2, 28).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
245 | Assert.AreEqual(new DateTime(2017, 2, 28).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
246 | Assert.AreEqual(new DateTime(2020, 2, 29).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
247 | }
248 |
249 | [TestMethod]
250 | public void Should_Generate_Yearly_Recurrences_With_End_Date_Same_Start_Date()
251 | {
252 | var recurrence = new AppointmentRecurrence
253 | {
254 | RecurrenceType = RecurrenceType.Yearly,
255 | Interval = 1,
256 | DayOfMonth = 31,
257 | MonthOfYear = 12,
258 | Sunday = true,
259 | Monday = true,
260 | Tuesday = true,
261 | Wednesday = true,
262 | Thursday = true,
263 | Friday = true,
264 | Saturday = true,
265 | StartDate = new DateTime(2019, 12, 31, 16, 0, 0),
266 | Occurrences = 3
267 | };
268 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
269 | Assert.AreEqual(3, occurrences.Count, "Should create 3 occurrences");
270 | Assert.AreEqual(new DateTime(2019, 12, 31, 16, 0, 0), occurrences[0], "Date 1 should be correct");
271 | Assert.AreEqual(new DateTime(2020, 12, 31, 16, 0, 0), occurrences[1], "Date 2 should be correct");
272 | Assert.AreEqual(new DateTime(2021, 12, 31, 16, 0, 0), occurrences[2], "Date 3 should be correct");
273 | }
274 |
275 | [TestMethod]
276 | public void Should_Generate_Yearly_Recurrences_Without_End_Date()
277 | {
278 | var recurrence = CreateYearlyRecurrence();
279 | recurrence.Occurrences = 3;
280 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
281 | Assert.AreEqual(3, occurrences.Count, "Should create 3 occurrences");
282 | Assert.AreEqual(new DateTime(2014, 2, 28).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
283 | Assert.AreEqual(new DateTime(2017, 2, 28).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
284 | Assert.AreEqual(new DateTime(2020, 2, 29).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
285 | }
286 |
287 | [TestMethod]
288 | public void Should_Generate_YearNth_Recurrences_Without_End_Date()
289 | {
290 | var recurrence = CreateYearNthRecurrence();
291 | recurrence.Occurrences = 3;
292 | var occurrences = _calendarUtility.CalculateOccurrences(recurrence).ToList();
293 | Assert.AreEqual(3, occurrences.Count, "Should create 3 occurrences");
294 | Assert.AreEqual(new DateTime(2014, 2, 18).Add(_startDate.TimeOfDay), occurrences[0], "Date 1 should be correct");
295 | Assert.AreEqual(new DateTime(2017, 2, 21).Add(_startDate.TimeOfDay), occurrences[1], "Date 2 should be correct");
296 | Assert.AreEqual(new DateTime(2020, 2, 18).Add(_startDate.TimeOfDay), occurrences[2], "Date 3 should be correct");
297 | }
298 |
299 | [TestMethod]
300 | public void Should_Compute_Monthly_Recurrence_Start_Date_Greater_Then_Day_Scheduled()
301 | {
302 | var recurrence = new AppointmentRecurrence
303 | {
304 | RecurrenceType = RecurrenceType.Monthly,
305 | Interval = 1,
306 | DayOfMonth = 18,
307 | StartDate = new DateTime(2023, 01, 31),
308 | Occurrences = 2
309 | };
310 | var specificCalendar = new Calculator(DayOfWeek.Monday);
311 | var occurrences = specificCalendar.CalculateOccurrences(recurrence).ToList();
312 | Assert.AreEqual(2, occurrences.Count, "Should create 1 occurrence");
313 | Assert.AreEqual(new DateTime(2023, 02, 18), occurrences[0], "Date 1 should be correct");
314 | Assert.AreEqual(new DateTime(2023, 03, 18), occurrences[1], "Date 3 should be correct");
315 | }
316 |
317 | [TestMethod]
318 | public void Should_Compute_Monthly_Recurrence_Start_Date_Less_Then_Day_Scheduled()
319 | {
320 | var recurrence = new AppointmentRecurrence
321 | {
322 | RecurrenceType = RecurrenceType.Monthly,
323 | Interval = 1,
324 | DayOfMonth = 18,
325 | StartDate = new DateTime(2023, 2, 1),
326 | Occurrences = 2
327 | };
328 | var specificCalendar = new Calculator(DayOfWeek.Monday);
329 | var occurrences = specificCalendar.CalculateOccurrences(recurrence).ToList();
330 | Assert.AreEqual(2, occurrences.Count, "Should create 1 occurrence");
331 | Assert.AreEqual(new DateTime(2023, 02, 18), occurrences[0], "Date 1 should be correct");
332 | Assert.AreEqual(new DateTime(2023, 03, 18), occurrences[1], "Date 3 should be correct");
333 | }
334 |
335 | private AppointmentRecurrence CreateWeeklyRecurrence()
336 | {
337 | return new AppointmentRecurrence
338 | {
339 | RecurrenceType = RecurrenceType.Weekly,
340 | Interval = 2,
341 | Sunday = false,
342 | Monday = false,
343 | Tuesday = true,
344 | Wednesday = false,
345 | Thursday = true,
346 | Friday = false,
347 | Saturday = false,
348 | StartDate = _startDate,
349 | Occurrences = 5
350 | };
351 | }
352 |
353 | private AppointmentRecurrence CreateMonthlyRecurrence()
354 | {
355 | return new AppointmentRecurrence
356 | {
357 | RecurrenceType = RecurrenceType.Monthly,
358 | Interval = 3,
359 | DayOfMonth = 31,
360 | Sunday = false,
361 | Monday = false,
362 | Tuesday = true,
363 | Wednesday = false,
364 | Thursday = false,
365 | Friday = false,
366 | Saturday = false,
367 | StartDate = _startDate,
368 | Occurrences = 3
369 | };
370 | }
371 |
372 | private AppointmentRecurrence CreateMonthNthRecurrence()
373 | {
374 | return new AppointmentRecurrence
375 | {
376 | RecurrenceType = RecurrenceType.MonthNth,
377 | Interval = 2,
378 | Instance = 3,
379 | Sunday = false,
380 | Monday = false,
381 | Tuesday = true,
382 | Wednesday = false,
383 | Thursday = false,
384 | Friday = false,
385 | Saturday = false,
386 | StartDate = _startDate,
387 | Occurrences = 3
388 | };
389 | }
390 |
391 | private AppointmentRecurrence CreateYearlyRecurrence()
392 | {
393 | return new AppointmentRecurrence
394 | {
395 | RecurrenceType = RecurrenceType.Yearly,
396 | Interval = 3,
397 | DayOfMonth = 31,
398 | MonthOfYear = 2,
399 | Sunday = false,
400 | Monday = false,
401 | Tuesday = true,
402 | Wednesday = false,
403 | Thursday = false,
404 | Friday = false,
405 | Saturday = false,
406 | StartDate = _startDate,
407 | Occurrences = 3
408 | };
409 | }
410 |
411 | private AppointmentRecurrence CreateYearNthRecurrence()
412 | {
413 | return new AppointmentRecurrence
414 | {
415 | RecurrenceType = RecurrenceType.YearNth,
416 | Interval = 3,
417 | Instance = 3,
418 | MonthOfYear = 2,
419 | Sunday = false,
420 | Monday = false,
421 | Tuesday = true,
422 | Wednesday = false,
423 | Thursday = false,
424 | Friday = false,
425 | Saturday = false,
426 | StartDate = _startDate,
427 | Occurrences = 3
428 | };
429 | }
430 |
431 |
432 | private AppointmentRecurrence CreateDailyWeekdayRecurrence()
433 | {
434 | var recurrence = CreateDailyRecurrence();
435 | recurrence.Sunday = false;
436 | recurrence.Saturday = false;
437 | recurrence.Occurrences = 3;
438 | return recurrence;
439 | }
440 | private AppointmentRecurrence CreateDailyRecurrence()
441 | {
442 |
443 | return new AppointmentRecurrence
444 | {
445 | RecurrenceType = RecurrenceType.Daily,
446 | Interval = 2,
447 | Sunday = true,
448 | Monday = true,
449 | Tuesday = true,
450 | Wednesday = true,
451 | Thursday = true,
452 | Friday = true,
453 | Saturday = true,
454 | StartDate = _startDate,
455 | Occurrences = 10
456 | };
457 | }
458 | }
459 | }
460 |
--------------------------------------------------------------------------------