none
time_t to displayable format?

    Question

  • 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?
    Saturday, October 14, 2006 1:30 PM

Answers

  • Once you are using MFC, you can avoid all that rope dancing by searching for time_t_to_human_radable_string_CRTs or cool_STL_stuff_that_can_do_everything.

    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 examples
    // returns date in "dd.mm.yyyy" format
    CString time_t_ToString(time_t tt)
    {
       CTime tim = tt;
       return tim.Format(_T("%d.%m.%Y"));
    }
    
    // returns date/time formatted according to user locale settings
    CString time_t_ToString2(time_t tt)
    {
       COleDateTime tim = tt;
       return tim.Format();
    }
    Sunday, October 15, 2006 12:58 PM
  • 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

    template<class T> string tostring(const T& source)
    {
        ostringstream oss;
        oss << source;
        return oss.str();
    };

    Which can be used in a fashion such as

    MessageBox(0, tostring(42).c_str(), 0, 0);

    Saturday, October 14, 2006 2:02 PM
    Moderator
  •  Ovidiu Cucu wrote:

    Once you are using MFC, you can avoid all that rope dancing by searching for time_t_to_human_radable_string_CRTs or cool_STL_stuff_that_can_do_everything.

    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.

    Sunday, October 15, 2006 1:40 PM
    Moderator

All replies

  • 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

    template<class T> string tostring(const T& source)
    {
        ostringstream oss;
        oss << source;
        return oss.str();
    };

    Which can be used in a fashion such as

    MessageBox(0, tostring(42).c_str(), 0, 0);

    Saturday, October 14, 2006 2:02 PM
    Moderator
  • Once you are using MFC, you can avoid all that rope dancing by searching for time_t_to_human_radable_string_CRTs or cool_STL_stuff_that_can_do_everything.

    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 examples
    // returns date in "dd.mm.yyyy" format
    CString time_t_ToString(time_t tt)
    {
       CTime tim = tt;
       return tim.Format(_T("%d.%m.%Y"));
    }
    
    // returns date/time formatted according to user locale settings
    CString time_t_ToString2(time_t tt)
    {
       COleDateTime tim = tt;
       return tim.Format();
    }
    Sunday, October 15, 2006 12:58 PM
  •  Ovidiu Cucu wrote:

    Once you are using MFC, you can avoid all that rope dancing by searching for time_t_to_human_radable_string_CRTs or cool_STL_stuff_that_can_do_everything.

    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.

    Sunday, October 15, 2006 1:40 PM
    Moderator
  • Thanks guys... appreciate it. I started programming using java, so i'm not used to casting with c++.
    Monday, October 16, 2006 1:59 PM
  • 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.

    Monday, October 16, 2006 3:34 PM