diff options
| author | Paul Eggert <eggert@cs.ucla.edu> | 2024-06-08 09:48:25 -0700 |
|---|---|---|
| committer | Paul Eggert <eggert@cs.ucla.edu> | 2024-06-15 08:53:50 -0700 |
| commit | ee768a30fe9099972dad04c49972c2508a208b9b (patch) | |
| tree | 69a6b1b1b594175971df40344ee796ae928770d3 /manual | |
| parent | 41d6461484d6d456fb07f495fb595e06b44758c4 (diff) | |
| download | glibc-ee768a30fe9099972dad04c49972c2508a208b9b.tar.xz glibc-ee768a30fe9099972dad04c49972c2508a208b9b.zip | |
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.
Diffstat (limited to 'manual')
| -rw-r--r-- | manual/conf.texi | 2 | ||||
| -rw-r--r-- | manual/contrib.texi | 2 | ||||
| -rw-r--r-- | manual/examples/strftim.c | 30 | ||||
| -rw-r--r-- | manual/examples/timespec_subtract.c | 36 | ||||
| -rw-r--r-- | manual/examples/timeval_subtract.c | 44 | ||||
| -rw-r--r-- | manual/install.texi | 12 | ||||
| -rw-r--r-- | manual/startup.texi | 3 | ||||
| -rw-r--r-- | manual/time.texi | 917 |
8 files changed, 584 insertions, 462 deletions
diff --git a/manual/conf.texi b/manual/conf.texi index 158285dbf0..be680e0692 100644 --- a/manual/conf.texi +++ b/manual/conf.texi @@ -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}. diff --git a/manual/contrib.texi b/manual/contrib.texi index 6f987aab22..5b2710f0ba 100644 --- a/manual/contrib.texi +++ b/manual/contrib.texi @@ -630,7 +630,7 @@ a different license: @itemize @bullet @item -The timezone support code is derived from the public-domain timezone +The time zone support code is derived from the public-domain time zone package by Arthur David Olson and his many contributors. @item diff --git a/manual/examples/strftim.c b/manual/examples/strftim.c index 304981dfc3..6e1e0474c7 100644 --- a/manual/examples/strftim.c +++ b/manual/examples/strftim.c @@ -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; } diff --git a/manual/examples/timespec_subtract.c b/manual/examples/timespec_subtract.c new file mode 100644 index 0000000000..380d173aab --- /dev/null +++ b/manual/examples/timespec_subtract.c @@ -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); +} diff --git a/manual/examples/timeval_subtract.c b/manual/examples/timeval_subtract.c deleted file mode 100644 index 20c246320d..0000000000 --- a/manual/examples/timeval_subtract.c +++ /dev/null @@ -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; -} diff --git a/manual/install.texi b/manual/install.texi index 7c44594617..c5f159163a 100644 --- a/manual/install.texi +++ b/manual/install.texi @@ -206,7 +206,7 @@ This option hardcodes the newly built C library path in dynamic tests so that they can be invoked directly. @item --disable-timezone-tools -By default, timezone related utilities (@command{zic}, @command{zdump}, +By default, time zone related utilities (@command{zic}, @command{zdump}, and @command{tzselect}) are installed with @theglibc{}. If you are building these independently (e.g. by using the @samp{tzcode} package), then this option will allow disabling the install of these. @@ -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,12 +481,12 @@ 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 paths are for an installation with @samp{--prefix=/usr}), link the -timezone file which is in @file{/usr/share/zoneinfo} to the file +time zone file which is in @file{/usr/share/zoneinfo} to the file @file{/etc/localtime}. For Germany, you might execute @samp{ln -s /usr/share/zoneinfo/Europe/Berlin /etc/localtime}. diff --git a/manual/startup.texi b/manual/startup.texi index 96a7a472bb..224dd98c1e 100644 --- a/manual/startup.texi +++ b/manual/startup.texi @@ -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 diff --git a/manual/time.texi b/manual/time.texi index 2d3833755f..6b1080db06 100644 --- a/manual/time.texi +++ b/manual/time.texi @@ -26,18 +26,22 @@ Discussing time in a technical manual can be difficult because the word we use a rigorous terminology to avoid confusion, and the only thing we use the simple word ``time'' for is to talk about the abstract concept. -A @dfn{calendar time} is a point in the time continuum, for example -November 4, 1990, at 18:02.5 UTC. Sometimes this is called ``absolute -time''. +A @dfn{calendar time}, sometimes called ``absolute time'', +is a point in the Earth's time continuum, for example +June 9, 2024, at 13:50:06.5 Coordinated Universal Time (UTC)@. @cindex calendar time +UTC, formerly called Greenwich Mean Time, is the primary time +standard on Earth, and is the basis for civil time and time zones. +@cindex Coordinated Universal Time +@cindex UTC We don't speak of a ``date'', because that is inherent in a calendar time. @cindex date An @dfn{interval} is a contiguous part of the time continuum between two -calendar times, for example the hour between 9:00 and 10:00 on July 4, -1980. +calendar times, for example the hour on June 9, 2024, +between 13:00 and 14:00 UTC. @cindex interval An @dfn{elapsed time} is the length of an interval, for example, 35 @@ -85,6 +89,16 @@ easier for people to understand, but more difficult to calculate with. @cindex Gregorian calendar @cindex calendar, Gregorian +A @dfn{time zone} is a single fixed offset from UTC, along with +a @dfn{time zone abbreviation} that is a string of characters +that can include ASCII alphanumerics, @samp{+}, and @samp{-}. +For example, the current time zone in Japan is +9 hours ahead (east) of the Prime Meridian with abbreviation @t{"JST"}. + +A @dfn{time zone ruleset} maps each simple calendar time to a single +time zone. For example, Paris's time zone ruleset might list over a +dozen time zones that Paris has experienced during its history. + @dfn{CPU time} measures the amount of time that a single process has actively used a CPU to perform computations. It does not include the time that process has spent waiting for external events. The system @@ -132,16 +146,17 @@ and pass them to the functions that convert them to broken-down time (@pxref{Broken-down Time}). On POSIX-conformant systems, @code{time_t} is an integer type and its -values represent the number of seconds elapsed since the @dfn{epoch}, -which is 00:00:00 on January 1, 1970, Coordinated Universal Time. +values represent the number of seconds elapsed since the @dfn{POSIX Epoch}, +which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC)@. The count of seconds ignores leap seconds. @Theglibc{} additionally guarantees that @code{time_t} is a signed type, and that all of its functions operate correctly on negative -@code{time_t} values, which are interpreted as times before the epoch. -Functions like @code{localtime} assume the Gregorian calendar even -though this is historically inaccurate for timestamps before the -calendar was introduced or after the calendar will become obsolete. +@code{time_t} values, which are interpreted as times before the POSIX Epoch. +Functions like @code{localtime} assume the Gregorian calendar and UTC +even though this is historically inaccurate for dates before 1582, +for times before 1960, and for timestamps after the Gregorian calendar +and UTC will become obsolete. @cindex epoch @Theglibc{} also supports leap seconds as an option, in which case @code{time_t} counts leap seconds instead of ignoring them. @@ -226,7 +241,7 @@ a value of type @code{double}. On POSIX-conformant systems, the advantage of using @samp{difftime (@var{end}, @var{begin})} over @samp{@var{end} - @var{begin}} -is that it will produce the mathematically correct result even if +is that it will not overflow even if @var{end} and @var{begin} are so far apart that a simple subtraction would overflow. However, if they are so far apart that a @code{double} cannot exactly represent the difference, the result will be inexact. @@ -237,13 +252,13 @@ would be the only way to compute their difference. @end deftypefun @Theglibc{} does not provide any functions for computing the -difference between two values of type @w{@code{struct timeval}} or -@w{@code{struct timespec}}. Here is the recommended way to do this -calculation by hand. It works even on some peculiar operating systems +difference between two values of type @w{@code{struct timespec}} or +@w{@code{struct timeval}}. Here is one way to do this +calculation by hand. It works even on peculiar operating systems where the @code{tv_sec} member has an unsigned type. @smallexample -@include timeval_subtract.c.texi +@include timespec_subtract.c.texi @end smallexample @node Processor And CPU Time @@ -252,7 +267,7 @@ where the @code{tv_sec} member has an unsigned type. If you're trying to optimize your program or measure its efficiency, it's very useful to know how much processor time it uses. For that, calendar time and elapsed times are useless because a process may spend -time waiting for I/O or for other processes to use the CPU. However, +time waiting for I/O or for other processes to use the CPU@. However, you can get the information with the functions in this section. CPU time (@pxref{Time Basics}) is represented by the data type @@ -267,12 +282,16 @@ time before and after the computation. @cindex clock ticks @cindex ticks, clock +@defvr Macro CLOCKS_PER_SEC +@standards{ISO, time.h} +The number of clock ticks per second. +@end defvr + On @gnulinuxhurdsystems{}, @code{clock_t} is equivalent to @code{long int} and @code{CLOCKS_PER_SEC} is an integer value. But in other systems, both @code{clock_t} and the macro @code{CLOCKS_PER_SEC} can be either integer -or floating-point types. Casting CPU time values to @code{double}, as -in the example above, makes sure that operations such as arithmetic and -printing work properly and consistently no matter what the underlying +or floating-point types. Converting CPU time values to @code{double} +can help code be more portable no matter what the underlying representation is. Note that the clock can wrap around. On a 32bit system with @@ -434,8 +453,8 @@ manipulating calendar times. * Formatting Calendar Time:: Converting times to strings. * Parsing Date and Time:: Convert textual time and date information back into broken-down time values. -* TZ Variable:: How users specify the time zone. -* Time Zone Functions:: Functions to examine or specify the time zone. +* TZ Variable:: How users specify the time zone ruleset. +* Time Zone State:: Time zone state variables. * Time Functions Example:: An example program showing use of some of the time functions. @end menu @@ -464,30 +483,30 @@ This function cannot fail. Some applications need more precise timekeeping than is possible with a @code{time_t} alone. Some applications also need more control over -what is meant by ``the current time.'' For these applications, POSIX -provides a function @code{clock_gettime} that can retrieve the time +what is meant by ``the current time.'' For these applications, +POSIX and @w{ISO C} provide functions to retrieve the time with up to nanosecond precision, from a variety of different clocks. Clocks can be system-wide, measuring time the same for all processes; or they can be per-process or per-thread, measuring CPU time consumed by a particular process, or some other similar resource. Each clock -has its own resolution and epoch. You can find the resolution of a -clock with the function @code{clock_getres}. There is no function to +has its own resolution and epoch. POSIX and @w{ISO C} also provide functions +for finding the resolution of a clock. There is no function to get the epoch for a clock; either it is fixed and documented, or the clock is not meant to be used to measure absolute times. @deftp {Data Type} clockid_t @standards{POSIX.1, time.h} The type @code{clockid_t} is used for constants that indicate which of -several system clocks one wishes to use. +several POSIX system clocks one wishes to use. @end deftp -All systems that support this family of functions will define at least +All systems that support the POSIX functions will define at least this clock constant: @deftypevr Macro clockid_t CLOCK_REALTIME @standards{POSIX.1, time.h} -This clock uses the POSIX epoch, 00:00:00 on January 1, 1970, Coordinated -Universal Time. It is close to, but not necessarily in lock-step with, the +This POSIX clock uses the POSIX Epoch, 1970-01-01 00:00:00 UTC@. +It is close to, but not necessarily in lock-step with, the clocks of @code{time} (above) and of @code{gettimeofday} (below). @end deftypevr @@ -499,7 +518,7 @@ measurements are not affected by changes to the system clock. @deftypevr Macro clockid_t CLOCK_MONOTONIC @standards{POSIX.1, time.h} -System-wide clock that continuously measures the advancement of +This system-wide POSIX clock continuously measures the advancement of calendar time, ignoring discontinuous changes to the system's setting for absolute calendar time. @@ -509,7 +528,7 @@ Therefore, @code{CLOCK_MONOTONIC} cannot be used to measure absolute time, only elapsed time. @end deftypevr -Systems may support more than just these two clocks. +Systems may support more than just these two POSIX clocks. @deftypefun int clock_gettime (clockid_t @var{clock}, struct timespec *@var{ts}) @standards{POSIX.1, time.h} @@ -552,15 +571,45 @@ The clock identified by @var{clock} is not supported. @end table @end deftypefun -These functions, and the constants that identify particular clocks, -are declared in @file{time.h}. - @strong{Portability Note:} On some systems, including systems that use older versions of @theglibc{}, programs that use @code{clock_gettime} or @code{clock_setres} must be linked with the @code{-lrt} library. This has not been necessary with @theglibc{} since version 2.17. -@Theglibc{} also provides an older, but still widely used, function +The following @w{ISO C} macros and functions for higher-resolution +timestamps were standardized more recently than the POSIX functions, +so they are less portable to older POSIX systems. However, the @w{ISO +C} functions are portable to C platforms that do not support POSIX. + +@deftypevr Macro int TIME_UTC +@standards{ISO, time.h} +This is a positive integer constant designating a simple calendar time base. +In @theglibc{} and other POSIX systems, +this is equivalent to the POSIX @code{CLOCK_REALTIME} clock. +On non-POSIX systems, though, the epoch is implementation-defined. +@end deftypevr + +Systems may support more than just this @w{ISO C} clock. + +@deftypefun int timespec_get (struct timespec *@var{ts}, int @var{base}) +@standards{ISO, time.h} +Store into @code{*@var{ts}} the current time according to the @w{ISO +C} time @var{base}. + +The return value is @var{base} on success and @code{0} on failure. +@end deftypefun + +@deftypefun int timespec_getres (struct timespec *@var{res}, int @var{base}) +@standards{ISO, time.h} +If @var{ts} is non-null, store into @code{*@var{ts}} the resolution of +the time provided by @code{timespec_get} function for the @w{ISO C} +time @var{base}. + +The return value is @var{base} on success and @code{0} on failure. |
