How to make an arbitrary Microsoft FILETIME?

-1

I would like to make an arbitrary Microsoft FILETIME. The algorithm is more important than the Microsoft programming language.

For example if today is June 2 2016, I would like to calculate the FILETIME at midnight June 2016.

Any help is greatly appreciated.

[EDIT June 2 2016 5:09 P.M Here is the program which is causing me headaches:

Microsoft SYSTEMTIME in nanoseconds is 1467432000. Microsoft FILETIME in nanoseconds for the same date-time stamp is 1470186549.

Here is my algorithm. May I ask what I did wrong?]

void _wapi_time_t_to_filetime(time_t timeval, FILETIME* filetime)
{
    int64_t ticks;

    ticks = ((int64_t)timeval * 10000000) + 116444736000000000ULL;
    filetime->dwLowDateTime = ticks & 0xFFFFFFFF;
    filetime->dwHighDateTime = ticks >> 32;
}


int64_t FileTime_to_POSIX(FILETIME ft)
{
    FILETIME localFileTime;
    FileTimeToLocalFileTime(&ft, &localFileTime);
    SYSTEMTIME sysTime;
    FileTimeToSystemTime(&localFileTime, &sysTime);
    struct tm tmtime = { 0 };
    tmtime.tm_year = sysTime.wYear - 1900;
    tmtime.tm_mon = sysTime.wMonth; // -1;
    tmtime.tm_mday = sysTime.wDay;
    tmtime.tm_hour = sysTime.wHour;
    tmtime.tm_min = sysTime.wMinute;
    tmtime.tm_sec = sysTime.wSecond;
    tmtime.tm_wday = 0;
    tmtime.tm_yday = 0;
    tmtime.tm_isdst = -1;
    time_t ret = mktime(&tmtime);
    return ret;
}


int _tmain(int argc, _TCHAR* argv[])
{
    SYSTEMTIME stUTCStart, stUTCFinish;
    FILETIME  ftStart, ftFinish;

    int64_t elapsedNanoSeconds(0);

    GetSystemTime(&stUTCStart);
    printf("%d nanoseconds\n", ConvertSYSTEMTIMEToNanoseconds(stUTCStart));

    struct tm tmtime = { 0 };
    tmtime.tm_year = 2016 - 1900;
    tmtime.tm_mon = 6; 
    tmtime.tm_mday = 2; 
    tmtime.tm_hour = stUTCStart.wHour;
    tmtime.tm_min = stUTCStart.wSecond;
    tmtime.tm_sec = stUTCStart.wSecond;
    tmtime.tm_wday = stUTCStart.wDayOfWeek;
    tmtime.tm_yday = 132;
    tmtime.tm_isdst = -1;
    time_t ret = mktime(&tmtime);
    _wapi_time_t_to_filetime(ret, &ftStart);

    printf("%d nanoseconds\n", FileTime_to_POSIX(ftStart));
    return 0;


  }
windows-7
date-time
algorithm
asked on Super User Jun 2, 2016 by Frank • edited Jun 2, 2016 by Frank

2 Answers

0

Here is the answer I found in the mono source code.

void _wapi_time_t_to_filetime (time_t timeval, WapiFileTime *filetime)
{
    guint64 ticks;

    ticks = ((guint64)timeval * 10000000) + 116444736000000000ULL;
    filetime->dwLowDateTime = ticks & 0xFFFFFFFF;
    filetime->dwHighDateTime = ticks >> 32;
}
answered on Super User Jun 2, 2016 by Frank
0

there are at least 3 different filetimes for windows filesystem.

the easiest way to do it is powershell, this is the preferred way when dealing with Windows on OS level and should be the right way for a question in the superuser area of the stack exchange network.

$time = get-date -hour 0 -Minute 0   # this is today midnight
$file = "c:\yourfile.txt"
$file.CreationTime = $time

and if you like:

$file.LastAccessTime = $time
$file.LastWriteTime = $time
answered on Super User Jun 4, 2016 by Falco Alexander

User contributions licensed under CC BY-SA 3.0