This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,42 @@
using System;
using System.Globalization;
namespace Kreta.BusinessLogic.Classes
{
public static class DateHelper
{
// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetISO8601WeekOfYear(this DateTime date)
{
var calendar = CultureInfo.InvariantCulture.Calendar;
// Seriously cheat. If its Monday, Tuesday or Wednesday, then itll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = calendar.GetDayOfWeek(date);
if (DayOfWeek.Monday <= day && day <= DayOfWeek.Wednesday)
{
date = date.AddDays(3);
}
// Return the week of our adjusted day
return calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
public static int GetISO8601WeekOfYear(this DateTime? date)
{
if (date.HasValue)
{
return date.Value.GetISO8601WeekOfYear();
}
return GetWeeksInYear();
}
private static int GetWeeksInYear()
{
return GetISO8601WeekOfYear(new DateTime(DateTime.Now.Year, 12, 31));
}
}
}