Mengubah Waktu File ke Waktu Saat Ini

Contoh berikut mengatur waktu penulisan terakhir untuk file ke waktu sistem saat ini menggunakan fungsi SetFileTime .

Sistem file NTFS menyimpan nilai waktu dalam format UTC, sehingga tidak terpengaruh oleh perubahan zona waktu atau waktu musim panas. Sistem file FAT menyimpan nilai waktu berdasarkan waktu lokal komputer.

File harus dibuka dengan fungsi CreateFile menggunakan akses FILE_WRITE_ATTRIBUTES.

#include <windows.h>

// SetFileToCurrentTime - sets last write time to current system time
// Return value - TRUE if successful, FALSE otherwise
// hFile  - must be a valid file handle

BOOL SetFileToCurrentTime(HANDLE hFile)
{
    FILETIME ft;
    SYSTEMTIME st;
    BOOL f;

    GetSystemTime(&st);              // Gets the current system time
    SystemTimeToFileTime(&st, &ft);  // Converts the current system time to file time format
    f = SetFileTime(hFile,           // Sets last-write time of the file 
        (LPFILETIME) NULL,           // to the converted current system time 
        (LPFILETIME) NULL, 
        &ft);    

    return f;
}