C++ equivalent for strftime()?

M

Marcus Kwok

I have a simple LogFile class that I use to log messages, and each
message has the current date and time prepended to the message.
Currently, I am using strftime() to format the date, and placing the
result into a character array. Of course, there is now the issue of
whether the length of the string is longer than the character array.

Is there an equivalent way to do this so that it is more C++-like? For
example, something that will place the result into a std::string or
something, so I don't have to worry about if timeStringLength will be
large enough.


void LogFile::logEntry(const char* message)
{
// Output date and time in ISO 8601:2004 format, i.e., YYYY-MM-DD HH:MM:SS
const char* timeStringFormat = "%Y-%m-%d %H:%M:%S";
const int timeStringLength = 20;
char timeString[timeStringLength];

// get current date and time
time_t t = time(0);
tm *curTime = localtime(&t);

strftime(timeString, timeStringLength, timeStringFormat, curTime);
logFile << timeString << '\t' << message << '\n';
flush(logFile);
}
 
M

Moritz Beller

On Mon, 26 Sep 2005 19:08:21 +0000 (UTC)
Is there an equivalent way to do this so that it is more C++-like?

Is there any good reason that prevents you from creating the char array
dynamically with new or simply casting strftimes's first argument to
string?

best regards / Gruß
Moritz Beller
 
M

Marcus Kwok

Moritz Beller said:
Is there any good reason that prevents you from creating the char array
dynamically with new or simply casting strftimes's first argument to
string?

Well, the problem with dynamically creating the char array is that the
appropriate size still cannot be determined before the call. I mean, 20
characters is enough to hold the format I am using, but to me it still
looks "ugly" and I was trying to find out if there is a way to do it
"more C++" and "less C".

I don't think I can cast strftime()'s first argument to string because
strftime() expects a pointer to a character array as its first argument.
 
J

Jeff Flinn

Marcus Kwok said:
I have a simple LogFile class that I use to log messages, and each
message has the current date and time prepended to the message.
Currently, I am using strftime() to format the date, and placing the
result into a character array. Of course, there is now the issue of
whether the length of the string is longer than the character array.

Is there an equivalent way to do this so that it is more C++-like? For
example, something that will place the result into a std::string or
something, so I don't have to worry about if timeStringLength will be
large enough.

See the boost date_time library at
http://www.boost.org/doc/html/date_time.html

Jeff
 
M

Marcus Kwok

Jeff Flinn said:

Looks pretty nice. If/when it gets adopted into the new C++ standard I
will use it, but I don't think I will be able to convince my colleague
(whom I am working with on this project) to install a third-party
library for this, considering that it's a minor implementation detail,
and the other problems he's been having with his code. I was hoping for
something that's already in the standard C++ implementation, but if not
then strftime() will have to do in the meantime.

The Boost libraries look pretty awesome, and seem to fill a lot of holes
in the standard libraries that have emerged now that C++ has been used
for so long and people realize what they're missing.

Thanks.
 
L

Larry I Smith

Marcus Kwok wrote:
[snip]
I was hoping for
something that's already in the standard C++ implementation, but if not
then strftime() will have to do in the meantime.
[snip]

The ANSI C lib IS part of standard C++. So, strftime() is
part of standard C++ - because it's part of the ANSI C lib.

Larry
 
M

Marcus Kwok

Larry I Smith said:
The ANSI C lib IS part of standard C++. So, strftime() is
part of standard C++ - because it's part of the ANSI C lib.

Right, but I was talking about a standard library function that would
duplicate strftime()'s functionality but that would utilize a
std::string instead of a character buffer (array). Sorry for the
misunderstanding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top