tags:

views:

192

answers:

4

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 if I am on a 32 bit system, and have to read the integer in from a file?

C not C++

+1  A: 

Put the time into a struct tm and call strftime().

It's all declared in <time.h>.

pmg
+2  A: 

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.

dirkgently
isn't this specific to windows?
I assumed you meant the Windows data structure.
dirkgently
+2  A: 

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.
Thom Smith
how can i subtract 5329584000 from a number on 32bit since this #is larger than 32bits
Use a bigger int.
Thom Smith
Even on 32-bit architectures, C has support for 64-bit integers. http://en.wikipedia.org/wiki/C_syntax#Integral_types As noted, look at the limits.h on your system. actually, just do some tests: sizeof(unsigned long long). Or you can try to something fancy like create a struct with a 64-bit integer, eg: struct {unsigned int foo:64}, though the implementation of that will vary.
rascher
thanks rascher!
A: 

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.

Oleg Zhylin