mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
getting correct days
This commit is contained in:
45
Management.Test/Features/CalendarMonthTests.cs
Normal file
45
Management.Test/Features/CalendarMonthTests.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
public class CalendarMonthTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void TestCalendarMonthCanGetFirstWeek()
|
||||||
|
{
|
||||||
|
var month = new CalendarMonth(2023, 2);
|
||||||
|
|
||||||
|
int?[] expectedFirstWeek = new int?[] {
|
||||||
|
null, null, null, 1, 2, 3, 4
|
||||||
|
};
|
||||||
|
|
||||||
|
month.Weeks.First().Should().BeEquivalentTo(expectedFirstWeek);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestCanGetAnotherMonthsFirstWeek()
|
||||||
|
{
|
||||||
|
var month = new CalendarMonth(2023, 4);
|
||||||
|
|
||||||
|
int?[] expectedFirstWeek = new int?[] {
|
||||||
|
null, null, null, null, null, null, 1
|
||||||
|
};
|
||||||
|
|
||||||
|
month.Weeks.First().Should().BeEquivalentTo(expectedFirstWeek);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestCorrectNumberOfWeeks()
|
||||||
|
{
|
||||||
|
var month = new CalendarMonth(2023, 4);
|
||||||
|
|
||||||
|
month.Weeks.Count().Should().Be(6);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestLastWeekIsCorrect()
|
||||||
|
{
|
||||||
|
var month = new CalendarMonth(2023, 4);
|
||||||
|
int?[] expectedLastWeek = new int?[] {
|
||||||
|
30, null, null, null, null, null, null,
|
||||||
|
};
|
||||||
|
|
||||||
|
month.Weeks.Last().Should().BeEquivalentTo(expectedLastWeek);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,6 +33,7 @@ public class SemesterPlannerTests
|
|||||||
|
|
||||||
semester.Months.Count().Should().Be(2);
|
semester.Months.Count().Should().Be(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestNewPlannerHandlesTermsThatWrapYears()
|
public void TestNewPlannerHandlesTermsThatWrapYears()
|
||||||
{
|
{
|
||||||
@@ -47,4 +48,5 @@ public class SemesterPlannerTests
|
|||||||
|
|
||||||
semester.Months.Count().Should().Be(2);
|
semester.Months.Count().Should().Be(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,83 @@
|
|||||||
public class CalendarMonth
|
public class CalendarMonth
|
||||||
{
|
{
|
||||||
|
private DateOnly firstDay { get; }
|
||||||
|
private int year { get => firstDay.Year; }
|
||||||
|
private int month { get => firstDay.Month; }
|
||||||
|
public IEnumerable<IEnumerable<int?>> Weeks
|
||||||
|
{
|
||||||
|
get =>
|
||||||
|
DaysByWeek.Select(
|
||||||
|
week => week.Select(d => d?.Day)
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IEnumerable<DateTime?>> DaysByWeek
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var weeks = new List<List<DateTime?>>();
|
||||||
|
var weeksInMonth = WeeksInMonth(year, month);
|
||||||
|
var daysInMonth = DateTime.DaysInMonth(year, month);
|
||||||
|
|
||||||
|
var firstDayOfMonth = new DateTime(year, month, 1).DayOfWeek;
|
||||||
|
|
||||||
|
var currentDay = 1;
|
||||||
|
for (int i = 0; i < weeksInMonth; i++)
|
||||||
|
{
|
||||||
|
var thisWeek = new List<DateTime?>();
|
||||||
|
if (i == 0 && firstDayOfMonth != DayOfWeek.Sunday)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 7; j++)
|
||||||
|
{
|
||||||
|
if (j < (int)firstDayOfMonth)
|
||||||
|
{
|
||||||
|
thisWeek.Add(null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
thisWeek.Add(new DateTime(year, month, currentDay));
|
||||||
|
currentDay++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 7; j++)
|
||||||
|
{
|
||||||
|
if (currentDay <= daysInMonth)
|
||||||
|
{
|
||||||
|
thisWeek.Add(new DateTime(year, month, currentDay));
|
||||||
|
currentDay++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
thisWeek.Add(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
weeks.Add(thisWeek);
|
||||||
|
}
|
||||||
|
return weeks;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int WeeksInMonth(int year, int month)
|
||||||
|
{
|
||||||
|
var firstDayOfMonth = new DateTime(year, month, 1).DayOfWeek;
|
||||||
|
var daysInMonth = DateTime.DaysInMonth(year, month);
|
||||||
|
var longDaysInMonth = daysInMonth + (int)(firstDayOfMonth);
|
||||||
|
var weeks = longDaysInMonth / 7;
|
||||||
|
if ((longDaysInMonth % 7) > 0)
|
||||||
|
{
|
||||||
|
weeks += 1;
|
||||||
|
}
|
||||||
|
return weeks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CalendarMonth(int year, int month)
|
||||||
|
{
|
||||||
|
firstDay = new DateOnly(year, month, 1);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,6 @@ public class SemesterPlanner
|
|||||||
+ canvasTerm.EndAt?.Month - canvasTerm.StartAt?.Month
|
+ canvasTerm.EndAt?.Month - canvasTerm.StartAt?.Month
|
||||||
?? throw new Exception($"Canvas Term must have a start and end date. Term: {canvasTerm.Id}, start: {canvasTerm.StartAt}, end: {canvasTerm.EndAt}");
|
?? throw new Exception($"Canvas Term must have a start and end date. Term: {canvasTerm.Id}, start: {canvasTerm.StartAt}, end: {canvasTerm.EndAt}");
|
||||||
|
|
||||||
Months = Enumerable.Range(0, monthsInTerm).Select(_ => new CalendarMonth());
|
Months = Enumerable.Range(0, monthsInTerm).Select(_ => new CalendarMonth(2022, 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user