Leap year and week numbers?

HTX

New Member
Good day,


I'm having a bit of Leap year/week number troubles
In a n existing and functioning program i use the following code to calculate the week number.

Code:
/[font=Courier New][size=1]* Assumptions:													 */
/* 1. Weeks start on SUNDAYS										*/
/* 2. If January 1st falls on Friday, Saturday, Sunday or Monday	*/
/*	then week 1 for this year will start on the first Monday	  */
/*	the same year. If not, week 1 will start on the last Monday   */
/*	previous year.												*/
/*	(In other words: At least 4 of the seven days of week 1 for   */
/*	 a given year must fall into this year)					   */[/size][/font]

[font=Courier New][size=1]DEFINE INPUT  PARAMETER indate   AS DATE.  /* Input date , eg 10/17/90 */
DEFINE OUTPUT PARAMETER yyyyww   AS INT.   /* Output week, eg 199042   */[/size][/font]
[font=Courier New][size=1]DEFINE VARIABLE yr   AS INT.  /* Year of indate, eg 1990	  */
DEFINE VARIABLE d1   AS INT.  /* Weekday of 1/1 current year, eg 2  */
		 /* (01/01/90 is a Monday)	  */
DEFINE VARIABLE dat1 AS DATE. /* Starting date of week 1	 */
DEFINE VARIABLE wn   AS INT.  /* Week number , eg 45		 */[/size][/font]
[font=Courier New][size=1]ASSIGN
  yr   = YEAR(indate)
  d1   = WEEKDAY(DATE( 1 , 1 , yr))
  dat1 = (IF d1 LE 5 THEN DATE(1,  3, yr) - d1 ELSE
	 DATE(1, 10, yr) - d1 )
  wn   = TRUNCATE((indate - dat1 + 8) / 7 , 0)
  yyyyww = yr * 100 + wn.[/size][/font]
[font=Courier New][size=1]IF wn < 1 THEN	   /* Week 52 or 53 previous year ? */
ASSIGN
  yr	 = yr - 1
  d1	 = WEEKDAY(DATE( 1 , 1 , yr))
  dat1   = (IF d1 LE 5 THEN DATE(1,  3, yr) - d1 ELSE
		DATE(1, 10, yr) - d1 )
  wn	 = TRUNCATE((indate - dat1 + 8) / 7 , 0)
  yyyyww = yr * 100 + wn.[/size][/font]
[font=Courier New][size=1]ELSE IF wn > 52 THEN  /* Week 53 this year or week 1 next year ? */
ASSIGN
  yr	 = yr + 1
  d1	 = WEEKDAY(DATE( 1 , 1 , yr))
  yyyyww = IF d1 EQ 6 OR d1 EQ 7 OR d1 EQ 1
	   THEN (yr - 1) * 100 + 53 ELSE yr * 100 + 1.[/size][/font]

The problem is when this code is executed for the 29th of february.
It stops and comes with **the day of the month is invalid error -81

I'm not much of a programmer so i don't really understand this code.

Can anyone pls advise
 

ddavis

New Member
HTX said:
Good day,

I'm having a bit of Leap year/week number troubles
In a n existing and functioning program i use the following code to calculate the week number.

The problem is when this code is executed for the 29th of february.
It stops and comes with **the day of the month is invalid error -81

I'm not much of a programmer so i don't really understand this code.

Can anyone pls advise

This code runs fine for me unless I pass in a year that is not a leap year. ie 02/29/04 works, 02/29/03 gives your error.
 
Top