Modernize and fix doc’s “Date and Time” (BZ 31876)

POSIX.1-2024 (now official) specifies tm_gmtoff and tm_zone.
This is a good time to update the manual’s “Date and Time”
chapter so I went through it, fixed some outdated
stuff that had been in there for decades, and improved it to match
POSIX.1-2024 better and to clarify some implementation-defined
behavior.  Glibc already conforms to POSIX.1-2024 in these matters, so
this is merely a documentation change.

* manual/examples/strftim.c: Use snprintf instead of now-deprecated
  function asctime.  Check for localtime failure.  Simplify by using
  puts instead of fputs.  Prefer ‘buf, sizeof buf’ to less-obvious
  ‘buffer, SIZE’.

* manual/examples/timespec_subtract.c: Modernize to use struct
  timespec not struct timeval, and rename from timeval_subtract.c.
  All uses changed.  Check for overflow.  Do not check for negative
  return value, which ought to be OK since negative time_t is OK.
  Use GNU indenting style.

* manual/time.texi:

  Document CLOCKS_PER_SEC, TIME_UTC, timespec_get, timespec_getres,
  strftime_l.

  Document the storage lifetime of tm_zone and of tzname.

  Caution against use of tzname, timezone and daylight, saying that
  these variables have unspecified values when TZ is geographic.
  This is what glibc actually does (contrary to what the manual said
  before this patch), and POSIX is planned to say the same thing
  <https://austingroupbugs.net/view.php?id=1816>.
  Also say that directly accessing the variables is not thread-safe.

  Say that localtime_r and ctime_r don’t necessarily set time zone
  state.  Similarly, in the tzset documentation, say that it is called
  by ctime, localtime, mktime, strftime, not that it is called by all
  time conversion functions that depend on the time zone.

  Say that tm_isdst is useful mostly just for mktime, and that
  other uses should prefer tm_gmtoff and tm_zone instead.

  Do not say that strftime ignores tm_gmtoff and tm_zone, because
  it doesn’t do that.

  Document what gmtime does to tm_gmtoff and tm_zone.

  Say that the asctime, asctime_r, ctime, and ctime_r are now deprecated
  and/or obsolescent, and that behavior is undefined if the year is <
  1000 or > 9999.  Document strftime before these now-obsolescent
  functions, so that readers see the useful function first.

  Coin the terms “geographical format” and “proleptic format” for the
  two main formats of TZ settings, to simplify exposition.  Use this
  wording consistently.

  Update top-level proleptic syntax to match POSIX.1-2024, which glibc
  already implements.  Document the angle-bracket quoted forms of time
  zone abbreviations in proleptic TZ.  Say that time zone abbreviations
  can contain only ASCII alphanumerics, ‘+’, and ‘-’.

  Document what happens if the proleptic form specifies a DST
  abbreviation and offset but omits the rules.  POSIX says this is
  implementation-defined so we need to document it.  Although this
  documentation mentions ‘posixrules’ tersely, we need to rethink
  ‘posixrules’ since I think it stops working after 2038.

  Clarify wording about TZ settings beginning with ‘;’.

  Say that timegm is in ISO C (as of C23).

  Say that POSIX.1-2024 removed gettimeofday.

  Say that tm_gmtoff and tm_zone are extensions to ISO C, which is
  clearer than saying they are invisible in a struct ISO C enviroment,
  and gives us more wiggle room if we want to make them visible in
  strict ISO C, something that ISO C allows.

  Drop mention of old standards like POSIX.1c and POSIX.2-1992 in the
  text when the history is so old that it’s no longer useful in a
  general-purpose manual.

  Define Coordinated Universal Time (UTC), time zone, time zone ruleset,
  and POSIX Epoch, and use these phrases more consistently.

  Improve TZ examples to show more variety, and to reflect current
  practice and timestamps.  Remove obsolete example about Argentina.
  Add an example for Ireland.

  Don’t rely on GCC extensions when explaining ctime_r.

  Do not say that difftime produces the mathematically correct result,
  since it might be inexact.

  For clock_t don’t say “as in the example above” when there is no
  such example, and don’t say that casting to double works “properly
  and consistently no matter what”, as it suffers from rounding and
  overflow.

  Don’t say broken-down time is not useful for calculations; it’s
  merely painful.

  Say that UTC is not defined before 1960.

  Rename Time Zone Functions to Time Zone State.  All uses changed.

  Update Internet RFC 822 → 5322, 1305 → 5905.  Drop specific years of
  ISO 8601 as they don’t matter.

  Minor style changes: @code{"..."} → @t{"..."} to avoid overquoting in
  info files, @code → @env for environment variables, Daylight Saving
  Time → daylight saving time, white space → whitespace, prime meridian
  → Prime Meridian.
This commit is contained in:
Paul Eggert 2024-06-08 09:48:25 -07:00
parent 41d6461484
commit ee768a30fe
10 changed files with 588 additions and 463 deletions

View File

@ -14260,7 +14260,7 @@ sigusr.c
dir2.c
inetsrv.c
argp-ex3.c
timeval_subtract.c
timespec_subtract.c
popen.c
filecli.c
db.c

View File

@ -89,7 +89,7 @@ process can have open simultaneously. @xref{Opening Streams}.
@deftypevr Macro int TZNAME_MAX
@standards{POSIX.1, limits.h}
If defined, the unvarying maximum length of a time zone abbreviation.
@xref{Time Zone Functions}.
@xref{TZ Variable}.
@end deftypevr
These limit macros are always defined in @file{limits.h}.

View File

@ -18,30 +18,32 @@
#include <time.h>
#include <stdio.h>
#define SIZE 256
int
main (void)
{
char buffer[SIZE];
time_t curtime;
struct tm *loctime;
/* This buffer is big enough that the strftime calls
below cannot possibly exhaust it. */
char buf[256];
/* Get the current time. */
curtime = time (NULL);
time_t curtime = time (NULL);
/* Convert it to local time representation. */
loctime = localtime (&curtime);
struct tm *lt = localtime (&curtime);
if (!lt)
return 1;
/* Print out the date and time in the standard format. */
fputs (asctime (loctime), stdout);
/* Print the date and time in a simple format
that is independent of locale. */
strftime (buf, sizeof buf, "%Y-%m-%d %H:%M:%S", lt);
puts (buf);
/*@group*/
/* Print it out in a nice format. */
strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
fputs (buffer, stdout);
strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
fputs (buffer, stdout);
/* Print it in a nicer English format. */
strftime (buf, sizeof buf, "Today is %A, %B %d.", lt);
puts (buf);
strftime (buf, sizeof buf, "The time is %I:%M %p.", lt);
puts (buf);
return 0;
}

View File

@ -0,0 +1,36 @@
/* struct timespec subtraction.
Copyright (C) 1991-2024 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#include <stdckdint.h>
#include <time.h>
/* Put into *R the difference between X and Y.
Return true if overflow occurs, false otherwise. */
bool
timespec_subtract (struct timespec *r,
struct timespec x, struct timespec y)
{
/* Compute nanoseconds, setting @var{borrow} to 1, 0, or -1
for propagation into seconds. */
long int nsec_diff = x.tv_nsec - y.tv_nsec;
int borrow = (nsec_diff < 0) - ! (nsec_diff < 1000000000);
r->tv_nsec = nsec_diff + 1000000000 * borrow;
/* Compute seconds, returning true if this overflows. */
bool v = ckd_sub (&r->tv_sec, x.tv_sec, y.tv_sec);
return v ^ ckd_sub (&r->tv_sec, r->tv_sec, borrow);
}

View File

@ -1,44 +0,0 @@
/* struct timeval subtraction.
Copyright (C) 1991-2024 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
/* Subtract the `struct timeval' values X and Y,
storing the result in RESULT.
Return 1 if the difference is negative, otherwise 0. */
int
timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
{
/* Perform the carry for the later subtraction by updating @var{y}. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
@code{tv_usec} is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}

View File

@ -456,9 +456,9 @@ permissions on a pseudoterminal so it can be used by the calling process.
If you are using a Linux kernel with the @code{devpts} filesystem enabled
and mounted at @file{/dev/pts}, you don't need this program.
After installation you should configure the timezone and install locales
for your system. The time zone configuration ensures that your system
time matches the time for your current timezone. The locales ensure that
After installation you should configure the time zone ruleset and install
locales for your system. The time zone ruleset ensures that timestamps
are processed correctly for your location. The locales ensure that
the display of information on your system matches the expectations of
your language and geographic region.
@ -481,7 +481,7 @@ as files in the default configured locale installation directory (derived from
root use @samp{DESTDIR} e.g.@: @samp{make localedata/install-locale-files
DESTDIR=/opt/glibc}, but note that this does not change the configured prefix.
To configure the locally used timezone, set the @code{TZ} environment
To configure the time zone ruleset, set the @code{TZ} environment
variable. The script @code{tzselect} helps you to select the right value.
As an example, for Germany, @code{tzselect} would tell you to use
@samp{TZ='Europe/Berlin'}. For a system wide installation (the given

View File

@ -567,8 +567,7 @@ Manual}) use the @code{TERM} environment variable, for example.
@item TZ
@cindex @code{TZ} environment variable
This specifies the time zone. @xref{TZ Variable}, for information about
the format of this string and how it is used.
This specifies the time zone ruleset. @xref{TZ Variable}.
@item LANG
@cindex @code{LANG} environment variable

File diff suppressed because it is too large Load Diff

View File

@ -18,7 +18,10 @@
#include <time.h>
/* The C Standard says that localtime and gmtime return the same pointer. */
/* C89 says that localtime and gmtime return the same pointer.
Although C99 and later relax this to let localtime and gmtime
return different pointers, POSIX and glibc currently follow C89's stricter
requirement even though this can cause naive programs to misbehave. */
struct tm _tmbuf;