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 at securelevel >= 2 (see init(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 the adjtime(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 supplied timeval value 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