Cecil
19+ years progress programming and still learning.
This seems to do the trick. It seems that you need to do a hack.
Code:
// SOURCE https://docs.microsoft.com/en-us/dotnet/api/system.globalization.calendar.getweekofyear?view=net-6.0
using System.*.
using System.Globalization.*.
function getWeekNumber returns integer (input pDateTime as datetime ):
DEFINE VARIABLE myCI as class CultureInfo NO-UNDO.
DEFINE variable myCal as class Calendar NO-UNDO.
DEFINE VARIABLE myCWR as class CalendarWeekRule NO-UNDO.
DEFINE VARIABLE myFirstDOW as class DayOfWeek NO-UNDO.
// Gets the Calendar instance associated with a CultureInfo.
myCI = new CultureInfo("en-GB").
myCal = myCI:Calendar.
// Gets the DTFI properties required by GetWeekOfYear.
myFirstDOW = myCI:DateTimeFormat:FirstDayOfWeek. // FirstDayOfWeek System Defined
myCWR = myCI:DateTimeFormat:CalendarWeekRule. // CalendarWeekRule System Defined
//Debug
// message myFirstDOW:ToString().
// message myCWR:ToString().
// Override the systems CalendarWeekRule
myCWR = CalendarWeekRule:FirstDay. // Indicates that the first week of the year starts on the first day of the year and ends before the following designated first day of the week.
//myCWR = CalendarWeekRule:FirstFourDayWeek. //Indicates that the first week of the year is the first week with four or more days before the designated first day of the week.
//myCWR = CalendarWeekRule:FirstFullWeek. // Indicates that the first week of the year begins on the first occurrence of the designated first day of the week on or after the first day of the year
// message myCWR:ToString().
return myCal:GetWeekOfYear( pDateTime , myCWR, myFirstDOW ).
finally:
if valid-object(myCI) Then
delete object myCI.
end.
end function.
function getIso8601WeekOfYear return int (input pDateTime as datetime):
// REF: https://docs.microsoft.com/en-gb/archive/blogs/shawnste/iso-8601-week-of-year-format-in-microsoft-net
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
&scoped-define xiMonday 2
&scoped-define xiWednesday 4
if weekday(pDateTime) >= {&xiMonday} and weekday(pDateTime) <= {&xiWednesday} then
pDateTime = add-interval(pDateTime, 3, 'days').
// Return the week of our adjusted day
return getWeekNumber(input pDateTime).
end function.
message "Date:" datetime("31/12/2019") skip
"Week Number:" getIso8601WeekOfYear(input datetime("31/12/2019") )
view-as alert-box info title "Get Week Number".