Hi
How do I cast time_t into a format that I can print out
I want to store the value of a time_t into the registry, but I was given the error unable to cast the type into LPCSTR. I tried to wrap it in a CString and got the same error. I also tried to cast it by using (LPCSTR) (tTime) but it failed too.
Likewise, I have a DWORD which contains a number but I was unable to cast them to either a std::string or CString to print it out.
What should I do Will using reinteprete_cast<> work

time_t to displayable format?
karande23
Well, let's say we have a time_t value.
- Both MFC classes CTime and COleDateTime have a constructor taking a time_t parameter.
- Both MFC classes CTime and COleDateTime have a member function Format
Then you can do something like in the following two examplesTimmy0614
Well I don't know what all this MFC stuff is about, but then I come from a C background. There is a whole family of functions for use with time_t values. You use ctime() for simple formatting. Or, for more control, use gmtime() or localtime() to split the time_t into fields inside a struct tm. Then you can use strftime() to format the time exactly how you like; you provide a format specification much like you do for printf().
Why store the time_t as a REG_SZ inside the registry Easier just to store it as a REG_BINARY value. I would have said store it as a REG_DWORD, but now time_t tends to be 64 bits rather than 32.
Andrew Buyan
See http://msdn2.microsoft.com/en-us/library/w4ddyt9h.aspx, especially the section marked "Convert time from type time_t, __time32_t or __time64_t to character string. [...]"
You cannot simply cast numbers to strings. To convert them, you will have to either use one of the functions of the atoi-family (http://msdn.microsoft.com/library/default.asp url=/library/en-us/vclib/html/_crt_atof.2c_.atoi.2c_._atoi64.2c_.atol.asp), or by piping them through STL streams. The latter can be simplified through a helper function such as
Which can be used in a fashion such as
helsingfors
medel
Well that's somewhat harsh :)
The STL part addressed his second question, regarding lexical casts of numbers. For the sake of being portable, and not locked to MFC for even the most trivial of tasks, I suggested the stringstream approach.