Hi, Ive used c++ in the past to convert unix time stamps but now I have a 64 bit time stamp to convert 'CB 6E 70 FE B9 C8 01 00' and im not sure how to go about this? is it a similar process to the unix way? Im a bit stuck
Printable View
Hi, Ive used c++ in the past to convert unix time stamps but now I have a 64 bit time stamp to convert 'CB 6E 70 FE B9 C8 01 00' and im not sure how to go about this? is it a similar process to the unix way? Im a bit stuck
Where did that 64-bit time-stamp come from, and which UNIX way are you referring to?
my program basically examines a file which contains a 64 bit time stamp which I want to strip out of the file and display in a normal readable formatt. The time stamp spans from byte 2 to byte 10 of the file.
below is how ive converted time stamps before
Code:
struct tm t = *localtime(×tamp);
char buf[80];
if (strftime(buf, sizeof buf, "%a, %d %B %Y %H:%M:%S UTC", &t) == 0)
{
std::cerr << "Error calling strftime." << std::endl;
}
std::cout << buf << std::endl;
Again, where did that come from? Knowing what type of file you're looking at would greatly aid us. Right now, we have a sequence of 8 bytes, and some time-converting code that I'm assuming gives the wrong answer.
- In what type of file you found these bytes, so we might look up format info.
- Is this big endian? little endian?
- What makes you think it's a timestamp, and do you know what date/time those bytes correspond to?
no probs,
its a Windows 64 bit time stamps stored in little endian formatt which resides inside a link file(shortcut file). Its basically the created date for the link file and I would like to convert this to a readable format
In that case, this is probably what you want: FileTimeToSystemTime()
thanks, Ive been having a look at this today wen i stuimbled across it earlier but if im honest I just dont know how to start it! I have my time stamp stored in my variable
char modifiedTime[]
I presume I somehow pass this to
FileTimeToSystemTime()
but I believe it need to be converted into some sort of time styructure for it to be correctly read?
then to display the date you print it out using the wYear, wDay........so on;
This link should show you the usage of FileTimeToSystemTime().
ssharish
You'd need to convert the timestamp string into a 64-bit int.
Here you see that the FILETIME struct has two DWORD numbers for the low and high part of the 64-bit value, so take half the timestamp string and convert it to a DWORD and store it in the high part, then take the other half, convert it and store it in the low part...
Hi Ive been having a look at that today and although i dont totally understand how to implement this totally I've had a go at the following code. It returns back one error so I'm hoping I mite be close, can you guys see where i may have gone wrong?
Code:
char createdTime[] = {_ct1,_ct2,_ct3,_ct4,_ct5,_ct6,_ct7,_ct8,'\0'}; This contains my timestamp information stored from the file im processing
DATE dt;
memcpy(&dt, createdTime, sizeof(DATE));
SYSTEMTIME stime;
FileTimeToSystemTime( dt, &stime ); // convert to structure
cout << stime.wMonth << "/" << stime.wDay << "/ " << stime.wYear << " Time:" << stime.wHour<<":"<< stime.wMinute << ":"<<stime.wSecond <<"\n";
cout << "The created time is "<< createdTime << " "<< endl;
It would help if you told us the error you're getting, but I'm guessing it's because you're passing a DATE parameter to FileTimeToSystemTime() instead of the FILETIME parameter that it's expecting.
changing it to FILETIME i get the errors
cannot convert _FILETIME to const _FILETIME *
and
type mismatch in parameter lpfiletime
>cannot convert _FILETIME to const _FILETIME *
Did you try:
Code:FILETIME filetime;
memcpy(&filetime, createdTime, sizeof(DATE));
FileTimeToSystemTime( &filetime, &stime ); // convert to structure
sorry to be a pain but I'm starting to get lost off as to what im doing now. I have the following code
Code:
char createdTime[] = {_ct1,_ct2,_ct3,_ct4,_ct5,_ct6,_ct7,_ct8,'\0'}; //my time stamp
/***************DO I NEED BOTH OF THESE?***************/
DATE dt;
FILETIME filetime;
/*******************************************************/
memcpy(&filetime, createdTime, sizeof(DATE));
//THE BELOW CODE IS WHERE I START TO GET THE ERRORS
FileTimeToSystemTime( &filetime, &stime ); // convert to structure
cout << stime.wMonth << "/" << stime.wDay << "/ " << stime.wYear << " Time:" << stime.wHour<<":"<< stime.wMinute << ":"<<stime.wSecond <<"\n";
The errors i now get is "pointer to overloaded function stime" and "type mismatch in lpsystemtime". I'm a little confused as to where stime comes from here
thank you for your patients
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:)!!
> 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.
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:
Or you can try flipping around {_ct1,_ct2,_ct3,_ct4,_ct5,_ct6,_ct7,_ct8 in the array, and see what happens.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
ive just tried both without any luck
>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;
}
After putting that in my program it printed out '4'.......from your response im guessing thats not good!
It shouldn't be that hard. Here's a simple example I wrote that works:
Also note that the FileTimeToSystemTime() documentation says: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;
}
The timestamp you posted starts with "CB" which is higher than "80".Quote:
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.
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').
>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')?
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!
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
where as you haveCode:char createdTime[] = {_ct1,_ct2,_ct3,_ct4,_ct5,_ct6,_ct7,_ct8, '/0'};
I've been trying to insert mine asCode:std::string hexTime( "01 6E 70 FE B9 C8 01 00 " );
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?Code:std::string hexTime =createdTime;
>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:
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: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';
char createdTime[] = {_ct1,_ct2,_ct3,_ct4,_ct5,_ct6,_ct7,_ct8, '/0'};
Be sure to #include <cstring> for the strlen() function.
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?