0

I'm looking for some guidance on how to proceed with a scheduling problem I'm facing.

At a high level the problem involves scheduling 'events' such that for a given week number (1-52/53), an event is 'active' for the period of that week. ISO weeks seemed like a great fit for this since it provides uniform week lengths and each length should be active for 7 days.

The issue is, the events should start each Sunday but ISO 8601 considers Monday the start of each week. I need to be able to provide the current date and time and determine what the current week is and thus which event should be 'active'.

I'm using C# to implement the program but there doesn't seem to be a way to adjust the week number calculation given a date and time using the ISOWeek class. I thought of just decrementing the specified date by 1 before the week number is calculated but that seems naïve and I'm not sure how that could affect some of the weird week-year edge cases.

An example would be 2022/01/02, default ISO week behaviour would tell you that this is the 52nd week of 2021, but if the weeks are adjusted to start on a Sunday then that should in fact be week 1 of 2022.

I found that NodaTime allows you to specify the DayOfWeek when creating a WeekYear rule, something like:

WeekYearRules.ForMinDaysInFirstWeek(4, IsoDayOfWeek.Sunday)

Which appears to generate the correct week numbers, but again I can't be certain that it behaves sensibly for all edge-cases.

I wasn't able to find a great deal of prior art around adjustable ISO weeks so perhaps I'm missing something and the behaviour I'm looking for is achievable with the default implementation?

EDIT:

The method DateTimeFormatInfo.CurrentInfo.Calendar.GetWeekOfYear is actually flawed and gives incorrect results when compared to ISO 8601. This issue is noted here: https://learn.microsoft.com/en-gb/archive/blogs/shawnste/iso-8601-week-of-year-format-in-microsoft-net

cm_samp
  • 1
  • 1
  • Have you seen this answer? https://stackoverflow.com/a/50405661/633098 – silkfire Oct 01 '22 at 00:02
  • The key method you need is this: `DateTimeFormatInfo.CurrentInfo.Calendar.GetWeekOfYear(date, rule, firstDayOfWeek)`. – Enigmativity Oct 01 '22 at 00:28
  • 1
    I am aware of that particular method, however unfortunately it doesn't exactly follow the rules of ISO 8601: https://learn.microsoft.com/en-gb/archive/blogs/shawnste/iso-8601-week-of-year-format-in-microsoft-net – cm_samp Oct 02 '22 at 04:08

0 Answers0