Blame manual/time.texi

Packit Service 82fcde
@node Date and Time, Resource Usage And Limitation, Arithmetic, Top
Packit Service 82fcde
@c %MENU% Functions for getting the date and time and formatting them nicely
Packit Service 82fcde
@chapter Date and Time
Packit Service 82fcde
Packit Service 82fcde
This chapter describes functions for manipulating dates and times,
Packit Service 82fcde
including functions for determining what time it is and conversion
Packit Service 82fcde
between different time representations.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Time Basics::                 Concepts and definitions.
Packit Service 82fcde
* Elapsed Time::                Data types to represent elapsed times
Packit Service 82fcde
* Processor And CPU Time::      Time a program has spent executing.
Packit Service 82fcde
* Calendar Time::               Manipulation of ``real'' dates and times.
Packit Service 82fcde
* Setting an Alarm::            Sending a signal after a specified time.
Packit Service 82fcde
* Sleeping::                    Waiting for a period of time.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Time Basics
Packit Service 82fcde
@section Time Basics
Packit Service 82fcde
@cindex time
Packit Service 82fcde
Packit Service 82fcde
Discussing time in a technical manual can be difficult because the word
Packit Service 82fcde
``time'' in English refers to lots of different things.  In this manual,
Packit Service 82fcde
we use a rigorous terminology to avoid confusion, and the only thing we
Packit Service 82fcde
use the simple word ``time'' for is to talk about the abstract concept.
Packit Service 82fcde
Packit Service 82fcde
A @dfn{calendar time} is a point in the time continuum, for example
Packit Service 82fcde
November 4, 1990, at 18:02.5 UTC.  Sometimes this is called ``absolute
Packit Service 82fcde
time''.
Packit Service 82fcde
@cindex calendar time
Packit Service 82fcde
Packit Service 82fcde
We don't speak of a ``date'', because that is inherent in a calendar
Packit Service 82fcde
time.
Packit Service 82fcde
@cindex date
Packit Service 82fcde
Packit Service 82fcde
An @dfn{interval} is a contiguous part of the time continuum between two
Packit Service 82fcde
calendar times, for example the hour between 9:00 and 10:00 on July 4,
Packit Service 82fcde
1980.
Packit Service 82fcde
@cindex interval
Packit Service 82fcde
Packit Service 82fcde
An @dfn{elapsed time} is the length of an interval, for example, 35
Packit Service 82fcde
minutes.  People sometimes sloppily use the word ``interval'' to refer
Packit Service 82fcde
to the elapsed time of some interval.
Packit Service 82fcde
@cindex elapsed time
Packit Service 82fcde
@cindex time, elapsed
Packit Service 82fcde
Packit Service 82fcde
An @dfn{amount of time} is a sum of elapsed times, which need not be of
Packit Service 82fcde
any specific intervals.  For example, the amount of time it takes to
Packit Service 82fcde
read a book might be 9 hours, independently of when and in how many
Packit Service 82fcde
sittings it is read.
Packit Service 82fcde
Packit Service 82fcde
A @dfn{period} is the elapsed time of an interval between two events,
Packit Service 82fcde
especially when they are part of a sequence of regularly repeating
Packit Service 82fcde
events.
Packit Service 82fcde
@cindex period of time
Packit Service 82fcde
Packit Service 82fcde
@dfn{CPU time} is like calendar time, except that it is based on the
Packit Service 82fcde
subset of the time continuum when a particular process is actively
Packit Service 82fcde
using a CPU.  CPU time is, therefore, relative to a process.
Packit Service 82fcde
@cindex CPU time
Packit Service 82fcde
Packit Service 82fcde
@dfn{Processor time} is an amount of time that a CPU is in use.  In
Packit Service 82fcde
fact, it's a basic system resource, since there's a limit to how much
Packit Service 82fcde
can exist in any given interval (that limit is the elapsed time of the
Packit Service 82fcde
interval times the number of CPUs in the processor).  People often call
Packit Service 82fcde
this CPU time, but we reserve the latter term in this manual for the
Packit Service 82fcde
definition above.
Packit Service 82fcde
@cindex processor time
Packit Service 82fcde
Packit Service 82fcde
@node Elapsed Time
Packit Service 82fcde
@section Elapsed Time
Packit Service 82fcde
@cindex elapsed time
Packit Service 82fcde
Packit Service 82fcde
One way to represent an elapsed time is with a simple arithmetic data
Packit Service 82fcde
type, as with the following function to compute the elapsed time between
Packit Service 82fcde
two calendar times.  This function is declared in @file{time.h}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun double difftime (time_t @var{time1}, time_t @var{time0})
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
The @code{difftime} function returns the number of seconds of elapsed
Packit Service 82fcde
time between calendar time @var{time1} and calendar time @var{time0}, as
Packit Service 82fcde
a value of type @code{double}.  The difference ignores leap seconds
Packit Service 82fcde
unless leap second support is enabled.
Packit Service 82fcde
Packit Service 82fcde
In @theglibc{}, you can simply subtract @code{time_t} values.  But on
Packit Service 82fcde
other systems, the @code{time_t} data type might use some other encoding
Packit Service 82fcde
where subtraction doesn't work directly.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@Theglibc{} provides two data types specifically for representing
Packit Service 82fcde
an elapsed time.  They are used by various @glibcadj{} functions, and
Packit Service 82fcde
you can use them for your own purposes too.  They're exactly the same
Packit Service 82fcde
except that one has a resolution in microseconds, and the other, newer
Packit Service 82fcde
one, is in nanoseconds.
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} {struct timeval}
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
@cindex timeval
Packit Service 82fcde
The @code{struct timeval} structure represents an elapsed time.  It is
Packit Service 82fcde
declared in @file{sys/time.h} and has the following members:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item time_t tv_sec
Packit Service 82fcde
This represents the number of whole seconds of elapsed time.
Packit Service 82fcde
Packit Service 82fcde
@item long int tv_usec
Packit Service 82fcde
This is the rest of the elapsed time (a fraction of a second),
Packit Service 82fcde
represented as the number of microseconds.  It is always less than one
Packit Service 82fcde
million.
Packit Service 82fcde
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} {struct timespec}
Packit Service 82fcde
@standards{POSIX.1, sys/time.h}
Packit Service 82fcde
@cindex timespec
Packit Service 82fcde
The @code{struct timespec} structure represents an elapsed time.  It is
Packit Service 82fcde
declared in @file{time.h} and has the following members:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item time_t tv_sec
Packit Service 82fcde
This represents the number of whole seconds of elapsed time.
Packit Service 82fcde
Packit Service 82fcde
@item long int tv_nsec
Packit Service 82fcde
This is the rest of the elapsed time (a fraction of a second),
Packit Service 82fcde
represented as the number of nanoseconds.  It is always less than one
Packit Service 82fcde
billion.
Packit Service 82fcde
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
It is often necessary to subtract two values of type @w{@code{struct
Packit Service 82fcde
timeval}} or @w{@code{struct timespec}}.  Here is the best way to do
Packit Service 82fcde
this.  It works even on some peculiar operating systems where the
Packit Service 82fcde
@code{tv_sec} member has an unsigned type.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@include timeval_subtract.c.texi
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Common functions that use @code{struct timeval} are @code{gettimeofday}
Packit Service 82fcde
and @code{settimeofday}.
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
There are no @glibcadj{} functions specifically oriented toward
Packit Service 82fcde
dealing with elapsed times, but the calendar time, processor time, and
Packit Service 82fcde
alarm and sleeping functions have a lot to do with them.
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Processor And CPU Time
Packit Service 82fcde
@section Processor And CPU Time
Packit Service 82fcde
Packit Service 82fcde
If you're trying to optimize your program or measure its efficiency,
Packit Service 82fcde
it's very useful to know how much processor time it uses.  For that,
Packit Service 82fcde
calendar time and elapsed times are useless because a process may spend
Packit Service 82fcde
time waiting for I/O or for other processes to use the CPU.  However,
Packit Service 82fcde
you can get the information with the functions in this section.
Packit Service 82fcde
Packit Service 82fcde
CPU time (@pxref{Time Basics}) is represented by the data type
Packit Service 82fcde
@code{clock_t}, which is a number of @dfn{clock ticks}.  It gives the
Packit Service 82fcde
total amount of time a process has actively used a CPU since some
Packit Service 82fcde
arbitrary event.  On @gnusystems{}, that event is the creation of the
Packit Service 82fcde
process.  While arbitrary in general, the event is always the same event
Packit Service 82fcde
for any particular process, so you can always measure how much time on
Packit Service 82fcde
the CPU a particular computation takes by examining the process' CPU
Packit Service 82fcde
time before and after the computation.
Packit Service 82fcde
@cindex CPU time
Packit Service 82fcde
@cindex clock ticks
Packit Service 82fcde
@cindex ticks, clock
Packit Service 82fcde
Packit Service 82fcde
On @gnulinuxhurdsystems{}, @code{clock_t} is equivalent to @code{long int} and
Packit Service 82fcde
@code{CLOCKS_PER_SEC} is an integer value.  But in other systems, both
Packit Service 82fcde
@code{clock_t} and the macro @code{CLOCKS_PER_SEC} can be either integer
Packit Service 82fcde
or floating-point types.  Casting CPU time values to @code{double}, as
Packit Service 82fcde
in the example above, makes sure that operations such as arithmetic and
Packit Service 82fcde
printing work properly and consistently no matter what the underlying
Packit Service 82fcde
representation is.
Packit Service 82fcde
Packit Service 82fcde
Note that the clock can wrap around.  On a 32bit system with
Packit Service 82fcde
@code{CLOCKS_PER_SEC} set to one million this function will return the
Packit Service 82fcde
same value approximately every 72 minutes.
Packit Service 82fcde
Packit Service 82fcde
For additional functions to examine a process' use of processor time,
Packit Service 82fcde
and to control it, see @ref{Resource Usage And Limitation}.
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* CPU Time::                    The @code{clock} function.
Packit Service 82fcde
* Processor Time::              The @code{times} function.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node CPU Time
Packit Service 82fcde
@subsection CPU Time Inquiry
Packit Service 82fcde
Packit Service 82fcde
To get a process' CPU time, you can use the @code{clock} function.  This
Packit Service 82fcde
facility is declared in the header file @file{time.h}.
Packit Service 82fcde
@pindex time.h
Packit Service 82fcde
Packit Service 82fcde
In typical usage, you call the @code{clock} function at the beginning
Packit Service 82fcde
and end of the interval you want to time, subtract the values, and then
Packit Service 82fcde
divide by @code{CLOCKS_PER_SEC} (the number of clock ticks per second)
Packit Service 82fcde
to get processor time, like this:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@group
Packit Service 82fcde
#include <time.h>
Packit Service 82fcde
Packit Service 82fcde
clock_t start, end;
Packit Service 82fcde
double cpu_time_used;
Packit Service 82fcde
Packit Service 82fcde
start = clock();
Packit Service 82fcde
@dots{} /* @r{Do the work.} */
Packit Service 82fcde
end = clock();
Packit Service 82fcde
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
Packit Service 82fcde
@end group
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Do not use a single CPU time as an amount of time; it doesn't work that
Packit Service 82fcde
way.  Either do a subtraction as shown above or query processor time
Packit Service 82fcde
directly.  @xref{Processor Time}.
Packit Service 82fcde
Packit Service 82fcde
Different computers and operating systems vary wildly in how they keep
Packit Service 82fcde
track of CPU time.  It's common for the internal processor clock
Packit Service 82fcde
to have a resolution somewhere between a hundredth and millionth of a
Packit Service 82fcde
second.
Packit Service 82fcde
Packit Service 82fcde
@deftypevr Macro int CLOCKS_PER_SEC
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
The value of this macro is the number of clock ticks per second measured
Packit Service 82fcde
by the @code{clock} function.  POSIX requires that this value be one
Packit Service 82fcde
million independent of the actual resolution.
Packit Service 82fcde
@end deftypevr
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} clock_t
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
This is the type of the value returned by the @code{clock} function.
Packit Service 82fcde
Values of type @code{clock_t} are numbers of clock ticks.
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun clock_t clock (void)
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c On Hurd, this calls task_info twice and adds user and system time
Packit Service 82fcde
@c from both basic and thread time info structs.  On generic posix,
Packit Service 82fcde
@c calls times and adds utime and stime.  On bsd, calls getrusage and
Packit Service 82fcde
@c safely converts stime and utime to clock.  On linux, calls
Packit Service 82fcde
@c clock_gettime.
Packit Service 82fcde
This function returns the calling process' current CPU time.  If the CPU
Packit Service 82fcde
time is not available or cannot be represented, @code{clock} returns the
Packit Service 82fcde
value @code{(clock_t)(-1)}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Processor Time
Packit Service 82fcde
@subsection Processor Time Inquiry
Packit Service 82fcde
Packit Service 82fcde
The @code{times} function returns information about a process'
Packit Service 82fcde
consumption of processor time in a @w{@code{struct tms}} object, in
Packit Service 82fcde
addition to the process' CPU time.  @xref{Time Basics}.  You should
Packit Service 82fcde
include the header file @file{sys/times.h} to use this facility.
Packit Service 82fcde
@cindex processor time
Packit Service 82fcde
@cindex CPU time
Packit Service 82fcde
@pindex sys/times.h
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} {struct tms}
Packit Service 82fcde
@standards{POSIX.1, sys/times.h}
Packit Service 82fcde
The @code{tms} structure is used to return information about process
Packit Service 82fcde
times.  It contains at least the following members:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item clock_t tms_utime
Packit Service 82fcde
This is the total processor time the calling process has used in
Packit Service 82fcde
executing the instructions of its program.
Packit Service 82fcde
Packit Service 82fcde
@item clock_t tms_stime
Packit Service 82fcde
This is the processor time the system has used on behalf of the calling
Packit Service 82fcde
process.
Packit Service 82fcde
Packit Service 82fcde
@item clock_t tms_cutime
Packit Service 82fcde
This is the sum of the @code{tms_utime} values and the @code{tms_cutime}
Packit Service 82fcde
values of all terminated child processes of the calling process, whose
Packit Service 82fcde
status has been reported to the parent process by @code{wait} or
Packit Service 82fcde
@code{waitpid}; see @ref{Process Completion}.  In other words, it
Packit Service 82fcde
represents the total processor time used in executing the instructions
Packit Service 82fcde
of all the terminated child processes of the calling process, excluding
Packit Service 82fcde
child processes which have not yet been reported by @code{wait} or
Packit Service 82fcde
@code{waitpid}.
Packit Service 82fcde
@cindex child process
Packit Service 82fcde
Packit Service 82fcde
@item clock_t tms_cstime
Packit Service 82fcde
This is similar to @code{tms_cutime}, but represents the total processor
Packit Service 82fcde
time the system has used on behalf of all the terminated child processes
Packit Service 82fcde
of the calling process.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
All of the times are given in numbers of clock ticks.  Unlike CPU time,
Packit Service 82fcde
these are the actual amounts of time; not relative to any event.
Packit Service 82fcde
@xref{Creating a Process}.
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypevr Macro int CLK_TCK
Packit Service 82fcde
@standards{POSIX.1, time.h}
Packit Service 82fcde
This is an obsolete name for the number of clock ticks per second.  Use
Packit Service 82fcde
@code{sysconf (_SC_CLK_TCK)} instead.
Packit Service 82fcde
@end deftypevr
Packit Service 82fcde
Packit Service 82fcde
@deftypefun clock_t times (struct tms *@var{buffer})
Packit Service 82fcde
@standards{POSIX.1, sys/times.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c On HURD, this calls task_info twice, for basic and thread times info,
Packit Service 82fcde
@c adding user and system times into tms, and then gettimeofday, to
Packit Service 82fcde
@c compute the real time.  On BSD, it calls getclktck, getrusage (twice)
Packit Service 82fcde
@c and time.  On Linux, it's a syscall with special handling to account
Packit Service 82fcde
@c for clock_t counts that look like error values.
Packit Service 82fcde
The @code{times} function stores the processor time information for
Packit Service 82fcde
the calling process in @var{buffer}.
Packit Service 82fcde
Packit Service 82fcde
The return value is the number of clock ticks since an arbitrary point
Packit Service 82fcde
in the past, e.g. since system start-up.  @code{times} returns
Packit Service 82fcde
@code{(clock_t)(-1)} to indicate failure.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability Note:} The @code{clock} function described in
Packit Service 82fcde
@ref{CPU Time} is specified by the @w{ISO C} standard.  The
Packit Service 82fcde
@code{times} function is a feature of POSIX.1.  On @gnusystems{}, the
Packit Service 82fcde
CPU time is defined to be equivalent to the sum of the @code{tms_utime}
Packit Service 82fcde
and @code{tms_stime} fields returned by @code{times}.
Packit Service 82fcde
Packit Service 82fcde
@node Calendar Time
Packit Service 82fcde
@section Calendar Time
Packit Service 82fcde
Packit Service 82fcde
This section describes facilities for keeping track of calendar time.
Packit Service 82fcde
@xref{Time Basics}.
Packit Service 82fcde
Packit Service 82fcde
@Theglibc{} represents calendar time three ways:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
@dfn{Simple time} (the @code{time_t} data type) is a compact
Packit Service 82fcde
representation, typically giving the number of seconds of elapsed time
Packit Service 82fcde
since some implementation-specific base time.
Packit Service 82fcde
@cindex simple time
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
There is also a "high-resolution time" representation.  Like simple
Packit Service 82fcde
time, this represents a calendar time as an elapsed time since a base
Packit Service 82fcde
time, but instead of measuring in whole seconds, it uses a @code{struct
Packit Service 82fcde
timeval} data type, which includes fractions of a second.  Use this time
Packit Service 82fcde
representation instead of simple time when you need greater precision.
Packit Service 82fcde
@cindex high-resolution time
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
@dfn{Local time} or @dfn{broken-down time} (the @code{struct tm} data
Packit Service 82fcde
type) represents a calendar time as a set of components specifying the
Packit Service 82fcde
year, month, and so on in the Gregorian calendar, for a specific time
Packit Service 82fcde
zone.  This calendar time representation is usually used only to
Packit Service 82fcde
communicate with people.
Packit Service 82fcde
@cindex local time
Packit Service 82fcde
@cindex broken-down time
Packit Service 82fcde
@cindex Gregorian calendar
Packit Service 82fcde
@cindex calendar, Gregorian
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Simple Calendar Time::        Facilities for manipulating calendar time.
Packit Service 82fcde
* High-Resolution Calendar::    A time representation with greater precision.
Packit Service 82fcde
* Broken-down Time::            Facilities for manipulating local time.
Packit Service 82fcde
* High Accuracy Clock::         Maintaining a high accuracy system clock.
Packit Service 82fcde
* Formatting Calendar Time::    Converting times to strings.
Packit Service 82fcde
* Parsing Date and Time::       Convert textual time and date information back
Packit Service 82fcde
                                 into broken-down time values.
Packit Service 82fcde
* TZ Variable::                 How users specify the time zone.
Packit Service 82fcde
* Time Zone Functions::         Functions to examine or specify the time zone.
Packit Service 82fcde
* Time Functions Example::      An example program showing use of some of
Packit Service 82fcde
				 the time functions.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Simple Calendar Time
Packit Service 82fcde
@subsection Simple Calendar Time
Packit Service 82fcde
Packit Service 82fcde
This section describes the @code{time_t} data type for representing calendar
Packit Service 82fcde
time as simple time, and the functions which operate on simple time objects.
Packit Service 82fcde
These facilities are declared in the header file @file{time.h}.
Packit Service 82fcde
@pindex time.h
Packit Service 82fcde
Packit Service 82fcde
@cindex epoch
Packit Service 82fcde
@deftp {Data Type} time_t
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
This is the data type used to represent simple time.  Sometimes, it also
Packit Service 82fcde
represents an elapsed time.  When interpreted as a calendar time value,
Packit Service 82fcde
it represents the number of seconds elapsed since 00:00:00 on January 1,
Packit Service 82fcde
1970, Coordinated Universal Time.  (This calendar time is sometimes
Packit Service 82fcde
referred to as the @dfn{epoch}.)  POSIX requires that this count not
Packit Service 82fcde
include leap seconds, but on some systems this count includes leap seconds
Packit Service 82fcde
if you set @code{TZ} to certain values (@pxref{TZ Variable}).
Packit Service 82fcde
Packit Service 82fcde
Note that a simple time has no concept of local time zone.  Calendar
Packit Service 82fcde
Time @var{T} is the same instant in time regardless of where on the
Packit Service 82fcde
globe the computer is.
Packit Service 82fcde
Packit Service 82fcde
In @theglibc{}, @code{time_t} is equivalent to @code{long int}.
Packit Service 82fcde
In other systems, @code{time_t} might be either an integer or
Packit Service 82fcde
floating-point type.
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
The function @code{difftime} tells you the elapsed time between two
Packit Service 82fcde
simple calendar times, which is not always as easy to compute as just
Packit Service 82fcde
subtracting.  @xref{Elapsed Time}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun time_t time (time_t *@var{result})
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
The @code{time} function returns the current calendar time as a value of
Packit Service 82fcde
type @code{time_t}.  If the argument @var{result} is not a null pointer,
Packit Service 82fcde
the calendar time value is also stored in @code{*@var{result}}.  If the
Packit Service 82fcde
current calendar time is not available, the value
Packit Service 82fcde
@w{@code{(time_t)(-1)}} is returned.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@c The GNU C library implements stime() with a call to settimeofday() on
Packit Service 82fcde
@c Linux.
Packit Service 82fcde
@deftypefun int stime (const time_t *@var{newtime})
Packit Service 82fcde
@standards{SVID, time.h}
Packit Service 82fcde
@standards{XPG, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c On unix, this is implemented in terms of settimeofday.
Packit Service 82fcde
@code{stime} sets the system clock, i.e., it tells the system that the
Packit Service 82fcde
current calendar time is @var{newtime}, where @code{newtime} is
Packit Service 82fcde
interpreted as described in the above definition of @code{time_t}.
Packit Service 82fcde
Packit Service 82fcde
@code{settimeofday} is a newer function which sets the system clock to
Packit Service 82fcde
better than one second precision.  @code{settimeofday} is generally a
Packit Service 82fcde
better choice than @code{stime}.  @xref{High-Resolution Calendar}.
Packit Service 82fcde
Packit Service 82fcde
Only the superuser can set the system clock.
Packit Service 82fcde
Packit Service 82fcde
If the function succeeds, the return value is zero.  Otherwise, it is
Packit Service 82fcde
@code{-1} and @code{errno} is set accordingly:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item EPERM
Packit Service 82fcde
The process is not superuser.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node High-Resolution Calendar
Packit Service 82fcde
@subsection High-Resolution Calendar
Packit Service 82fcde
Packit Service 82fcde
The @code{time_t} data type used to represent simple times has a
Packit Service 82fcde
resolution of only one second.  Some applications need more precision.
Packit Service 82fcde
Packit Service 82fcde
So, @theglibc{} also contains functions which are capable of
Packit Service 82fcde
representing calendar times to a higher resolution than one second.  The
Packit Service 82fcde
functions and the associated data types described in this section are
Packit Service 82fcde
declared in @file{sys/time.h}.
Packit Service 82fcde
@pindex sys/time.h
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} {struct timezone}
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
The @code{struct timezone} structure is used to hold minimal information
Packit Service 82fcde
about the local time zone.  It has the following members:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item int tz_minuteswest
Packit Service 82fcde
This is the number of minutes west of UTC.
Packit Service 82fcde
Packit Service 82fcde
@item int tz_dsttime
Packit Service 82fcde
If nonzero, Daylight Saving Time applies during some part of the year.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
The @code{struct timezone} type is obsolete and should never be used.
Packit Service 82fcde
Instead, use the facilities described in @ref{Time Zone Functions}.
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int gettimeofday (struct timeval *@var{tp}, struct timezone *@var{tzp})
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c On most GNU/Linux systems this is a direct syscall, but the posix/
Packit Service 82fcde
@c implementation (not used on GNU/Linux or GNU/Hurd) relies on time and
Packit Service 82fcde
@c localtime_r, saving and restoring tzname in an unsafe manner.
Packit Service 82fcde
@c On some GNU/Linux variants, ifunc resolvers are used in shared libc
Packit Service 82fcde
@c for vdso resolution.  ifunc-vdso-revisit.
Packit Service 82fcde
The @code{gettimeofday} function returns the current calendar time as
Packit Service 82fcde
the elapsed time since the epoch in the @code{struct timeval} structure
Packit Service 82fcde
indicated by @var{tp}.  (@pxref{Elapsed Time} for a description of
Packit Service 82fcde
@code{struct timeval}).  Information about the time zone is returned in
Packit Service 82fcde
the structure pointed to by @var{tzp}.  If the @var{tzp} argument is a null
Packit Service 82fcde
pointer, time zone information is ignored.
Packit Service 82fcde
Packit Service 82fcde
The return value is @code{0} on success and @code{-1} on failure.  The
Packit Service 82fcde
following @code{errno} error condition is defined for this function:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item ENOSYS
Packit Service 82fcde
The operating system does not support getting time zone information, and
Packit Service 82fcde
@var{tzp} is not a null pointer.  @gnusystems{} do not
Packit Service 82fcde
support using @w{@code{struct timezone}} to represent time zone
Packit Service 82fcde
information; that is an obsolete feature of 4.3 BSD.
Packit Service 82fcde
Instead, use the facilities described in @ref{Time Zone Functions}.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int settimeofday (const struct timeval *@var{tp}, const struct timezone *@var{tzp})
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c On HURD, it calls host_set_time with a privileged port.  On other
Packit Service 82fcde
@c unix systems, it's a syscall.
Packit Service 82fcde
The @code{settimeofday} function sets the current calendar time in the
Packit Service 82fcde
system clock according to the arguments.  As for @code{gettimeofday},
Packit Service 82fcde
the calendar time is represented as the elapsed time since the epoch.
Packit Service 82fcde
As for @code{gettimeofday}, time zone information is ignored if
Packit Service 82fcde
@var{tzp} is a null pointer.
Packit Service 82fcde
Packit Service 82fcde
You must be a privileged user in order to use @code{settimeofday}.
Packit Service 82fcde
Packit Service 82fcde
Some kernels automatically set the system clock from some source such as
Packit Service 82fcde
a hardware clock when they start up.  Others, including Linux, place the
Packit Service 82fcde
system clock in an ``invalid'' state (in which attempts to read the clock
Packit Service 82fcde
fail).  A call of @code{stime} removes the system clock from an invalid
Packit Service 82fcde
state, and system startup scripts typically run a program that calls
Packit Service 82fcde
@code{stime}.
Packit Service 82fcde
Packit Service 82fcde
@code{settimeofday} causes a sudden jump forwards or backwards, which
Packit Service 82fcde
can cause a variety of problems in a system.  Use @code{adjtime} (below)
Packit Service 82fcde
to make a smooth transition from one time to another by temporarily
Packit Service 82fcde
speeding up or slowing down the clock.
Packit Service 82fcde
Packit Service 82fcde
With a Linux kernel, @code{adjtimex} does the same thing and can also
Packit Service 82fcde
make permanent changes to the speed of the system clock so it doesn't
Packit Service 82fcde
need to be corrected as often.
Packit Service 82fcde
Packit Service 82fcde
The return value is @code{0} on success and @code{-1} on failure.  The
Packit Service 82fcde
following @code{errno} error conditions are defined for this function:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item EPERM
Packit Service 82fcde
This process cannot set the clock because it is not privileged.
Packit Service 82fcde
Packit Service 82fcde
@item ENOSYS
Packit Service 82fcde
The operating system does not support setting time zone information, and
Packit Service 82fcde
@var{tzp} is not a null pointer.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@c On Linux, GNU libc implements adjtime() as a call to adjtimex().
Packit Service 82fcde
@deftypefun int adjtime (const struct timeval *@var{delta}, struct timeval *@var{olddelta})
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c On hurd and mach, call host_adjust_time with a privileged port.  On
Packit Service 82fcde
@c Linux, it's implemented in terms of adjtimex.  On other unixen, it's
Packit Service 82fcde
@c a syscall.
Packit Service 82fcde
This function speeds up or slows down the system clock in order to make
Packit Service 82fcde
a gradual adjustment.  This ensures that the calendar time reported by
Packit Service 82fcde
the system clock is always monotonically increasing, which might not
Packit Service 82fcde
happen if you simply set the clock.
Packit Service 82fcde
Packit Service 82fcde
The @var{delta} argument specifies a relative adjustment to be made to
Packit Service 82fcde
the clock time.  If negative, the system clock is slowed down for a
Packit Service 82fcde
while until it has lost this much elapsed time.  If positive, the system
Packit Service 82fcde
clock is speeded up for a while.
Packit Service 82fcde
Packit Service 82fcde
If the @var{olddelta} argument is not a null pointer, the @code{adjtime}
Packit Service 82fcde
function returns information about any previous time adjustment that
Packit Service 82fcde
has not yet completed.
Packit Service 82fcde
Packit Service 82fcde
This function is typically used to synchronize the clocks of computers
Packit Service 82fcde
in a local network.  You must be a privileged user to use it.
Packit Service 82fcde
Packit Service 82fcde
With a Linux kernel, you can use the @code{adjtimex} function to
Packit Service 82fcde
permanently change the clock speed.
Packit Service 82fcde
Packit Service 82fcde
The return value is @code{0} on success and @code{-1} on failure.  The
Packit Service 82fcde
following @code{errno} error condition is defined for this function:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item EPERM
Packit Service 82fcde
You do not have privilege to set the time.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability Note:}  The @code{gettimeofday}, @code{settimeofday},
Packit Service 82fcde
and @code{adjtime} functions are derived from BSD.
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
Symbols for the following function are declared in @file{sys/timex.h}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int adjtimex (struct timex *@var{timex})
Packit Service 82fcde
@standards{GNU, sys/timex.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c It's a syscall, only available on linux.
Packit Service 82fcde
Packit Service 82fcde
@code{adjtimex} is functionally identical to @code{ntp_adjtime}.
Packit Service 82fcde
@xref{High Accuracy Clock}.
Packit Service 82fcde
Packit Service 82fcde
This function is present only with a Linux kernel.
Packit Service 82fcde
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Broken-down Time
Packit Service 82fcde
@subsection Broken-down Time
Packit Service 82fcde
@cindex broken-down time
Packit Service 82fcde
@cindex calendar time and broken-down time
Packit Service 82fcde
Packit Service 82fcde
Calendar time is represented by the usual @glibcadj{} functions as an
Packit Service 82fcde
elapsed time since a fixed base calendar time.  This is convenient for
Packit Service 82fcde
computation, but has no relation to the way people normally think of
Packit Service 82fcde
calendar time.  By contrast, @dfn{broken-down time} is a binary
Packit Service 82fcde
representation of calendar time separated into year, month, day, and so
Packit Service 82fcde
on.  Broken-down time values are not useful for calculations, but they
Packit Service 82fcde
are useful for printing human readable time information.
Packit Service 82fcde
Packit Service 82fcde
A broken-down time value is always relative to a choice of time
Packit Service 82fcde
zone, and it also indicates which time zone that is.
Packit Service 82fcde
Packit Service 82fcde
The symbols in this section are declared in the header file @file{time.h}.
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} {struct tm}
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
This is the data type used to represent a broken-down time.  The structure
Packit Service 82fcde
contains at least the following members, which can appear in any order.
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item int tm_sec
Packit Service 82fcde
This is the number of full seconds since the top of the minute (normally
Packit Service 82fcde
in the range @code{0} through @code{59}, but the actual upper limit is
Packit Service 82fcde
@code{60}, to allow for leap seconds if leap second support is
Packit Service 82fcde
available).
Packit Service 82fcde
@cindex leap second
Packit Service 82fcde
Packit Service 82fcde
@item int tm_min
Packit Service 82fcde
This is the number of full minutes since the top of the hour (in the
Packit Service 82fcde
range @code{0} through @code{59}).
Packit Service 82fcde
Packit Service 82fcde
@item int tm_hour
Packit Service 82fcde
This is the number of full hours past midnight (in the range @code{0} through
Packit Service 82fcde
@code{23}).
Packit Service 82fcde
Packit Service 82fcde
@item int tm_mday
Packit Service 82fcde
This is the ordinal day of the month (in the range @code{1} through @code{31}).
Packit Service 82fcde
Watch out for this one!  As the only ordinal number in the structure, it is
Packit Service 82fcde
inconsistent with the rest of the structure.
Packit Service 82fcde
Packit Service 82fcde
@item int tm_mon
Packit Service 82fcde
This is the number of full calendar months since the beginning of the
Packit Service 82fcde
year (in the range @code{0} through @code{11}).  Watch out for this one!
Packit Service 82fcde
People usually use ordinal numbers for month-of-year (where January = 1).
Packit Service 82fcde
Packit Service 82fcde
@item int tm_year
Packit Service 82fcde
This is the number of full calendar years since 1900.
Packit Service 82fcde
Packit Service 82fcde
@item int tm_wday
Packit Service 82fcde
This is the number of full days since Sunday (in the range @code{0} through
Packit Service 82fcde
@code{6}).
Packit Service 82fcde
Packit Service 82fcde
@item int tm_yday
Packit Service 82fcde
This is the number of full days since the beginning of the year (in the
Packit Service 82fcde
range @code{0} through @code{365}).
Packit Service 82fcde
Packit Service 82fcde
@item int tm_isdst
Packit Service 82fcde
@cindex Daylight Saving Time
Packit Service 82fcde
@cindex summer time
Packit Service 82fcde
This is a flag that indicates whether Daylight Saving Time is (or was, or
Packit Service 82fcde
will be) in effect at the time described.  The value is positive if
Packit Service 82fcde
Daylight Saving Time is in effect, zero if it is not, and negative if the
Packit Service 82fcde
information is not available.
Packit Service 82fcde
Packit Service 82fcde
@item long int tm_gmtoff
Packit Service 82fcde
This field describes the time zone that was used to compute this
Packit Service 82fcde
broken-down time value, including any adjustment for daylight saving; it
Packit Service 82fcde
is the number of seconds that you must add to UTC to get local time.
Packit Service 82fcde
You can also think of this as the number of seconds east of UTC.  For
Packit Service 82fcde
example, for U.S. Eastern Standard Time, the value is @code{-5*60*60}.
Packit Service 82fcde
The @code{tm_gmtoff} field is derived from BSD and is a GNU library
Packit Service 82fcde
extension; it is not visible in a strict @w{ISO C} environment.
Packit Service 82fcde
Packit Service 82fcde
@item const char *tm_zone
Packit Service 82fcde
This field is the name for the time zone that was used to compute this
Packit Service 82fcde
broken-down time value.  Like @code{tm_gmtoff}, this field is a BSD and
Packit Service 82fcde
GNU extension, and is not visible in a strict @w{ISO C} environment.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {struct tm *} localtime (const time_t *@var{time})
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:tmbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c Calls tz_convert with a static buffer.
Packit Service 82fcde
@c localtime @mtasurace:tmbuf @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  tz_convert dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
The @code{localtime} function converts the simple time pointed to by
Packit Service 82fcde
@var{time} to broken-down time representation, expressed relative to the
Packit Service 82fcde
user's specified time zone.
Packit Service 82fcde
Packit Service 82fcde
The return value is a pointer to a static broken-down time structure, which
Packit Service 82fcde
might be overwritten by subsequent calls to @code{ctime}, @code{gmtime},
Packit Service 82fcde
or @code{localtime}.  (But no other library function overwrites the contents
Packit Service 82fcde
of this object.)
Packit Service 82fcde
Packit Service 82fcde
The return value is the null pointer if @var{time} cannot be represented
Packit Service 82fcde
as a broken-down time; typically this is because the year cannot fit into
Packit Service 82fcde
an @code{int}.
Packit Service 82fcde
Packit Service 82fcde
Calling @code{localtime} also sets the current time zone as if
Packit Service 82fcde
@code{tzset} were called.  @xref{Time Zone Functions}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Using the @code{localtime} function is a big problem in multi-threaded
Packit Service 82fcde
programs.  The result is returned in a static buffer and this is used in
Packit Service 82fcde
all threads.  POSIX.1c introduced a variant of this function.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {struct tm *} localtime_r (const time_t *@var{time}, struct tm *@var{resultp})
Packit Service 82fcde
@standards{POSIX.1c, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c localtime_r @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  tz_convert(use_localtime) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c   libc_lock_lock dup @asulock @aculock
Packit Service 82fcde
@c   tzset_internal @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c     always called with tzset_lock held
Packit Service 82fcde
@c     sets static is_initialized before initialization;
Packit Service 82fcde
@c     reads and sets old_tz; sets tz_rules.
Packit Service 82fcde
@c     some of the issues only apply on the first call.
Packit Service 82fcde
@c     subsequent calls only trigger these when called by localtime;
Packit Service 82fcde
@c     otherwise, they're ok.
Packit Service 82fcde
@c    getenv dup @mtsenv
Packit Service 82fcde
@c    strcmp dup ok
Packit Service 82fcde
@c    strdup @ascuheap
Packit Service 82fcde
@c    tzfile_read @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c     memcmp dup ok
Packit Service 82fcde
@c     strstr dup ok
Packit Service 82fcde
@c     getenv dup @mtsenv
Packit Service 82fcde
@c     asprintf dup @mtslocale @ascuheap @acsmem
Packit Service 82fcde
@c     stat64 dup ok
Packit Service 82fcde
@c     fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
Packit Service 82fcde
@c     fileno dup ok
Packit Service 82fcde
@c     fstat64 dup ok
Packit Service 82fcde
@c     fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c     free dup @ascuheap @acsmem
Packit Service 82fcde
@c     fsetlocking dup ok [no @mtasurace:stream @asulock, exclusive]
Packit Service 82fcde
@c     fread_unlocked dup ok [no @mtasurace:stream @asucorrupt @acucorrupt]
Packit Service 82fcde
@c     memcpy dup ok
Packit Service 82fcde
@c     decode ok
Packit Service 82fcde
@c      bswap_32 dup ok
Packit Service 82fcde
@c     fseek dup ok [no @mtasurace:stream @asucorrupt @acucorrupt]
Packit Service 82fcde
@c     ftello dup ok [no @mtasurace:stream @asucorrupt @acucorrupt]
Packit Service 82fcde
@c     malloc dup @ascuheap @acsmem
Packit Service 82fcde
@c     decode64 ok
Packit Service 82fcde
@c      bswap_64 dup ok
Packit Service 82fcde
@c     getc_unlocked ok [no @mtasurace:stream @asucorrupt @acucorrupt]
Packit Service 82fcde
@c     tzstring dup @ascuheap @acsmem
Packit Service 82fcde
@c     compute_tzname_max dup ok [guarded by tzset_lock]
Packit Service 82fcde
@c    memset dup ok
Packit Service 82fcde
@c    update_vars ok [guarded by tzset_lock]
Packit Service 82fcde
@c      sets daylight, timezone, tzname and tzname_cur_max;
Packit Service 82fcde
@c      called only with tzset_lock held, unless tzset_parse_tz
Packit Service 82fcde
@c      (internal, but not static) gets called by users; given the its
Packit Service 82fcde
@c      double-underscore-prefixed name, this interface violation could
Packit Service 82fcde
@c      be regarded as undefined behavior.
Packit Service 82fcde
@c     strlen ok
Packit Service 82fcde
@c    tzset_parse_tz @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c     sscanf dup @mtslocale @ascuheap @acsmem
Packit Service 82fcde
@c     isalnum dup @mtsenv
Packit Service 82fcde
@c     tzstring @ascuheap @acsmem
Packit Service 82fcde
@c       reads and changes tzstring_list without synchronization, but
Packit Service 82fcde
@c       only called with tzset_lock held (save for interface violations)
Packit Service 82fcde
@c      strlen dup ok
Packit Service 82fcde
@c      malloc dup @ascuheap @acsmem
Packit Service 82fcde
@c      strcpy dup ok
Packit Service 82fcde
@c     isdigit dup @mtslocale
Packit Service 82fcde
@c     compute_offset ok
Packit Service 82fcde
@c     tzfile_default @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c       sets tzname, timezone, types, zone_names, rule_*off, etc; no guards
Packit Service 82fcde
@c      strlen dup ok
Packit Service 82fcde
@c      tzfile_read dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c      mempcpy dup ok
Packit Service 82fcde
@c      compute_tzname_max ok [if guarded by tzset_lock]
Packit Service 82fcde
@c        iterates over zone_names; no guards
Packit Service 82fcde
@c     free dup @ascuheap @acsmem
Packit Service 82fcde
@c     strtoul dup @mtslocale
Packit Service 82fcde
@c     update_vars dup ok
Packit Service 82fcde
@c   tzfile_compute(use_localtime) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c     sets tzname; no guards.  with !use_localtime, as in gmtime, it's ok
Packit Service 82fcde
@c    tzstring dup @acsuheap @acsmem
Packit Service 82fcde
@c    tzset_parse_tz dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    offtime dup ok
Packit Service 82fcde
@c    tz_compute dup ok
Packit Service 82fcde
@c    strcmp dup ok
Packit Service 82fcde
@c   offtime ok
Packit Service 82fcde
@c    isleap dup ok
Packit Service 82fcde
@c   tz_compute ok
Packit Service 82fcde
@c    compute_change ok
Packit Service 82fcde
@c     isleap ok
Packit Service 82fcde
@c   libc_lock_unlock dup @aculock
Packit Service 82fcde
Packit Service 82fcde
The @code{localtime_r} function works just like the @code{localtime}
Packit Service 82fcde
function.  It takes a pointer to a variable containing a simple time
Packit Service 82fcde
and converts it to the broken-down time format.
Packit Service 82fcde
Packit Service 82fcde
But the result is not placed in a static buffer.  Instead it is placed
Packit Service 82fcde
in the object of type @code{struct tm} to which the parameter
Packit Service 82fcde
@var{resultp} points.
Packit Service 82fcde
Packit Service 82fcde
If the conversion is successful the function returns a pointer to the
Packit Service 82fcde
object the result was written into, i.e., it returns @var{resultp}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {struct tm *} gmtime (const time_t *@var{time})
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:tmbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c gmtime @mtasurace:tmbuf @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  tz_convert dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
This function is similar to @code{localtime}, except that the broken-down
Packit Service 82fcde
time is expressed as Coordinated Universal Time (UTC) (formerly called
Packit Service 82fcde
Greenwich Mean Time (GMT)) rather than relative to a local time zone.
Packit Service 82fcde
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
As for the @code{localtime} function we have the problem that the result
Packit Service 82fcde
is placed in a static variable.  POSIX.1c also provides a replacement for
Packit Service 82fcde
@code{gmtime}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {struct tm *} gmtime_r (const time_t *@var{time}, struct tm *@var{resultp})
Packit Service 82fcde
@standards{POSIX.1c, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c You'd think tz_convert could avoid some safety issues with
Packit Service 82fcde
@c !use_localtime, but no such luck: tzset_internal will always bring
Packit Service 82fcde
@c about all possible AS and AC problems when it's first called.
Packit Service 82fcde
@c Calling any of localtime,gmtime_r once would run the initialization
Packit Service 82fcde
@c and avoid the heap, mem and fd issues in gmtime* in subsequent calls,
Packit Service 82fcde
@c but the unsafe locking would remain.
Packit Service 82fcde
@c gmtime_r @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  tz_convert(gmtime_r) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
This function is similar to @code{localtime_r}, except that it converts
Packit Service 82fcde
just like @code{gmtime} the given time as Coordinated Universal Time.
Packit Service 82fcde
Packit Service 82fcde
If the conversion is successful the function returns a pointer to the
Packit Service 82fcde
object the result was written into, i.e., it returns @var{resultp}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@deftypefun time_t mktime (struct tm *@var{brokentime})
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c mktime @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c   passes a static localtime_offset to mktime_internal; it is read
Packit Service 82fcde
@c   once, used as an initial guess, and updated at the end, but not
Packit Service 82fcde
@c   used except as a guess for subsequent calls, so it should be safe.
Packit Service 82fcde
@c   Even though a compiler might delay the load and perform it multiple
Packit Service 82fcde
@c   times (bug 16346), there are at least two unconditional uses of the
Packit Service 82fcde
@c   auto variable in which the first load is stored, separated by a
Packit Service 82fcde
@c   call to an external function, and a conditional change of the
Packit Service 82fcde
@c   variable before the external call, so refraining from allocating a
Packit Service 82fcde
@c   local variable at the first load would be a very bad optimization.
Packit Service 82fcde
@c  tzset dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  mktime_internal(localtime_r) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c   ydhms_diff ok
Packit Service 82fcde
@c   ranged_convert(localtime_r) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    *convert = localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    time_t_avg dup ok
Packit Service 82fcde
@c   guess_time_tm dup ok
Packit Service 82fcde
@c    ydhms_diff dup ok
Packit Service 82fcde
@c    time_t_add_ok ok
Packit Service 82fcde
@c     time_t_avg ok
Packit Service 82fcde
@c   isdst_differ ok
Packit Service 82fcde
@c   time_t_int_add_ok ok
Packit Service 82fcde
The @code{mktime} function converts a broken-down time structure to a
Packit Service 82fcde
simple time representation.  It also normalizes the contents of the
Packit Service 82fcde
broken-down time structure, and fills in some components based on the
Packit Service 82fcde
values of the others.
Packit Service 82fcde
Packit Service 82fcde
The @code{mktime} function ignores the specified contents of the
Packit Service 82fcde
@code{tm_wday}, @code{tm_yday}, @code{tm_gmtoff}, and @code{tm_zone}
Packit Service 82fcde
members of the broken-down time
Packit Service 82fcde
structure.  It uses the values of the other components to determine the
Packit Service 82fcde
calendar time; it's permissible for these components to have
Packit Service 82fcde
unnormalized values outside their normal ranges.  The last thing that
Packit Service 82fcde
@code{mktime} does is adjust the components of the @var{brokentime}
Packit Service 82fcde
structure, including the members that were initially ignored.
Packit Service 82fcde
Packit Service 82fcde
If the specified broken-down time cannot be represented as a simple time,
Packit Service 82fcde
@code{mktime} returns a value of @code{(time_t)(-1)} and does not modify
Packit Service 82fcde
the contents of @var{brokentime}.
Packit Service 82fcde
Packit Service 82fcde
Calling @code{mktime} also sets the current time zone as if
Packit Service 82fcde
@code{tzset} were called; @code{mktime} uses this information instead
Packit Service 82fcde
of @var{brokentime}'s initial @code{tm_gmtoff} and @code{tm_zone}
Packit Service 82fcde
members.  @xref{Time Zone Functions}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun time_t timelocal (struct tm *@var{brokentime})
Packit Service 82fcde
@standards{???, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c Alias to mktime.
Packit Service 82fcde
Packit Service 82fcde
@code{timelocal} is functionally identical to @code{mktime}, but more
Packit Service 82fcde
mnemonically named.  Note that it is the inverse of the @code{localtime}
Packit Service 82fcde
function.
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability note:}  @code{mktime} is essentially universally
Packit Service 82fcde
available.  @code{timelocal} is rather rare.
Packit Service 82fcde
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun time_t timegm (struct tm *@var{brokentime})
Packit Service 82fcde
@standards{???, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c timegm @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c   gmtime_offset triggers the same caveats as localtime_offset in mktime.
Packit Service 82fcde
@c   although gmtime_r, as called by mktime, might save some issues,
Packit Service 82fcde
@c   tzset calls tzset_internal with always, which forces
Packit Service 82fcde
@c   reinitialization, so all issues may arise.
Packit Service 82fcde
@c  tzset dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  mktime_internal(gmtime_r) @asulock @aculock
Packit Service 82fcde
@c ..gmtime_r @asulock @aculock
Packit Service 82fcde
@c    ... dup ok
Packit Service 82fcde
@c    tz_convert(!use_localtime) @asulock @aculock
Packit Service 82fcde
@c     ... dup @asulock @aculock
Packit Service 82fcde
@c     tzfile_compute(!use_localtime) ok
Packit Service 82fcde
Packit Service 82fcde
@code{timegm} is functionally identical to @code{mktime} except it
Packit Service 82fcde
always takes the input values to be Coordinated Universal Time (UTC)
Packit Service 82fcde
regardless of any local time zone setting.
Packit Service 82fcde
Packit Service 82fcde
Note that @code{timegm} is the inverse of @code{gmtime}.
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability note:}  @code{mktime} is essentially universally
Packit Service 82fcde
available.  @code{timegm} is rather rare.  For the most portable
Packit Service 82fcde
conversion from a UTC broken-down time to a simple time, set
Packit Service 82fcde
the @code{TZ} environment variable to UTC, call @code{mktime}, then set
Packit Service 82fcde
@code{TZ} back.
Packit Service 82fcde
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node High Accuracy Clock
Packit Service 82fcde
@subsection High Accuracy Clock
Packit Service 82fcde
Packit Service 82fcde
@cindex time, high precision
Packit Service 82fcde
@cindex clock, high accuracy
Packit Service 82fcde
@pindex sys/timex.h
Packit Service 82fcde
@c On Linux, GNU libc implements ntp_gettime() and npt_adjtime() as calls
Packit Service 82fcde
@c to adjtimex().
Packit Service 82fcde
The @code{ntp_gettime} and @code{ntp_adjtime} functions provide an
Packit Service 82fcde
interface to monitor and manipulate the system clock to maintain high
Packit Service 82fcde
accuracy time.  For example, you can fine tune the speed of the clock
Packit Service 82fcde
or synchronize it with another time source.
Packit Service 82fcde
Packit Service 82fcde
A typical use of these functions is by a server implementing the Network
Packit Service 82fcde
Time Protocol to synchronize the clocks of multiple systems and high
Packit Service 82fcde
precision clocks.
Packit Service 82fcde
Packit Service 82fcde
These functions are declared in @file{sys/timex.h}.
Packit Service 82fcde
Packit Service 82fcde
@tindex struct ntptimeval
Packit Service 82fcde
@deftp {Data Type} {struct ntptimeval}
Packit Service 82fcde
This structure is used for information about the system clock.  It
Packit Service 82fcde
contains the following members:
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item struct timeval time
Packit Service 82fcde
This is the current calendar time, expressed as the elapsed time since
Packit Service 82fcde
the epoch.  The @code{struct timeval} data type is described in
Packit Service 82fcde
@ref{Elapsed Time}.
Packit Service 82fcde
Packit Service 82fcde
@item long int maxerror
Packit Service 82fcde
This is the maximum error, measured in microseconds.  Unless updated
Packit Service 82fcde
via @code{ntp_adjtime} periodically, this value will reach some
Packit Service 82fcde
platform-specific maximum value.
Packit Service 82fcde
Packit Service 82fcde
@item long int esterror
Packit Service 82fcde
This is the estimated error, measured in microseconds.  This value can
Packit Service 82fcde
be set by @code{ntp_adjtime} to indicate the estimated offset of the
Packit Service 82fcde
system clock from the true calendar time.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int ntp_gettime (struct ntptimeval *@var{tptr})
Packit Service 82fcde
@standards{GNU, sys/timex.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Wrapper for adjtimex.
Packit Service 82fcde
The @code{ntp_gettime} function sets the structure pointed to by
Packit Service 82fcde
@var{tptr} to current values.  The elements of the structure afterwards
Packit Service 82fcde
contain the values the timer implementation in the kernel assumes.  They
Packit Service 82fcde
might or might not be correct.  If they are not, an @code{ntp_adjtime}
Packit Service 82fcde
call is necessary.
Packit Service 82fcde
Packit Service 82fcde
The return value is @code{0} on success and other values on failure.  The
Packit Service 82fcde
following @code{errno} error conditions are defined for this function:
Packit Service 82fcde
Packit Service 82fcde
@vtable @code
Packit Service 82fcde
@item TIME_ERROR
Packit Service 82fcde
The precision clock model is not properly set up at the moment, thus the
Packit Service 82fcde
clock must be considered unsynchronized, and the values should be
Packit Service 82fcde
treated with care.
Packit Service 82fcde
@end vtable
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@tindex struct timex
Packit Service 82fcde
@deftp {Data Type} {struct timex}
Packit Service 82fcde
This structure is used to control and monitor the system clock.  It
Packit Service 82fcde
contains the following members:
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item unsigned int modes
Packit Service 82fcde
This variable controls whether and which values are set.  Several
Packit Service 82fcde
symbolic constants have to be combined with @emph{binary or} to specify
Packit Service 82fcde
the effective mode.  These constants start with @code{MOD_}.
Packit Service 82fcde
Packit Service 82fcde
@item long int offset
Packit Service 82fcde
This value indicates the current offset of the system clock from the true
Packit Service 82fcde
calendar time.  The value is given in microseconds.  If bit
Packit Service 82fcde
@code{MOD_OFFSET} is set in @code{modes}, the offset (and possibly other
Packit Service 82fcde
dependent values) can be set.  The offset's absolute value must not
Packit Service 82fcde
exceed @code{MAXPHASE}.
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@item long int frequency
Packit Service 82fcde
This value indicates the difference in frequency between the true
Packit Service 82fcde
calendar time and the system clock.  The value is expressed as scaled
Packit Service 82fcde
PPM (parts per million, 0.0001%).  The scaling is @code{1 <<
Packit Service 82fcde
SHIFT_USEC}.  The value can be set with bit @code{MOD_FREQUENCY}, but
Packit Service 82fcde
the absolute value must not exceed @code{MAXFREQ}.
Packit Service 82fcde
Packit Service 82fcde
@item long int maxerror
Packit Service 82fcde
This is the maximum error, measured in microseconds.  A new value can be
Packit Service 82fcde
set using bit @code{MOD_MAXERROR}.  Unless updated via
Packit Service 82fcde
@code{ntp_adjtime} periodically, this value will increase steadily
Packit Service 82fcde
and reach some platform-specific maximum value.
Packit Service 82fcde
Packit Service 82fcde
@item long int esterror
Packit Service 82fcde
This is the estimated error, measured in microseconds.  This value can
Packit Service 82fcde
be set using bit @code{MOD_ESTERROR}.
Packit Service 82fcde
Packit Service 82fcde
@item int status
Packit Service 82fcde
This variable reflects the various states of the clock machinery.  There
Packit Service 82fcde
are symbolic constants for the significant bits, starting with
Packit Service 82fcde
@code{STA_}.  Some of these flags can be updated using the
Packit Service 82fcde
@code{MOD_STATUS} bit.
Packit Service 82fcde
Packit Service 82fcde
@item long int constant
Packit Service 82fcde
This value represents the bandwidth or stiffness of the PLL (phase
Packit Service 82fcde
locked loop) implemented in the kernel.  The value can be changed using
Packit Service 82fcde
bit @code{MOD_TIMECONST}.
Packit Service 82fcde
Packit Service 82fcde
@item long int precision
Packit Service 82fcde
This value represents the accuracy or the maximum error when reading the
Packit Service 82fcde
system clock.  The value is expressed in microseconds.
Packit Service 82fcde
Packit Service 82fcde
@item long int tolerance
Packit Service 82fcde
This value represents the maximum frequency error of the system clock in
Packit Service 82fcde
scaled PPM.  This value is used to increase the @code{maxerror} every
Packit Service 82fcde
second.
Packit Service 82fcde
Packit Service 82fcde
@item struct timeval time
Packit Service 82fcde
The current calendar time.
Packit Service 82fcde
Packit Service 82fcde
@item long int tick
Packit Service 82fcde
The elapsed time between clock ticks in microseconds.  A clock tick is a
Packit Service 82fcde
periodic timer interrupt on which the system clock is based.
Packit Service 82fcde
Packit Service 82fcde
@item long int ppsfreq
Packit Service 82fcde
This is the first of a few optional variables that are present only if
Packit Service 82fcde
the system clock can use a PPS (pulse per second) signal to discipline
Packit Service 82fcde
the system clock.  The value is expressed in scaled PPM and it denotes
Packit Service 82fcde
the difference in frequency between the system clock and the PPS signal.
Packit Service 82fcde
Packit Service 82fcde
@item long int jitter
Packit Service 82fcde
This value expresses a median filtered average of the PPS signal's
Packit Service 82fcde
dispersion in microseconds.
Packit Service 82fcde
Packit Service 82fcde
@item int shift
Packit Service 82fcde
This value is a binary exponent for the duration of the PPS calibration
Packit Service 82fcde
interval, ranging from @code{PPS_SHIFT} to @code{PPS_SHIFTMAX}.
Packit Service 82fcde
Packit Service 82fcde
@item long int stabil
Packit Service 82fcde
This value represents the median filtered dispersion of the PPS
Packit Service 82fcde
frequency in scaled PPM.
Packit Service 82fcde
Packit Service 82fcde
@item long int jitcnt
Packit Service 82fcde
This counter represents the number of pulses where the jitter exceeded
Packit Service 82fcde
the allowed maximum @code{MAXTIME}.
Packit Service 82fcde
Packit Service 82fcde
@item long int calcnt
Packit Service 82fcde
This counter reflects the number of successful calibration intervals.
Packit Service 82fcde
Packit Service 82fcde
@item long int errcnt
Packit Service 82fcde
This counter represents the number of calibration errors (caused by
Packit Service 82fcde
large offsets or jitter).
Packit Service 82fcde
Packit Service 82fcde
@item long int stbcnt
Packit Service 82fcde
This counter denotes the number of calibrations where the stability
Packit Service 82fcde
exceeded the threshold.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int ntp_adjtime (struct timex *@var{tptr})
Packit Service 82fcde
@standards{GNU, sys/timex.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Alias to adjtimex syscall.
Packit Service 82fcde
The @code{ntp_adjtime} function sets the structure specified by
Packit Service 82fcde
@var{tptr} to current values.
Packit Service 82fcde
Packit Service 82fcde
In addition, @code{ntp_adjtime} updates some settings to match what you
Packit Service 82fcde
pass to it in *@var{tptr}.  Use the @code{modes} element of *@var{tptr}
Packit Service 82fcde
to select what settings to update.  You can set @code{offset},
Packit Service 82fcde
@code{freq}, @code{maxerror}, @code{esterror}, @code{status},
Packit Service 82fcde
@code{constant}, and @code{tick}.
Packit Service 82fcde
Packit Service 82fcde
@code{modes} = zero means set nothing.
Packit Service 82fcde
Packit Service 82fcde
Only the superuser can update settings.
Packit Service 82fcde
Packit Service 82fcde
@c On Linux, ntp_adjtime() also does the adjtime() function if you set
Packit Service 82fcde
@c modes = ADJ_OFFSET_SINGLESHOT (in fact, that is how GNU libc implements
Packit Service 82fcde
@c adjtime()).  But this should be considered an internal function because
Packit Service 82fcde
@c it's so inconsistent with the rest of what ntp_adjtime() does and is
Packit Service 82fcde
@c forced in an ugly way into the struct timex.  So we don't document it
Packit Service 82fcde
@c and instead document adjtime() as the way to achieve the function.
Packit Service 82fcde
Packit Service 82fcde
The return value is @code{0} on success and other values on failure.  The
Packit Service 82fcde
following @code{errno} error conditions are defined for this function:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item TIME_ERROR
Packit Service 82fcde
The high accuracy clock model is not properly set up at the moment, thus the
Packit Service 82fcde
clock must be considered unsynchronized, and the values should be
Packit Service 82fcde
treated with care.  Another reason could be that the specified new values
Packit Service 82fcde
are not allowed.
Packit Service 82fcde
Packit Service 82fcde
@item EPERM
Packit Service 82fcde
The process specified a settings update, but is not superuser.
Packit Service 82fcde
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
For more details see RFC1305 (Network Time Protocol, Version 3) and
Packit Service 82fcde
related documents.
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability note:} Early versions of @theglibc{} did not
Packit Service 82fcde
have this function but did have the synonymous @code{adjtimex}.
Packit Service 82fcde
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Formatting Calendar Time
Packit Service 82fcde
@subsection Formatting Calendar Time
Packit Service 82fcde
Packit Service 82fcde
The functions described in this section format calendar time values as
Packit Service 82fcde
strings.  These functions are declared in the header file @file{time.h}.
Packit Service 82fcde
@pindex time.h
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {char *} asctime (const struct tm *@var{brokentime})
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:asctime} @mtslocale{}}@asunsafe{}@acsafe{}}
Packit Service 82fcde
@c asctime @mtasurace:asctime @mtslocale
Packit Service 82fcde
@c   Uses a static buffer.
Packit Service 82fcde
@c  asctime_internal @mtslocale
Packit Service 82fcde
@c   snprintf dup @mtslocale [no @acsuheap @acsmem]
Packit Service 82fcde
@c   ab_day_name @mtslocale
Packit Service 82fcde
@c   ab_month_name @mtslocale
Packit Service 82fcde
The @code{asctime} function converts the broken-down time value that
Packit Service 82fcde
@var{brokentime} points to into a string in a standard format:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
"Tue May 21 13:46:22 1991\n"
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The abbreviations for the days of week are: @samp{Sun}, @samp{Mon},
Packit Service 82fcde
@samp{Tue}, @samp{Wed}, @samp{Thu}, @samp{Fri}, and @samp{Sat}.
Packit Service 82fcde
Packit Service 82fcde
The abbreviations for the months are: @samp{Jan}, @samp{Feb},
Packit Service 82fcde
@samp{Mar}, @samp{Apr}, @samp{May}, @samp{Jun}, @samp{Jul}, @samp{Aug},
Packit Service 82fcde
@samp{Sep}, @samp{Oct}, @samp{Nov}, and @samp{Dec}.
Packit Service 82fcde
Packit Service 82fcde
The return value points to a statically allocated string, which might be
Packit Service 82fcde
overwritten by subsequent calls to @code{asctime} or @code{ctime}.
Packit Service 82fcde
(But no other library function overwrites the contents of this
Packit Service 82fcde
string.)
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {char *} asctime_r (const struct tm *@var{brokentime}, char *@var{buffer})
Packit Service 82fcde
@standards{POSIX.1c, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c asctime_r @mtslocale
Packit Service 82fcde
@c  asctime_internal dup @mtslocale
Packit Service 82fcde
This function is similar to @code{asctime} but instead of placing the
Packit Service 82fcde
result in a static buffer it writes the string in the buffer pointed to
Packit Service 82fcde
by the parameter @var{buffer}.  This buffer should have room
Packit Service 82fcde
for at least 26 bytes, including the terminating null.
Packit Service 82fcde
Packit Service 82fcde
If no error occurred the function returns a pointer to the string the
Packit Service 82fcde
result was written into, i.e., it returns @var{buffer}.  Otherwise
Packit Service 82fcde
it returns @code{NULL}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {char *} ctime (const time_t *@var{time})
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:tmbuf} @mtasurace{:asctime} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c ctime @mtasurace:tmbuf @mtasurace:asctime @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  localtime dup @mtasurace:tmbuf @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  asctime dup @mtasurace:asctime @mtslocale
Packit Service 82fcde
The @code{ctime} function is similar to @code{asctime}, except that you
Packit Service 82fcde
specify the calendar time argument as a @code{time_t} simple time value
Packit Service 82fcde
rather than in broken-down local time format.  It is equivalent to
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
asctime (localtime (@var{time}))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Calling @code{ctime} also sets the current time zone as if
Packit Service 82fcde
@code{tzset} were called.  @xref{Time Zone Functions}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {char *} ctime_r (const time_t *@var{time}, char *@var{buffer})
Packit Service 82fcde
@standards{POSIX.1c, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c ctime_r @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  asctime_r dup @mtslocale
Packit Service 82fcde
This function is similar to @code{ctime}, but places the result in the
Packit Service 82fcde
string pointed to by @var{buffer}.  It is equivalent to (written using
Packit Service 82fcde
gcc extensions, @pxref{Statement Exprs,,,gcc,Porting and Using gcc}):
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
(@{ struct tm tm; asctime_r (localtime_r (time, &tm), buf); @})
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
If no error occurred the function returns a pointer to the string the
Packit Service 82fcde
result was written into, i.e., it returns @var{buffer}.  Otherwise
Packit Service 82fcde
it returns @code{NULL}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@deftypefun size_t strftime (char *@var{s}, size_t @var{size}, const char *@var{template}, const struct tm *@var{brokentime})
Packit Service 82fcde
@standards{ISO, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c strftime @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit Service 82fcde
@c  strftime_l @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit Service 82fcde
@c   strftime_internal @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit Service 82fcde
@c    add ok
Packit Service 82fcde
@c     memset_zero dup ok
Packit Service 82fcde
@c     memset_space dup ok
Packit Service 82fcde
@c    strlen dup ok
Packit Service 82fcde
@c    mbrlen @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd [no @mtasurace:mbstate/!ps]
Packit Service 82fcde
@c    mbsinit dup ok
Packit Service 82fcde
@c    cpy ok
Packit Service 82fcde
@c     add dup ok
Packit Service 82fcde
@c     memcpy_lowcase ok
Packit Service 82fcde
@c      TOLOWER ok
Packit Service 82fcde
@c       tolower_l ok
Packit Service 82fcde
@c     memcpy_uppcase ok
Packit Service 82fcde
@c      TOUPPER ok
Packit Service 82fcde
@c       toupper_l ok
Packit Service 82fcde
@c     MEMCPY ok
Packit Service 82fcde
@c      memcpy dup ok
Packit Service 82fcde
@c    ISDIGIT ok
Packit Service 82fcde
@c    STRLEN ok
Packit Service 82fcde
@c     strlen dup ok
Packit Service 82fcde
@c    strftime_internal dup @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit Service 82fcde
@c    TOUPPER dup ok
Packit Service 82fcde
@c    nl_get_era_entry @ascuheap @asulock @acsmem @aculock
Packit Service 82fcde
@c     nl_init_era_entries @ascuheap @asulock @acsmem @aculock
Packit Service 82fcde
@c      libc_rwlock_wrlock dup @asulock @aculock
Packit Service 82fcde
@c      malloc dup @ascuheap @acsmem
Packit Service 82fcde
@c      memset dup ok
Packit Service 82fcde
@c      free dup @ascuheap @acsmem
Packit Service 82fcde
@c      realloc dup @ascuheap @acsmem
Packit Service 82fcde
@c      memcpy dup ok
Packit Service 82fcde
@c      strchr dup ok
Packit Service 82fcde
@c      wcschr dup ok
Packit Service 82fcde
@c      libc_rwlock_unlock dup @asulock @aculock
Packit Service 82fcde
@c     ERA_DATE_CMP ok
Packit Service 82fcde
@c    DO_NUMBER ok
Packit Service 82fcde
@c    DO_NUMBER_SPACEPAD ok
Packit Service 82fcde
@c    nl_get_alt_digit @ascuheap @asulock @acsmem @aculock
Packit Service 82fcde
@c     libc_rwlock_wrlock dup @asulock @aculock
Packit Service 82fcde
@c     nl_init_alt_digit @ascuheap @acsmem
Packit Service 82fcde
@c      malloc dup @ascuheap @acsmem
Packit Service 82fcde
@c      memset dup ok
Packit Service 82fcde
@c      strchr dup ok
Packit Service 82fcde
@c     libc_rwlock_unlock dup @aculock
Packit Service 82fcde
@c    memset_space ok
Packit Service 82fcde
@c     memset dup ok
Packit Service 82fcde
@c    memset_zero ok
Packit Service 82fcde
@c     memset dup ok
Packit Service 82fcde
@c    mktime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    iso_week_days ok
Packit Service 82fcde
@c    isleap ok
Packit Service 82fcde
@c    tzset dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    gmtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    tm_diff ok
Packit Service 82fcde
This function is similar to the @code{sprintf} function (@pxref{Formatted
Packit Service 82fcde
Input}), but the conversion specifications that can appear in the format
Packit Service 82fcde
template @var{template} are specialized for printing components of the date
Packit Service 82fcde
and time @var{brokentime} according to the locale currently specified for
Packit Service 82fcde
time conversion (@pxref{Locales}) and the current time zone
Packit Service 82fcde
(@pxref{Time Zone Functions}).
Packit Service 82fcde
Packit Service 82fcde
Ordinary characters appearing in the @var{template} are copied to the
Packit Service 82fcde
output string @var{s}; this can include multibyte character sequences.
Packit Service 82fcde
Conversion specifiers are introduced by a @samp{%} character, followed
Packit Service 82fcde
by an optional flag which can be one of the following.  These flags
Packit Service 82fcde
are all GNU extensions.  The first three affect only the output of
Packit Service 82fcde
numbers:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item _
Packit Service 82fcde
The number is padded with spaces.
Packit Service 82fcde
Packit Service 82fcde
@item -
Packit Service 82fcde
The number is not padded at all.
Packit Service 82fcde
Packit Service 82fcde
@item 0
Packit Service 82fcde
The number is padded with zeros even if the format specifies padding
Packit Service 82fcde
with spaces.
Packit Service 82fcde
Packit Service 82fcde
@item ^
Packit Service 82fcde
The output uses uppercase characters, but only if this is possible
Packit Service 82fcde
(@pxref{Case Conversion}).
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
The default action is to pad the number with zeros to keep it a constant
Packit Service 82fcde
width.  Numbers that do not have a range indicated below are never
Packit Service 82fcde
padded, since there is no natural width for them.
Packit Service 82fcde
Packit Service 82fcde
Following the flag an optional specification of the width is possible.
Packit Service 82fcde
This is specified in decimal notation.  If the natural size of the
Packit Service 82fcde
output of the field has less than the specified number of characters,
Packit Service 82fcde
the result is written right adjusted and space padded to the given
Packit Service 82fcde
size.
Packit Service 82fcde
Packit Service 82fcde
An optional modifier can follow the optional flag and width
Packit Service 82fcde
specification.  The modifiers, which were first standardized by
Packit Service 82fcde
POSIX.2-1992 and by @w{ISO C99}, are:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item E
Packit Service 82fcde
Use the locale's alternate representation for date and time.  This
Packit Service 82fcde
modifier applies to the @code{%c}, @code{%C}, @code{%x}, @code{%X},
Packit Service 82fcde
@code{%y} and @code{%Y} format specifiers.  In a Japanese locale, for
Packit Service 82fcde
example, @code{%Ex} might yield a date format based on the Japanese
Packit Service 82fcde
Emperors' reigns.
Packit Service 82fcde
Packit Service 82fcde
@item O
Packit Service 82fcde
With all format specifiers that produce numbers: use the locale's
Packit Service 82fcde
alternate numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
With @code{%B}, @code{%b}, and @code{%h}: use the grammatical form for
Packit Service 82fcde
month names that is appropriate when the month is named by itself,
Packit Service 82fcde
rather than the form that is appropriate when the month is used as
Packit Service 82fcde
part of a complete date.  This is a GNU extension.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
If the format supports the modifier but no alternate representation
Packit Service 82fcde
is available, it is ignored.
Packit Service 82fcde
Packit Service 82fcde
The conversion specifier ends with a format specifier taken from the
Packit Service 82fcde
following list.  The whole @samp{%} sequence is replaced in the output
Packit Service 82fcde
string as follows:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item %a
Packit Service 82fcde
The abbreviated weekday name according to the current locale.
Packit Service 82fcde
Packit Service 82fcde
@item %A
Packit Service 82fcde
The full weekday name according to the current locale.
Packit Service 82fcde
Packit Service 82fcde
@item %b
Packit Service 82fcde
The abbreviated month name according to the current locale, in the
Packit Service 82fcde
grammatical form used when the month is part of a complete date.
Packit Service 82fcde
As a GNU extension, the @code{O} modifier can be used (@code{%Ob})
Packit Service 82fcde
to get the grammatical form used when the month is named by itself.
Packit Service 82fcde
Packit Service 82fcde
@item %B
Packit Service 82fcde
The full month name according to the current locale, in the
Packit Service 82fcde
grammatical form used when the month is part of a complete date.
Packit Service 82fcde
As a GNU extension, the @code{O} modifier can be used (@code{%OB})
Packit Service 82fcde
to get the grammatical form used when the month is named by itself.
Packit Service 82fcde
Packit Service 82fcde
Note that not all languages need two different forms of the month
Packit Service 82fcde
names, so the text produced by @code{%B} and @code{%OB}, and by
Packit Service 82fcde
@code{%b} and @code{%Ob}, may or may not be the same, depending on
Packit Service 82fcde
the locale.
Packit Service 82fcde
Packit Service 82fcde
@item %c
Packit Service 82fcde
The preferred calendar time representation for the current locale.
Packit Service 82fcde
Packit Service 82fcde
@item %C
Packit Service 82fcde
The century of the year.  This is equivalent to the greatest integer not
Packit Service 82fcde
greater than the year divided by 100.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
Packit Service 82fcde
@item %d
Packit Service 82fcde
The day of the month as a decimal number (range @code{01} through @code{31}).
Packit Service 82fcde
Packit Service 82fcde
@item %D
Packit Service 82fcde
The date using the format @code{%m/%d/%y}.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
Packit Service 82fcde
@item %e
Packit Service 82fcde
The day of the month like with @code{%d}, but padded with spaces (range
Packit Service 82fcde
@code{ 1} through @code{31}).
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
Packit Service 82fcde
@item %F
Packit Service 82fcde
The date using the format @code{%Y-%m-%d}.  This is the form specified
Packit Service 82fcde
in the @w{ISO 8601} standard and is the preferred form for all uses.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by @w{ISO C99} and by POSIX.1-2001.
Packit Service 82fcde
Packit Service 82fcde
@item %g
Packit Service 82fcde
The year corresponding to the ISO week number, but without the century
Packit Service 82fcde
(range @code{00} through @code{99}).  This has the same format and value
Packit Service 82fcde
as @code{%y}, except that if the ISO week number (see @code{%V}) belongs
Packit Service 82fcde
to the previous or next year, that year is used instead.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by @w{ISO C99} and by POSIX.1-2001.
Packit Service 82fcde
Packit Service 82fcde
@item %G
Packit Service 82fcde
The year corresponding to the ISO week number.  This has the same format
Packit Service 82fcde
and value as @code{%Y}, except that if the ISO week number (see
Packit Service 82fcde
@code{%V}) belongs to the previous or next year, that year is used
Packit Service 82fcde
instead.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by @w{ISO C99} and by POSIX.1-2001
Packit Service 82fcde
but was previously available as a GNU extension.
Packit Service 82fcde
Packit Service 82fcde
@item %h
Packit Service 82fcde
The abbreviated month name according to the current locale.  The action
Packit Service 82fcde
is the same as for @code{%b}.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
Packit Service 82fcde
@item %H
Packit Service 82fcde
The hour as a decimal number, using a 24-hour clock (range @code{00} through
Packit Service 82fcde
@code{23}).
Packit Service 82fcde
Packit Service 82fcde
@item %I
Packit Service 82fcde
The hour as a decimal number, using a 12-hour clock (range @code{01} through
Packit Service 82fcde
@code{12}).
Packit Service 82fcde
Packit Service 82fcde
@item %j
Packit Service 82fcde
The day of the year as a decimal number (range @code{001} through @code{366}).
Packit Service 82fcde
Packit Service 82fcde
@item %k
Packit Service 82fcde
The hour as a decimal number, using a 24-hour clock like @code{%H}, but
Packit Service 82fcde
padded with spaces (range @code{ 0} through @code{23}).
Packit Service 82fcde
Packit Service 82fcde
This format is a GNU extension.
Packit Service 82fcde
Packit Service 82fcde
@item %l
Packit Service 82fcde
The hour as a decimal number, using a 12-hour clock like @code{%I}, but
Packit Service 82fcde
padded with spaces (range @code{ 1} through @code{12}).
Packit Service 82fcde
Packit Service 82fcde
This format is a GNU extension.
Packit Service 82fcde
Packit Service 82fcde
@item %m
Packit Service 82fcde
The month as a decimal number (range @code{01} through @code{12}).
Packit Service 82fcde
Packit Service 82fcde
@item %M
Packit Service 82fcde
The minute as a decimal number (range @code{00} through @code{59}).
Packit Service 82fcde
Packit Service 82fcde
@item %n
Packit Service 82fcde
A single @samp{\n} (newline) character.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
Packit Service 82fcde
@item %p
Packit Service 82fcde
Either @samp{AM} or @samp{PM}, according to the given time value; or the
Packit Service 82fcde
corresponding strings for the current locale.  Noon is treated as
Packit Service 82fcde
@samp{PM} and midnight as @samp{AM}.  In most locales
Packit Service 82fcde
@samp{AM}/@samp{PM} format is not supported, in such cases @code{"%p"}
Packit Service 82fcde
yields an empty string.
Packit Service 82fcde
Packit Service 82fcde
@ignore
Packit Service 82fcde
We currently have a problem with makeinfo.  Write @samp{AM} and @samp{am}
Packit Service 82fcde
both results in `am'.  I.e., the difference in case is not visible anymore.
Packit Service 82fcde
@end ignore
Packit Service 82fcde
@item %P
Packit Service 82fcde
Either @samp{am} or @samp{pm}, according to the given time value; or the
Packit Service 82fcde
corresponding strings for the current locale, printed in lowercase
Packit Service 82fcde
characters.  Noon is treated as @samp{pm} and midnight as @samp{am}.  In
Packit Service 82fcde
most locales @samp{AM}/@samp{PM} format is not supported, in such cases
Packit Service 82fcde
@code{"%P"} yields an empty string.
Packit Service 82fcde
Packit Service 82fcde
This format is a GNU extension.
Packit Service 82fcde
Packit Service 82fcde
@item %r
Packit Service 82fcde
The complete calendar time using the AM/PM format of the current locale.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
In the POSIX locale, this format is equivalent to @code{%I:%M:%S %p}.
Packit Service 82fcde
Packit Service 82fcde
@item %R
Packit Service 82fcde
The hour and minute in decimal numbers using the format @code{%H:%M}.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by @w{ISO C99} and by POSIX.1-2001
Packit Service 82fcde
but was previously available as a GNU extension.
Packit Service 82fcde
Packit Service 82fcde
@item %s
Packit Service 82fcde
The number of seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC.
Packit Service 82fcde
Leap seconds are not counted unless leap second support is available.
Packit Service 82fcde
Packit Service 82fcde
This format is a GNU extension.
Packit Service 82fcde
Packit Service 82fcde
@item %S
Packit Service 82fcde
The seconds as a decimal number (range @code{00} through @code{60}).
Packit Service 82fcde
Packit Service 82fcde
@item %t
Packit Service 82fcde
A single @samp{\t} (tabulator) character.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
Packit Service 82fcde
@item %T
Packit Service 82fcde
The time of day using decimal numbers using the format @code{%H:%M:%S}.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
Packit Service 82fcde
@item %u
Packit Service 82fcde
The day of the week as a decimal number (range @code{1} through
Packit Service 82fcde
@code{7}), Monday being @code{1}.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
Packit Service 82fcde
@item %U
Packit Service 82fcde
The week number of the current year as a decimal number (range @code{00}
Packit Service 82fcde
through @code{53}), starting with the first Sunday as the first day of
Packit Service 82fcde
the first week.  Days preceding the first Sunday in the year are
Packit Service 82fcde
considered to be in week @code{00}.
Packit Service 82fcde
Packit Service 82fcde
@item %V
Packit Service 82fcde
The @w{ISO 8601:1988} week number as a decimal number (range @code{01}
Packit Service 82fcde
through @code{53}).  ISO weeks start with Monday and end with Sunday.
Packit Service 82fcde
Week @code{01} of a year is the first week which has the majority of its
Packit Service 82fcde
days in that year; this is equivalent to the week containing the year's
Packit Service 82fcde
first Thursday, and it is also equivalent to the week containing January
Packit Service 82fcde
4.  Week @code{01} of a year can contain days from the previous year.
Packit Service 82fcde
The week before week @code{01} of a year is the last week (@code{52} or
Packit Service 82fcde
@code{53}) of the previous year even if it contains days from the new
Packit Service 82fcde
year.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by POSIX.2-1992 and by @w{ISO C99}.
Packit Service 82fcde
Packit Service 82fcde
@item %w
Packit Service 82fcde
The day of the week as a decimal number (range @code{0} through
Packit Service 82fcde
@code{6}), Sunday being @code{0}.
Packit Service 82fcde
Packit Service 82fcde
@item %W
Packit Service 82fcde
The week number of the current year as a decimal number (range @code{00}
Packit Service 82fcde
through @code{53}), starting with the first Monday as the first day of
Packit Service 82fcde
the first week.  All days preceding the first Monday in the year are
Packit Service 82fcde
considered to be in week @code{00}.
Packit Service 82fcde
Packit Service 82fcde
@item %x
Packit Service 82fcde
The preferred date representation for the current locale.
Packit Service 82fcde
Packit Service 82fcde
@item %X
Packit Service 82fcde
The preferred time of day representation for the current locale.
Packit Service 82fcde
Packit Service 82fcde
@item %y
Packit Service 82fcde
The year without a century as a decimal number (range @code{00} through
Packit Service 82fcde
@code{99}).  This is equivalent to the year modulo 100.
Packit Service 82fcde
Packit Service 82fcde
@item %Y
Packit Service 82fcde
The year as a decimal number, using the Gregorian calendar.  Years
Packit Service 82fcde
before the year @code{1} are numbered @code{0}, @code{-1}, and so on.
Packit Service 82fcde
Packit Service 82fcde
@item %z
Packit Service 82fcde
@w{RFC 822}/@w{ISO 8601:1988} style numeric time zone (e.g.,
Packit Service 82fcde
@code{-0600} or @code{+0100}), or nothing if no time zone is
Packit Service 82fcde
determinable.
Packit Service 82fcde
Packit Service 82fcde
This format was first standardized by @w{ISO C99} and by POSIX.1-2001
Packit Service 82fcde
but was previously available as a GNU extension.
Packit Service 82fcde
Packit Service 82fcde
In the POSIX locale, a full @w{RFC 822} timestamp is generated by the format
Packit Service 82fcde
@w{@samp{"%a, %d %b %Y %H:%M:%S %z"}} (or the equivalent
Packit Service 82fcde
@w{@samp{"%a, %d %b %Y %T %z"}}).
Packit Service 82fcde
Packit Service 82fcde
@item %Z
Packit Service 82fcde
The time zone abbreviation (empty if the time zone can't be determined).
Packit Service 82fcde
Packit Service 82fcde
@item %%
Packit Service 82fcde
A literal @samp{%} character.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
The @var{size} parameter can be used to specify the maximum number of
Packit Service 82fcde
characters to be stored in the array @var{s}, including the terminating
Packit Service 82fcde
null character.  If the formatted time requires more than @var{size}
Packit Service 82fcde
characters, @code{strftime} returns zero and the contents of the array
Packit Service 82fcde
@var{s} are undefined.  Otherwise the return value indicates the
Packit Service 82fcde
number of characters placed in the array @var{s}, not including the
Packit Service 82fcde
terminating null character.
Packit Service 82fcde
Packit Service 82fcde
@emph{Warning:} This convention for the return value which is prescribed
Packit Service 82fcde
in @w{ISO C} can lead to problems in some situations.  For certain
Packit Service 82fcde
format strings and certain locales the output really can be the empty
Packit Service 82fcde
string and this cannot be discovered by testing the return value only.
Packit Service 82fcde
E.g., in most locales the AM/PM time format is not supported (most of
Packit Service 82fcde
the world uses the 24 hour time representation).  In such locales
Packit Service 82fcde
@code{"%p"} will return the empty string, i.e., the return value is
Packit Service 82fcde
zero.  To detect situations like this something similar to the following
Packit Service 82fcde
code should be used:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
buf[0] = '\1';
Packit Service 82fcde
len = strftime (buf, bufsize, format, tp);
Packit Service 82fcde
if (len == 0 && buf[0] != '\0')
Packit Service 82fcde
  @{
Packit Service 82fcde
    /* Something went wrong in the strftime call.  */
Packit Service 82fcde
    @dots{}
Packit Service 82fcde
  @}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
If @var{s} is a null pointer, @code{strftime} does not actually write
Packit Service 82fcde
anything, but instead returns the number of characters it would have written.
Packit Service 82fcde
Packit Service 82fcde
Calling @code{strftime} also sets the current time zone as if
Packit Service 82fcde
@code{tzset} were called; @code{strftime} uses this information
Packit Service 82fcde
instead of @var{brokentime}'s @code{tm_gmtoff} and @code{tm_zone}
Packit Service 82fcde
members.  @xref{Time Zone Functions}.
Packit Service 82fcde
Packit Service 82fcde
For an example of @code{strftime}, see @ref{Time Functions Example}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun size_t wcsftime (wchar_t *@var{s}, size_t @var{size}, const wchar_t *@var{template}, const struct tm *@var{brokentime})
Packit Service 82fcde
@standards{ISO/Amend1, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c wcsftime @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit Service 82fcde
@c  wcsftime_l @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit Service 82fcde
@c   wcsftime_internal @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit Service 82fcde
@c    add ok
Packit Service 82fcde
@c     memset_zero dup ok
Packit Service 82fcde
@c     memset_space dup ok
Packit Service 82fcde
@c    wcslen dup ok
Packit Service 82fcde
@c    cpy ok
Packit Service 82fcde
@c     add dup ok
Packit Service 82fcde
@c     memcpy_lowcase ok
Packit Service 82fcde
@c      TOLOWER ok
Packit Service 82fcde
@c       towlower_l dup ok
Packit Service 82fcde
@c     memcpy_uppcase ok
Packit Service 82fcde
@c      TOUPPER ok
Packit Service 82fcde
@c       towupper_l dup ok
Packit Service 82fcde
@c     MEMCPY ok
Packit Service 82fcde
@c      wmemcpy dup ok
Packit Service 82fcde
@c    widen @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit Service 82fcde
@c     memset dup ok
Packit Service 82fcde
@c     mbsrtowcs_l @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd [no @mtasurace:mbstate/!ps]
Packit Service 82fcde
@c    ISDIGIT ok
Packit Service 82fcde
@c    STRLEN ok
Packit Service 82fcde
@c     wcslen dup ok
Packit Service 82fcde
@c    wcsftime_internal dup @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit Service 82fcde
@c    TOUPPER dup ok
Packit Service 82fcde
@c    nl_get_era_entry dup @ascuheap @asulock @acsmem @aculock
Packit Service 82fcde
@c    DO_NUMBER ok
Packit Service 82fcde
@c    DO_NUMBER_SPACEPAD ok
Packit Service 82fcde
@c    nl_get_walt_digit dup @ascuheap @asulock @acsmem @aculock
Packit Service 82fcde
@c     libc_rwlock_wrlock dup @asulock @aculock
Packit Service 82fcde
@c     nl_init_alt_digit dup @ascuheap @acsmem
Packit Service 82fcde
@c     malloc dup @ascuheap @acsmem
Packit Service 82fcde
@c     memset dup ok
Packit Service 82fcde
@c     wcschr dup ok
Packit Service 82fcde
@c     libc_rwlock_unlock dup @aculock
Packit Service 82fcde
@c    memset_space ok
Packit Service 82fcde
@c     wmemset dup ok
Packit Service 82fcde
@c    memset_zero ok
Packit Service 82fcde
@c     wmemset dup ok
Packit Service 82fcde
@c    mktime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    iso_week_days ok
Packit Service 82fcde
@c    isleap ok
Packit Service 82fcde
@c    tzset dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    gmtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    tm_diff ok
Packit Service 82fcde
The @code{wcsftime} function is equivalent to the @code{strftime}
Packit Service 82fcde
function with the difference that it operates on wide character
Packit Service 82fcde
strings.  The buffer where the result is stored, pointed to by @var{s},
Packit Service 82fcde
must be an array of wide characters.  The parameter @var{size} which
Packit Service 82fcde
specifies the size of the output buffer gives the number of wide
Packit Service 82fcde
characters, not the number of bytes.
Packit Service 82fcde
Packit Service 82fcde
Also the format string @var{template} is a wide character string.  Since
Packit Service 82fcde
all characters needed to specify the format string are in the basic
Packit Service 82fcde
character set it is portably possible to write format strings in the C
Packit Service 82fcde
source code using the @code{L"@dots{}"} notation.  The parameter
Packit Service 82fcde
@var{brokentime} has the same meaning as in the @code{strftime} call.
Packit Service 82fcde
Packit Service 82fcde
The @code{wcsftime} function supports the same flags, modifiers, and
Packit Service 82fcde
format specifiers as the @code{strftime} function.
Packit Service 82fcde
Packit Service 82fcde
The return value of @code{wcsftime} is the number of wide characters
Packit Service 82fcde
stored in @code{s}.  When more characters would have to be written than
Packit Service 82fcde
can be placed in the buffer @var{s} the return value is zero, with the
Packit Service 82fcde
same problems indicated in the @code{strftime} documentation.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Parsing Date and Time
Packit Service 82fcde
@subsection Convert textual time and date information back
Packit Service 82fcde
Packit Service 82fcde
The @w{ISO C} standard does not specify any functions which can convert
Packit Service 82fcde
the output of the @code{strftime} function back into a binary format.
Packit Service 82fcde
This led to a variety of more-or-less successful implementations with
Packit Service 82fcde
different interfaces over the years.  Then the Unix standard was
Packit Service 82fcde
extended by the addition of two functions: @code{strptime} and
Packit Service 82fcde
@code{getdate}.  Both have strange interfaces but at least they are
Packit Service 82fcde
widely available.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Low-Level Time String Parsing::  Interpret string according to given format.
Packit Service 82fcde
* General Time String Parsing::    User-friendly function to parse data and
Packit Service 82fcde
                                    time strings.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Low-Level Time String Parsing
Packit Service 82fcde
@subsubsection Interpret string according to given format
Packit Service 82fcde
Packit Service 82fcde
The first function is rather low-level.  It is nevertheless frequently
Packit Service 82fcde
used in software since it is better known.  Its interface and
Packit Service 82fcde
implementation are heavily influenced by the @code{getdate} function,
Packit Service 82fcde
which is defined and implemented in terms of calls to @code{strptime}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {char *} strptime (const char *@var{s}, const char *@var{fmt}, struct tm *@var{tp})
Packit Service 82fcde
@standards{XPG4, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c strptime @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  strptime_internal @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c   memset dup ok
Packit Service 82fcde
@c   ISSPACE ok
Packit Service 82fcde
@c    isspace_l dup ok
Packit Service 82fcde
@c   match_char ok
Packit Service 82fcde
@c   match_string ok
Packit Service 82fcde
@c    strlen dup ok
Packit Service 82fcde
@c    strncasecmp_l dup ok
Packit Service 82fcde
@c   strcmp dup ok
Packit Service 82fcde
@c   recursive @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c    strptime_internal dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c   get_number ok
Packit Service 82fcde
@c    ISSPACE dup ok
Packit Service 82fcde
@c   localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c   nl_select_era_entry @ascuheap @asulock @acsmem @aculock
Packit Service 82fcde
@c    nl_init_era_entries dup @ascuheap @asulock @acsmem @aculock
Packit Service 82fcde
@c   get_alt_number dup @ascuheap @asulock @acsmem @aculock
Packit Service 82fcde
@c    nl_parse_alt_digit dup @ascuheap @asulock @acsmem @aculock
Packit Service 82fcde
@c     libc_rwlock_wrlock dup @asulock @aculock
Packit Service 82fcde
@c     nl_init_alt_digit dup @ascuheap @acsmem
Packit Service 82fcde
@c     libc_rwlock_unlock dup @aculock
Packit Service 82fcde
@c    get_number dup ok
Packit Service 82fcde
@c   day_of_the_week ok
Packit Service 82fcde
@c   day_of_the_year ok
Packit Service 82fcde
The @code{strptime} function parses the input string @var{s} according
Packit Service 82fcde
to the format string @var{fmt} and stores its results in the
Packit Service 82fcde
structure @var{tp}.
Packit Service 82fcde
Packit Service 82fcde
The input string could be generated by a @code{strftime} call or
Packit Service 82fcde
obtained any other way.  It does not need to be in a human-recognizable
Packit Service 82fcde
format; e.g. a date passed as @code{"02:1999:9"} is acceptable, even
Packit Service 82fcde
though it is ambiguous without context.  As long as the format string
Packit Service 82fcde
@var{fmt} matches the input string the function will succeed.
Packit Service 82fcde
Packit Service 82fcde
The user has to make sure, though, that the input can be parsed in a
Packit Service 82fcde
unambiguous way.  The string @code{"1999112"} can be parsed using the
Packit Service 82fcde
format @code{"%Y%m%d"} as 1999-1-12, 1999-11-2, or even 19991-1-2.  It
Packit Service 82fcde
is necessary to add appropriate separators to reliably get results.
Packit Service 82fcde
Packit Service 82fcde
The format string consists of the same components as the format string
Packit Service 82fcde
of the @code{strftime} function.  The only difference is that the flags
Packit Service 82fcde
@code{_}, @code{-}, @code{0}, and @code{^} are not allowed.
Packit Service 82fcde
@comment Is this really the intention?  --drepper
Packit Service 82fcde
Several of the distinct formats of @code{strftime} do the same work in
Packit Service 82fcde
@code{strptime} since differences like case of the input do not matter.
Packit Service 82fcde
For reasons of symmetry all formats are supported, though.
Packit Service 82fcde
Packit Service 82fcde
The modifiers @code{E} and @code{O} are also allowed everywhere the
Packit Service 82fcde
@code{strftime} function allows them.
Packit Service 82fcde
Packit Service 82fcde
The formats are:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item %a
Packit Service 82fcde
@itemx %A
Packit Service 82fcde
The weekday name according to the current locale, in abbreviated form or
Packit Service 82fcde
the full name.
Packit Service 82fcde
Packit Service 82fcde
@item %b
Packit Service 82fcde
@itemx %B
Packit Service 82fcde
@itemx %h
Packit Service 82fcde
A month name according to the current locale.  All three specifiers
Packit Service 82fcde
will recognize both abbreviated and full month names.  If the
Packit Service 82fcde
locale provides two different grammatical forms of month names,
Packit Service 82fcde
all three specifiers will recognize both forms.
Packit Service 82fcde
Packit Service 82fcde
As a GNU extension, the @code{O} modifier can be used with these
Packit Service 82fcde
specifiers; it has no effect, as both grammatical forms of month
Packit Service 82fcde
names are recognized.
Packit Service 82fcde
Packit Service 82fcde
@item %c
Packit Service 82fcde
The date and time representation for the current locale.
Packit Service 82fcde
Packit Service 82fcde
@item %Ec
Packit Service 82fcde
Like @code{%c} but the locale's alternative date and time format is used.
Packit Service 82fcde
Packit Service 82fcde
@item %C
Packit Service 82fcde
The century of the year.
Packit Service 82fcde
Packit Service 82fcde
It makes sense to use this format only if the format string also
Packit Service 82fcde
contains the @code{%y} format.
Packit Service 82fcde
Packit Service 82fcde
@item %EC
Packit Service 82fcde
The locale's representation of the period.
Packit Service 82fcde
Packit Service 82fcde
Unlike @code{%C} it sometimes makes sense to use this format since some
Packit Service 82fcde
cultures represent years relative to the beginning of eras instead of
Packit Service 82fcde
using the Gregorian years.
Packit Service 82fcde
Packit Service 82fcde
@item %d
Packit Service 82fcde
@item %e
Packit Service 82fcde
The day of the month as a decimal number (range @code{1} through @code{31}).
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@item %Od
Packit Service 82fcde
@itemx %Oe
Packit Service 82fcde
Same as @code{%d} but using the locale's alternative numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@item %D
Packit Service 82fcde
Equivalent to @code{%m/%d/%y}.
Packit Service 82fcde
Packit Service 82fcde
@item %F
Packit Service 82fcde
Equivalent to @code{%Y-%m-%d}, which is the @w{ISO 8601} date
Packit Service 82fcde
format.
Packit Service 82fcde
Packit Service 82fcde
This is a GNU extension following an @w{ISO C99} extension to
Packit Service 82fcde
@code{strftime}.
Packit Service 82fcde
Packit Service 82fcde
@item %g
Packit Service 82fcde
The year corresponding to the ISO week number, but without the century
Packit Service 82fcde
(range @code{00} through @code{99}).
Packit Service 82fcde
Packit Service 82fcde
@emph{Note:} Currently, this is not fully implemented.  The format is
Packit Service 82fcde
recognized, input is consumed but no field in @var{tm} is set.
Packit Service 82fcde
Packit Service 82fcde
This format is a GNU extension following a GNU extension of @code{strftime}.
Packit Service 82fcde
Packit Service 82fcde
@item %G
Packit Service 82fcde
The year corresponding to the ISO week number.
Packit Service 82fcde
Packit Service 82fcde
@emph{Note:} Currently, this is not fully implemented.  The format is
Packit Service 82fcde
recognized, input is consumed but no field in @var{tm} is set.
Packit Service 82fcde
Packit Service 82fcde
This format is a GNU extension following a GNU extension of @code{strftime}.
Packit Service 82fcde
Packit Service 82fcde
@item %H
Packit Service 82fcde
@itemx %k
Packit Service 82fcde
The hour as a decimal number, using a 24-hour clock (range @code{00} through
Packit Service 82fcde
@code{23}).
Packit Service 82fcde
Packit Service 82fcde
@code{%k} is a GNU extension following a GNU extension of @code{strftime}.
Packit Service 82fcde
Packit Service 82fcde
@item %OH
Packit Service 82fcde
Same as @code{%H} but using the locale's alternative numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
@item %I
Packit Service 82fcde
@itemx %l
Packit Service 82fcde
The hour as a decimal number, using a 12-hour clock (range @code{01} through
Packit Service 82fcde
@code{12}).
Packit Service 82fcde
Packit Service 82fcde
@code{%l} is a GNU extension following a GNU extension of @code{strftime}.
Packit Service 82fcde
Packit Service 82fcde
@item %OI
Packit Service 82fcde
Same as @code{%I} but using the locale's alternative numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
@item %j
Packit Service 82fcde
The day of the year as a decimal number (range @code{1} through @code{366}).
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@item %m
Packit Service 82fcde
The month as a decimal number (range @code{1} through @code{12}).
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@item %Om
Packit Service 82fcde
Same as @code{%m} but using the locale's alternative numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
@item %M
Packit Service 82fcde
The minute as a decimal number (range @code{0} through @code{59}).
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@item %OM
Packit Service 82fcde
Same as @code{%M} but using the locale's alternative numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
@item %n
Packit Service 82fcde
@itemx %t
Packit Service 82fcde
Matches any white space.
Packit Service 82fcde
Packit Service 82fcde
@item %p
Packit Service 82fcde
@item %P
Packit Service 82fcde
The locale-dependent equivalent to @samp{AM} or @samp{PM}.
Packit Service 82fcde
Packit Service 82fcde
This format is not useful unless @code{%I} or @code{%l} is also used.
Packit Service 82fcde
Another complication is that the locale might not define these values at
Packit Service 82fcde
all and therefore the conversion fails.
Packit Service 82fcde
Packit Service 82fcde
@code{%P} is a GNU extension following a GNU extension to @code{strftime}.
Packit Service 82fcde
Packit Service 82fcde
@item %r
Packit Service 82fcde
The complete time using the AM/PM format of the current locale.
Packit Service 82fcde
Packit Service 82fcde
A complication is that the locale might not define this format at all
Packit Service 82fcde
and therefore the conversion fails.
Packit Service 82fcde
Packit Service 82fcde
@item %R
Packit Service 82fcde
The hour and minute in decimal numbers using the format @code{%H:%M}.
Packit Service 82fcde
Packit Service 82fcde
@code{%R} is a GNU extension following a GNU extension to @code{strftime}.
Packit Service 82fcde
Packit Service 82fcde
@item %s
Packit Service 82fcde
The number of seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC.
Packit Service 82fcde
Leap seconds are not counted unless leap second support is available.
Packit Service 82fcde
Packit Service 82fcde
@code{%s} is a GNU extension following a GNU extension to @code{strftime}.
Packit Service 82fcde
Packit Service 82fcde
@item %S
Packit Service 82fcde
The seconds as a decimal number (range @code{0} through @code{60}).
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@strong{NB:} The Unix specification says the upper bound on this value
Packit Service 82fcde
is @code{61}, a result of a decision to allow double leap seconds.  You
Packit Service 82fcde
will not see the value @code{61} because no minute has more than one
Packit Service 82fcde
leap second, but the myth persists.
Packit Service 82fcde
Packit Service 82fcde
@item %OS
Packit Service 82fcde
Same as @code{%S} but using the locale's alternative numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
@item %T
Packit Service 82fcde
Equivalent to the use of @code{%H:%M:%S} in this place.
Packit Service 82fcde
Packit Service 82fcde
@item %u
Packit Service 82fcde
The day of the week as a decimal number (range @code{1} through
Packit Service 82fcde
@code{7}), Monday being @code{1}.
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@emph{Note:} Currently, this is not fully implemented.  The format is
Packit Service 82fcde
recognized, input is consumed but no field in @var{tm} is set.
Packit Service 82fcde
Packit Service 82fcde
@item %U
Packit Service 82fcde
The week number of the current year as a decimal number (range @code{0}
Packit Service 82fcde
through @code{53}).
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@item %OU
Packit Service 82fcde
Same as @code{%U} but using the locale's alternative numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
@item %V
Packit Service 82fcde
The @w{ISO 8601:1988} week number as a decimal number (range @code{1}
Packit Service 82fcde
through @code{53}).
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@emph{Note:} Currently, this is not fully implemented.  The format is
Packit Service 82fcde
recognized, input is consumed but no field in @var{tm} is set.
Packit Service 82fcde
Packit Service 82fcde
@item %w
Packit Service 82fcde
The day of the week as a decimal number (range @code{0} through
Packit Service 82fcde
@code{6}), Sunday being @code{0}.
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@emph{Note:} Currently, this is not fully implemented.  The format is
Packit Service 82fcde
recognized, input is consumed but no field in @var{tm} is set.
Packit Service 82fcde
Packit Service 82fcde
@item %Ow
Packit Service 82fcde
Same as @code{%w} but using the locale's alternative numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
@item %W
Packit Service 82fcde
The week number of the current year as a decimal number (range @code{0}
Packit Service 82fcde
through @code{53}).
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
@emph{Note:} Currently, this is not fully implemented.  The format is
Packit Service 82fcde
recognized, input is consumed but no field in @var{tm} is set.
Packit Service 82fcde
Packit Service 82fcde
@item %OW
Packit Service 82fcde
Same as @code{%W} but using the locale's alternative numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
@item %x
Packit Service 82fcde
The date using the locale's date format.
Packit Service 82fcde
Packit Service 82fcde
@item %Ex
Packit Service 82fcde
Like @code{%x} but the locale's alternative data representation is used.
Packit Service 82fcde
Packit Service 82fcde
@item %X
Packit Service 82fcde
The time using the locale's time format.
Packit Service 82fcde
Packit Service 82fcde
@item %EX
Packit Service 82fcde
Like @code{%X} but the locale's alternative time representation is used.
Packit Service 82fcde
Packit Service 82fcde
@item %y
Packit Service 82fcde
The year without a century as a decimal number (range @code{0} through
Packit Service 82fcde
@code{99}).
Packit Service 82fcde
Packit Service 82fcde
Leading zeroes are permitted but not required.
Packit Service 82fcde
Packit Service 82fcde
Note that it is questionable to use this format without
Packit Service 82fcde
the @code{%C} format.  The @code{strptime} function does regard input
Packit Service 82fcde
values in the range @math{68} to @math{99} as the years @math{1969} to
Packit Service 82fcde
@math{1999} and the values @math{0} to @math{68} as the years
Packit Service 82fcde
@math{2000} to @math{2068}.  But maybe this heuristic fails for some
Packit Service 82fcde
input data.
Packit Service 82fcde
Packit Service 82fcde
Therefore it is best to avoid @code{%y} completely and use @code{%Y}
Packit Service 82fcde
instead.
Packit Service 82fcde
Packit Service 82fcde
@item %Ey
Packit Service 82fcde
The offset from @code{%EC} in the locale's alternative representation.
Packit Service 82fcde
Packit Service 82fcde
@item %Oy
Packit Service 82fcde
The offset of the year (from @code{%C}) using the locale's alternative
Packit Service 82fcde
numeric symbols.
Packit Service 82fcde
Packit Service 82fcde
@item %Y
Packit Service 82fcde
The year as a decimal number, using the Gregorian calendar.
Packit Service 82fcde
Packit Service 82fcde
@item %EY
Packit Service 82fcde
The full alternative year representation.
Packit Service 82fcde
Packit Service 82fcde
@item %z
Packit Service 82fcde
The offset from GMT in @w{ISO 8601}/RFC822 format.
Packit Service 82fcde
Packit Service 82fcde
@item %Z
Packit Service 82fcde
The timezone name.
Packit Service 82fcde
Packit Service 82fcde
@emph{Note:} Currently, this is not fully implemented.  The format is
Packit Service 82fcde
recognized, input is consumed but no field in @var{tm} is set.
Packit Service 82fcde
Packit Service 82fcde
@item %%
Packit Service 82fcde
A literal @samp{%} character.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
All other characters in the format string must have a matching character
Packit Service 82fcde
in the input string.  Exceptions are white spaces in the input string
Packit Service 82fcde
which can match zero or more whitespace characters in the format string.
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability Note:} The XPG standard advises applications to use
Packit Service 82fcde
at least one whitespace character (as specified by @code{isspace}) or
Packit Service 82fcde
other non-alphanumeric characters between any two conversion
Packit Service 82fcde
specifications.  @Theglibc{} does not have this limitation but
Packit Service 82fcde
other libraries might have trouble parsing formats like
Packit Service 82fcde
@code{"%d%m%Y%H%M%S"}.
Packit Service 82fcde
Packit Service 82fcde
The @code{strptime} function processes the input string from right to
Packit Service 82fcde
left.  Each of the three possible input elements (white space, literal,
Packit Service 82fcde
or format) are handled one after the other.  If the input cannot be
Packit Service 82fcde
matched to the format string the function stops.  The remainder of the
Packit Service 82fcde
format and input strings are not processed.
Packit Service 82fcde
Packit Service 82fcde
The function returns a pointer to the first character it was unable to
Packit Service 82fcde
process.  If the input string contains more characters than required by
Packit Service 82fcde
the format string the return value points right after the last consumed
Packit Service 82fcde
input character.  If the whole input string is consumed the return value
Packit Service 82fcde
points to the @code{NULL} byte at the end of the string.  If an error
Packit Service 82fcde
occurs, i.e., @code{strptime} fails to match all of the format string,
Packit Service 82fcde
the function returns @code{NULL}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
The specification of the function in the XPG standard is rather vague,
Packit Service 82fcde
leaving out a few important pieces of information.  Most importantly, it
Packit Service 82fcde
does not specify what happens to those elements of @var{tm} which are
Packit Service 82fcde
not directly initialized by the different formats.  The
Packit Service 82fcde
implementations on different Unix systems vary here.
Packit Service 82fcde
Packit Service 82fcde
The @glibcadj{} implementation does not touch those fields which are not
Packit Service 82fcde
directly initialized.  Exceptions are the @code{tm_wday} and
Packit Service 82fcde
@code{tm_yday} elements, which are recomputed if any of the year, month,
Packit Service 82fcde
or date elements changed.  This has two implications:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
Before calling the @code{strptime} function for a new input string, you
Packit Service 82fcde
should prepare the @var{tm} structure you pass.  Normally this will mean
Packit Service 82fcde
initializing all values to zero.  Alternatively, you can set all
Packit Service 82fcde
fields to values like @code{INT_MAX}, allowing you to determine which
Packit Service 82fcde
elements were set by the function call.  Zero does not work here since
Packit Service 82fcde
it is a valid value for many of the fields.
Packit Service 82fcde
Packit Service 82fcde
Careful initialization is necessary if you want to find out whether a
Packit Service 82fcde
certain field in @var{tm} was initialized by the function call.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
You can construct a @code{struct tm} value with several consecutive
Packit Service 82fcde
@code{strptime} calls.  A useful application of this is e.g. the parsing
Packit Service 82fcde
of two separate strings, one containing date information and the other
Packit Service 82fcde
time information.  By parsing one after the other without clearing the
Packit Service 82fcde
structure in-between, you can construct a complete broken-down time.
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
The following example shows a function which parses a string which
Packit Service 82fcde
contains the date information in either US style or @w{ISO 8601} form:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
const char *
Packit Service 82fcde
parse_date (const char *input, struct tm *tm)
Packit Service 82fcde
@{
Packit Service 82fcde
  const char *cp;
Packit Service 82fcde
Packit Service 82fcde
  /* @r{First clear the result structure.}  */
Packit Service 82fcde
  memset (tm, '\0', sizeof (*tm));
Packit Service 82fcde
Packit Service 82fcde
  /* @r{Try the ISO format first.}  */
Packit Service 82fcde
  cp = strptime (input, "%F", tm);
Packit Service 82fcde
  if (cp == NULL)
Packit Service 82fcde
    @{
Packit Service 82fcde
      /* @r{Does not match.  Try the US form.}  */
Packit Service 82fcde
      cp = strptime (input, "%D", tm);
Packit Service 82fcde
    @}
Packit Service 82fcde
Packit Service 82fcde
  return cp;
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@node General Time String Parsing
Packit Service 82fcde
@subsubsection A More User-friendly Way to Parse Times and Dates
Packit Service 82fcde
Packit Service 82fcde
The Unix standard defines another function for parsing date strings.
Packit Service 82fcde
The interface is weird, but if the function happens to suit your
Packit Service 82fcde
application it is just fine.  It is problematic to use this function
Packit Service 82fcde
in multi-threaded programs or libraries, since it returns a pointer to
Packit Service 82fcde
a static variable, and uses a global variable and global state (an
Packit Service 82fcde
environment variable).
Packit Service 82fcde
Packit Service 82fcde
@defvar getdate_err
Packit Service 82fcde
@standards{Unix98, time.h}
Packit Service 82fcde
This variable of type @code{int} contains the error code of the last
Packit Service 82fcde
unsuccessful call to @code{getdate}.  Defined values are:
Packit Service 82fcde
Packit Service 82fcde
@table @math
Packit Service 82fcde
@item 1
Packit Service 82fcde
The environment variable @code{DATEMSK} is not defined or null.
Packit Service 82fcde
@item 2
Packit Service 82fcde
The template file denoted by the @code{DATEMSK} environment variable
Packit Service 82fcde
cannot be opened.
Packit Service 82fcde
@item 3
Packit Service 82fcde
Information about the template file cannot retrieved.
Packit Service 82fcde
@item 4
Packit Service 82fcde
The template file is not a regular file.
Packit Service 82fcde
@item 5
Packit Service 82fcde
An I/O error occurred while reading the template file.
Packit Service 82fcde
@item 6
Packit Service 82fcde
Not enough memory available to execute the function.
Packit Service 82fcde
@item 7
Packit Service 82fcde
The template file contains no matching template.
Packit Service 82fcde
@item 8
Packit Service 82fcde
The input date is invalid, but would match a template otherwise.  This
Packit Service 82fcde
includes dates like February 31st, and dates which cannot be represented
Packit Service 82fcde
in a @code{time_t} variable.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end defvar
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {struct tm *} getdate (const char *@var{string})
Packit Service 82fcde
@standards{Unix98, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:getdate} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c getdate @mtasurace:getdate @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  getdate_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
The interface to @code{getdate} is the simplest possible for a function
Packit Service 82fcde
to parse a string and return the value.  @var{string} is the input
Packit Service 82fcde
string and the result is returned in a statically-allocated variable.
Packit Service 82fcde
Packit Service 82fcde
The details about how the string is processed are hidden from the user.
Packit Service 82fcde
In fact, they can be outside the control of the program.  Which formats
Packit Service 82fcde
are recognized is controlled by the file named by the environment
Packit Service 82fcde
variable @code{DATEMSK}.  This file should contain
Packit Service 82fcde
lines of valid format strings which could be passed to @code{strptime}.
Packit Service 82fcde
Packit Service 82fcde
The @code{getdate} function reads these format strings one after the
Packit Service 82fcde
other and tries to match the input string.  The first line which
Packit Service 82fcde
completely matches the input string is used.
Packit Service 82fcde
Packit Service 82fcde
Elements not initialized through the format string retain the values
Packit Service 82fcde
present at the time of the @code{getdate} function call.
Packit Service 82fcde
Packit Service 82fcde
The formats recognized by @code{getdate} are the same as for
Packit Service 82fcde
@code{strptime}.  See above for an explanation.  There are only a few
Packit Service 82fcde
extensions to the @code{strptime} behavior:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
If the @code{%Z} format is given the broken-down time is based on the
Packit Service 82fcde
current time of the timezone matched, not of the current timezone of the
Packit Service 82fcde
runtime environment.
Packit Service 82fcde
Packit Service 82fcde
@emph{Note}: This is not implemented (currently).  The problem is that
Packit Service 82fcde
timezone names are not unique.  If a fixed timezone is assumed for a
Packit Service 82fcde
given string (say @code{EST} meaning US East Coast time), then uses for
Packit Service 82fcde
countries other than the USA will fail.  So far we have found no good
Packit Service 82fcde
solution to this.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
If only the weekday is specified the selected day depends on the current
Packit Service 82fcde
date.  If the current weekday is greater than or equal to the @code{tm_wday}
Packit Service 82fcde
value the current week's day is chosen, otherwise the day next week is chosen.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
A similar heuristic is used when only the month is given and not the
Packit Service 82fcde
year.  If the month is greater than or equal to the current month, then
Packit Service 82fcde
the current year is used.  Otherwise it wraps to next year.  The first
Packit Service 82fcde
day of the month is assumed if one is not explicitly specified.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
The current hour, minute, and second are used if the appropriate value is
Packit Service 82fcde
not set through the format.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
If no date is given tomorrow's date is used if the time is
Packit Service 82fcde
smaller than the current time.  Otherwise today's date is taken.
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
It should be noted that the format in the template file need not only
Packit Service 82fcde
contain format elements.  The following is a list of possible format
Packit Service 82fcde
strings (taken from the Unix standard):
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
%m
Packit Service 82fcde
%A %B %d, %Y %H:%M:%S
Packit Service 82fcde
%A
Packit Service 82fcde
%B
Packit Service 82fcde
%m/%d/%y %I %p
Packit Service 82fcde
%d,%m,%Y %H:%M
Packit Service 82fcde
at %A the %dst of %B in %Y
Packit Service 82fcde
run job at %I %p,%B %dnd
Packit Service 82fcde
%A den %d. %B %Y %H.%M Uhr
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
As you can see, the template list can contain very specific strings like
Packit Service 82fcde
@code{run job at %I %p,%B %dnd}.  Using the above list of templates and
Packit Service 82fcde
assuming the current time is Mon Sep 22 12:19:47 EDT 1986, we can obtain the
Packit Service 82fcde
following results for the given input.
Packit Service 82fcde
Packit Service 82fcde
@multitable {xxxxxxxxxxxx} {xxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
Packit Service 82fcde
@item        Input @tab     Match @tab Result
Packit Service 82fcde
@item        Mon @tab       %a @tab    Mon Sep 22 12:19:47 EDT 1986
Packit Service 82fcde
@item        Sun @tab       %a @tab    Sun Sep 28 12:19:47 EDT 1986
Packit Service 82fcde
@item        Fri @tab       %a @tab    Fri Sep 26 12:19:47 EDT 1986
Packit Service 82fcde
@item        September @tab %B @tab    Mon Sep 1 12:19:47 EDT 1986
Packit Service 82fcde
@item        January @tab   %B @tab    Thu Jan 1 12:19:47 EST 1987
Packit Service 82fcde
@item        December @tab  %B @tab    Mon Dec 1 12:19:47 EST 1986
Packit Service 82fcde
@item        Sep Mon @tab   %b %a @tab Mon Sep 1 12:19:47 EDT 1986
Packit Service 82fcde
@item        Jan Fri @tab   %b %a @tab Fri Jan 2 12:19:47 EST 1987
Packit Service 82fcde
@item        Dec Mon @tab   %b %a @tab Mon Dec 1 12:19:47 EST 1986
Packit Service 82fcde
@item        Jan Wed 1989 @tab  %b %a %Y @tab Wed Jan 4 12:19:47 EST 1989
Packit Service 82fcde
@item        Fri 9 @tab     %a %H @tab Fri Sep 26 09:00:00 EDT 1986
Packit Service 82fcde
@item        Feb 10:30 @tab %b %H:%S @tab Sun Feb 1 10:00:30 EST 1987
Packit Service 82fcde
@item        10:30 @tab     %H:%M @tab Tue Sep 23 10:30:00 EDT 1986
Packit Service 82fcde
@item        13:30 @tab     %H:%M @tab Mon Sep 22 13:30:00 EDT 1986
Packit Service 82fcde
@end multitable
Packit Service 82fcde
Packit Service 82fcde
The return value of the function is a pointer to a static variable of
Packit Service 82fcde
type @w{@code{struct tm}}, or a null pointer if an error occurred.  The
Packit Service 82fcde
result is only valid until the next @code{getdate} call, making this
Packit Service 82fcde
function unusable in multi-threaded applications.
Packit Service 82fcde
Packit Service 82fcde
The @code{errno} variable is @emph{not} changed.  Error conditions are
Packit Service 82fcde
stored in the global variable @code{getdate_err}.  See the
Packit Service 82fcde
description above for a list of the possible error values.
Packit Service 82fcde
Packit Service 82fcde
@emph{Warning:} The @code{getdate} function should @emph{never} be
Packit Service 82fcde
used in SUID-programs.  The reason is obvious: using the
Packit Service 82fcde
@code{DATEMSK} environment variable you can get the function to open
Packit Service 82fcde
any arbitrary file and chances are high that with some bogus input
Packit Service 82fcde
(such as a binary file) the program will crash.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int getdate_r (const char *@var{string}, struct tm *@var{tp})
Packit Service 82fcde
@standards{GNU, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c getdate_r @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  getenv dup @mtsenv
Packit Service 82fcde
@c  stat64 dup ok
Packit Service 82fcde
@c  access dup ok
Packit Service 82fcde
@c  fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
Packit Service 82fcde
@c  fsetlocking dup ok [no @mtasurace:stream @asulock, exclusive]
Packit Service 82fcde
@c  isspace dup @mtslocale
Packit Service 82fcde
@c  strlen dup ok
Packit Service 82fcde
@c  malloc dup @ascuheap @acsmem
Packit Service 82fcde
@c  fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  memcpy dup ok
Packit Service 82fcde
@c  getline dup @ascuheap @acsmem [no @asucorrupt @aculock @acucorrupt, exclusive]
Packit Service 82fcde
@c  strptime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  feof_unlocked dup ok
Packit Service 82fcde
@c  free dup @ascuheap @acsmem
Packit Service 82fcde
@c  ferror_unlocked dup dup ok
Packit Service 82fcde
@c  time dup ok
Packit Service 82fcde
@c  localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  first_wday @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c   memset dup ok
Packit Service 82fcde
@c   mktime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  check_mday ok
Packit Service 82fcde
@c  mktime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
The @code{getdate_r} function is the reentrant counterpart of
Packit Service 82fcde
@code{getdate}.  It does not use the global variable @code{getdate_err}
Packit Service 82fcde
to signal an error, but instead returns an error code.  The same error
Packit Service 82fcde
codes as described in the @code{getdate_err} documentation above are
Packit Service 82fcde
used, with 0 meaning success.
Packit Service 82fcde
Packit Service 82fcde
Moreover, @code{getdate_r} stores the broken-down time in the variable
Packit Service 82fcde
of type @code{struct tm} pointed to by the second argument, rather than
Packit Service 82fcde
in a static variable.
Packit Service 82fcde
Packit Service 82fcde
This function is not defined in the Unix standard.  Nevertheless it is
Packit Service 82fcde
available on some other Unix systems as well.
Packit Service 82fcde
Packit Service 82fcde
The warning against using @code{getdate} in SUID-programs applies to
Packit Service 82fcde
@code{getdate_r} as well.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node TZ Variable
Packit Service 82fcde
@subsection Specifying the Time Zone with @code{TZ}
Packit Service 82fcde
Packit Service 82fcde
In POSIX systems, a user can specify the time zone by means of the
Packit Service 82fcde
@code{TZ} environment variable.  For information about how to set
Packit Service 82fcde
environment variables, see @ref{Environment Variables}.  The functions
Packit Service 82fcde
for accessing the time zone are declared in @file{time.h}.
Packit Service 82fcde
@pindex time.h
Packit Service 82fcde
@cindex time zone
Packit Service 82fcde
Packit Service 82fcde
You should not normally need to set @code{TZ}.  If the system is
Packit Service 82fcde
configured properly, the default time zone will be correct.  You might
Packit Service 82fcde
set @code{TZ} if you are using a computer over a network from a
Packit Service 82fcde
different time zone, and would like times reported to you in the time
Packit Service 82fcde
zone local to you, rather than what is local to the computer.
Packit Service 82fcde
Packit Service 82fcde
In POSIX.1 systems the value of the @code{TZ} variable can be in one of
Packit Service 82fcde
three formats.  With @theglibc{}, the most common format is the
Packit Service 82fcde
last one, which can specify a selection from a large database of time
Packit Service 82fcde
zone information for many regions of the world.  The first two formats
Packit Service 82fcde
are used to describe the time zone information directly, which is both
Packit Service 82fcde
more cumbersome and less precise.  But the POSIX.1 standard only
Packit Service 82fcde
specifies the details of the first two formats, so it is good to be
Packit Service 82fcde
familiar with them in case you come across a POSIX.1 system that doesn't
Packit Service 82fcde
support a time zone information database.
Packit Service 82fcde
Packit Service 82fcde
The first format is used when there is no Daylight Saving Time (or
Packit Service 82fcde
summer time) in the local time zone:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@r{@var{std} @var{offset}}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The @var{std} string specifies the name of the time zone.  It must be
Packit Service 82fcde
three or more characters long and must not contain a leading colon,
Packit Service 82fcde
embedded digits, commas, nor plus and minus signs.  There is no space
Packit Service 82fcde
character separating the time zone name from the @var{offset}, so these
Packit Service 82fcde
restrictions are necessary to parse the specification correctly.
Packit Service 82fcde
Packit Service 82fcde
The @var{offset} specifies the time value you must add to the local time
Packit Service 82fcde
to get a Coordinated Universal Time value.  It has syntax like
Packit Service 82fcde
[@code{+}|@code{-}]@var{hh}[@code{:}@var{mm}[@code{:}@var{ss}]].  This
Packit Service 82fcde
is positive if the local time zone is west of the Prime Meridian and
Packit Service 82fcde
negative if it is east.  The hour must be between @code{0} and
Packit Service 82fcde
@code{24}, and the minute and seconds between @code{0} and @code{59}.
Packit Service 82fcde
Packit Service 82fcde
For example, here is how we would specify Eastern Standard Time, but
Packit Service 82fcde
without any Daylight Saving Time alternative:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
EST+5
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The second format is used when there is Daylight Saving Time:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@r{@var{std} @var{offset} @var{dst} [@var{offset}]@code{,}@var{start}[@code{/}@var{time}]@code{,}@var{end}[@code{/}@var{time}]}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The initial @var{std} and @var{offset} specify the standard time zone, as
Packit Service 82fcde
described above.  The @var{dst} string and @var{offset} specify the name
Packit Service 82fcde
and offset for the corresponding Daylight Saving Time zone; if the
Packit Service 82fcde
@var{offset} is omitted, it defaults to one hour ahead of standard time.
Packit Service 82fcde
Packit Service 82fcde
The remainder of the specification describes when Daylight Saving Time is
Packit Service 82fcde
in effect.  The @var{start} field is when Daylight Saving Time goes into
Packit Service 82fcde
effect and the @var{end} field is when the change is made back to standard
Packit Service 82fcde
time.  The following formats are recognized for these fields:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item J@var{n}
Packit Service 82fcde
This specifies the Julian day, with @var{n} between @code{1} and @code{365}.
Packit Service 82fcde
February 29 is never counted, even in leap years.
Packit Service 82fcde
Packit Service 82fcde
@item @var{n}
Packit Service 82fcde
This specifies the Julian day, with @var{n} between @code{0} and @code{365}.
Packit Service 82fcde
February 29 is counted in leap years.
Packit Service 82fcde
Packit Service 82fcde
@item M@var{m}.@var{w}.@var{d}
Packit Service 82fcde
This specifies day @var{d} of week @var{w} of month @var{m}.  The day
Packit Service 82fcde
@var{d} must be between @code{0} (Sunday) and @code{6}.  The week
Packit Service 82fcde
@var{w} must be between @code{1} and @code{5}; week @code{1} is the
Packit Service 82fcde
first week in which day @var{d} occurs, and week @code{5} specifies the
Packit Service 82fcde
@emph{last} @var{d} day in the month.  The month @var{m} should be
Packit Service 82fcde
between @code{1} and @code{12}.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
The @var{time} fields specify when, in the local time currently in
Packit Service 82fcde
effect, the change to the other time occurs.  If omitted, the default is
Packit Service 82fcde
@code{02:00:00}.  The hours part of the time fields can range from
Packit Service 82fcde
@minus{}167 through 167; this is an extension to POSIX.1, which allows
Packit Service 82fcde
only the range 0 through 24.
Packit Service 82fcde
Packit Service 82fcde
Here are some example @code{TZ} values, including the appropriate
Packit Service 82fcde
Daylight Saving Time and its dates of applicability.  In North
Packit Service 82fcde
American Eastern Standard Time (EST) and Eastern Daylight Time (EDT),
Packit Service 82fcde
the normal offset from UTC is 5 hours; since this is
Packit Service 82fcde
west of the prime meridian, the sign is positive.  Summer time begins on
Packit Service 82fcde
March's second Sunday at 2:00am, and ends on November's first Sunday
Packit Service 82fcde
at 2:00am.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
EST+5EDT,M3.2.0/2,M11.1.0/2
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Israel Standard Time (IST) and Israel Daylight Time (IDT) are 2 hours
Packit Service 82fcde
ahead of the prime meridian in winter, springing forward an hour on
Packit Service 82fcde
March's fourth Thursday at 26:00 (i.e., 02:00 on the first Friday on or
Packit Service 82fcde
after March 23), and falling back on October's last Sunday at 02:00.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
IST-2IDT,M3.4.4/26,M10.5.0
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Western Argentina Summer Time (WARST) is 3 hours behind the prime
Packit Service 82fcde
meridian all year.  There is a dummy fall-back transition on December
Packit Service 82fcde
31 at 25:00 daylight saving time (i.e., 24:00 standard time,
Packit Service 82fcde
equivalent to January 1 at 00:00 standard time), and a simultaneous
Packit Service 82fcde
spring-forward transition on January 1 at 00:00 standard time, so
Packit Service 82fcde
daylight saving time is in effect all year and the initial @code{WART}
Packit Service 82fcde
is a placeholder.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
WART4WARST,J1/0,J365/25
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Western Greenland Time (WGT) and Western Greenland Summer Time (WGST)
Packit Service 82fcde
are 3 hours behind UTC in the winter.  Its clocks follow the European
Packit Service 82fcde
Union rules of springing forward by one hour on March's last Sunday at
Packit Service 82fcde
01:00 UTC (@minus{}02:00 local time) and falling back on October's
Packit Service 82fcde
last Sunday at 01:00 UTC (@minus{}01:00 local time).
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
WGT3WGST,M3.5.0/-2,M10.5.0/-1
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The schedule of Daylight Saving Time in any particular jurisdiction has
Packit Service 82fcde
changed over the years.  To be strictly correct, the conversion of dates
Packit Service 82fcde
and times in the past should be based on the schedule that was in effect
Packit Service 82fcde
then.  However, this format has no facilities to let you specify how the
Packit Service 82fcde
schedule has changed from year to year.  The most you can do is specify
Packit Service 82fcde
one particular schedule---usually the present day schedule---and this is
Packit Service 82fcde
used to convert any date, no matter when.  For precise time zone
Packit Service 82fcde
specifications, it is best to use the time zone information database
Packit Service 82fcde
(see below).
Packit Service 82fcde
Packit Service 82fcde
The third format looks like this:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
:@var{characters}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Each operating system interprets this format differently; in
Packit Service 82fcde
@theglibc{}, @var{characters} is the name of a file which describes the time
Packit Service 82fcde
zone.
Packit Service 82fcde
Packit Service 82fcde
@pindex /etc/localtime
Packit Service 82fcde
@pindex localtime
Packit Service 82fcde
If the @code{TZ} environment variable does not have a value, the
Packit Service 82fcde
operation chooses a time zone by default.  In @theglibc{}, the
Packit Service 82fcde
default time zone is like the specification @samp{TZ=:/etc/localtime}
Packit Service 82fcde
(or @samp{TZ=:/usr/local/etc/localtime}, depending on how @theglibc{}
Packit Service 82fcde
was configured; @pxref{Installation}).  Other C libraries use their own
Packit Service 82fcde
rule for choosing the default time zone, so there is little we can say
Packit Service 82fcde
about them.
Packit Service 82fcde
Packit Service 82fcde
@cindex time zone database
Packit Service 82fcde
@pindex /usr/share/zoneinfo
Packit Service 82fcde
@pindex zoneinfo
Packit Service 82fcde
If @var{characters} begins with a slash, it is an absolute file name;
Packit Service 82fcde
otherwise the library looks for the file
Packit Service 82fcde
@w{@file{/usr/share/zoneinfo/@var{characters}}}.  The @file{zoneinfo}
Packit Service 82fcde
directory contains data files describing local time zones in many
Packit Service 82fcde
different parts of the world.  The names represent major cities, with
Packit Service 82fcde
subdirectories for geographical areas; for example,
Packit Service 82fcde
@file{America/New_York}, @file{Europe/London}, @file{Asia/Hong_Kong}.
Packit Service 82fcde
These data files are installed by the system administrator, who also
Packit Service 82fcde
sets @file{/etc/localtime} to point to the data file for the local time
Packit Service 82fcde
zone.  The files typically come from the @url{http://www.iana.org/time-zones,
Packit Service 82fcde
Time Zone Database} of time zone and daylight saving time
Packit Service 82fcde
information for most regions of the world, which is maintained by a
Packit Service 82fcde
community of volunteers and put in the public domain.
Packit Service 82fcde
Packit Service 82fcde
@node Time Zone Functions
Packit Service 82fcde
@subsection Functions and Variables for Time Zones
Packit Service 82fcde
Packit Service 82fcde
@deftypevar {char *} tzname [2]
Packit Service 82fcde
@standards{POSIX.1, time.h}
Packit Service 82fcde
The array @code{tzname} contains two strings, which are the standard
Packit Service 82fcde
names of the pair of time zones (standard and Daylight
Packit Service 82fcde
Saving) that the user has selected.  @code{tzname[0]} is the name of
Packit Service 82fcde
the standard time zone (for example, @code{"EST"}), and @code{tzname[1]}
Packit Service 82fcde
is the name for the time zone when Daylight Saving Time is in use (for
Packit Service 82fcde
example, @code{"EDT"}).  These correspond to the @var{std} and @var{dst}
Packit Service 82fcde
strings (respectively) from the @code{TZ} environment variable.  If
Packit Service 82fcde
Daylight Saving Time is never used, @code{tzname[1]} is the empty string.
Packit Service 82fcde
Packit Service 82fcde
The @code{tzname} array is initialized from the @code{TZ} environment
Packit Service 82fcde
variable whenever @code{tzset}, @code{ctime}, @code{strftime},
Packit Service 82fcde
@code{mktime}, or @code{localtime} is called.  If multiple abbreviations
Packit Service 82fcde
have been used (e.g. @code{"EWT"} and @code{"EDT"} for U.S. Eastern War
Packit Service 82fcde
Time and Eastern Daylight Time), the array contains the most recent
Packit Service 82fcde
abbreviation.
Packit Service 82fcde
Packit Service 82fcde
The @code{tzname} array is required for POSIX.1 compatibility, but in
Packit Service 82fcde
GNU programs it is better to use the @code{tm_zone} member of the
Packit Service 82fcde
broken-down time structure, since @code{tm_zone} reports the correct
Packit Service 82fcde
abbreviation even when it is not the latest one.
Packit Service 82fcde
Packit Service 82fcde
Though the strings are declared as @code{char *} the user must refrain
Packit Service 82fcde
from modifying these strings.  Modifying the strings will almost certainly
Packit Service 82fcde
lead to trouble.
Packit Service 82fcde
Packit Service 82fcde
@end deftypevar
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void tzset (void)
Packit Service 82fcde
@standards{POSIX.1, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c tzset @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  libc_lock_lock dup @asulock @aculock
Packit Service 82fcde
@c  tzset_internal dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit Service 82fcde
@c  libc_lock_unlock dup @aculock
Packit Service 82fcde
The @code{tzset} function initializes the @code{tzname} variable from
Packit Service 82fcde
the value of the @code{TZ} environment variable.  It is not usually
Packit Service 82fcde
necessary for your program to call this function, because it is called
Packit Service 82fcde
automatically when you use the other time conversion functions that
Packit Service 82fcde
depend on the time zone.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
The following variables are defined for compatibility with System V
Packit Service 82fcde
Unix.  Like @code{tzname}, these variables are set by calling
Packit Service 82fcde
@code{tzset} or the other time conversion functions.
Packit Service 82fcde
Packit Service 82fcde
@deftypevar {long int} timezone
Packit Service 82fcde
@standards{SVID, time.h}
Packit Service 82fcde
This contains the difference between UTC and the latest local standard
Packit Service 82fcde
time, in seconds west of UTC.  For example, in the U.S. Eastern time
Packit Service 82fcde
zone, the value is @code{5*60*60}.  Unlike the @code{tm_gmtoff} member
Packit Service 82fcde
of the broken-down time structure, this value is not adjusted for
Packit Service 82fcde
daylight saving, and its sign is reversed.  In GNU programs it is better
Packit Service 82fcde
to use @code{tm_gmtoff}, since it contains the correct offset even when
Packit Service 82fcde
it is not the latest one.
Packit Service 82fcde
@end deftypevar
Packit Service 82fcde
Packit Service 82fcde
@deftypevar int daylight
Packit Service 82fcde
@standards{SVID, time.h}
Packit Service 82fcde
This variable has a nonzero value if Daylight Saving Time rules apply.
Packit Service 82fcde
A nonzero value does not necessarily mean that Daylight Saving Time is
Packit Service 82fcde
now in effect; it means only that Daylight Saving Time is sometimes in
Packit Service 82fcde
effect.
Packit Service 82fcde
@end deftypevar
Packit Service 82fcde
Packit Service 82fcde
@node Time Functions Example
Packit Service 82fcde
@subsection Time Functions Example
Packit Service 82fcde
Packit Service 82fcde
Here is an example program showing the use of some of the calendar time
Packit Service 82fcde
functions.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@include strftim.c.texi
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
It produces output like this:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
Wed Jul 31 13:02:36 1991
Packit Service 82fcde
Today is Wednesday, July 31.
Packit Service 82fcde
The time is 01:02 PM.
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Setting an Alarm
Packit Service 82fcde
@section Setting an Alarm
Packit Service 82fcde
Packit Service 82fcde
The @code{alarm} and @code{setitimer} functions provide a mechanism for a
Packit Service 82fcde
process to interrupt itself in the future.  They do this by setting a
Packit Service 82fcde
timer; when the timer expires, the process receives a signal.
Packit Service 82fcde
Packit Service 82fcde
@cindex setting an alarm
Packit Service 82fcde
@cindex interval timer, setting
Packit Service 82fcde
@cindex alarms, setting
Packit Service 82fcde
@cindex timers, setting
Packit Service 82fcde
Each process has three independent interval timers available:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
A real-time timer that counts elapsed time.  This timer sends a
Packit Service 82fcde
@code{SIGALRM} signal to the process when it expires.
Packit Service 82fcde
@cindex real-time timer
Packit Service 82fcde
@cindex timer, real-time
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
A virtual timer that counts processor time used by the process.  This timer
Packit Service 82fcde
sends a @code{SIGVTALRM} signal to the process when it expires.
Packit Service 82fcde
@cindex virtual timer
Packit Service 82fcde
@cindex timer, virtual
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
A profiling timer that counts both processor time used by the process,
Packit Service 82fcde
and processor time spent in system calls on behalf of the process.  This
Packit Service 82fcde
timer sends a @code{SIGPROF} signal to the process when it expires.
Packit Service 82fcde
@cindex profiling timer
Packit Service 82fcde
@cindex timer, profiling
Packit Service 82fcde
Packit Service 82fcde
This timer is useful for profiling in interpreters.  The interval timer
Packit Service 82fcde
mechanism does not have the fine granularity necessary for profiling
Packit Service 82fcde
native code.
Packit Service 82fcde
@c @xref{profil} !!!
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
You can only have one timer of each kind set at any given time.  If you
Packit Service 82fcde
set a timer that has not yet expired, that timer is simply reset to the
Packit Service 82fcde
new value.
Packit Service 82fcde
Packit Service 82fcde
You should establish a handler for the appropriate alarm signal using
Packit Service 82fcde
@code{signal} or @code{sigaction} before issuing a call to
Packit Service 82fcde
@code{setitimer} or @code{alarm}.  Otherwise, an unusual chain of events
Packit Service 82fcde
could cause the timer to expire before your program establishes the
Packit Service 82fcde
handler.  In this case it would be terminated, since termination is the
Packit Service 82fcde
default action for the alarm signals.  @xref{Signal Handling}.
Packit Service 82fcde
Packit Service 82fcde
To be able to use the alarm function to interrupt a system call which
Packit Service 82fcde
might block otherwise indefinitely it is important to @emph{not} set the
Packit Service 82fcde
@code{SA_RESTART} flag when registering the signal handler using
Packit Service 82fcde
@code{sigaction}.  When not using @code{sigaction} things get even
Packit Service 82fcde
uglier: the @code{signal} function has fixed semantics with respect
Packit Service 82fcde
to restarts.  The BSD semantics for this function is to set the flag.
Packit Service 82fcde
Therefore, if @code{sigaction} for whatever reason cannot be used, it is
Packit Service 82fcde
necessary to use @code{sysv_signal} and not @code{signal}.
Packit Service 82fcde
Packit Service 82fcde
The @code{setitimer} function is the primary means for setting an alarm.
Packit Service 82fcde
This facility is declared in the header file @file{sys/time.h}.  The
Packit Service 82fcde
@code{alarm} function, declared in @file{unistd.h}, provides a somewhat
Packit Service 82fcde
simpler interface for setting the real-time timer.
Packit Service 82fcde
@pindex unistd.h
Packit Service 82fcde
@pindex sys/time.h
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} {struct itimerval}
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
This structure is used to specify when a timer should expire.  It contains
Packit Service 82fcde
the following members:
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item struct timeval it_interval
Packit Service 82fcde
This is the period between successive timer interrupts.  If zero, the
Packit Service 82fcde
alarm will only be sent once.
Packit Service 82fcde
Packit Service 82fcde
@item struct timeval it_value
Packit Service 82fcde
This is the period between now and the first timer interrupt.  If zero,
Packit Service 82fcde
the alarm is disabled.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
The @code{struct timeval} data type is described in @ref{Elapsed Time}.
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int setitimer (int @var{which}, const struct itimerval *@var{new}, struct itimerval *@var{old})
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtstimer{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c This function is marked with @mtstimer because the same set of timers
Packit Service 82fcde
@c is shared by all threads of a process, so calling it in one thread
Packit Service 82fcde
@c may interfere with timers set by another thread.  This interference
Packit Service 82fcde
@c is not regarded as destructive, because the interface specification
Packit Service 82fcde
@c makes this overriding while returning the previous value the expected
Packit Service 82fcde
@c behavior, and the kernel will serialize concurrent calls so that the
Packit Service 82fcde
@c last one prevails, with each call getting the timer information from
Packit Service 82fcde
@c the timer installed by the previous call in that serialization.
Packit Service 82fcde
The @code{setitimer} function sets the timer specified by @var{which}
Packit Service 82fcde
according to @var{new}.  The @var{which} argument can have a value of
Packit Service 82fcde
@code{ITIMER_REAL}, @code{ITIMER_VIRTUAL}, or @code{ITIMER_PROF}.
Packit Service 82fcde
Packit Service 82fcde
If @var{old} is not a null pointer, @code{setitimer} returns information
Packit Service 82fcde
about any previous unexpired timer of the same kind in the structure it
Packit Service 82fcde
points to.
Packit Service 82fcde
Packit Service 82fcde
The return value is @code{0} on success and @code{-1} on failure.  The
Packit Service 82fcde
following @code{errno} error conditions are defined for this function:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item EINVAL
Packit Service 82fcde
The timer period is too large.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int getitimer (int @var{which}, struct itimerval *@var{old})
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
The @code{getitimer} function stores information about the timer specified
Packit Service 82fcde
by @var{which} in the structure pointed at by @var{old}.
Packit Service 82fcde
Packit Service 82fcde
The return value and error conditions are the same as for @code{setitimer}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@vtable @code
Packit Service 82fcde
@item ITIMER_REAL
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
This constant can be used as the @var{which} argument to the
Packit Service 82fcde
@code{setitimer} and @code{getitimer} functions to specify the real-time
Packit Service 82fcde
timer.
Packit Service 82fcde
Packit Service 82fcde
@item ITIMER_VIRTUAL
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
This constant can be used as the @var{which} argument to the
Packit Service 82fcde
@code{setitimer} and @code{getitimer} functions to specify the virtual
Packit Service 82fcde
timer.
Packit Service 82fcde
Packit Service 82fcde
@item ITIMER_PROF
Packit Service 82fcde
@standards{BSD, sys/time.h}
Packit Service 82fcde
This constant can be used as the @var{which} argument to the
Packit Service 82fcde
@code{setitimer} and @code{getitimer} functions to specify the profiling
Packit Service 82fcde
timer.
Packit Service 82fcde
@end vtable
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {unsigned int} alarm (unsigned int @var{seconds})
Packit Service 82fcde
@standards{POSIX.1, unistd.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtstimer{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Wrapper for setitimer.
Packit Service 82fcde
The @code{alarm} function sets the real-time timer to expire in
Packit Service 82fcde
@var{seconds} seconds.  If you want to cancel any existing alarm, you
Packit Service 82fcde
can do this by calling @code{alarm} with a @var{seconds} argument of
Packit Service 82fcde
zero.
Packit Service 82fcde
Packit Service 82fcde
The return value indicates how many seconds remain before the previous
Packit Service 82fcde
alarm would have been sent.  If there was no previous alarm, @code{alarm}
Packit Service 82fcde
returns zero.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
The @code{alarm} function could be defined in terms of @code{setitimer}
Packit Service 82fcde
like this:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
unsigned int
Packit Service 82fcde
alarm (unsigned int seconds)
Packit Service 82fcde
@{
Packit Service 82fcde
  struct itimerval old, new;
Packit Service 82fcde
  new.it_interval.tv_usec = 0;
Packit Service 82fcde
  new.it_interval.tv_sec = 0;
Packit Service 82fcde
  new.it_value.tv_usec = 0;
Packit Service 82fcde
  new.it_value.tv_sec = (long int) seconds;
Packit Service 82fcde
  if (setitimer (ITIMER_REAL, &new, &old) < 0)
Packit Service 82fcde
    return 0;
Packit Service 82fcde
  else
Packit Service 82fcde
    return old.it_value.tv_sec;
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
There is an example showing the use of the @code{alarm} function in
Packit Service 82fcde
@ref{Handler Returns}.
Packit Service 82fcde
Packit Service 82fcde
If you simply want your process to wait for a given number of seconds,
Packit Service 82fcde
you should use the @code{sleep} function.  @xref{Sleeping}.
Packit Service 82fcde
Packit Service 82fcde
You shouldn't count on the signal arriving precisely when the timer
Packit Service 82fcde
expires.  In a multiprocessing environment there is typically some
Packit Service 82fcde
amount of delay involved.
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability Note:} The @code{setitimer} and @code{getitimer}
Packit Service 82fcde
functions are derived from BSD Unix, while the @code{alarm} function is
Packit Service 82fcde
specified by the POSIX.1 standard.  @code{setitimer} is more powerful than
Packit Service 82fcde
@code{alarm}, but @code{alarm} is more widely used.
Packit Service 82fcde
Packit Service 82fcde
@node Sleeping
Packit Service 82fcde
@section Sleeping
Packit Service 82fcde
Packit Service 82fcde
The function @code{sleep} gives a simple way to make the program wait
Packit Service 82fcde
for a short interval.  If your program doesn't use signals (except to
Packit Service 82fcde
terminate), then you can expect @code{sleep} to wait reliably throughout
Packit Service 82fcde
the specified interval.  Otherwise, @code{sleep} can return sooner if a
Packit Service 82fcde
signal arrives; if you want to wait for a given interval regardless of
Packit Service 82fcde
signals, use @code{select} (@pxref{Waiting for I/O}) and don't specify
Packit Service 82fcde
any descriptors to wait for.
Packit Service 82fcde
@c !!! select can get EINTR; using SA_RESTART makes sleep win too.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {unsigned int} sleep (unsigned int @var{seconds})
Packit Service 82fcde
@standards{POSIX.1, unistd.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtascusig{:SIGCHLD/linux}}@asunsafe{}@acunsafe{}}
Packit Service 82fcde
@c On Mach, it uses ports and calls time.  On generic posix, it calls
Packit Service 82fcde
@c nanosleep.  On Linux, it temporarily blocks SIGCHLD, which is MT- and
Packit Service 82fcde
@c AS-Unsafe, and in a way that makes it AC-Unsafe (C-unsafe, even!).
Packit Service 82fcde
The @code{sleep} function waits for @var{seconds} seconds or until a signal
Packit Service 82fcde
is delivered, whichever happens first.
Packit Service 82fcde
Packit Service 82fcde
If @code{sleep} returns because the requested interval is over,
Packit Service 82fcde
it returns a value of zero.  If it returns because of delivery of a
Packit Service 82fcde
signal, its return value is the remaining time in the sleep interval.
Packit Service 82fcde
Packit Service 82fcde
The @code{sleep} function is declared in @file{unistd.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Resist the temptation to implement a sleep for a fixed amount of time by
Packit Service 82fcde
using the return value of @code{sleep}, when nonzero, to call
Packit Service 82fcde
@code{sleep} again.  This will work with a certain amount of accuracy as
Packit Service 82fcde
long as signals arrive infrequently.  But each signal can cause the
Packit Service 82fcde
eventual wakeup time to be off by an additional second or so.  Suppose a
Packit Service 82fcde
few signals happen to arrive in rapid succession by bad luck---there is
Packit Service 82fcde
no limit on how much this could shorten or lengthen the wait.
Packit Service 82fcde
Packit Service 82fcde
Instead, compute the calendar time at which the program should stop
Packit Service 82fcde
waiting, and keep trying to wait until that calendar time.  This won't
Packit Service 82fcde
be off by more than a second.  With just a little more work, you can use
Packit Service 82fcde
@code{select} and make the waiting period quite accurate.  (Of course,
Packit Service 82fcde
heavy system load can cause additional unavoidable delays---unless the
Packit Service 82fcde
machine is dedicated to one application, there is no way you can avoid
Packit Service 82fcde
this.)
Packit Service 82fcde
Packit Service 82fcde
On some systems, @code{sleep} can do strange things if your program uses
Packit Service 82fcde
@code{SIGALRM} explicitly.  Even if @code{SIGALRM} signals are being
Packit Service 82fcde
ignored or blocked when @code{sleep} is called, @code{sleep} might
Packit Service 82fcde
return prematurely on delivery of a @code{SIGALRM} signal.  If you have
Packit Service 82fcde
established a handler for @code{SIGALRM} signals and a @code{SIGALRM}
Packit Service 82fcde
signal is delivered while the process is sleeping, the action taken
Packit Service 82fcde
might be just to cause @code{sleep} to return instead of invoking your
Packit Service 82fcde
handler.  And, if @code{sleep} is interrupted by delivery of a signal
Packit Service 82fcde
whose handler requests an alarm or alters the handling of @code{SIGALRM},
Packit Service 82fcde
this handler and @code{sleep} will interfere.
Packit Service 82fcde
Packit Service 82fcde
On @gnusystems{}, it is safe to use @code{sleep} and @code{SIGALRM} in
Packit Service 82fcde
the same program, because @code{sleep} does not work by means of
Packit Service 82fcde
@code{SIGALRM}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int nanosleep (const struct timespec *@var{requested_time}, struct timespec *@var{remaining})
Packit Service 82fcde
@standards{POSIX.1, time.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c On Linux, it's a syscall.  On Mach, it calls gettimeofday and uses
Packit Service 82fcde
@c ports.
Packit Service 82fcde
If resolution to seconds is not enough the @code{nanosleep} function can
Packit Service 82fcde
be used.  As the name suggests the sleep interval can be specified in
Packit Service 82fcde
nanoseconds.  The actual elapsed time of the sleep interval might be
Packit Service 82fcde
longer since the system rounds the elapsed time you request up to the
Packit Service 82fcde
next integer multiple of the actual resolution the system can deliver.
Packit Service 82fcde
Packit Service 82fcde
*@code{requested_time} is the elapsed time of the interval you want to
Packit Service 82fcde
sleep.
Packit Service 82fcde
Packit Service 82fcde
The function returns as *@code{remaining} the elapsed time left in the
Packit Service 82fcde
interval for which you requested to sleep.  If the interval completed
Packit Service 82fcde
without getting interrupted by a signal, this is zero.
Packit Service 82fcde
Packit Service 82fcde
@code{struct timespec} is described in @xref{Elapsed Time}.
Packit Service 82fcde
Packit Service 82fcde
If the function returns because the interval is over the return value is
Packit Service 82fcde
zero.  If the function returns @math{-1} the global variable @var{errno}
Packit Service 82fcde
is set to the following values:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item EINTR
Packit Service 82fcde
The call was interrupted because a signal was delivered to the thread.
Packit Service 82fcde
If the @var{remaining} parameter is not the null pointer the structure
Packit Service 82fcde
pointed to by @var{remaining} is updated to contain the remaining
Packit Service 82fcde
elapsed time.
Packit Service 82fcde
Packit Service 82fcde
@item EINVAL
Packit Service 82fcde
The nanosecond value in the @var{requested_time} parameter contains an
Packit Service 82fcde
illegal value.  Either the value is negative or greater than or equal to
Packit Service 82fcde
1000 million.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
This function is a cancellation point in multi-threaded programs.  This
Packit Service 82fcde
is a problem if the thread allocates some resources (like memory, file
Packit Service 82fcde
descriptors, semaphores or whatever) at the time @code{nanosleep} is
Packit Service 82fcde
called.  If the thread gets canceled these resources stay allocated
Packit Service 82fcde
until the program ends.  To avoid this calls to @code{nanosleep} should
Packit Service 82fcde
be protected using cancellation handlers.
Packit Service 82fcde
@c ref pthread_cleanup_push / pthread_cleanup_pop
Packit Service 82fcde
Packit Service 82fcde
The @code{nanosleep} function is declared in @file{time.h}.
Packit Service 82fcde
@end deftypefun