473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C++ equivalent for strftime()?

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
Sep 26 '05 #1
6 14890
On Mon, 26 Sep 2005 19:08:21 +0000 (UTC)
ri******@gehennom.net (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
Sep 26 '05 #2
Moritz Beller <mo*****************@t-online.de> 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
Sep 26 '05 #3

"Marcus Kwok" <ri******@gehennom.net> wrote in message
news:dh**********@news-int.gatech.edu...
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
Sep 26 '05 #4
Jeff Flinn <NO****@nowhere.com> 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
Sep 26 '05 #5
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
Sep 27 '05 #6
Larry I Smith <la***********@verizon.net> 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
Sep 27 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Hunter | last post by:
>>> from datetime import date >>> dt = date(1005,1,1) >>> print dt.strftime('%Y') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: year=1005 is before 1900; the...
5
by: Sheila King | last post by:
I have a web app that has been running just fine for several months under Python 2.2.2. We are preparing to upgrade the server to run Python 2.4.1. However, part of my web app is throwing an...
4
by: Andy Leszczynski | last post by:
Python 2.2/Unix >>time.strftime("%T") '22:12:15' >>time.strftime("%X") '22:12:17' Python 2.3/Windows >>time.strftime("%X")
2
by: Dan Jacobson | last post by:
Let's say you don't have any more documentation than man pages, and you want to figure out how to do the equivalent of $ date in C. I.e. just simply print out the current date and time. How to...
2
by: Mike Conmackie | last post by:
Greetings, Is there any way to force strftime() to ignore locale settings when formatting the resulting string? I have a requirement to create a specific date-time string format in UTC. ...
1
by: Dennis Benzinger | last post by:
Is there a library with a strftime replacement which supports Unicode format strings? Bye, Dennis
3
by: HSeganfredo | last post by:
Folks, my code below truncates the output as: chars 0,200709 chars 0,1839 If I change to: i = strftime(date, 9, "%Y%m%d", brokentime); j = strftime(hour, 7, "%H%M%S", brokentime); it...
2
by: Giovanni Bajo | last post by:
Hello, I am trying to find a good way to portably get the output of strftime() and put it onto a dialog (I'm using PyQt, but it doesn't really matter). The problem is that I need to decode the...
3
by: Andrii V. Mishkovskyi | last post by:
2008/5/7 Alexandr N Zamaraev <tonal@promsoft.ru>: Unicode and str objects are not the same. Why do you think that this is a bug? Anyway, you can always use 'encode' method of unicode objects: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.