ctime \n rationale

R

Richard Tobin

Pietro Cerutti said:
I'm just curious about the rationale of having ctime() put a new line
character at the end of its return value, as:

"Thu Nov 24 18:22:48 1986\n\0"

Personally, I find it stupid and resource wasting.

Does anyone have a good explanation about why it's done this way?

Probably because its original purpose was to implement the date
program in unix.

-- Richard
 
P

Pietro Cerutti

Hi to all,
I'm just curious about the rationale of having ctime() put a new line
character at the end of its return value, as:

"Thu Nov 24 18:22:48 1986\n\0"

Personally, I find it stupid and resource wasting.

Does anyone have a good explanation about why it's done this way?

Tnx, regards.
 
P

Pietro Cerutti

Richard said:
Probably because its original purpose was to implement the date
program in unix.

Even in that case, I would had written:
printf("%s\n", ctime(time(NULL)));
instead. Don't you agree?
 
M

Mark McIntyre

Richard Tobin wrote:

Even in that case, I would had written:
printf("%s\n", ctime(time(NULL)));
instead. Don't you agree?

But then the function would not be /implementing/ the unix 'date'
programme.

But surely this is no big deal - if you don't want it, rip it off.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
R

Richard Tobin

Even in that case, I would had written:
printf("%s\n", ctime(time(NULL)));
instead. Don't you agree?

Oh yes. I'm just guessing an explanation, not trying to justify it.

-- Richard
 
C

Clever Monkey

Pietro said:
Hi to all,
I'm just curious about the rationale of having ctime() put a new line
character at the end of its return value, as:

"Thu Nov 24 18:22:48 1986\n\0"

Personally, I find it stupid and resource wasting.

Does anyone have a good explanation about why it's done this way?
Well, it is defined in terms of an English-language representation of
the time. That is, it is equivalent (for the purposes of this
discussion) to "asctime(localtime(tod))".

As suggested else-thread, this is probably just legacy from Unix system
usage.
 
R

Richard Tobin

As suggested else-thread, this is probably just legacy from Unix system
usage.

In Third Edition Unix, it didn't have a linefeed. Nor did it include
the day of the week or the year. The BUGS section of the manual page
says:

The routine must be reassembled for leap year.
Dec 31 is followed by Dec 32 and so on.

I'm not sure how this was supposed to work, since the manual page is
dated 1973, and time() returned seconds since Jan 1 1972. time()'s
BUGS section says:

The time is stored in 32 bits. This guarantees a
crisis every 2.26 years.

Maybe they really reset the clock every new year.

By Sixth Edition, ctime() was as we know it now.

-- Richard
 
C

Clever Monkey

Richard said:
In Third Edition Unix, it didn't have a linefeed. Nor did it include
the day of the week or the year. The BUGS section of the manual page
says:

The routine must be reassembled for leap year.
Dec 31 is followed by Dec 32 and so on.

I'm not sure how this was supposed to work, since the manual page is
dated 1973, and time() returned seconds since Jan 1 1972. time()'s
BUGS section says:

The time is stored in 32 bits. This guarantees a
crisis every 2.26 years.
I wonder what that means? Seems to me that we have 4,294,967,295
seconds since Jan 1, 1970 to play with (according to POSIX, anyway) and
the C runtime just accepts what it gets from the hosted environment in
terms of seconds since epoch.

Of course, UTC did not exist in its present form until 1972, so values
below 63072000 are not precisely defined.

Still have no idea why the line-feed is added on text representations of
time constructed by ctime() and friends, but my guess is that it was a
convenience for the most common (in English) representation of the
current time.

That is, one should be able to construct any locale-specific string with
or without any trailing whitespace you like using strftime(), while
asctime() and ctime() were kept as reasonable defaults for typical usage.

Just guessing, though.
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top