Thread: Convert from CTimeSpan to Char?

  1. #1
    UW_COOP
    Guest

    Convert from CTimeSpan to Char?

    Hi,

    I want to print out the number of seconds returned from a CTimeSpan object.

    ie. CTimeSpan::GetTotalSeconds;

    I want to print this out in a message box.
    How would I cast it??

    Thanks.

    Ryan

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    To my limited knowledge, a message box only takes C strings. You can use a stringstream to convert a non-string value into a std::string object like so:
    Code:
    template <class T>
    string make_string(T& data)
    {
        stringstream ss;
    
        ss<< data;
    
        return ss.str();
    }
    Then simply call MessageBox with the c_str() member function of the string class:
    Code:
    MessageBox(NULL, TEXT(make_string(value).c_str()), TEXT("FOO"), MB_OK);
    My best code is written with the delete key.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Lookup the CTimeSpan::Format member.

    This will return a CString object that can be used in any MessageBox wrapper for MFC

    ala

    Code:
    	CTime TimeThen,TimeNow;
    	TimeThen = CTime::GetCurrentTime();
    	::Sleep(10000);
    	TimeNow = CTime::GetCurrentTime();
    	CTimeSpan TimeSpan = TimeNow - TimeThen;
    	CString TimeStr = TimeSpan.Format(_T("Number of Seconds: %S"));
    	MessageBox(TimeStr);
    Also, beware

    Code:
    TEXT(make_string(value).c_str())
    The TEXT macro only works for string literals

    Code:
    __TEXT(quote) L##quote
    If you want your code to be UNICODE safe, use a CString for MFC, or some preprocessor work to define between std::string or std::wstring

  4. #4
    UW_COOP
    Guest

    Thanks for the help.

    Thanks for the help. It was able to compile without errors but does not give the right returned value.

    I am retrieving values of 'datetime' type from SQL and have the following code:


    start = CTime(CString(RsITEM(spRS, "start_date")));
    spRS->MoveNext();
    end = CTime(CString(RsITEM(spRS, "start_date")));
    timeDiff = end - start;
    TimeStr = timeDiff.Format(_T("Number of Seconds: %S"));
    MessageBox(NULL,TimeStr, "Time Elapsed",NULL);


    *RsITEM returns a specific cell value from the current row of the recordset 'spRS'

    RYAN

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    At a guess I would say that RsITEM doesnt return a string in the same format as the parameters for the construtor of CTime....

    Look on MSDN and see if the formats are different

  6. #6
    UW_COOP
    Guest

    RsITEM

    RsITEM returns values of type "_variant_t"

    I need to be able to cast as different types, but can't seem to figure it out?

    Any help would be graciously appreciated.

    Ryan

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Hmm...in that case I'm not sure how it's even compiling! _variant_t has an operator _bstr_t( ) which is converting it to a CString......but CTime doesnt seem to take a string as a construtor....

    I'll have a look at this when I'm at home...

  8. #8
    UW_COOP
    Guest
    Hi,

    Sorry, the code i pasted above does not compile. I was jsut trying different things out. It's just to show i guess, what i'm trying to accomplish.

    Thanks.

    Ryan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM