I am writing a program that shows the current years sales from the beginning of the fiscal year to the current date, compared to the same date range of the year before.
My question is, what efforts do I need to take for leap year?
UPDATE:
OK they want it like I said (compare last year up to same date) but if today is non leap year and last year is and today is feb 28th compare to last year up to 29th. Or if today is Feb 29th compare to last year up to 28th.
Surely that depends on what the business wants you to do. Isn't this a question that should be answered by an accountant?
This strikes me as a business decision. Depending on the type of business, that extra day may not matter. Otherwise, I suppose you could treat is as "first n days of the year" rather than "Jan 1 through X".
Here's an idea, but like others have said it might be based on your specific domain.
- Consider 1 "normalized" year = 365.242199 days counting all the leap stuff (says google)
- Calculate the average sales per day in your year based on the real number of days in that year
- Scale it up or down to sales per 365.242199 days
So for example
2007 = $4000 in sales.
There's 365 days in 2007, so avg sale per day = $10.96
Multpiplying times num days in a normalized year (365.242199)
gives you $4003.05 normalized sales
You can compare this directly to a similar calculation for 2008,
2008 = $5000 in sales.
There's 366 days in 2008, so avg sale per day = $13.66
Multpiplying times num days in a normalized year (365.242199)
gives you $4975.655 normalized sales for 2008!
You could scale down the leap year values to take the extra day into account.
So if you compare, say, the 1st of September of a regular year with the 1st of September of a leap year you would do:
if(year == leapyear && day > 28Feb)
Convert date to dayOfYear
leapYearValue *= dayOfYear / (dayOfYear + 1)
This should really be in the specification, though.