Thread: converting 64 bit time and date

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    I promise this will be my last post on this topic, Ive tried that code now and im getting an "incompatible type conversion " this is driving me nuts


    editted below

    Code:
    DATE dt;
    SYSTEMTIME stime;
              FILETIME filetime;
    
    /*******************************************************/
       
            memcpy(&filetime, createdTime, sizeof(DATE));
    
    
    
      FileTimeToSystemTime( &filetime, &stime ); // convert to structure
    
    cout << stime.wMonth << "/" << stime.wDay << "/ " << stime.wYear << "     Time:"  <<           stime.wHour<<":"<< stime.wMinute << ":"<<stime.wSecond  
    
    <<"\n";

    Ive got this to compile but the date and time is not correct.....IM SURE IM CLOSE!!
    Last edited by pastitprogram; 06-30-2008 at 04:14 PM.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > memcpy(&filetime, createdTime, sizeof(DATE));
    I didn't notice this earlier, but the third argument should be sizeof(filetime) or sizeof(FILETIME):
    memcpy(&filetime, createdTime, sizeof(filetime));

    >im getting an "incompatible type conversion "
    On which line? Just taking a quick glance I don't see it.

    Also I think you can remove the DATE dt; line, unless you think you may need it in the future.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also, if it's still not giving the expected time value, it could be because the bytes are flipped in the wrong order. I don't know how you read the timestamp from the file, but you could do it like this:
    Code:
    	string filename = "file.txt";	//Make this the name of your file
    	FILETIME filetime;
    	SYSTEMTIME stime;
    
    	ifstream in(filename.c_str(), ios::binary);
    	if (!in.is_open())
    
    	{
    		cerr << "Unable to open file: " << filename << endl;
    		return 1;
    	}
    	in.read(reinterpret_cast<char *> &filetime, sizeof(filetime));
    
    	FileTimeToSystemTime( &filetime, &stime ); // convert to structure
    Or you can try flipping around {_ct1,_ct2,_ct3,_ct4,_ct5,_ct6,_ct7,_ct8 in the array, and see what happens.
    Last edited by swoopy; 06-30-2008 at 05:59 PM.

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    ive just tried both without any luck

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >ive just tried both without any luck
    Try printing out the sizeof(time_t) and see what value it prints. If it's 8, then maybe the timestamp is a time_t. If it's something like 4, then I don't know. I'm not familiar with Windows timestamps.
    Code:
    #include <ctime>
    #include <iostream>
    int main()
    {
    	std::cout << sizeof(time_t) << std::endl;
    
    	return 0;
    }

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    After putting that in my program it printed out '4'.......from your response im guessing thats not good!

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It shouldn't be that hard. Here's a simple example I wrote that works:

    Code:
    #include <ios>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <windows.h>
    
    int main()
    {
    	std::string hexTime( "01 6E 70 FE B9 C8 01 00 " );	// Note: the space at the end is required!
    	std::stringstream ss;
    	unsigned int highNum = 0;
    	unsigned int lowNum = 0;
    	unsigned int temp = 0;
    
    	for ( int i = 0; i < 4; ++i )
    	{
    		ss << std::hex << hexTime;
    		ss >> temp;
    		std::cout << "Num is: " << temp << std::endl;
    		highNum <<= 8;
    		highNum += temp;
    	}
    
    	for ( int i = 0; i < 4; ++i )
    	{
    		ss << std::hex << hexTime;
    		ss >> temp;
    		std::cout << "Num is: " << temp << std::endl;
    		lowNum += temp;
    		lowNum <<= 8;
    	}
    
    	FILETIME ft;
    	ft.dwHighDateTime = highNum;
    	ft.dwLowDateTime = lowNum;
    	SYSTEMTIME st;
    
    	if ( FileTimeToSystemTime( &ft, &st ) == FALSE )
    	{
    		std::cout << "FileTimeToSystemTime() failed with error code: " << GetLastError() << std::endl;
    		return 1;
    	}
    
    	std::cout << "Date & Time is:  " << st.wYear << "/"
    					 << st.wMonth << "/"
    					 << st.wDay << " "
    					 << st.wHour << ":"
    					 << st.wMinute << ":"
    					 << st.wSecond << ":"
    					 << st.wMilliseconds << std::endl;
    
    	return 0;
    }
    Also note that the FileTimeToSystemTime() documentation says:
    lpFileTime [in]
    A pointer to a FILETIME structure containing the file time to convert to system date and time format.

    This value must be less than 0x8000000000000000. Otherwise, the function fails.
    The timestamp you posted starts with "CB" which is higher than "80".

  8. #23
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What value for year did you get cpjust? For your timestamp, I'm getting 1927 for the year. For pastitprogram's timestamp, I'm getting 1602. Which leads me to believe the original timestamp is in question ('CB 6E 70 FE B9 C8 01 00').

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >After putting that in my program it printed out '4'
    Right, it can't be a time_t. Are the numbers you posted the actual timestamp ('CB 6E 70 FE B9 C8 01 00')?

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    Thank you guys for all your help and patients with me, ive finally got something working now but wud never have been able to do it without your pushes in the right direction. Much appreciated!

  11. #26
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by swoopy View Post
    What value for year did you get cpjust? For your timestamp, I'm getting 1927 for the year. For pastitprogram's timestamp, I'm getting 1602. Which leads me to believe the original timestamp is in question ('CB 6E 70 FE B9 C8 01 00').
    Yes, for the timestamp I used starting with "01" I get 1927. If I used the "CB" timestamp FileTimeToSystemTime() fails with an error code of 87 (Invalid parameter).

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by cpjust View Post
    Yes, for the timestamp I used starting with "01" I get 1927. If I used the "CB" timestamp FileTimeToSystemTime() fails with an error code of 87 (Invalid parameter).
    Ok, thanks for the info. It looks like pastitprogram got it working thanks to the code you posted.

  13. #28
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    thanks again for all the help guys, as with your example of the time which I think could work alot better than mine I have my time stamp store in the array

    Code:
    char createdTime[] = {_ct1,_ct2,_ct3,_ct4,_ct5,_ct6,_ct7,_ct8, '/0'};
    where as you have

    Code:
    std::string hexTime( "01 6E 70 FE B9 C8 01 00 " );
    I've been trying to insert mine as

    Code:
    std::string hexTime =createdTime;
    It compiles but then gives me crazy output so i'm guessing this is not the right way to go about it. I was wondering if it is something to do with my declaration or theb fact my CreatedTime array is storing each byte of the time stamp in its ascii format and not in the hex characters as you have in your string?
    Last edited by pastitprogram; 07-02-2008 at 04:10 AM.

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >CreatedTime array is storing each byte of the time stamp in its ascii format and not in the hex characters as you have in your string?
    I think it's actually the opposite. Your array stores 2 hex digits in one byte, where the std::string stores 2 hex digits in 2 bytes. If you want to use cpjust's code, then convert your createdTime to a std::string:
    Code:
    std::ostringstream converter;
    int len = strlen(createdTime);
    for (int i=0; i<len; i++)
    {
    	converter << hex << (int) createdTime[i] << " ";
    }
    std::string hexTime = converter.str();
    std::cout << hexTime << '\n';
    If you print out hexTime at this point, it should look something like what you show above. Just add the above code right below this line:
    char createdTime[] = {_ct1,_ct2,_ct3,_ct4,_ct5,_ct6,_ct7,_ct8, '/0'};
    Be sure to #include <cstring> for the strlen() function.
    Last edited by swoopy; 07-02-2008 at 02:47 PM.

  15. #30
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I have no idea what you're doing with createdTime? It's an array of 9 chars, but the hex date string is 25 chars.
    How are you reading the hex date timestamp from the file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. How to get and set windows time and date
    By huwan in forum Windows Programming
    Replies: 18
    Last Post: 05-13-2008, 10:33 AM
  4. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM