I hope I can explain this clearly -- our company uses a financial period calendar (13 4-week periods) instead of a monthly calendar. I have several functions that calculate when a given period begins or ends, what period we're in today, etc. Everything was fine until this past Sunday (12/26/10) when all of a sudden everything from that date forward is off by a week.

For example, period 12 of this year began on 11/28 and ended on 12/25. The beginning date is shown accurately but the ending date is showing as 1/1/11. Everything prior to 12/26/10 displays correctly and everything from 12/26/10 on is a week behind. Basically period 13 of this year is showing as being five weeks long, which is throwing everything after that date off.

A sample function is below. Can anyone assist me in finding what might be causing this to happen? I'm sure it's some flaw in the logic but I didn't write the functions and I don't know where to begin.

FUNCTION 1:

ALTER FUNCTION [dbo].[GetPeriod]
(@Date datetime)
RETURNS int
AS
BEGIN
DECLARE
@returnPeriod int
SET @returnPeriod = floor((datediff(dd, DATEADD(day, floor(datediff(dd, '12/31/2000', @date)/1820)*7, '12/31/2000'), @date) % 364)/28)+1


return (@returnPeriod)
end

FUNCTION 2:

ALTER FUNCTION [dbo].[GetPeriodBeginDate]
( @PeriodYear int,
@Period int )
RETURNS datetime
AS
BEGIN
DECLARE
@LeapPeriods int,
@returnDate datetime,
@add int
set @returnDate = '12/31/2000'
set @period = 13*(@periodYear - 2001) + (@period - 1)
set @LeapPeriods = FLOOR(@period / 65)
set @add = @LeapPeriods * 7
set @returndate = DATEADD(dd,(@period * 28) + @add, @returnDate)


return (@returnDate)
end

FUNCTION 3:

ALTER FUNCTION [dbo].[GetPeriodEndDate]
( @PeriodYear int,
@Period int )
RETURNS datetime
AS
BEGIN
DECLARE
@LeapPeriods int,
@returnDate datetime,
@add int
set @returnDate = '12/30/2000'
set @period = 13*(@periodYear - 2001) + @period
set @LeapPeriods = FLOOR(@period / 65)
set @add = @LeapPeriods * 7
set @returndate = DATEADD(dd,(@period * 28) + @add, @returnDate)


return (@returnDate)
end

FUNCTION 4:

ALTER FUNCTION [dbo].[GetPeriodYear]
(@Date datetime)
RETURNS int
AS
BEGIN
DECLARE
@returnPeriodYear int
SET @returnPeriodYear = 2001 + floor(floor(datediff(dd, DATEADD(day, floor(datediff(dd, '12/31/2000', @date)/1820)*7, '12/31/2000'), @date)/28)/13)


return (@returnPeriodYear)
end