Example on how to calculate the week number of the year from a date

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".
 

Cringer

ProgressTalk.com Moderator
Staff member
I was trying to get a .Net solution working last week which was why I asked if it's Windows or *nix. Unfortunately support for ISO 8601 was only baked into .Net Core and not .Net Framework. Core has an ISOWeek class that does all the magic with very little effort. But that's not an available option in OpenEdge at the moment AFAIK.
 

Den Duze

Member
@Cecil: yes this does the trick and it also verifies that my code (with the little change - see above in this thread) also works correct (so usable in our application that uses the same code on Windows and Linux).
I've tested for every day for 60 years (yes I know that this is not really needed but yeah for these things computers are made :)) and both codes always result in the same weeknumbers!
 
Top