Hi,
how can I convert time_t to CString? (I need the seconds since midnight 01.01.1970 as a CString.
Thanks in advance!
radu
Printable View
Hi,
how can I convert time_t to CString? (I need the seconds since midnight 01.01.1970 as a CString.
Thanks in advance!
radu
Do you mean Something Like this.
ThanxCode:time_t aclock;
struct tm *newtime;
time( &aclock ); // Get time in seconds
newtime = localtime( &aclock ); // Convert time to struct tm form
CString str = asctime(newtime);
Since time_t is an unsigned int you can use CString member function Format for converting it.
For eg:
CString csTimeConv;
csTimeConv.Format( _T( "%u", tYour_time_t );
yeah . .but that gives you a number. . in fact. . it 's the secondcount from '1970 jan 1'Quote:
Originally Posted by thomas_nibu
so .. in your case it would be 'the current time is 16899024' or something like that.. . It think the TS wants a displayable time.
Thanks guys!
I was looking for the solution given by thomas_nibu. Just wanted to have the number as CString.
Have a nice day!
radu
Skizmo,
He said that he want's the number. Not a formatted one.
yeah . .My mistake. .. I noticed after posting :)Quote:
Originally Posted by thomas_nibu
one more question:
I am getting 5 bytes data. Isn't it supposed to be 4 bytes? i.e. now I was getting 1169467720
First thing OP Doesn't Mention in His First Post What is his Actual Requirement.He Simply asked how to convert time_t to CString. So Anyone will think that might be he want some formatted text so he can use somewhere or he want to print it in some control etc etc.So it's always good if you Specify your question in a efficient manner so you can get a proper answer of your question.
Thanx
well, I think "I need the seconds since midnight 01.01.1970 as a CString" should be clear enough. There is nothing mentioned about "formatted" in my post. :)Quote:
Originally Posted by humptydumpty
Format it as hex value (Format( _T( "%x", tYour_time_t )) and you'll get 4 bytes. :rolleyes:Quote:
Originally Posted by Radu
oh man, ..
sure! :blush:
:lol:Quote:
Originally Posted by VictorN
Correction: in order to always get four bytex for int/long value the format specification must be a little changed:Quote:
Originally Posted by VictorN
Code:CString csTimeConv;
csTimeConv.Format( _T( "%08x", tYour_time_t );
// or
csTimeConv.Format( _T( "% 8x", tYour_time_t );
Thanks Victor! :wave: