Re: Various ways to get month name from inbuilt C library

Discussion in 'C Programming' started by santosh, Feb 28, 2010.

  1. santosh

    santosh Guest

    Debanjan <> writes:

    > What are the various ways to get the name of the month
    > corresponding to an integer value from inbuilt C/C++ library, I am
    > familiar to strftime. Any other means to do the same ?


    ctime() and the deprecated asctime() return string representations of
    their calender time and broken down time respectively. It's possible
    to extract the month name (or it's abbreviation) from the string. But
    strftime() is far better for this purpose.
     
    santosh, Feb 28, 2010
    #1
    1. Advertising

  2. santosh

    santosh Guest

    santosh <> writes:

    > Debanjan <> writes:
    >
    >> What are the various ways to get the name of the month
    >> corresponding to an integer value from inbuilt C/C++ library, I am
    >> familiar to strftime. Any other means to do the same ?

    >
    > ctime() and the deprecated asctime() return string representations
    > of their calender time and broken down time respectively. It's
    > possible to extract the month name (or it's abbreviation) from the
    > string. But strftime() is far better for this purpose.


    Correction: asctime() isn't deprecated. It's just fragile to use.
     
    santosh, Feb 28, 2010
    #2
    1. Advertising

  3. santosh

    Richard Bos Guest

    santosh <> wrote:

    > santosh <> writes:
    >
    > > Debanjan <> writes:
    > >
    > >> What are the various ways to get the name of the month
    > >> corresponding to an integer value from inbuilt C/C++ library, I am
    > >> familiar to strftime. Any other means to do the same ?

    > >
    > > ctime() and the deprecated asctime() return string representations
    > > of their calender time and broken down time respectively. It's
    > > possible to extract the month name (or it's abbreviation) from the
    > > string. But strftime() is far better for this purpose.

    >
    > Correction: asctime() isn't deprecated. It's just fragile to use.


    Also, since ctime(t) is exactly equivalent to asctime(localtime(t)), it
    is equally as inadvisable.
    Not that I'd say asctime() is particularly fragile. It breaks only when
    its parameter is later than the year 9999 or before -999, which in
    itself is good enough for daily use, although of course it is less than
    perfect for general purposes. But the format it returns is particularly
    useless, so strftime() is still the better choice.

    Richard
     
    Richard Bos, Mar 1, 2010
    #3
  4. (Richard Bos) writes:
    > santosh <> wrote:

    [...]
    >> Correction: asctime() isn't deprecated. It's just fragile to use.

    >
    > Also, since ctime(t) is exactly equivalent to asctime(localtime(t)), it
    > is equally as inadvisable.


    No, ctime() isn't quite as bad; asctime() by itself provides more
    opportunities for undefined behavior.

    ctime()'s argument is a pointer to a time_t. On many systems, time_t
    can't even represent times outside the range that asctime() supports.
    Even on systems with a wider time_t, ctime() can't call asctime()
    with, for example, tm_sec==9999.

    > Not that I'd say asctime() is particularly fragile. It breaks only when
    > its parameter is later than the year 9999 or before -999, which in
    > itself is good enough for daily use, although of course it is less than
    > perfect for general purposes. But the format it returns is particularly
    > useless, so strftime() is still the better choice.


    Agreed. Personally, my biggest problem with asctime() isn't its
    misbehavior on bad arguments (that's solvable by being careful with
    what you pass it), it's the silly outdated output format, especially
    the trailing '\n'. If I want to print a date, I almost always use
    YYYY-MM-DD; asctime() and ctime() don't support that, but strftime()
    does.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 1, 2010
    #4
  5. santosh

    Richard Bos Guest

    Keith Thompson <> wrote:

    > (Richard Bos) writes:
    > > santosh <> wrote:

    > [...]
    > >> Correction: asctime() isn't deprecated. It's just fragile to use.

    > >
    > > Also, since ctime(t) is exactly equivalent to asctime(localtime(t)), it
    > > is equally as inadvisable.

    >
    > No, ctime() isn't quite as bad; asctime() by itself provides more
    > opportunities for undefined behavior.
    >
    > ctime()'s argument is a pointer to a time_t. On many systems, time_t
    > can't even represent times outside the range that asctime() supports.
    > Even on systems with a wider time_t, ctime() can't call asctime()
    > with, for example, tm_sec==9999.


    You are, of course, correct. However, I would consider that to fall
    under "prone to intentional abuse" rather than "prone to accidental
    misuse". So, perhaps I should say, _to the honest programmer_ ctime() is
    nearly as inadvisable as asctime().

    > > Not that I'd say asctime() is particularly fragile. It breaks only when
    > > its parameter is later than the year 9999 or before -999, which in
    > > itself is good enough for daily use, although of course it is less than
    > > perfect for general purposes. But the format it returns is particularly
    > > useless, so strftime() is still the better choice.

    >
    > Agreed. Personally, my biggest problem with asctime() isn't its
    > misbehavior on bad arguments (that's solvable by being careful with
    > what you pass it), it's the silly outdated output format, especially
    > the trailing '\n'.


    I don't know that it is outdated; I think that it never was a reasonable
    format to use in the first place.

    Richard
     
    Richard Bos, Mar 10, 2010
    #5
  6. On 10 Mar, 14:14, (Richard Bos) wrote:

    > I don't know that [asctime()] is outdated; I think that it never was a reasonable
    > format to use in the first place.


    isn't it reasonably common format in the US? Though mixing the time up
    in the middle of the date does seem weird!

    Sun Sep 16 01:03:52 1973\n\0
     
    Nick Keighley, Mar 11, 2010
    #6
    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. Simnet
    Replies:
    0
    Views:
    391
    Simnet
    Dec 3, 2004
  2. Raja
    Replies:
    2
    Views:
    15,034
  3. Michal Maciejczak
    Replies:
    1
    Views:
    2,008
    Matthias Kaeppler
    Apr 23, 2005
  4. VYAS ASHISH M-NTB837

    RE: Convert month name to month number faster

    VYAS ASHISH M-NTB837, Jan 6, 2010, in forum: Python
    Replies:
    0
    Views:
    645
    VYAS ASHISH M-NTB837
    Jan 6, 2010
  5. Steven D'Aprano

    Re: Convert month name to month number faster

    Steven D'Aprano, Jan 6, 2010, in forum: Python
    Replies:
    0
    Views:
    505
    Steven D'Aprano
    Jan 6, 2010
Loading...

Share This Page