liveBook · Manning

Time_t

As explained in Rust in Action: Systems programming concepts and techniques

time_t is a libc type alias representing elapsed whole seconds since the Unix epoch.

time_t is a libc type alias for the whole-second portion of a time value: the number of seconds elapsed since an epoch, commonly the Unix epoch. Its underlying integer type depends on the platform, so code should use the alias rather than assume a fixed-width integer. section 9.4 section 9.7.2

Keep these properties in mind when working with libc time values:

  • Treat time_t as the seconds component; suseconds_t represents the fractional component. section 9.7.2
  • Expect the representation to vary: libc implementations may use signed or unsigned integer types, and some platforms use a different epoch. section 9.4
  • Use the alias when converting application timestamps for operating-system interfaces instead of hard-coding an integer type. section 9.7.2

Putting seconds into timeval

A libc clock-setting operation stores whole seconds in timeval.tv_sec and fractional microseconds in timeval.tv_usec. The application converts its timestamp into the platform’s aliases before calling settimeofday. section 9.7.2

#![allow(non_camel_case_types)]
 
type time_t = i64;
type suseconds_t = i64;
 
pub struct timeval {
    pub tv_sec: time_t,
    pub tv_usec: suseconds_t,
}

This compact definition shows the alias relationship and how time_t and suseconds_t become the field types of timeval. section 9.7.2

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Defines the alias and its clock-structure role, then stops short of prescribing one integer representation across platforms.
Examples
Uses timeval.tv_sec, timeval.tv_usec, timestamp conversion, and settimeofday to demonstrate a clock-setting operation.
What it assumes
Assumes basic Rust and operating-system concepts while teaching libc naming conventions, aliases, and timeval fields.
What you can do after
Operate/configure system time through libc while preserving the platform’s expected seconds and fractional-seconds types.
How it connects
Connects time_t with libc, Unix epoch, timeval, suseconds_t, settimeofday, and platform-specific operating-system timekeeping.

Also explained in

Compare how 2 books explain time_t →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.