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: 540
  • Last Modified:

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
0
healingtao
Asked:
healingtao
  • 4
  • 4
1 Solution
 
jkrCommented:
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)
}

0
 
healingtaoAuthor Commented:
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;
}
0
 
jkrCommented:
How is 'Date' declared and how are you calling this function?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
healingtaoAuthor Commented:
Date date("04/06/2005");
MYClass myObject = new MYClass();
myObject->AddDateDiff(date, 1);
0
 
healingtaoAuthor Commented:
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);
}
0
 
healingtaoAuthor Commented:
I should say "Date class, not method" at the top of last posting
0
 
jkrCommented:
Hmm, I actually was referring the declaration of the 'Date' class :o)
0
 
jkrCommented:
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.
0
 
novitiateCommented:
I have kind of reconstructed your code and it seems to be working, see if your code looks like this...

#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
class InvalidParameterException
{
};
class Date : public FILETIME
{
public:
      Date::Date(const char* tm, const char* format = NULL)
      {
            char buffer[64];
            
            SYSTEMTIME st;
            ::memset(&st, 0, sizeof(SYSTEMTIME));
            
            char* slash = ::strchr(tm, '/');
            if (slash == NULL)
                  throw InvalidParameterException();
            
            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();
            
            count = slash - tm - oldCount;
            ::memcpy(buffer, tm + oldCount, count);
            buffer[count] = '\0';
            st.wDay = ::atoi(buffer);
            
            st.wYear = ::atoi(slash + 1);
            
            operator=(st);
      }
      
      Date & operator = (SYSTEMTIME & st)
      {
            
            ::SystemTimeToFileTime(&st, this);
            return (*this);
      }

      friend ostream & operator << (ostream & os, Date & d);      
private:
};

ostream & operator << (ostream & os, Date & d)
{
      SYSTEMTIME st = {0};
      ::FileTimeToSystemTime(&d, &st);
      cout << st.wMonth << "/" << st.wDay << "/" << st.wYear << endl;
      return os;
}
class CMyClass
{
public:
      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)
      }
      
};
int main()
{
      Date date("04/06/2005");

      cout << date;
      CMyClass *myObject = new CMyClass();
      myObject->AddDateDiff(date, 1);
      
      cout << date;
      myObject->AddDateDiff(date, -1);
      cout << date;
      myObject->AddDateDiff(date, -1);
      cout << date;
      return 0;
}

_novi_
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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