ctime \n rationale

Discussion in 'C Programming' started by Richard Tobin, Jun 24, 2007.

  1. In article <5a51e$467e7f7f$50dabbcd$>,
    Pietro Cerutti <> wrote:

    >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
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Jun 24, 2007
    #1
    1. Advertising

  2. 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.

    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
    Pietro Cerutti, Jun 24, 2007
    #2
    1. Advertising

  3. Richard Tobin wrote:
    > In article <5a51e$467e7f7f$50dabbcd$>,
    > Pietro Cerutti <> wrote:
    >
    >> 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.


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

    >
    > -- Richard



    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
    Pietro Cerutti, Jun 24, 2007
    #3
  4. On Sun, 24 Jun 2007 16:58:21 +0200, in comp.lang.c , Pietro Cerutti
    <> wrote:

    >Richard Tobin wrote:


    >> 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?


    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
    Mark McIntyre, Jun 24, 2007
    #4
  5. In article <8c64d$467e8690$50dabbcd$>,
    Pietro Cerutti <> wrote:
    >>> I'm just curious about the rationale of having ctime() put a new line
    >>> character at the end of its return value, as:


    >> 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?


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

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Jun 24, 2007
    #5
  6. Pietro Cerutti wrote:
    > 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.
    --
    clvrmnky <mailto:>

    Direct replies will be blacklisted. Replace "spamtrap" with my name to
    contact me directly.
    Clever Monkey, Jun 25, 2007
    #6
  7. In article <5TNfi.15111$!nnrp1.uunet.ca>,
    Clever Monkey <> wrote:
    >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
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Jun 25, 2007
    #7
  8. Richard Tobin wrote:
    > In article <5TNfi.15111$!nnrp1.uunet.ca>,
    > Clever Monkey <> wrote:
    >> 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.
    >

    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.
    --
    clvrmnky <mailto:>

    Direct replies will be blacklisted. Replace "spamtrap" with my name to
    contact me directly.
    Clever Monkey, Jun 25, 2007
    #8
  9. In article <LJPfi.15117$!nnrp1.uunet.ca>,
    Clever Monkey <> wrote:

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


    >I wonder what that means?


    Presumably time was stored in 1/HZ s (where HZ=60) internally.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Jun 25, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ernst Murnleitner

    replacement for <ctime>?

    Ernst Murnleitner, Jul 16, 2003, in forum: C++
    Replies:
    8
    Views:
    529
    Limech
    Jul 20, 2003
  2. Tivo Escobar

    Question about ctime

    Tivo Escobar, Jul 23, 2003, in forum: C++
    Replies:
    1
    Views:
    389
    Victor Bazarov
    Jul 23, 2003
  3. Oplec
    Replies:
    2
    Views:
    493
    Oplec
    Oct 26, 2003
  4. Kevin

    ctime Help

    Kevin, May 16, 2004, in forum: C++
    Replies:
    3
    Views:
    779
    osmium
    May 16, 2004
  5. wukexin
    Replies:
    1
    Views:
    467
    Karl Heinz Buchegger
    May 25, 2005
Loading...

Share This Page