bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] Re: xctime


From: Simon Josefsson
Subject: [Bug-gnulib] Re: xctime
Date: Sun, 19 Sep 2004 12:46:20 +0200
User-agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50 (gnu/linux)

Paul Eggert <address@hidden> writes:

> Simon Josefsson <address@hidden> writes:
>
>> Do ctime_r ever return a string longer than 26 characters?
>
> Yes, in some implementations.  In practice I suspect that the behavior
> is undefined if the output would be longer than 26 bytes.
>
>> Taking into account locales etc?
>
> No, it's locale-independent.  But years can have more than 4 digits if
> time_t is sufficiently wide (e.g., 64-bit time_t hosts, which are
> becoming more common).
>
> I don't think it's worth putting ctime-related stuff into gnulib.
> ctime is an obsolescent interface that is kept around because it was
> the only easy way to generate strings back in 1977, so lots of old
> software uses it.  And in practice ctime is buggy on many platforms
> unless 0 <= time_t < 2**31.

I agree.

Wouldn't it be useful to collect your discussion (and the similar
discussion regarding inet_ntoa) in a gnulib manual?  It could discuss
various C/POSIX functions, and suggest how to use it in the best way.
Straw man attempt below.

> strftime is the way to go.

Perhaps a wrapper to get non-locale output from strftime would be
useful in gnulib?  Sometimes you need the C locale month/day names
(e.g., for RFC 2822 Date: headers).  A xstrftime seem useful as well.

@section ctime
@index ctime

The @code{ctime} function need not be reentrant, and consequently is
not required to be thread safe.  Implementations of @code{ctime}
typically write the time stamp into static buffer.  If two threads
call @code{ctime} at roughly the same time, you might end up with the
wrong date in one of the threads, or some undefined string.  There is
a re-entrant interface @code{ctime_r}, that take a pre-allocated
buffer and length of the buffer, and return @code{NULL} on errors.
The input buffer should be at least 26 bytes in size.  The output
string is locale-independent.  However, years can have more than 4
digits if @code{time_t} is sufficiently wide, so the length of the
required output buffer is not easy to determine.  Increasing the
buffer size when @code{ctime_r} return @code{NULL} is not necessarily
sufficient. The @code{NULL} return value could mean some other error
condition, which will not go away by increasing the buffer size.

A more flexible function is @code{strftime}.  However, note that it is
locale dependent.
@section inet_ntoa
@index inet_ntoa

The @code{inet_ntoa} function need not be reentrant, and consequently
is not required to be thread safe.  Implementations of
@code{inet_ntoa} typically write the time stamp into static buffer.
If two threads call @code{inet_ntoa} at roughly the same time, you
might end up with the wrong date in one of the threads, or some
undefined string.  Further, @code{inet_ntoa} is specific for
@acronym{IPv4} addresses.

A protocol independent function is @code{inet_ntop}.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]