man-pages/man2/gettimeofday.2.html
2021-03-31 01:06:50 +01:00

409 lines
8.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETTIMEOFDAY</TITLE>
</HEAD><BODY>
<H1>GETTIMEOFDAY</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
gettimeofday, settimeofday - get / set time
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/time.h">sys/time.h</A>&gt;</B>
<B>int gettimeofday(struct timeval *</B><I>tv</I><B>, struct timezone *</B><I>tz</I><B>);</B>
<B>int settimeofday(const struct timeval *</B><I>tv</I><B>, const struct timezone *</B><I>tz</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>settimeofday</B>():
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Since&nbsp;glibc&nbsp;2.19:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_DEFAULT_SOURCE
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Glibc&nbsp;2.19&nbsp;and&nbsp;earlier:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_BSD_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The functions
<B>gettimeofday</B>()
and
<B>settimeofday</B>()
can get and set the time as well as a timezone.
<P>
The
<I>tv</I>
argument is a
<I>struct timeval</I>
(as specified in
<I>&lt;<A HREF="file:///usr/include/sys/time.h">sys/time.h</A>&gt;</I>):
<P>
struct timeval {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;time_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tv_sec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;seconds&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;suseconds_t&nbsp;tv_usec;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;microseconds&nbsp;*/
};
<P>
and gives the number of seconds and microseconds since the Epoch (see
<B><A HREF="/cgi-bin/man/man2html?2+time">time</A></B>(2)).
<P>
The
<I>tz</I>
argument is a
<I>struct timezone</I>:
<P>
struct timezone {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;tz_minuteswest;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;minutes&nbsp;west&nbsp;of&nbsp;Greenwich&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;tz_dsttime;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;type&nbsp;of&nbsp;DST&nbsp;correction&nbsp;*/
};
<P>
If either
<I>tv</I>
or
<I>tz</I>
is NULL, the corresponding structure is not set or returned.
(However, compilation warnings will result if
<I>tv</I>
is NULL.)
<P>
The use of the
<I>timezone</I>
structure is obsolete; the
<I>tz</I>
argument should normally be specified as NULL.
(See NOTES below.)
<P>
Under Linux, there are some peculiar &quot;warp clock&quot; semantics associated
with the
<B>settimeofday</B>()
system call if on the very first call (after booting)
that has a non-NULL
<I>tz</I>
argument, the
<I>tv</I>
argument is NULL and the
<I>tz_minuteswest</I>
field is nonzero.
(The
<I>tz_dsttime</I>
field should be zero for this case.)
In such a case it is assumed that the CMOS clock
is on local time, and that it has to be incremented by this amount
to get UTC system time.
No doubt it is a bad idea to use this feature.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
<B>gettimeofday</B>()
and
<B>settimeofday</B>()
return 0 for success, or -1 for failure (in which case
<I>errno</I>
is set appropriately).
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EFAULT</B>
<DD>
One of
<I>tv</I>
or
<I>tz</I>
pointed outside the accessible address space.
<DT id="2"><B>EINVAL</B>
<DD>
(<B>settimeofday</B>()):
<I>timezone</I>
is invalid.
<DT id="3"><B>EINVAL</B>
<DD>
(<B>settimeofday</B>()):
<I>tv.tv_sec</I>
is negative or
<I>tv.tv_usec</I>
is outside the range [0..999,999].
<DT id="4"><B>EINVAL</B> (since Linux 4.3)
<DD>
(<B>settimeofday</B>()):
An attempt was made to set the time to a value less than
the current value of the
<B>CLOCK_MONOTONIC</B>
clock (see
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2)).
<DT id="5"><B>EPERM</B>
<DD>
The calling process has insufficient privilege to call
<B>settimeofday</B>();
under Linux the
<B>CAP_SYS_TIME</B>
capability is required.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
SVr4, 4.3BSD.
POSIX.1-2001 describes
<B>gettimeofday</B>()
but not
<B>settimeofday</B>().
POSIX.1-2008 marks
<B>gettimeofday</B>()
as obsolete, recommending the use of
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2)
instead.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
The time returned by
<B>gettimeofday</B>()
<I>is</I>
affected by discontinuous jumps in the system time
(e.g., if the system administrator manually changes the system time).
If you need a monotonically increasing clock, see
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2).
<P>
Macros for operating on
<I>timeval</I>
structures are described in
<B><A HREF="/cgi-bin/man/man2html?3+timeradd">timeradd</A></B>(3).
<P>
Traditionally, the fields of
<I>struct timeval</I>
were of type
<I>long</I>.
<A NAME="lbAI">&nbsp;</A>
<H3>C library/kernel differences</H3>
On some architectures, an implementation of
<B>gettimeofday</B>()
is provided in the
<B><A HREF="/cgi-bin/man/man2html?7+vdso">vdso</A></B>(7).
<A NAME="lbAJ">&nbsp;</A>
<H3>The tz_dsttime field</H3>
On a non-Linux kernel, with glibc, the
<I>tz_dsttime</I>
field of
<I>struct timezone</I>
will be set to a nonzero value by
<B>gettimeofday</B>()
if the current timezone has ever had or will have a daylight saving
rule applied.
In this sense it exactly mirrors the meaning of
<B><A HREF="/cgi-bin/man/man2html?3+daylight">daylight</A></B>(3)
for the current zone.
On Linux, with glibc, the setting of the
<I>tz_dsttime</I>
field of
<I>struct timezone</I>
has never been used by
<B>settimeofday</B>()
or
<B>gettimeofday</B>().
Thus, the following is purely of historical interest.
<P>
On old systems, the field
<I>tz_dsttime</I>
contains a symbolic constant (values are given below)
that indicates in which part of the year Daylight Saving Time
is in force.
(Note: this value is constant throughout the year:
it does not indicate that DST is in force, it just selects an
algorithm.)
The daylight saving time algorithms defined are as follows:
<P>
<B>DST_NONE</B> /* not on DST */
<B>DST_USA</B> /* USA style DST */
<B>DST_AUST</B> /* Australian style DST */
<B>DST_WET</B> /* Western European DST */
<B>DST_MET</B> /* Middle European DST */
<B>DST_EET</B> /* Eastern European DST */
<B>DST_CAN</B> /* Canada */
<B>DST_GB</B> /* Great Britain and Eire */
<B>DST_RUM</B> /* Romania */
<B>DST_TUR</B> /* Turkey */
<B>DST_AUSTALT</B> /* Australian style with shift in 1986 */
<P>
Of course it turned out that the period in which
Daylight Saving Time is in force cannot be given
by a simple algorithm, one per country; indeed,
this period is determined by unpredictable political
decisions.
So this method of representing timezones
has been abandoned.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+date">date</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+adjtimex">adjtimex</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+time">time</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+ctime">ctime</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+ftime">ftime</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+timeradd">timeradd</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+time">time</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+vdso">vdso</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?8+hwclock">hwclock</A></B>(8)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="12"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="13"><A HREF="#lbAI">C library/kernel differences</A><DD>
<DT id="14"><A HREF="#lbAJ">The tz_dsttime field</A><DD>
</DL>
<DT id="15"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="16"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>