Week Number of a Given Date in Lotus Notes @Formulas

Ok first of all I am one of those unlucky guys who has to develop things with Lotus Domino. Especially the formula language is kind of crap (my personal opinion).

Anyway, I had to program a calendar and therefore needed to get the week number of a given date. Thanks to notesweb where I found an almost working version, I just needed to patch it a bit.

Here is my final code which seems to do the trick (at least for my purpose).

REM { returns the week number from a given date };

Date:=@Date(2005; 07; 27);

FirstOfYear := @Date(@Year(Date); 1; 1);
LastOfYear := @Date(@Year(Date); 12; 31);
ISOFirstDayNum := @Weekday(FirstOfYear);
ISOLastDayNum := @Weekday(LastOfYear);  

REM {
   The first and last ISO week is the first
   and last ISO week to include Thursday
};
IsFirstWeek := 7 - ISOFirstDayNum > 2;
IsLastWeek := 7 - ISOLastDayNum < 4;

REM {The date of the first day of the first ISO week};
ISOFirstDay := @If(IsFirstWeek;
@Adjust(FirstOfYear; 0; 0; 1 - ISOFirstDayNum; 0; 0; 0);
@Adjust(FirstOfYear; 0; 0; 8 - ISOFirstDayNum; 0; 0; 0));

REM {The date of the last day of the last ISO week};
ISOLastDay := @If(IsLastWeek;
@Adjust(LastOfYear; 0; 0; 7 - ISOLastDayNum; 0; 0; 0);
@Adjust(LastOfYear; 0; 0; -ISOLastDayNum; 0; 0; 0));

REM {Date outside ISOFirstDay and ISOlastDay};
REM {are from the previous or next year};
REM {Return the ISO week number and exit};
LastWeekLastYear := (Date - @Adjust(FirstOfYear;-1;0;0;0;0;0))/60/60/24/7;
AdjustLastWeek := 1 - (LastWeekLastYear - @Integer(LastWeekLastYear));
@Set("LastWeekLastYear"; LastWeekLastYear + AdjustLastWeek);

REM {If you get this far, the date falls into an ISO week this year};
REM {Convert the difference in seconds to weeks};
NumWeeks := (Date - ISOFirstDay)/60/60/24/7;

REM {Fractions indicate that the date falls in the middle of the ISO week};
ISOWeekNum:=@If(NumWeeks != @Integer(NumWeeks);@Integer(NumWeeks)+1;NumWeeks);
Pad:=@If(ISOWeekNum<10;"0";"");

@Prompt([OK];"Weeknumber";"Week " + @Text(Pad+@Text(ISOWeekNum)));

REM { Prints "Week 30" };