Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a time string into a CTime class

Status
Not open for further replies.

sparafucile17

Programmer
Apr 5, 2002
40
0
0
US
Ok here's the problem.....

I have a string(in char* format) representation of time that looks like this: &quot;%Y%m%d%H:%M:%S&quot;. An example on the time right now would look like: &quot;2003021611:50:32&quot;. But the problem is that I need it in a CTime format. I'll be using the operators '<' and '>' later on to grab the struct with the latest date and time.

So how can I do this? Besides the obvious, which is to parse the full string into little strings that contain year, month, etc and the atoi() them to ints, finally calling the CTime constructor with the all the ints. (I am currently doing this now, but I'm looking for the optimum way to do it.)

I know that CTime will take a time_time object, but I'm not familiar with it. Perhaps the string can be assigned to a time_t object and then a CTime?

Thanks in advance,
Jeff
 
In what context will you be using this time record? If all you want to do is compare times as to whether they are less than, equal to or greater than an other time why don't you simply store a time_t in the first place rather than a string. The time_t is the number of elapsed seconds since the beginning of 1970 therefore you can compare two time_t's just like you can compare two integers. You can also work out the difference in seconds (and therefore everything else).
If you must use the string notation then you'll have to manually go through your string and examine the appropriate characters. If you want to use MFC then I suggest creating a CString object of your char* and using the CString::Left() to get the leftmost 2 characters and then CString::Delete to delete the leftmost 2 characters. Then repeat this process as necessary. You can use the old atoi() or atol() functions to convert these smaller strings into a numeric value.
If you don't want to use MFC you could use the std::string data type but this may be a little more work.

[tt]
char* dateTime = &quot;2003021611:50:32&quot;;
CString str = dateTime;

int year = atoi(str.Left(4));
str.Delete(0,4);

int month = atoi(str.Left(2));
str.Delete(0,2);

// and so on....[/tt]


tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Well, as to your question of why do I choose to use a string? I don't choose actually, I'm forced to do this becuase it is a field from a CodeBase Database that is of type DateTime. When I retrieve the data for that field, I can either get a String format, or a Date4 format.

The Date4 format is some crazy Codebase class, that doesn't seem to work at all! So I'm left with only the string format which has proven to be reliable. I actually like your idea of using CStrings (I was using char arrays and copying each char one at a time!) Unless some else has a better idea, I'll use the CString Left() and Delete() implementation.

By the way, in case you were wondering why Codebase? Well, I'm doing this job for a company via contract work. I'm supposed to add some features to an existing app, which already has Codebase integrated. So, I'm forced to do whatever Codebase tells me to do. :-(

Thanks for the help so far!
Jeff
 
Why not convert the CTime value into a string with the CTime FORMAT function and then compare the strings for less than, greater than, or equal?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top