Need to calculate date ranges. How do I do this?

Started by
8 comments, last by jwezorek 14 years ago
I'm working on a program unrelated to games. I need to calculate the bi-weekly pay periods for this year and several years out. I can do this easily enough in a spreadsheet, but I need to do it in code. Let's say the pay period starts today. In Excel or OpenOffice, I would have a cell (say, A1) with "5/9/2010". In the next column, I would have a cell with "=A1+13", which would give me the correct ending date of 5/22/2010. I can calculate all of the previous and future pay periods in the same fashion. Again, this is easy in a spreadsheet. How do I go about this in code? It isn't a simple matter of adding days to the date. For portability and ease of coding, I'm using ANSI C date functions.

No, I am not a professional programmer. I'm just a hobbyist having fun...

Advertisement
Quote:Original post by maspeir
I'm working on a program unrelated to games. I need to calculate the bi-weekly pay periods for this year and several years out. I can do this easily enough in a spreadsheet, but I need to do it in code.


Oh you poor thing. You have my sympathy. And in C of all the languages... Why are you being tortured this way? Can't you appeal to Geneva convention? Or request asylum in some faraway land?

Quote:Let's say the pay period starts today. In Excel or OpenOffice, I would have a cell (say, A1) with "5/9/2010". In the next column, I would have a cell with "=A1+13", which would give me the correct ending date of 5/22/2010. I can calculate all of the previous and future pay periods in the same fashion. Again, this is easy in a spreadsheet.
Well, that works if locale doesn't change. But consider that holidays depend on country you are in, that time adjustments depend on geographic location, that there are leap years and other adjustments, that you may need to consider for arbitrary corrections to any of the above....

Quote:How do I go about this in code? It isn't a simple matter of adding days to the date. For portability and ease of coding, I'm using ANSI C date functions.


First read this.

Next start either reading incredibly carefully through all the documentation on C date functions *for your platform and compiler version* and working around the potentially missing functionality.

Alternatively, use a saner platform, such as Java and JodaTime library. The sheer effort needed to get this thing right is worth using another platform.


Yes - it really is such a horribly convoluted problem, since it's not mathematically pure, but needs to take into consideration potentially different historical timelines as agreed upon by various international committees.
OK, don't know what that was about...

Can someone post something useful?

No, I am not a professional programmer. I'm just a hobbyist having fun...

If you can use standard Windows functions, you should be able to do most of what you need by storing and doing maths on your dates stored in a FILETIME structure (or 64-bit integer), and using FileTimeToSystemTime() to display them.
This post was me being lazy. I posted it shortly after I got up this morning. I was hoping for an easy solution (like a single function), but I have it working. Not complicated at all.

No, I am not a professional programmer. I'm just a hobbyist having fun...

Wow! This is embarrassing! I really need to wake up fully before posting from now on.

Anyway...

void IncrementDate(int *month,int *day,int *year,int inc_d){	int		m,d,y;	int		days_in_months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};	int		days_in_months_leap[12] = {31,29,31,30,31,30,31,31,30,31,30,31};	m = *month;	d = *day;	y = *year;	if(!(y % 4)) // Leap Year	{		do{			d++;			if(d > days_in_months_leap[m - 1])			{				d -= days_in_months_leap[m - 1];				m++;				if(m > 12)				{					m = 1;					y++;				}			}		}while(--inc_d);	}else{		do{			d++;			if(d > days_in_months[m - 1])			{				d -= days_in_months[m - 1];				m++;				if(m > 12)				{					m = 1;					y++;				}			}		}while(--inc_d);	}	*month = m;	*day = d;	*year = y;}...for(i = 0;i < 26;i++){	printf_s("%0.2d/%0.2d/%d - ",month,day,year);	IncrementDate(&month,&day,&year,13);	printf_s("%0.2d/%0.2d/%d\n",month,day,year);	IncrementDate(&month,&day,&year,1);}


No, I am not a professional programmer. I'm just a hobbyist having fun...

Quote:
if(!(y % 4)) // Leap Year


Almost.
Quote:Original post by Antheus
Quote:
if(!(y % 4)) // Leap Year


Almost.


Thanks! I knew it couldn't be that easy.

No, I am not a professional programmer. I'm just a hobbyist having fun...

Quote:Original post by maspeir
Thanks! I knew it couldn't be that easy.


It's OK. Even Microsoft and Sony didn't get it right the first time.
There's also Boost.Date_Time.

This topic is closed to new replies.

Advertisement