PDA

View Full Version : CML: Time to String


bph
09-11-2006, 10:47 PM
If I have a Card8 variable, set to some number of ticks like this:
m_LastChangedByEnviracom := m_TimeInfo.GetCurStamp() ;
and I want to see it as a string like "12:47:03" or even "124703", I don't understand what this means:

Time objects can be formatted to text in a number of flexible ways, because there are often needs to display only the time info or only the date info or to use certain well known standard formats. So each time objects has a format string, which uses the standard token replacement scheme of the MEng.String class. You can set a default format string on the time object, which contains replacement tokens, and when you format it, each token is replaced by the indicated field. The tokens are:

I am confused by "when you format it" and "standard token replacement scheme of the String class". What does "when you format it" actually mean? There is nothing about a format method on either the string page or the time page, that I can find.

Some examples would really help the documentation of these classes.

bph
09-11-2006, 10:54 PM
Ok, I found this:

http://www.charmedquark.com/vb_forum/showthread.php?t=241)
If you want to format something out to a string, then you should use the StringOutStream class, which is an output stream over a string. It extends the base output stream class by adding a GetText() method to get to the formatted string inside it that you've formatted out to. Be sure to flush the stream first to insure it's all pushed out to the string buffer.

Or, you could use the String class' AppendFmt() method to append a formatted version of the time object directly into the string and avoid the stream.


Which is kind of like finding the chinese instructions because you were having trouble with the spanish.

I have 6 minutes before bedtime to figure out how to call m_timeInfo.givemeaformattedversionofyourself

Dean Roddey
09-11-2006, 11:19 PM
At the top of the Time class it explains what the tokens are. You set up the format you want by building up a string like:

"%(m) %(D), %(Y)"

and you set that as the format on the time string. When you format the time object to a stream, it will use the format you set on it. The above would do something like "Jun 15, 2003".

You can format any object that derives from Formattable by calling the .AppendFmt() on a string object and passing the object to format. There are other specialized AppendXXX() methods for the fundamental types, but the AppendFmt() handles any formattable class.

bph
09-11-2006, 11:24 PM
At the top of the Time class it explains what the tokens are. You set up the format you want by building up a string like:

"%(m) %(D), %(Y)"

and you set that as the format on the time string. When you format the time object to a stream, it will use the format you set on it. The above would do something like "Jun 15, 2003".

You can format any object that derives from Formattable by calling the .AppendFmt() on a string object and passing the object to format. There are other specialized AppendXXX() methods for the fundamental types, but the AppendFmt() handles any formattable class.
So, what did I do wrong:
m_TimeInfo.SetDefFmt ( Time.kFmtHHMMSS );
WorkStr.AppendFmt (m_TimeInfo.GetCurStamp());

Dean Roddey
09-11-2006, 11:52 PM
Don't get the stamp, just format the time object it self.