convert time from int in c
If I have an integer in c that represents the date how do I print out the actuall mm/dd/year?
64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC)
1183042181
what开发者_JS百科 if I am on a 32 bit system, and have to read the integer in from a file?
C not C++
C's library contains the strftime function, which will format a struct tm using a specified format string. From there, of course, you can print it. So:
- Convert your FILETIME to seconds since 1601 by dividing by 10,000,000.
- Convert to a standard UNIX timestamp by subtracting the number of seconds between 1601-01-01 and 1970-01-01.
- Use localtime() to convert that to a struct tm.
- Use strftime() to get a formatted string.
- Print it.
- Write a function to do all of this so you don't have to do it again.
Read the MSDN section:
Remarks
To convert a FILETIME structure into a time that is easy to display to a user, use the FileTimeToSystemTime function.
Put the time into a struct tm
and call strftime()
.
It's all declared in <time.h>.
You can use this routine to convert your 64 bit value to time_t structure
time_t FileTimeToUnixTime(FILETIME& ft)
{
ULARGE_INTEGER li;
assert(sizeof(ULARGE_INTEGER) == sizeof(FILETIME));
memcpy(&li, &ft, sizeof(FILETIME));
li.QuadPart -= 116444736000000000;
li.QuadPart /= 10000000;
return (time_t)(li.LowPart);
}
Certainly this will only work if your FILETIME is really in time_t range. I suppose you know what to do with resulting time_t.
32 bit system won't prevent you from reading 64 bit as a binary value. If you prefer scanf you can use "%llu" format.
精彩评论