CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2006
    Posts
    78

    Convert string to time_t

    I need to read in a date/time stamp from a text file in the format of:

    02/19/06 12:14:03

    I need to put this in time_t format to write it to a binary file that requires it be in the time_t format.

    How can I accomplish this?

    Thanks!

  2. #2
    Join Date
    Nov 1999
    Location
    Los Angeles, USA
    Posts
    253

    Re: Convert string to time_t

    You could use the COleDateTime and it's ParseDateTime function to parse your text. If parsed successfully use the GetMonth/Day/Year/etc functions to put together your time_t variable.

    Jay

  3. #3
    Join Date
    Jun 2005
    Location
    Chennai , India
    Posts
    1,375

    Thumbs up Re: Convert string to time_t

    It takes seconds for rating…that actually compensates the minutes taken for giving answers
    The biggest guru-mantra is: Never share your secrets with anybody. It will destroy you.
    Regards, Be generous->Rate people
    Jayender!!

  4. #4
    Join Date
    Feb 2006
    Posts
    78

    Re: Convert string to time_t

    Thanks to everyone. Here is my dilemma. I have a string such as:

    My Time Stamp: 02/16/06 12:31:42

    I need to just parse out the "02/16/06 12:31:42" part.

    Once I have that, then I have a function to parse out each field and put it in the format I need.

    My current question is: How do I grab just the ""02/16/06 12:31:42" part as a CString for me to pass to the function I have. The function is:

    long CMine::ConvertDateTime(CString buff)
    {
    int yy, mm, dd, hour, min, sec;
    struct tm when;
    long tme;

    sscanf(buff, "%d/%d/%d %d:%d:%d", &mm, &dd, &yy, &hour, &min, &sec);

    if( yy < 70 ) yy+= 100; // year 2000 compliant
    time(&tme);
    when = *localtime(&tme);
    when.tm_year = yy; when.tm_mon = mm-1; when.tm_mday = dd;
    when.tm_hour = hour; when.tm_min = min; when.tm_sec = sec;
    return( mktime(&when) );
    }

  5. #5
    Join Date
    May 2005
    Posts
    4,954

    Re: Convert string to time_t

    Quote Originally Posted by xikspan
    Thanks to everyone. Here is my dilemma. I have a string such as:

    My Time Stamp: 02/16/06 12:31:42

    I need to just parse out the "02/16/06 12:31:42" part.

    Once I have that, then I have a function to parse out each field and put it in the format I need.

    My current question is: How do I grab just the ""02/16/06 12:31:42" part as a CString for me to pass to the function I have. The function is:
    If this is the fixed format you are having:
    Code:
    My Time Stamp:  02/16/06 12:31:42
    That’s mean before every date and time you will have the My Time Stamp: string then you can extract it simply like this:
    Code:
      CString str("My Time Stamp: 02/16/06 12:31:42");
      int len = strlen("My Time Stamp: ");
      CString strRes = str.Right(str.GetLength() -len);
      // strRes == "02/16/06 12:31:42"
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Convert string to time_t

    Quote Originally Posted by golanshahar
    If this is the fixed format you are having:
    Code:
    My Time Stamp:  02/16/06 12:31:42
    That’s mean before every date and time you will have the My Time Stamp: string then you can extract it simply like this:
    Code:
      CString str("My Time Stamp: 02/16/06 12:31:42");
      int len = strlen("My Time Stamp: ");
      CString strRes = str.Right(str.GetLength() -len);
      // strRes == "02/16/06 12:31:42"
    Cheers
    str.Replace ("My Time Stamp: ", "") would do it too.

  7. #7
    Join Date
    Feb 2006
    Posts
    78

    Re: Convert string to time_t

    You guys rock! That worked perfectly for what I needed! Thanks!

  8. #8
    Join Date
    May 2005
    Posts
    4,954

    Re: Convert string to time_t

    Quote Originally Posted by GCDEF
    str.Replace ("My Time Stamp: ", "") would do it too.
    Yup, just that you are referring to the content of the "header" while I am referring to the size of it...

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

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