It all started with a requirement to code a report in SAP that mirrored a legacy report. I was using a function module but wasn't quite getting the desired results.
So I checked out the code on the legacy system to see what it is doing and found that it is assigning a week number to the report based on the week that the report was run, not the week that the data in the report was for. Problem one solved.
Second problem was Julian day. It seems to keep the SAP report ans the legacy reports acting the same way, they will need to use the same date handling.
So the legacy code was looked at found to be very basic. week number = Julian + 7 / 7.
So the week number returned is next weeks number based on a Julian date card + seven days divided by 7. So how do I get the date in SAP to convert into a Julian number?
I looked, I searched, I checked out SDN forums, blogs and I even googled it. I didn't find Jack. Then I remembered having this same problem for a report I had written almost 6 months ago, so I looked it over and found the answer.
Calculate the Julian.
There is no function module to calculate the Julian you just have to do it.
I don't remember where I found the formula but it works and here is what you so:
Set up 2 working storage fields as local variables one type N (number) with a size of 3 to be the holder of the Julian day. the other is type D for date.
I left the local variables named the way I found them on my search way back then. so I have the following code:
DATA: Julian_day(3) TYPE n,
date_aux TYPE d.
CONCATENATE: s_date-low(4) '0101' INTO date_aux.
Julian_day = s_date-low + 3 - date_aux.
Julian_day = Julian_day + 1.
ws_report_week = ( Julian_day + 7 ) / 7.
To explain what this does is this... S-Date-low is a select-options required date for the report. it is the low date. It always will be Sunday for the report and being Sunday is either the first day of the week or the last day of the week depending on what calender you are looking at, I decided it would not be the best day to use so I add three to it and figure that Wednesday would be a good day to determine the report week for the program.
So I have taken the date and put the year [s_date-low(4)] and then the first day of the year ['0101'] and put that into a local field type date. [INTO date_aux]. Now that the first day of the year is determined, I can subtract that from the select-options date (+3 to get Wednesday). so using 03/28/2010 as a s_date-low example, I get 03/31/2010 - 01/01/2010 to get 089 and then add 1 to it to get the Julian day of 090. Now that I have the Julian day I can use the legacy coding to get my report week by taking Julian plus 7 (097) and dividing it by 7 to get 13.857 rounded or 14.
And 14 is the desired date so this will work. Keep in mind that 365 is not divisible by 7 so running with a date of 12/28 will get you a week of 53. We verified that the legacy is doing the same thing (same code so it doesn't surprise me any) and being that is how it is wanted then all is well.
Hope that this may be found useful to someone.