Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ equivalent for strftime()?

Reply
Thread Tools

C++ equivalent for strftime()?

 
 
Marcus Kwok
Guest
Posts: n/a
 
      09-26-2005
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);
}

--
Marcus Kwok
 
Reply With Quote
 
 
 
 
Moritz Beller
Guest
Posts: n/a
 
      09-26-2005
On Mon, 26 Sep 2005 19:08:21 +0000 (UTC)
http://www.velocityreviews.com/forums/(E-Mail Removed) (Marcus Kwok) wrote:

> 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
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpg http://gpg.notlong.com
 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      09-26-2005
Moritz Beller <(E-Mail Removed)> wrote:
> 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.

--
Marcus Kwok
 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      09-26-2005

"Marcus Kwok" <(E-Mail Removed)> wrote in message
news:dh9gv5$155$(E-Mail Removed)...
>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


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      09-26-2005
Jeff Flinn <(E-Mail Removed)> wrote:
> See the boost date_time library at
> http://www.boost.org/doc/html/date_time.html


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.

--
Marcus Kwok
 
Reply With Quote
 
Larry I Smith
Guest
Posts: n/a
 
      09-27-2005
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
 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      09-27-2005
Larry I Smith <(E-Mail Removed)> wrote:
> 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.

--
Marcus Kwok
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning:Xst:382 - Register A is equivalent to B mag VHDL 1 05-19-2005 05:10 PM
instancename of current entity/architecture -- equivalent to C++ this??? Eric Peers VHDL 2 11-18-2004 05:23 PM
VHDL equivalent of verilog trireg Sanjeev VHDL 4 07-23-2004 09:55 AM
equivalent types in different packages Lolo VHDL 3 09-22-2003 03:23 PM
Re: Image Scanning - TWAIN equivalent Brendan Duffy ASP .Net 0 07-24-2003 08:29 AM



Advertisments