CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    convert time_t to CString

    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
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: convert time_t to CString

    Do you mean Something Like this.
    Code:
    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);
    Thanx

  3. #3
    Join Date
    Nov 2005
    Location
    India, Kerala, Trivandrum
    Posts
    37

    Re: convert time_t to CString

    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 );

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: convert time_t to CString

    Quote Originally Posted by thomas_nibu
    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'

    so .. in your case it would be 'the current time is 16899024' or something like that.. . It think the TS wants a displayable time.

  5. #5
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    Re: convert time_t to CString

    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
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

  6. #6
    Join Date
    Nov 2005
    Location
    India, Kerala, Trivandrum
    Posts
    37

    Re: convert time_t to CString

    Skizmo,

    He said that he want's the number. Not a formatted one.

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: convert time_t to CString

    Quote Originally Posted by thomas_nibu
    Skizmo,

    He said that he want's the number. Not a formatted one.
    yeah . .My mistake. .. I noticed after posting

  8. #8
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    Re: convert time_t to CString

    one more question:
    I am getting 5 bytes data. Isn't it supposed to be 4 bytes? i.e. now I was getting 1169467720
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

  9. #9
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: convert time_t to CString

    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

  10. #10
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    Re: convert time_t to CString

    Quote Originally Posted by humptydumpty
    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.
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: convert time_t to CString

    Quote Originally Posted by Radu
    one more question:
    I am getting 5 bytes data. Isn't it supposed to be 4 bytes? i.e. now I was getting 1169467720
    Format it as hex value (Format( _T( "%x", tYour_time_t )) and you'll get 4 bytes.

  12. #12
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    Re: convert time_t to CString

    oh man, ..
    sure!
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

  13. #13
    Join Date
    Nov 2005
    Location
    India, Kerala, Trivandrum
    Posts
    37

    Re: convert time_t to CString

    Quote Originally Posted by VictorN
    Format it as hex value (Format( _T( "%x", tYour_time_t )) and you'll get 4 bytes.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: convert time_t to CString

    Quote Originally Posted by VictorN
    Format it as hex value (Format( _T( "%x", tYour_time_t )) and you'll get 4 bytes.
    Correction: in order to always get four bytex for int/long value the format specification must be a little changed:
    Code:
    CString csTimeConv;
    csTimeConv.Format( _T( "%08x", tYour_time_t ); 
    // or 
    csTimeConv.Format( _T( "% 8x", tYour_time_t );
    Last edited by VictorN; January 24th, 2007 at 04:39 AM.

  15. #15
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    Re: convert time_t to CString

    Thanks Victor!
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured