DATETIME like YYYYMMDDHHMMSS

W

White Wolf

Mike Wahler wrote:
[SNIP]
You can do it the same way in C, C++, VC++, gcc, g++, in fact in any C
or C++ compiler in the world.

#include <time.h>
#include <stdio.h>

time_t t = time(0);
struct tm* lt = localtime(&t);
char time_str[15];
sprintf(time_str, "%04d%02d%02d%02d%02d%02d",
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
lt->tm_hour, lt->tm_min, lt->tm_sec);

Untested code


Which part of the above is C++?

All of it. Remember that the C++ standard library contains
the (C90) C standard library. Also, tHe headers with .h, while
deprecated, are indeed part of standard C++.

And it is still a pure C solution. IIRC there was an agreement here that
when showing code, we show C++ code and the best C++ there is to demonstrate
the issue. I see no problem with pure C code as long as it is proven that
that piece of code is the best C++ solution as well.
 
J

John Harrison

I see. I do, since I do not believe that this is the only way of failure
waiting there.

OK, educate me, where are the other oppotunites for buffer overflow?
I have asked if it was a C++ solution. Later (having got the reply I did) I
hinted on it, that it had a buffer overflow opportunity. Prior to that all
I have been trying to point out is that it is C and not C++. Reason being
that we have tried for ages here to show C++ solutions or flag the pure C
solutions as such. Or tell (for dumb people like me) why is that C code the
best C++ to use.

I did flag it as a C solution. Whether it is the best solution is obviously
a matter of opinion. I just coded what I would have done given the OP's
problem.

john
 
M

Mike Wahler

JKop said:
int Blah()
{
return 5;
}

int main()
{
Blah(8);
}


Case and point.

What is your point? What you wrote above is *not* valid C++,
but is valid C (99).
C is irrelevant here.

It is relevant to "White Wolf"s remarks, since that was what
he talked about.
Just like a banger is still a car.

I don't know what a 'banger' is. (I'll guess it's some
sort of 'slang', but 'slang' and informal terms imo should
be eschewed in a technical forum. It only leads to confusion.

-Mike
 
M

Mike Wahler

White Wolf said:
I see. Do you not want to see the point?

Sure. So tell me what your point is.

Yes. The code is valid C++. Whether it's of 'good quality'
is of course a matter of opinion.
Or is it that code with C++ design is less likely to have that
problem?

What is "C++ design?".

It is code which conforms to the internation standard which
defines the C++ language.
I would not call it C++ code.

The C++ standard does.
It is pure C.

It qualifies as C and C++.
There is nothing C++ to it.

Everything in it is valid C++ according to the C++ standard.
You can call it whatever you
want, that won't change the facts.

That's right. And the facts are that is is valid C++.

-Mike
 
M

Mike Wahler

Gary Labowitz said:
John Harrison said:
White Wolf said:
Rob Williscroft wrote:
White Wolf wrote in in
comp.lang.c++:

Which part of the above is C++?

#include <time.h>
#include <stdio.h>

int main()
{
time_t t = time(0);
struct tm* lt = localtime(&t);
char time_str[15];
sprintf(time_str, "%04d%02d%02d%02d%02d%02d",
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
lt->tm_hour, lt->tm_min, lt->tm_sec
);
printf( "%s\n", time_str );
}

All of it apparently.

Starting with two deprecated headers and followed by pure C code. Here
and there introducing the opportunity for buffer overruns. I see. It is
code which compiles with a C++ compiler. I would not call it C++ code.

I see no buffer overflow before the year 10000. I don't care about that.

I was mainly trying to demonstrate to the OP that the same solution was
possible in C and C++ (he seemed concerned). But if you compare the partial
solution given by Gary Labowitz with mine I would still prefer the C like
solution above. Right tool for the right job I would say.

I've taught C, and I teach C++. There is not a mention of sprintf and printf
in my C++ courses. Why should there be?

That depends upon your teaching goals. However, for completeness,
perhaps you should at least mention that those functions exist in
C++, even if you don't actually teach about or use them in class.
I don't teach backward compatibility.

Backward compatibility is important in the 'real world'.
I don't even like having to explain
why <ctime> is used instead of just <time>.

Since the languages have diverged, there is no sense to me in trying to keep
them together.

Many C++ projects interface with C code. One should be at least
aware of the facilities for dealing with this, even if it's not
part of a "C++ only" course.

$.02,
-Mike
 
M

Mike Wahler

White Wolf said:
Mike Wahler wrote:
[SNIP]
You can do it the same way in C, C++, VC++, gcc, g++, in fact in any C
or C++ compiler in the world.

#include <time.h>
#include <stdio.h>

time_t t = time(0);
struct tm* lt = localtime(&t);
char time_str[15];
sprintf(time_str, "%04d%02d%02d%02d%02d%02d",
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
lt->tm_hour, lt->tm_min, lt->tm_sec);

Untested code


Which part of the above is C++?

All of it. Remember that the C++ standard library contains
the (C90) C standard library. Also, tHe headers with .h, while
deprecated, are indeed part of standard C++.

And it is still a pure C solution.


It is a solution which qualifies as legal C *or* C++. Both
language standards support this assertion.
IIRC there was an agreement here that
when showing code, we show C++ code


Well, yes, this is a C++ group.
and the best C++ there is

"Best" will never be agreed upon, since it's a subjective issue.
to demonstrate
the issue.

John gave what he (apparently) felt was the 'best' solution.
What did he feel was 'best' about it? I'm not sure. Perhaps
as simple as that he thought it would be easier for OP to follow
that e.g. something involving containers etc.

I see no problem with pure C code

John's code qualifies according to both standards as C *or* C++.
If it were not valid C, it would not be topical here. So far
I haven't seen anyone challenge the topicality of his code (and
anyone who did would be wrong to do so(.
as long as it is proven that
that piece of code is the best C++ solution as well.

Again, "best" can never be proven. It's a matter of opinion,
and dependent upon context as well.


-Mike
 
M

Mike Wahler

White Wolf said:
Neither. I am Attila and I believe that in a C++ group pure C code should
be flagged as such.

I agree with that, and also add that it should be flagged as
off topic. However John's code does not qualify as such.

-Mike
 
R

Rob Williscroft

Mike Wahler wrote in @newsread3.news.pas.earthlink.net in comp.lang.c++:
I think you meant <time.h>. There is no <time> header in
either language.

That *is* the point, i.e. why is <ctime> not <time>.

Rob.
 
M

Mike Wahler

Rob Williscroft said:
Mike Wahler wrote in @newsread3.news.pas.earthlink.net in comp.lang.c++:


That *is* the point, i.e. why is <ctime> not <time>.

Because as far as I can tell, the 'c' in the name
is to denote that the header comes from the portion
of the C library inherited by the C++ library.

-Mike
 
D

Default User

White Wolf wrote:

I see. I do, since I do not believe that this is the only way of
failure waiting there.


If you see a problem, point it out. Don't just hint.





Brian Rodenborn
 
O

Old Wolf

White Wolf said:
Is it? Or is it that code with C++ design is less likely to have that
problem?

There is no opportunity for buffer overruns in the posted code
(assuming an implementation that conforms to the C++ standard,
of course)
It is pure C. There is nothing C++ to it. You can call it whatever you
want, that won't change the facts.

Everything in it is C++. Have you read the C++ standard?
I'm not sure what your point is, but you seem to think
that C++ is a set of additions to C, and this code
does not use any of those additions? The truth is that
C++ is a language in its own right (which has tried to
remain as compatible with C as possible, even to the extent
that some text files constitute valid programs in both
languages).
 
O

Old Wolf

Gary Labowitz said:
I have always hated the languages that maintain lots of old version
constructs for backward compatibility. They don't want to break all the old
code, and you can't force people to upgrade all that software in a short
timeframe, but when? Microsoft puts the screws on by naming a date and
dropping support after that. It's cruel I guess, but otherwise they will
sink in a quagmire of versions. It's one of the worst aspects of computing.

The difference is that C is well-designed and the Windows API
isn't (in comparison). It costs MS millions to maintain backwards
compatibility to those old versions whereas it doesn't cost
anything extra to write an ANSI C program. If they had the chance
to fix all the problems in C, then there would be very little
change (look at the spectactular success of VLAs, flexible struct
members, ...) C is still chosen ahead of a myriad of other languages
as the language for a new project. In fact Digital Mars D is the
closest I've seen to such a 'fix' and even that is not very dissimilar
to C (and although I don't know that language well, it wouldn't
surprise me if John Harrison's code was, or was almost exactly, valid D).
 
G

Gary Labowitz

Rob Williscroft said:
Mike Wahler wrote in @newsread3.news.pas.earthlink.net in comp.lang.c++:


That *is* the point, i.e. why is <ctime> not <time>.

Thank you, Rob. Discussing <ctime> or <cmath> ends up mentioning <time.h>
and <math.h>. Indeed, we use the Dev-C++ IDE and it invokes MinGW. The
<cmath> file (and it is a file in MinGW) simply #include's <math.h>. If I
start explaining this it leads to discussion of headers files vs. headers,
and what all that implies. If I start on this sort of thing with the
beginning students their eyes spin a little and they shut down. I hate to
see that happen.

All languages are so complex that you cannot learn them ALL_AT_ONCE. They
must have a "baby talk" stage, and add onto that. It's very difficult to
select what goes in the baby talk and what waits until later. Remember, some
learners aren't all that solid with concepts like copying files, much less
easily accepting of passing by reference. I like to think I make it easier
by lying to them at first and then, when they can program in baby talk,
explain the subtleties.

Of course, I may be wrong. We don't mention struct's or classes until second
semester course either. Sometimes I think it would be smarter teaching
classes right out of the box. It would make it easier to start them with
<string>, etc. and introduce c-style strings later as we do now in the
Appendix.

It's hard teaching a language when the only correct information about it are
in an encyclopedia and a dictionary. [And some text that a committee selects
because it looks beautiful but is actually inaccurate and misleading.] And
when the newbie comes to the ng and asks a question about some simple
element of the language he often gets (among other things) direction on how
he should use templated classes instead of a simple answer to his question.
I cringe at those helpful souls' answers.
 
W

White Wolf

Mike said:
I agree with that, and also add that it should be flagged as
off topic. However John's code does not qualify as such.

So Mr John's code does not compile on a C compiler? What a surprise...
 
W

White Wolf

Mike said:
Sure. So tell me what your point is.


I did about 5 times.
Yes. The code is valid C++. Whether it's of 'good quality'
is of course a matter of opinion.

Nowhere I said it is not valid C++. Obfuscated C is valid C as well.
What is "C++ design?".

You are joking.
It is code which conforms to the internation standard which
defines the C++ language.


The C++ standard does.

Nope. Please show me wher does the C++ standard contain theabove code
verbatim and calls it the best C++ design.
It qualifies as C and C++.

Yes. Which means it is pure C, as long as it menas the same in C++ and C.
Everything in it is valid C++ according to the C++ standard.

And yet, there is nothing C++ to it.
That's right. And the facts are that is is valid C++.

The fact is, that it is pure C code, which happens ot be valid C++ code.
And yet, that does not make it good C++ code.
 
W

White Wolf

Old said:
There is no opportunity for buffer overruns in the posted code
(assuming an implementation that conforms to the C++ standard,
of course)

I see. Using sprintf, on a limited size buffer and there is no opportunity
of buffer overflow. Which C++ standard? Todays? 5 years from now? 50
years from now?
Everything in it is C++. Have you read the C++ standard?

Have you? I did not read it all. But hey, see you in Redmond and you can
explain to me what did I miss.
I'm not sure what your point is, but you seem to think
that C++ is a set of additions to C, and this code
does not use any of those additions?

C++ is *not* a set of additions to C.
The truth is that
C++ is a language in its own right (which has tried to
remain as compatible with C as possible, even to the extent
that some text files constitute valid programs in both
languages).

Yes. And that is why a pure C code needs explanation, telling why is it
posted to a C++ group. It may be the best C++ implementation possible. It
may not. But many people work on getting people to do C++ programming in
C++, and it does not do any justice to their work if others post pure C code
as C++. I can undertstand if someone prefers pure C, because it is a
library routine he wants both for C and C++. There may be many other
answers. But the answer which says, yeah it is also C++, is the wrong
answer. It sounds a lot like: Why did I do it? Because I could.
 
M

Mike Wahler

White Wolf said:
So Mr John's code does not compile on a C compiler? What a surprise...

I 'misspoke' on this. I meant it doesn't qualify as "C but not C++".

-Mike
 
M

Mike Wahler

White Wolf said:
I did about 5 times.

So perhaps I'm dense. :)
Nowhere I said it is not valid C++. Obfuscated C is valid C as well.

You did say, and I quote you:

"> >> I would not call it C++ code."

You are joking.

Not at all. C++ is a language. Design is design, regardless
of language.

See, here's what you said.


Yes. According to 14882, John's code is valid C++.
Please show me wher does the C++ standard contain theabove code
verbatim

I never said it did.
and calls it the best C++ design.

I never said it did. All I said was that the code is valid
C++.
Yes. Which means it is pure C,

It is equally 'pure C++'.
as long as it menas the same in C++ and C.

The meaning has nothing to do with it.
And yet, there is nothing C++ to it.

Every part of it is C++ (and also happens to be C).
The fact is, that it is pure C code, which happens ot be valid C++ code.
And yet, that does not make it good C++ code.

I specifically stated that I was not talking about code quality,
only validity according to the C++ standard.

-Mike
 
P

Peter van Merkerk

White said:
Nope. Please show me wher does the C++ standard contain theabove code
verbatim and calls it the best C++ design.

So by your definition only code that can be found in verbatim in the C++
standard and what the C++ standard explicitly states to be the best "C++
design" can be considered C++ code? Interesting definition, but that
leaves very few code that can be considered C++. In fact I don't
remember reading anywhere in the C++ standard about code considered to
be the best "C++ design". Could you tell in which paragraphs of the C++
standard statements like this are made?
 
A

Attila Feher

Mike said:
So perhaps I'm dense. :)

I am not qualified to decide that.
You did say, and I quote you:

"> >> I would not call it C++ code."

And which word of the above don't you understand? Especially pointing out
where does it talked about vailidity.
Not at all. C++ is a language. Design is design, regardless
of language.

Oh boy. You used to be a reasonable person.
See, here's what you said.

Which isn't what you have implied I said.
Yes. According to 14882, John's code is valid C++.

Valid as C++, but it is C.
I never said it did.

You did:
The C++ standard does.



I never said it did. All I said was that the code is valid
C++.

And what I said is that I would not call it C++. So?
It is equally 'pure C++'.

And yet, it is pure C.
The meaning has nothing to do with it.

Do you have your "disagreement" bit stuck?
Every part of it is C++ (and also happens to be C).

What is C, is C. And it may happen to compile as C++ (according to the
standard). It may even do the same thing as in C, when compiled with a C++
compiler. Yet, it is C.
I specifically stated that I was not talking about code quality,
only validity according to the C++ standard.

And I have specifically stated that I was talking about one quality of that
code: that it is C code.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top