Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Various ways to get month name from inbuilt C library

Reply
Thread Tools

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

 
 
santosh
Guest
Posts: n/a
 
      02-28-2010
Debanjan <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      02-28-2010
santosh <(E-Mail Removed)> writes:

> Debanjan <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      03-01-2010
santosh <(E-Mail Removed)> wrote:

> santosh <(E-Mail Removed)> writes:
>
> > Debanjan <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-01-2010
http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Bos) writes:
> santosh <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      03-10-2010
Keith Thompson <(E-Mail Removed)> wrote:

> (E-Mail Removed) (Richard Bos) writes:
> > santosh <(E-Mail Removed)> 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
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      03-11-2010
On 10 Mar, 14:14, (E-Mail Removed) (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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Convert month name to month number faster Steven D'Aprano Python 0 01-06-2010 12:48 PM
RE: Convert month name to month number faster VYAS ASHISH M-NTB837 Python 0 01-06-2010 11:14 AM
Figure out month number from month abbrievation Bill Python 10 04-13-2006 12:36 PM
MonthCalendar1 or DateTimePicker1 How to jump for one month or haw to get no of month? Michal Maciejczak C++ 1 04-23-2005 11:18 AM
various ways of manipulating database retrieved data... Simnet ASP .Net 0 12-03-2004 02:46 PM



Advertisments