Link to home
Start Free TrialLog in
Avatar of healingtao
healingtao

asked on

FILETIME question

I have the following function

I need to add 1 day or subtract one day from date which is derived from FILETIME(WIN sdk Structure), it's passed here as the first parameter
The second parameter will be either 1 or -1 but for some reason the date is never updated. Can anyone explain how to fix this problem?

void AddDateDiff(const Date& date, const __int64 num )
{
const __int64 nano100SecInDay=(__int64)10000000*60*60*24;  // one day
__int64* pi;
pi = (__int64*)&date;
(*pi) += nano100SecInDay * num; // (num is 1 or -1)
}

Thanks in advance
Avatar of jkr
jkr
Flag of Germany image

Well, 'date' is declared as 'const', which it should not be if you want to change it - try

void AddDateDiff(Date& date, const __int64 num ) // removed 'const' for 'date'
{
const __int64 nano100SecInDay=(__int64)10000000*60*60*24;  // one day
__int64* pi;
pi = (__int64*)&date;
(*pi) += nano100SecInDay * num; // (num is 1 or -1)
}

or

void AddDateDiff(Date* date, const __int64 num )
{
const __int64 nano100SecInDay=(__int64)10000000*60*60*24;  // one day
__int64* pi;
pi = (__int64*)date;
(*pi) += nano100SecInDay * num; // (num is 1 or -1)
}

Avatar of healingtao
healingtao

ASKER

Jkr,

I tried the following but it still doesn't work. The date is always the same, Not sure why
void AddDateDiff(Date* date, const __int64 num )
{
const __int64 nano100SecInDay=(__int64)10000000*60*60*24;  // add one day
__int64* pi;
pi = (__int64*)date;
(*pi) += nano100SecInDay * num;
}
How is 'Date' declared and how are you calling this function?
Date date("04/06/2005");
MYClass myObject = new MYClass();
myObject->AddDateDiff(date, 1);
I have Date method that allows this constructor. It's a library I need to use

Date::Date(const char* tm, const char* format)
{
      char buffer[64];

      SYSTEMTIME st;
      ::memset(&st, 0, sizeof(SYSTEMTIME));

      char* slash = ::strchr(tm, '/');
      if (slash == NULL)
            throw InvalidParameterException(String("Invalid date format: ") + tm);

      size_t count = slash - tm;
      ::memcpy(buffer, tm, count);
      buffer[count] = '\0';
      st.wMonth = ::atoi(buffer);
      
      size_t oldCount = count + 1;
      slash = ::strchr(tm + oldCount, '/');
      if (slash == NULL)
            throw InvalidParameterException(String("Invalid date format: ") + tm);

      count = slash - tm - oldCount;
      ::memcpy(buffer, tm + oldCount, count);
      buffer[count] = '\0';
      st.wDay = ::atoi(buffer);

      st.wYear = ::atoi(slash + 1);

      operator=(st);
}
I should say "Date class, not method" at the top of last posting
Hmm, I actually was referring the declaration of the 'Date' class :o)
Well, since all I have is that Date is somehow derive from FILETIME, and apparently has way more members than a FILETIME struct, you are probably altering other members of that class, since

pi = (__int64*)&date;
(*pi) += nano100SecInDay * num; // (num is 1 or -1)

would only work for a plain FILETIME structure.
ASKER CERTIFIED SOLUTION
Avatar of novitiate
novitiate

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial