GETTIMEOFDAY(2)
π GETTIMEOFDAY π
ok bestie, so we're in the System Calls Manual section, and this one is literally about the passage of time itself. iconic behavior.
β¨ NAME β¨
gettimeofday, settimeofday β get/set date and time π°οΈ
π LIBRARY π
Standard C Library (libc, -lc) β she's in the standard library, no extra linking drama required π«Ά
π SYNOPSIS π
ok so like, include the header first, obviously:
#include <sys/time.h>
then bestie you got two little functions:
int
gettimeofday(struct timeval *tp, struct timezone *tzp);
int
settimeofday(const struct timeval *tp, const struct timezone *tzp);
πΈ DESCRIPTION πΈ
So the system's whole notion of the current Greenwich time and the current time zone? You grab it with the gettimeofday() system call and you set it with the settimeofday() system call. That's the vibe. β¨
The time is expressed in seconds and microseconds since midnight (0 hour), January 1, 1970 β yes bestie, the epoch, we love her. The resolution of the system clock is hardware dependent, and the time may be updated continuously or in βticksβ. Not me finding out time might be ticking at you like a clock in a cartoon π
Also: if tp or tzp is NULL, the associated time information will not be returned or set. Respect the NULL, she's valid.
The structures that tp and tzp point to are defined in <sys/time.h> as:
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* and microseconds */
};
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of dst correction */
};
So the timezone structure tells you the local time zone (measured in minutes of time westward from Greenwich), plus a flag that, if nonzero, means Daylight Saving time applies locally during the appropriate part of the year. she's got range π
π¨ super-user only zone, no cap π¨ Only the super-user may set the time of day or time zone. And if the system is running atsecurelevel >= 2(seeinit(8)), the time may only be advanced or retarded by a maximum of one second. This limitation is imposed to prevent a malicious super-user from setting arbitrary time stamps on files. (the way that is a whole security plotline π) But good news: the system time can be adjusted backwards without restriction using theadjtime(2)system call, even when the system is secure. slay.
β RETURN VALUES β
Upon successful completion, the value 0 is returned. Otherwise, the value -1 is returned and the global variable errno is set to indicate the error. classic.
π ERRORS π
The following error codes may be set in errno:
[EINVAL]β The suppliedtimevalvalue is invalid. π«[EPERM]β A user other than the super-user attempted to set the time. bestie you are NOT him π
π₯ SEE ALSO π₯
date(1), adjtime(2), clock_gettime(2), ctime(3), timeradd(3), clocks(7), timed(8)
like a whole little friend group of time-related pages, we stan a supportive network π
π¦ HISTORY π¦
The gettimeofday() system call appeared in 4.2BSD. an OG, fr fr π
ok that's the whole page bestie, go forth and timestamp responsibly ππβ¨
this lil writeup is from the Debian edition, dated August 4, 2018 β tysm for reading, mwah, take care of ur clocks ππ π«Ά
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms