Blame man2/gettimeofday.2

Packit 7cfc04
.\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified by Michael Haardt (michael@moria.de)
Packit 7cfc04
.\" Modified 1993-07-23 by Rik Faith (faith@cs.unc.edu)
Packit 7cfc04
.\" Modified 1994-08-21 by Michael Chastain (mec@shell.portal.com):
Packit 7cfc04
.\"   Fixed necessary '#include' lines.
Packit 7cfc04
.\" Modified 1995-04-15 by Michael Chastain (mec@shell.portal.com):
Packit 7cfc04
.\"   Added reference to adjtimex.
Packit 7cfc04
.\" Removed some nonsense lines pointed out by Urs Thuermann,
Packit 7cfc04
.\"   (urs@isnogud.escape.de), aeb, 950722.
Packit 7cfc04
.\" Modified 1997-01-14 by Austin Donnelly (and1000@debian.org):
Packit 7cfc04
.\"   Added return values section, and bit on EFAULT
Packit 7cfc04
.\" Added clarification on timezone, aeb, 971210.
Packit 7cfc04
.\" Removed "#include <unistd.h>", aeb, 010316.
Packit 7cfc04
.\" Modified, 2004-05-27 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"   Added notes on capability requirement.
Packit 7cfc04
.\"
Packit 7cfc04
.TH GETTIMEOFDAY 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
gettimeofday, settimeofday \- get / set time
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <sys/time.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int gettimeofday(struct timeval *" tv ", struct timezone *" tz );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int settimeofday(const struct timeval *" tv \
Packit 7cfc04
", const struct timezone *" tz );
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR settimeofday ():
Packit 7cfc04
    Since glibc 2.19:
Packit 7cfc04
        _DEFAULT_SOURCE
Packit 7cfc04
    Glibc 2.19 and earlier:
Packit 7cfc04
        _BSD_SOURCE
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The functions
Packit 7cfc04
.BR gettimeofday ()
Packit 7cfc04
and
Packit 7cfc04
.BR settimeofday ()
Packit 7cfc04
can get and set the time as well as a timezone.
Packit 7cfc04
The
Packit 7cfc04
.I tv
Packit 7cfc04
argument is a
Packit 7cfc04
.I struct timeval
Packit 7cfc04
(as specified in
Packit 7cfc04
.IR <sys/time.h> ):
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct timeval {
Packit 7cfc04
    time_t      tv_sec;     /* seconds */
Packit 7cfc04
    suseconds_t tv_usec;    /* microseconds */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
and gives the number of seconds and microseconds since the Epoch (see
Packit 7cfc04
.BR time (2)).
Packit 7cfc04
The
Packit 7cfc04
.I tz
Packit 7cfc04
argument is a
Packit 7cfc04
.IR "struct timezone" :
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct timezone {
Packit 7cfc04
    int tz_minuteswest;     /* minutes west of Greenwich */
Packit 7cfc04
    int tz_dsttime;         /* type of DST correction */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
If either
Packit 7cfc04
.I tv
Packit 7cfc04
or
Packit 7cfc04
.I tz
Packit 7cfc04
is NULL, the corresponding structure is not set or returned.
Packit 7cfc04
.\" FIXME . The compilation warning looks to be going away in 2.17
Packit 7cfc04
.\" see glibc commit 4b7634a5e03b0da6f8875de9d3f74c1cf6f2a6e8
Packit 7cfc04
(However, compilation warnings will result if
Packit 7cfc04
.I tv
Packit 7cfc04
is NULL.)
Packit 7cfc04
.\" The following is covered under EPERM below:
Packit 7cfc04
.\" .PP
Packit 7cfc04
.\" Only the superuser may use
Packit 7cfc04
.\" .BR settimeofday ().
Packit 7cfc04
.PP
Packit 7cfc04
The use of the
Packit 7cfc04
.I timezone
Packit 7cfc04
structure is obsolete; the
Packit 7cfc04
.I tz
Packit 7cfc04
argument should normally be specified as NULL.
Packit 7cfc04
(See NOTES below.)
Packit 7cfc04
.PP
Packit 7cfc04
Under Linux, there are some peculiar "warp clock" semantics associated
Packit 7cfc04
with the
Packit 7cfc04
.BR settimeofday ()
Packit 7cfc04
system call if on the very first call (after booting)
Packit 7cfc04
that has a non-NULL
Packit 7cfc04
.I tz
Packit 7cfc04
argument, the
Packit 7cfc04
.I tv
Packit 7cfc04
argument is NULL and the
Packit 7cfc04
.I tz_minuteswest
Packit 7cfc04
field is nonzero.
Packit 7cfc04
(The
Packit 7cfc04
.I tz_dsttime
Packit 7cfc04
field should be zero for this case.)
Packit 7cfc04
In such a case it is assumed that the CMOS clock
Packit 7cfc04
is on local time, and that it has to be incremented by this amount
Packit 7cfc04
to get UTC system time.
Packit 7cfc04
No doubt it is a bad idea to use this feature.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
.BR gettimeofday ()
Packit 7cfc04
and
Packit 7cfc04
.BR settimeofday ()
Packit 7cfc04
return 0 for success, or \-1 for failure (in which case
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately).
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
One of
Packit 7cfc04
.I tv
Packit 7cfc04
or
Packit 7cfc04
.I tz
Packit 7cfc04
pointed outside the accessible address space.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
Timezone (or something else) is invalid.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The calling process has insufficient privilege to call
Packit 7cfc04
.BR settimeofday ();
Packit 7cfc04
under Linux the
Packit 7cfc04
.B CAP_SYS_TIME
Packit 7cfc04
capability is required.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
SVr4, 4.3BSD.
Packit 7cfc04
POSIX.1-2001 describes
Packit 7cfc04
.BR gettimeofday ()
Packit 7cfc04
but not
Packit 7cfc04
.BR settimeofday ().
Packit 7cfc04
POSIX.1-2008 marks
Packit 7cfc04
.BR gettimeofday ()
Packit 7cfc04
as obsolete, recommending the use of
Packit 7cfc04
.BR clock_gettime (2)
Packit 7cfc04
instead.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
The time returned by
Packit 7cfc04
.BR gettimeofday ()
Packit 7cfc04
.I is
Packit 7cfc04
affected by discontinuous jumps in the system time
Packit 7cfc04
(e.g., if the system administrator manually changes the system time).
Packit 7cfc04
If you need a monotonically increasing clock, see
Packit 7cfc04
.BR clock_gettime (2).
Packit 7cfc04
.PP
Packit 7cfc04
Macros for operating on
Packit 7cfc04
.I timeval
Packit 7cfc04
structures are described in
Packit 7cfc04
.BR timeradd (3).
Packit 7cfc04
.PP
Packit 7cfc04
Traditionally, the fields of
Packit 7cfc04
.I struct timeval
Packit 7cfc04
were of type
Packit 7cfc04
.IR long .
Packit 7cfc04
.\"
Packit 7cfc04
.SS C library/kernel differences
Packit 7cfc04
On some architectures, an implementation of
Packit 7cfc04
.BR gettimeofday ()
Packit 7cfc04
is provided in the
Packit 7cfc04
.BR vdso (7).
Packit 7cfc04
.\"
Packit 7cfc04
.SS The tz_dsttime field
Packit 7cfc04
On a non-Linux kernel, with glibc, the
Packit 7cfc04
.I tz_dsttime
Packit 7cfc04
field of
Packit 7cfc04
.I struct timezone
Packit 7cfc04
will be set to a nonzero value by
Packit 7cfc04
.BR gettimeofday ()
Packit 7cfc04
if the current timezone has ever had or will have a daylight saving
Packit 7cfc04
rule applied.
Packit 7cfc04
In this sense it exactly mirrors the meaning of
Packit 7cfc04
.BR daylight (3)
Packit 7cfc04
for the current zone.
Packit 7cfc04
On Linux, with glibc, the setting of the
Packit 7cfc04
.I tz_dsttime
Packit 7cfc04
field of
Packit 7cfc04
.I struct timezone
Packit 7cfc04
has never been used by
Packit 7cfc04
.BR settimeofday ()
Packit 7cfc04
or
Packit 7cfc04
.BR gettimeofday ().
Packit 7cfc04
.\" it has not
Packit 7cfc04
.\" been and will not be supported by libc or glibc.
Packit 7cfc04
.\" Each and every occurrence of this field in the kernel source
Packit 7cfc04
.\" (other than the declaration) is a bug.
Packit 7cfc04
Thus, the following is purely of historical interest.
Packit 7cfc04
.PP
Packit 7cfc04
On old systems, the field
Packit 7cfc04
.I tz_dsttime
Packit 7cfc04
contains a symbolic constant (values are given below)
Packit 7cfc04
that indicates in which part of the year Daylight Saving Time
Packit 7cfc04
is in force.
Packit 7cfc04
(Note: this value is constant throughout the year:
Packit 7cfc04
it does not indicate that DST is in force, it just selects an
Packit 7cfc04
algorithm.)
Packit 7cfc04
The daylight saving time algorithms defined are as follows:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
\fBDST_NONE\fP     /* not on DST */
Packit 7cfc04
\fBDST_USA\fP      /* USA style DST */
Packit 7cfc04
\fBDST_AUST\fP     /* Australian style DST */
Packit 7cfc04
\fBDST_WET\fP      /* Western European DST */
Packit 7cfc04
\fBDST_MET\fP      /* Middle European DST */
Packit 7cfc04
\fBDST_EET\fP      /* Eastern European DST */
Packit 7cfc04
\fBDST_CAN\fP      /* Canada */
Packit 7cfc04
\fBDST_GB\fP       /* Great Britain and Eire */
Packit 7cfc04
\fBDST_RUM\fP      /* Romania */
Packit 7cfc04
\fBDST_TUR\fP      /* Turkey */
Packit 7cfc04
\fBDST_AUSTALT\fP  /* Australian style with shift in 1986 */
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
Of course it turned out that the period in which
Packit 7cfc04
Daylight Saving Time is in force cannot be given
Packit 7cfc04
by a simple algorithm, one per country; indeed,
Packit 7cfc04
this period is determined by unpredictable political
Packit 7cfc04
decisions.
Packit 7cfc04
So this method of representing timezones
Packit 7cfc04
has been abandoned.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR date (1),
Packit 7cfc04
.BR adjtimex (2),
Packit 7cfc04
.BR clock_gettime (2),
Packit 7cfc04
.BR time (2),
Packit 7cfc04
.BR ctime (3),
Packit 7cfc04
.BR ftime (3),
Packit 7cfc04
.BR timeradd (3),
Packit 7cfc04
.BR capabilities (7),
Packit 7cfc04
.BR time (7),
Packit 7cfc04
.BR vdso (7),
Packit 7cfc04
.BR hwclock (8)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.