Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

Simple date functions in Microsoft VC++ 6

Hi,

I need to do the following ('simple things') with dates in VC++:
- Obtain todays date in dd/mm/yy format
- Pass a stored date (stored as 3 integers - d,m,y) into a 'date' variable
- Compare two dates, and obtain the number of days between them (this should allows for negative values)
- Add a number of days to a date, and return a valid date which can be stored again in three integers

I have managed to get as far as obtaining todays date, and saving as three integers:
      char dateToEncode[9], tempDateBits[3];
      int todayDay, todayMonth, todayYear;

      _strdate(dateToEncode);

      //Splitting the date into components
      tempDateBits[0] = dateToEncode[0];
      tempDateBits[1] = dateToEncode[1];
      todayMonth = atoi(tempDateBits);

      tempDateBits[0] = dateToEncode[3];
      tempDateBits[1] = dateToEncode[4];
      todayDay = atoi(tempDateBits);

      tempDateBits[0] = dateToEncode[6];
      tempDateBits[1] = dateToEncode[7];
      todayYear = atoi(tempDateBits);

But cannot do any of the calculations...

Would really appreciate any suggestions!

Thanks in advance,
Bob.
0
capsoftuk
Asked:
capsoftuk
  • 4
  • 2
  • 2
  • +2
4 Solutions
 
dimitryCommented:
You need to add '\0' string termination to allow your atoi() to work...
Something like this:
     tempDateBits[0] = dateToEncode[0];
     tempDateBits[1] = dateToEncode[1];
     tempDateBits[2] = '\0';
     todayMonth = atoi(tempDateBits);

     tempDateBits[0] = dateToEncode[3];
     tempDateBits[1] = dateToEncode[4];
     tempDateBits[2] = '\0';
     todayDay = atoi(tempDateBits);

     tempDateBits[0] = dateToEncode[6];
     tempDateBits[1] = dateToEncode[7];
     tempDateBits[2] = '\0';
     todayYear = atoi(tempDateBits);
0
 
capsoftukAuthor Commented:
Thanks for the help. This bit works fine regardless, but I will alter my code anyway, just to keep it neat!
0
 
dimitryCommented:
Actually, you need to set '\0' only once, so next code is also Ok:
     tempDateBits[0] = dateToEncode[0];
     tempDateBits[1] = dateToEncode[1];
     tempDateBits[2] = '\0';
     todayMonth = atoi(tempDateBits);

     tempDateBits[0] = dateToEncode[3];
     tempDateBits[1] = dateToEncode[4];
     todayDay = atoi(tempDateBits);

     tempDateBits[0] = dateToEncode[6];
     tempDateBits[1] = dateToEncode[7];
     todayYear = atoi(tempDateBits);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
allmerCommented:
What system are you working on?
in windows CTime may solve all your problems.
CTime time;
//This may be the class you want to use.
//Get the current time:
currTime = CTime::GetCurrentTime();

Otherwise have a look at this page:
http://www.codeproject.com/datetime/datetimedisc.asp

Hope it helps,
Jens
0
 
AxterCommented:
You can use strftime to format a string in your desired format
0
 
AxterCommented:
See following example:

char time_str[99] = {0};
time_t current_time = time(NULL);
struct tm *time_data = localtime(&current_time);
strftime(time_str, 30, "%d/%m/%y", time_data);
0
 
AxterCommented:
Correction:
strftime(time_str, sizeof(time_str), "%d/%m/%y", time_data);
0
 
AxterCommented:
Here's an MFC example:
CTime currentTime(CTime::GetCurrentTime());
CString TimeFormated = currentTime.Format("%d/%m/%y");
0
 
Jaime OlivaresSoftware ArchitectCommented:
> Compare two dates, and obtain the number of days between them (this should allows for negative values)
> Add a number of days to a date, and return a valid date which can be stored again in three integers

About this, you can use MFC CTimeSpan class

CTime a, b;
CTimeSpan s;

You can make additions and substractions, like these:
s = a - b;
b += s;

Also you can use it as:
int d = s.GetDays();

Have a look to:
http://msdn.microsoft.com/library/en-us/vclib/html/_MFC_CTimeSpan.asp
0
 
capsoftukAuthor Commented:
Hi guys,

Thanks for all the feedback. Unfortuanately I forgot to specify that I was trying to avoid MFC (I hear it has more dependencies, which I am trying to avoid - I am just creating a 'simple' dll).

Anyway, in the end I just wrote my own class:

//Only works with dates between 1/1/2000 and 31/12/2099
class rjedate
{
public:
      rjedate();
      rjedate(int dd, int mm, int yyyy);
      rjedate::rjedate(const rjedate& copyDate);
      ~rjedate();
      void setSide(double s);
      long rjedate::dateToDays(void);
private:
      int day, month, year;
};

rjedate::rjedate()
{
      day = 1;
      month = 1;
      year = 2000;
}

rjedate::rjedate(const rjedate& copyDate)
{
      day = copyDate.day;
      month = copyDate.month;
      year = copyDate.year;
}

rjedate::rjedate(int dd, int mm, int yyyy)
{
      if (yyyy < 2000) yyyy = 2000 + (yyyy % 100);
      if (dd < 1 || dd > 31) dd = 1;
      if (mm < 1) mm = 1;
      if (mm > 12) mm = 12;
      day = dd;
      month = mm;
      year = yyyy;
}

rjedate::~rjedate()
{
      day = -1;
      month = -1;
      year = -1;
}

//Number of Days from 01/01/2000
long rjedate::dateToDays(void)
{
      long tempYears, tempYearsA, tempYearsB, totalDays;

      //Work out whole years
      tempYears = this->year % 100;
      tempYearsA = (long) (tempYears + 3) / 4;
      tempYearsB = tempYears - tempYearsA;
      totalDays = tempYearsA * 366 + tempYearsB * 365;

      //Work out months
      switch(this->month)
      {
      case 1: totalDays += 0; break;
      case 2: totalDays += 31; break;
      case 3: totalDays += 59; break;
      case 4: totalDays += 90; break;
      case 5: totalDays += 120; break;
      case 6: totalDays += 151; break;
      case 7: totalDays += 181; break;
      case 8: totalDays += 212; break;
      case 9: totalDays += 243; break;
      case 10: totalDays += 293; break;
      case 11: totalDays += 304; break;
      case 12: totalDays += 334; break;
      }
      //if there is a Feb 29th, add this day
      if(this->year % 4 == 0 && this->month > 2) totalDays++;

      //Finally, add the days...
      totalDays += this->day;

      return(totalDays);
}

I have distributed the points between you, as it was my fault for not specifying.

Thanks again for your help, and if you can see any flaws with my code, please let me know!

Bob.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now