liveBook · Manning

Time.h

As explained in Modern C

time.h is the C standard header that declares facilities for representing, querying, and manipulating calendar and processor time.

time.h supplies C interfaces for calendar time and physical time. Calendar time can be represented compactly as time_t or decomposed into human-oriented fields in struct tm. section 8.5 section 8.8

Comparing two moments

A small elapsed-time measurement uses two timestamps: time_t first = time(0); /* work */ time_t second = time(0); double elapsed = difftime(second, first); The time function obtains the current calendar time, while difftime expresses the difference between two time_t values. TAKEAWAY: use time_t for representing a calendar moment and difftime for comparing such moments. section 8.5

struct tm exposes calendar components such as year, month, day, and second, but its fields use unequal conventions: tm_mon starts at 0, tm_year requires adding 1900, and tm_mday starts at 1. tm_wday, tm_yday, and tm_isdst provide weekday, day-of-year, and daylight-saving information. section 8.5

mktime normalizes the date fields, calculates weekday and day-of-year fields, adjusts a negative daylight-saving flag for the local platform, and returns the corresponding time_t. strftime formats a struct tm using conversion specifications such as %Y, %m, and %d. section 8.5

How Modern C covers it

How deep it goes
Defines core representations and conversion steps, while deferring detailed locale behavior and implementation-specific formatting results.
Examples
Uses paired time calls, difftime, struct tm fields, mktime normalization, and strftime format specifiers.
What you can do after
Apply via time, difftime, mktime, strftime, and timespec_get to represent, compare, normalize, and format time values.
How it connects
Connects time_t, struct tm, difftime, mktime, strftime, timespec_get, calendar time, physical time, and locale.

Also explained in

Compare how 2 books explain time.h →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.