2 Replies - 238 Views - Last Post: 07 August 2012 - 06:25 AM Rate Topic: -----

#1 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 23-April 12

Adding date to struct tm

Posted 07 August 2012 - 05:59 AM

I'm trying to add a date to a to a Date struct which gets the current system from struct tm in ctime and adds days to it.

The function assumes that every month is 31 days long (to add 1 to the month) but it can have days longer than 31. How do i determine the days of each month and is there an easier way to add the date by a function call to ctime?

Heres my code:
				void enterLoanDate(int weeks)
				{
					t = time(NULL);
					timePtr = localtime(&t);
					int overflow = 0;

					for (int i = 0; i <= (weeks * 7); i++)
					{
					  dueDate.day = timePtr->tm_mday + i;
					  if (dueDate.day > 31)
						overflow++;
					}
					if (overflow > 0)
					   dueDate.month = timePtr->tm_mon + overflow;
					else
					   dueDate.month = timePtr->tm_mon;

					if(dueDate.month < timePtr->tm_mon)
					   dueDate.year = timePtr->tm_year + YR + 1;
					else
					  dueDate.year = timePtr->tm_year + YR;
                }


This post has been edited by rethc: 07 August 2012 - 06:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding date to struct tm

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Adding date to struct tm

Posted 07 August 2012 - 06:13 AM

You can just do
timePtr->tm_mday += weeks * 7;

Then call mktime() to do all the work for you.
Was This Post Helpful? 1
  • +
  • -

#3 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 23-April 12

Re: Adding date to struct tm

Posted 07 August 2012 - 06:25 AM

View PostSalem_c, on 07 August 2012 - 06:13 AM, said:

You can just do
timePtr->tm_mday += weeks * 7;

Then call mktime() to do all the work for you.


Thank you, just what i was looking for :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1