Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Various ways to get month name from inbuilt C library

2 views
Skip to first unread message

Debanjan

unread,
Feb 28, 2010, 2:55:29 AM2/28/10
to
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 ?

santosh

unread,
Feb 28, 2010, 3:51:30 AM2/28/10
to
Debanjan <debanj...@gmail.com> writes:

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

unread,
Feb 28, 2010, 4:41:29 AM2/28/10
to
santosh <santo...@gmail.com> writes:

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


Richard Bos

unread,
Mar 1, 2010, 4:14:28 PM3/1/10
to
santosh <santo...@gmail.com> wrote:

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

Keith Thompson

unread,
Mar 1, 2010, 4:31:19 PM3/1/10
to
ral...@xs4all.nl (Richard Bos) writes:
> santosh <santo...@gmail.com> 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) ks...@mib.org <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"

Richard Bos

unread,
Mar 10, 2010, 9:14:14 AM3/10/10
to
Keith Thompson <ks...@mib.org> wrote:

> ral...@xs4all.nl (Richard Bos) writes:
> > santosh <santo...@gmail.com> 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

Nick Keighley

unread,
Mar 11, 2010, 6:34:14 AM3/11/10
to
On 10 Mar, 14:14, ralt...@xs4all.nl (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

0 new messages