Skip to content

Commit 2bd860a

Browse files
committed
Extended the implementation of the EssentialsMonth class
1 parent 2ad8dd1 commit 2bd860a

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

src/Skybrud.Essentials/Time/EssentialsMonth.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Skybrud.Essentials.Time {
45

@@ -99,6 +100,87 @@ public EssentialsMonth(EssentialsTime timestamp, TimeZoneInfo timeZone) {
99100

100101
#endregion
101102

103+
#region Member methods
104+
105+
/// <summary>
106+
/// Returns a new instance representing the previous month, according to the local time zone.
107+
/// </summary>
108+
/// <returns>An instance of <see cref="EssentialsMonth"/>.</returns>
109+
public EssentialsMonth GetPrevious() {
110+
return new EssentialsMonth(Start.GetStartOfMonth(TimeZoneInfo.Local).AddDays(-1));
111+
}
112+
113+
/// <summary>
114+
/// Returns a new instance representing the previous month, according to the specified <paramref name="timeZone"/>.
115+
/// </summary>
116+
/// <param name="timeZone">The time zone to be used for calculating the start and end dates.</param>
117+
/// <returns>An instance of <see cref="EssentialsMonth"/>.</returns>
118+
public EssentialsMonth GetPrevious(TimeZoneInfo timeZone) {
119+
return new EssentialsMonth(Start.GetStartOfMonth(timeZone).AddDays(-1));
120+
}
121+
122+
/// <summary>
123+
/// Returns a new instance representing the next month, according to the local time zone.
124+
/// </summary>
125+
/// <returns>An instance of <see cref="EssentialsMonth"/>.</returns>
126+
public EssentialsMonth GetNext() {
127+
return new EssentialsMonth(End.GetEndOfMonth(TimeZoneInfo.Local).AddDays(+1));
128+
}
129+
130+
/// <summary>
131+
/// Returns a new instance representing the next month, according to the specified <paramref name="timeZone"/>.
132+
/// </summary>
133+
/// <param name="timeZone">The time zone to be used for calculating the start and end dates.</param>
134+
/// <returns>An instance of <see cref="EssentialsMonth"/>.</returns>
135+
public EssentialsMonth GetNext(TimeZoneInfo timeZone) {
136+
return new EssentialsMonth(End.GetEndOfMonth(timeZone).AddDays(+1));
137+
}
138+
139+
#endregion
140+
141+
#region Static methods
142+
143+
/// <summary>
144+
/// Returns an array with the months from <paramref name="startYear"/> and <paramref name="startMonth"/>.
145+
///
146+
/// Start and end dates of each month are calculated based on <see cref="TimeZoneInfo.Local"/>.
147+
/// </summary>
148+
/// <param name="startYear">The year of the starting month.</param>
149+
/// <param name="startMonth">The starting month.</param>
150+
/// <param name="months">The amount of month, including the start month.</param>
151+
/// <returns>An array of <see cref="EssentialsMonth"/>.</returns>
152+
public static EssentialsMonth[] GetMonths(int startYear, int startMonth, int months) {
153+
return GetMonths(startYear, startMonth, months, TimeZoneInfo.Local);
154+
}
155+
156+
/// <summary>
157+
/// Returns an array with the months from <paramref name="startYear"/> and <paramref name="startMonth"/>.
158+
///
159+
/// Start and end dates of each month are calculated based on <paramref name="timeZone"/>.
160+
/// </summary>
161+
/// <param name="startYear">The year of the starting month.</param>
162+
/// <param name="startMonth">The starting month.</param>
163+
/// <param name="months">The amount of month, including the start month.</param>
164+
/// <param name="timeZone">The time zone to be used for calculating the start and end dates.</param>
165+
/// <returns>An array of <see cref="EssentialsMonth"/>.</returns>
166+
public static EssentialsMonth[] GetMonths(int startYear, int startMonth, int months, TimeZoneInfo timeZone) {
167+
168+
List<EssentialsMonth> temp = new List<EssentialsMonth>();
169+
170+
int year = startYear;
171+
172+
for (int i = 0; i < months; i++) {
173+
int month = (startMonth + i - 1) % 12 + 1;
174+
if (i > 0 && month == 1) year++;
175+
temp.Add(new EssentialsMonth(year, month, timeZone));
176+
}
177+
178+
return temp.ToArray();
179+
180+
}
181+
182+
#endregion
183+
102184
}
103185

104186
}

src/UnitTestProject1/Time/Time/EssentialMonthTests.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,134 @@ public void ConstructorRomance() {
7676

7777
}
7878

79+
[TestMethod]
80+
public void GetPrevious1() {
81+
82+
TimeZoneInfo romance = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
83+
84+
EssentialsMonth month = new EssentialsMonth(2021, 4, romance);
85+
86+
Assert.AreEqual("2021-04-01T00:00:00+02:00", month.Start.Iso8601);
87+
Assert.AreEqual("2021-04-30T23:59:59+02:00", month.End.Iso8601);
88+
89+
EssentialsMonth previous = month.GetPrevious(romance);
90+
91+
Assert.AreEqual("2021-03-01T00:00:00+01:00", previous.Start.Iso8601);
92+
Assert.AreEqual("2021-03-31T23:59:59+02:00", previous.End.Iso8601);
93+
94+
}
95+
96+
[TestMethod]
97+
public void GetNext1() {
98+
99+
TimeZoneInfo romance = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
100+
101+
EssentialsMonth month = new EssentialsMonth(2021, 3, romance);
102+
103+
Assert.AreEqual("2021-03-01T00:00:00+01:00", month.Start.Iso8601);
104+
Assert.AreEqual("2021-03-31T23:59:59+02:00", month.End.Iso8601);
105+
106+
EssentialsMonth next = month.GetNext(romance);
107+
108+
Assert.AreEqual("2021-04-01T00:00:00+02:00", next.Start.Iso8601);
109+
Assert.AreEqual("2021-04-30T23:59:59+02:00", next.End.Iso8601);
110+
111+
}
112+
113+
[TestMethod]
114+
public void GetMonths1() {
115+
116+
TimeZoneInfo romance = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
117+
118+
var period = EssentialsMonth.GetMonths(2021, 1, 12, romance);
119+
120+
Assert.AreEqual(12, period.Length);
121+
122+
Assert.AreEqual(2021, period[0].Year);
123+
Assert.AreEqual(1, period[0].Month);
124+
125+
Assert.AreEqual(2021, period[1].Year);
126+
Assert.AreEqual(2, period[1].Month);
127+
128+
Assert.AreEqual(2021, period[2].Year);
129+
Assert.AreEqual(3, period[2].Month);
130+
131+
Assert.AreEqual(2021, period[3].Year);
132+
Assert.AreEqual(4, period[3].Month);
133+
134+
Assert.AreEqual(2021, period[4].Year);
135+
Assert.AreEqual(5, period[4].Month);
136+
137+
Assert.AreEqual(2021, period[5].Year);
138+
Assert.AreEqual(6, period[5].Month);
139+
140+
Assert.AreEqual(2021, period[6].Year);
141+
Assert.AreEqual(7, period[6].Month);
142+
143+
Assert.AreEqual(2021, period[7].Year);
144+
Assert.AreEqual(8, period[7].Month);
145+
146+
Assert.AreEqual(2021, period[8].Year);
147+
Assert.AreEqual(9, period[8].Month);
148+
149+
Assert.AreEqual(2021, period[9].Year);
150+
Assert.AreEqual(10, period[9].Month);
151+
152+
Assert.AreEqual(2021, period[10].Year);
153+
Assert.AreEqual(11, period[10].Month);
154+
155+
Assert.AreEqual(2021, period[11].Year);
156+
Assert.AreEqual(12, period[11].Month);
157+
158+
}
159+
160+
[TestMethod]
161+
public void GetMonths2() {
162+
163+
TimeZoneInfo romance = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
164+
165+
var period = EssentialsMonth.GetMonths(2021, 7, 12, romance);
166+
167+
Assert.AreEqual(12, period.Length);
168+
169+
Assert.AreEqual(2021, period[0].Year);
170+
Assert.AreEqual(7, period[0].Month);
171+
172+
Assert.AreEqual(2021, period[1].Year);
173+
Assert.AreEqual(8, period[1].Month);
174+
175+
Assert.AreEqual(2021, period[2].Year);
176+
Assert.AreEqual(9, period[2].Month);
177+
178+
Assert.AreEqual(2021, period[3].Year);
179+
Assert.AreEqual(10, period[3].Month);
180+
181+
Assert.AreEqual(2021, period[4].Year);
182+
Assert.AreEqual(11, period[4].Month);
183+
184+
Assert.AreEqual(2021, period[5].Year);
185+
Assert.AreEqual(12, period[5].Month);
186+
187+
Assert.AreEqual(2022, period[6].Year);
188+
Assert.AreEqual(1, period[6].Month);
189+
190+
Assert.AreEqual(2022, period[7].Year);
191+
Assert.AreEqual(2, period[7].Month);
192+
193+
Assert.AreEqual(2022, period[8].Year);
194+
Assert.AreEqual(3, period[8].Month);
195+
196+
Assert.AreEqual(2022, period[9].Year);
197+
Assert.AreEqual(4, period[9].Month);
198+
199+
Assert.AreEqual(2022, period[10].Year);
200+
Assert.AreEqual(5, period[10].Month);
201+
202+
Assert.AreEqual(2022, period[11].Year);
203+
Assert.AreEqual(6, period[11].Month);
204+
205+
}
206+
79207
}
80208

81209
}

0 commit comments

Comments
 (0)