0

I have an imput .txt file, which contains a date. The date is in the form DDMONYY, as in 08JUN98 or 16NOV05. I need to compare this date to the creation date of the file, and see which is sooner.

FILETIME seems like the appropriate form, especially since there is a CompareFileTime function. However, I have a problem converting the date into file time.

I can extract the date from the file and convert it into numbers (days 1-31, month 1-12, year 1900-20whatever) so I don't need help with that, but feel free to comment on it anyway.

The real problem is that I can't figure out how to get these values into a FILETIME format. Right now I have them as three different strings(or int's if that helps more) one for day, month and year. If you could help me make the jump from string to FILETIME that would be great.

Or, if there is a completely better way that I missed during my research, please feel free to suggest it.

If you want, I can post what I have, but I don't think it'll help since it's only getting to the string/int's.


if it makes it any clearer, the values I have in my variables at this point, for May 3rd, 2002, would be something along the lines of

string day = "03";
string month ="5";
string year = "2002;
int dayint = 3;
int monthint = 5;
int yearint = 2002;

And I need a way to convert this to a FILETIME

Thanks a bunch.


EDIT:

I'm not sure how I would fill a SYSTEMTIME structure, or even how to do it for a different time system. So I guess that is more my question.

I should have also mentioned that I only need accuracy of days. Hours and anything smaller aren't really of concern to me.


EDIT2:

I must not have tried giving the SYSTEMTIME int values, because that seems to work. However, I am running into issues when I try to convert that systemtime to a filetime. It states "Error: no suitable converstion from "SYSTEMTIME" to "const SYSTEMTIME *" exists". I'm not sure how to fix this, since if I define the systime variable as const SYSTEMTIME I can't assign values to it.

here's the relevant code, if it helps

SYSTEMTIME systime;
LPFILETIME ftime1;
LPFILETIME ftime2;
int dayint=6;
int monthint=12;
int yearint=1989;

systime.wDay = dayint;
systime.wMonth = monthint;
//and so on for year, hour, etc. 
SystemTimeToFileTime(systime,ftime1);     //This is where the aforementioned error occurs
GetFileTime(filename, NULL, NULL, ftime2);   //I'm also not sure about this line... feel free to critique it. I'm only interested in the last written time so I put NULL for the other times... When I check the values for this (in SYSTEMTIME) it only returns 52428, so I don't think this line is working correctly...
CompareFileTime(ftime1,ftime2);

Of course, the values for day, month, year aren't hardcoded in the actual code.


EDIT3:

I have a bad handle....

I'm going to try to fix that.... Thanks for your help Ben Voigt If I can't get it, I guess I'll be back

user1533323
  • 3
  • 1
  • 3
  • If you don't know how to assign variables in a structure, get yourself a good C++ book: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Ben Voigt Jul 18 '12 at 03:45
  • You're almost there, but these functions want the address of the `SYSTEMTIME` or `FILETIME` structure, so say `SystemTimeToFileTime(&systime, &ftime1)` and so on (the `&` operator gets the address of the variable. This address is called a pointer.) You also need to set the other fields of `SYSTEMTIME`, such as hour, minute, and second. If you don't care, you can use midnight. And you should check the return value from those functions to make sure they succeed. – Ben Voigt Jul 18 '12 at 12:46
  • Thanks, that's exactly what I needed. I was missing the `&` infront of the times. So if I understand correctly, the `SystemTimeToFileTime` function needs the address of the variable, not the actual variable? Also, I'm now having issues using `GetFileTime`. It won't actually return the `FILETIME`, it just give me the value of 52428 for each field. I'm using the following, `GetFileTime(filename,NULL,NULL,&ftime2);` is there something wrong with that? – user1533323 Jul 18 '12 at 13:28
  • Like I already said, you need to check the return value and see whether `GetFileTime` succeeded. If not, call `GetLastError()` to find out the reason why it failed. For example, the filename could be misspelled. – Ben Voigt Jul 18 '12 at 13:59

1 Answers1

4

You can fill in a SYSTEMTIME structure and then call SystemTimeToFileTime

You may want to use LocalFileTimeToFileTime afterwards, depending on whether you want to specify the hours in UTC or local time.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • How would I fill a `SYSTEMTIME` structure? I can't quite figure out how to do that either. I guess I should have clarified that. I edited the post, so hopefully it will be clearer. Thanks for the help though, I think you're right on. – user1533323 Jul 18 '12 at 02:25
  • 2
    The fields of a SYSTEMTIME should be self-explanatory, but the MSDN documentation spells it out just in case. If you are having trouble filling it out, you need to explain in more detail what part you're having trouble with. – Raymond Chen Jul 18 '12 at 02:57
  • I editted the original post and added some more information, hopefully it's everything you were looking for. – user1533323 Jul 18 '12 at 11:52