Thread: Converting FILETIME to timestamp

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Converting FILETIME to timestamp

    I would like to convert my FILETIME structure to timestamp. Is there an easy way to do it?
    (The FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.)

    Actually I want to compare it to the current time and see if 29 days have passed since the file was last modified/created/accessed.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    FileTimeToSystemTime() ?

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    But how do I convert the system time to timestamp? (Timestamp means seconds from 1. january 1970, it's used in many places)
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > and see if 29 days have passed since the file was last modified/created/accessed.
    So why the need to rebase to seconds since 1/1/1970

    Just work out how many 100nS there are in 29 days (that's easy) and use the timestamp you have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    How about using Julian dates?

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    LONG CalcDays(int iYear, int iMonth, int iDay)
    {
        LONG lReturnDays = iDay;
        int iTemp1 = (14 - iMonth) / 12;
        int iTemp2 = iYear + 4800 - iTemp1;
        int iTemp3 = iMonth + 12 * iTemp1 - 3;
    
        lReturnDays += (153 * iTemp3 + 2)/5;
        lReturnDays += 365 * iTemp2 + iTemp2 / 4 - 32083;
        return lReturnDays;
    }
    
    BOOL CalcFileAge(char * szFile)
    {
        LONG lFileDays, lCurrentDays;
        FILETIME ftCreate, ftAccess, ftWrite, ftLocal;
        SYSTEMTIME stCreate, stNow;
        HANDLE hFile; 
    
        hFile = CreateFile(szFile,           
            GENERIC_READ,             
            FILE_SHARE_READ,           
            NULL,                     
            OPEN_EXISTING,             
            FILE_ATTRIBUTE_NORMAL,     
            NULL);                     
        if (hFile == INVALID_HANDLE_VALUE) 
            return FALSE;   
        if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
        {
            CloseHandle(hFile);
            return FALSE;
        }
        if (!FileTimeToLocalFileTime(&ftWrite, &ftLocal))
        {
            CloseHandle(hFile);
            return FALSE;
        }
        GetSystemTime(&stNow);
        FileTimeToSystemTime(&ftLocal, &stCreate);
        CloseHandle(hFile);
        lFileDays = CalcDays(stCreate.wYear,stCreate.wMonth,stCreate.wDay);
        lCurrentDays = CalcDays(stNow.wYear,stNow.wMonth,stNow.wDay);
        printf("%s is %ld day(s) old\n", szFile, (lCurrentDays - lFileDays));
        return TRUE;
    }
    
    int main(int argc, char **argv)
    {
        CalcFileAge(argv[1]);
        return 0;
    }

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I already did it as Salem said...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Filetime from FTP
    By knutso in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 06:32 AM
  3. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. Writing FILETIME or SYSTEMTIME to File?
    By Tojam in forum Windows Programming
    Replies: 2
    Last Post: 09-23-2003, 01:55 AM