-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved backend weekconverter to weekExtenstions
- Loading branch information
1 parent
459382c
commit f71cfdd
Showing
4 changed files
with
49 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace Core.Weeks; | ||
public static class WeekExtensions | ||
{ | ||
|
||
public struct MonthsOfWeek | ||
{ | ||
public int Month { get; init; } | ||
public int? SecondMonth { get; init; } | ||
public int Distribution { get; init; } | ||
} | ||
|
||
public static MonthsOfWeek GetMonthOfWeek(this Week week) | ||
{ | ||
int daysFromStartOfYear = 1 + (week.WeekNumber - 1) * 7; | ||
var dayOfWeek = new DateOnly(week.Year, 1, 1).AddDays(daysFromStartOfYear - 1); // Adjust start of year to the calculated day | ||
var monday = dayOfWeek.GetPreviousOrCurrentMonday(); | ||
int month = monday.Month; | ||
|
||
double distribution = 100; | ||
int? secondMonth = null; | ||
|
||
for (int i = 1; i < 7; i++) | ||
{ | ||
var addedDayDate = monday.AddDays(i); | ||
if (addedDayDate.Month != month) | ||
{ | ||
distribution = distribution / 7 * i; | ||
secondMonth = addedDayDate.Month; | ||
break; | ||
} | ||
} | ||
|
||
return new MonthsOfWeek | ||
{ | ||
Month = month, | ||
SecondMonth = secondMonth, | ||
Distribution = (int)distribution | ||
}; | ||
} | ||
public static DateOnly GetPreviousOrCurrentMonday(this DateOnly date) | ||
{ | ||
if (date.DayOfWeek == DayOfWeek.Monday) | ||
{ | ||
return date; | ||
} | ||
int daysToSubtract = (date.DayOfWeek - DayOfWeek.Monday + 7) % 7; | ||
return date.AddDays(-daysToSubtract); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters