comparing file times

I'm trying do something if a file is 1 day older than the one that is just created(kinda gets the current time since the blank file is created right there)

1
2
3
4
5
6
7
8
9
10
11
12
13
        HANDLE File = CreateFile(("CardData/Prices/"+TargetCard.Name+".txt").c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        HANDLE BlankFile = CreateFile("CardData/Prices/blank.txt", GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);

        LPFILETIME CreationTimeData = NULL;
        LPFILETIME CreationTimeBlank = NULL;
        GetFileTime(BlankFile, CreationTimeBlank, NULL, NULL);
        GetFileTime(File, CreationTimeData, NULL, NULL);


        if(CompareFileTime(CreationTimeData, CreationTimeBlank) == 1)//if date is greater than a day update it
        {
            Update = true;
        }


this works only for if the data file is even 1 second old. Does anyone now how to compare it for a day or greater?
Last edited on
How many seconds are there in a day?

Also use greater than or equal, not just equal.
Well the problem is this:

The return value is one of the following values.

First file time is earlier than second file time. -1
First file time is equal to second file time. 0
First file time is later than second file time. 1

that's from msdn.
FILETIME is in 100-nanosecond intervals, so you work out how many of these is one day. Then you can calculate the difference between the two times and compare that with this value.

See the "Performing Arithmetics with File Times" (sic) of the following MSDN article:

"INFO: Working with the FILETIME Structure"
http://support.microsoft.com/kb/188768

Basically, you have to convert the FILETIMEs to ULONGLONGs and then do the maths with those.

Andy

Last edited on
Sounds good thanks. Do you think this would work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
SYSTEMTIME Time;
        HANDLE File = CreateFile(("CardData/Prices/"+TargetCard.Name+".data").c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        HANDLE BlankFile = CreateFile("CardData/Prices/blank.data", GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
        GetLocalTime(&Time);

        FILETIME CreationTimeData;
        FILETIME CreationTimeBlank;
        GetFileTime(BlankFile, &CreationTimeBlank, NULL, NULL);
        GetFileTime(File, &CreationTimeData, NULL, NULL);

        ULONGLONG DataDays;
        ULONGLONG BlankDays;

        BlankDays = (((ULONGLONG) CreationTimeBlank.dwHighDateTime) << 32) + CreationTimeBlank.dwLowDateTime;
        BlankDays += _DAY;
        DataDays = (((ULONGLONG) CreationTimeData.dwHighDateTime) << 32) + CreationTimeData.dwLowDateTime;
        DataDays += _DAY;

        if((BlankDays-DataDays) > 0)//if date is greater than a day update it
        {
            Update = true;
            remove(("CardData/Prices/"+TargetCard.Name+".data").c_str());
        }
Last edited on
The maths doesn't look right.

You originally said
... if a file is 1 day older than the one that is just created ...

So you get the times of the two files, cslculate the difference, and see it that's bigger than a day.

Adding _DAY to both times doesn't help you achieve this!

Andy

P.S. I would do the final bit of maths in the if() test, as that makes it clear what your testing.
Last edited on
Instead of making a new file couldn't I get the data fie time then convert it to system time, and compare it with the system time?
Or you could use GetSystemTimeAsFileTime() and compare that to the file's time?

Andy

P.S. Before GetSystemTimeAsFileTime appeared, it was usual to do it the other way about.

GetSystemTime
SystemTimeToFileTime
...

Comparing FILETIMEs is easier as it's a simple count from January 1, 1601.
I think I got this to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
        SYSTEMTIME Time;
        SYSTEMTIME FileTime;
        HANDLE File = CreateFile(("CardData/Prices/"+TargetCard.Name+".data").c_str(), 
                                 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 
                                 FILE_ATTRIBUTE_NORMAL, NULL);
        FILETIME CreationTimeData;

        GetSystemTime(&Time);
        GetFileTime(File, &CreationTimeData, NULL, NULL);
        FileTimeToSystemTime(&CreationTimeData, &FileTime);

        if(Time.wDay > FileTime.wDay)
        {
            Update = true;
        }

        else if(Time.wMonth > FileTime.wMonth)
        {
            Update = true;
        }

        else if(Time.wYear > FileTime.wYear)
        {
            Update = true;
        }
Last edited on
This does look like the logic will be triggered if the file is created as 23:59 and the test run at 0:00 the next day. Is that OK?

Using the FILETIME approach will test to see if the file is older than 24 hours.
Last edited on
So do you think this should do it? I tried to take in account mouths and years since the days go back to one.
If you want to test if the file was created before today, whether it's 1 sec or decades, your code is sort of OK.

If you want to test if it's more than 24 hours old, then it does not work. That is easier to do the FILETIME difference way.

But note that SystemTime returns UTC time, rather than local time. So where the day ends is going to vary depending on your time zone.
Last edited on
Topic archived. No new replies allowed.