Dates and times in C++

V

Vaclav Haisman

Rune Allnor wrote, On 18.10.2009 12:54:
Hi all.

How does one read the system calendar and clock with C++?
I have found this link to the <time.h> header

http://www.cplusplus.com/reference/clibrary/ctime/

but I don't see any reason to revert to C code unless absolutely
necessary. Is there anything in Boost? TR1? C++0x?
If you need more sophisticated time and date handling then you can use
something like Boost.DateTime library
<http://www.boost.org/doc/libs/1_40_0/doc/html/date_time.html>.
 
R

Rune Allnor

  But why? It's not like there would be bad memory leak problems or such
with the C time functions. (For example, std::strftime is a quite handy
function to get the date in any format you want, and you can specify the
maximum size of the destination buffer, so if used properly there's no
danger of leaks.)

If used properly, there are no dangers with C at all.

It's a matter of personal preference. When I coded up the
timer based on the example above, I found myself in a place
I have been before, where I did not want to be, where I had
to consider all kinds of details I was not interested in.

When I write my application I want to focus on the application.
C forces me to focus on memory managment and all kinds of details
that distract me from th emain task.

Rune
 
R

Richard

[Please do not mail me a copy of your followup]

Juha Nieminen <[email protected]> spake the secret code
But why? It's not like there would be bad memory leak problems or such
with the C time functions. [...]

Its difficult to provide a test double to isolate my code from the
clock when I use C time functions.

If I use C++, I can push those functions behind an interface allowing
my test code to force the time or date to be a specific value for
testing, isolating my code from the clock.

Production code delegates from the interface to the C time function in
code that is so obviously correct that I don't feel the need to unit
test it.
 
O

osmium

Bart said:
The facilities in <time.h>/<ctime> are an integral part of C++. Using
them does in no way mean that you revert to C code.

I view that as a formal response to an informal statement. Yes, they are an
integral part of C++, but in a similar vein, I invested a lot of time in
learning how to avoid the use of printf() and it's brothers and cousins.

I have no wish to argue or discuss further, this is just an observation.
 
V

Victor Bazarov

osmium said:
I view that as a formal response to an informal statement. Yes, they are an
integral part of C++, but in a similar vein, I invested a lot of time in
learning how to avoid the use of printf() and it's brothers and cousins.

I have no wish to argue or discuss further, this is just an observation.

Not to attempt to discuss further; just another observation: 'printf'
has several well-known problems related to the use of '...' while
functions in <ctime> IIRC do not suffer from that. Hence the OP's
apprehension (of "reverting to C code") is IMHO unfounded, and your
likening of mechanisms from <ctime> to 'printf' does not work.

V
 
A

Anand Hariharan

Not to attempt to discuss further; just another observation: 'printf'
has several well-known problems related to the use of '...' while
functions in <ctime> IIRC do not suffer from that.  Hence the OP's
apprehension (of "reverting to C code") is IMHO unfounded, and your
likening of mechanisms from <ctime> to 'printf' does not work.

asctime is known to be error-prone, significantly tougher to get it
right when compared to printf.

- Anand
 
J

Jerry Coffin

[ ... ]
Not to attempt to discuss further; just another observation: 'printf'
has several well-known problems related to the use of '...' while
functions in <ctime> IIRC do not suffer from that. Hence the OP's
apprehension (of "reverting to C code") is IMHO unfounded, and your
likening of mechanisms from <ctime> to 'printf' does not work.

I think it's worth noting, however, that even though C's time
functions don't suffer from the _same_ problems as printf, they do
have some problems of their own. For example, a number use static
buffers that can be overwritten by successive calls to the same
function.

People can (and have) argued about whether iostreams are really a
_better_ alternative to C's standard I/O, but in the case of the time
functions, there's little real room for argument -- C++ simply
doesn't provide any alternatives.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top