-
February 19th, 2006, 07:20 PM
#1
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!
-
February 19th, 2006, 07:49 PM
#2
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
-
February 20th, 2006, 12:54 AM
#3
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!!
-
February 20th, 2006, 04:09 PM
#4
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) );
}
-
February 20th, 2006, 04:31 PM
#5
Re: Convert string to time_t
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
-
February 20th, 2006, 04:42 PM
#6
Re: Convert string to time_t
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.
-
February 20th, 2006, 04:51 PM
#7
Re: Convert string to time_t
You guys rock! That worked perfectly for what I needed! Thanks!
-
February 20th, 2006, 05:29 PM
#8
Re: Convert string to time_t
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|