liveBook · Manning

System time

As explained in Modern C, Third Edition

Calendar time is a representation of time organized for human interpretation as dates and clock times.

System time represents calendar time in a form suited to human interpretation: dates, clock times, appointments, and birthdays. In C, <time.h> supplies a current timestamp as time_t; two timestamps can be compared with difftime to express elapsed time. section 8.6 section 8.9

time_t time(time_t *t);
double difftime(time_t time1, time_t time0);
time_t mktime(struct tm tm[1]);
size_t strftime(char s[static 1], size_t max,
                char const format[static 1],
                struct tm const tm[static 1]);
int timespec_get(struct timespec ts[static 1], int base);
int timespec_getres(struct timespec ts[static 1], int base);

These declarations provide the core C interfaces for obtaining, comparing, converting, formatting, and querying calendar times. section 8.6

A calendar time can be represented in two complementary ways:

  • Use time_t for an arithmetic timestamp representing a calendar time and difftime for the difference between two timestamps. section 8.6
  • Use struct tm for hierarchical human fields such as year, month, day, weekday, and seconds. section 8.6
  • Remember that most struct tm fields begin at zero: tm_mon is zero-based, tm_wday 0 means Sunday, and tm_year requires adding 1900. section 8.6
  • Use mktime to normalize hierarchical fields, calculate tm_wday and tm_yday, update daylight-saving status when requested, and return a time_t. section 8.6

Reading struct tm fields

A struct tm with tm_mon equal to 0 denotes January, while tm_mday equal to 1 denotes the first day of the month. A tm_year value represents the Gregorian year minus 1900, so interpreting the fields requires applying the documented exceptions rather than treating every member alike. section 8.6

How Modern C, Third Edition covers it

How deep it goes
Explains time_t, struct tm, member ranges, and mktime normalization, while leaving physical-time representation as a separate topic.
Examples
Uses time_t timestamps, difftime, struct tm fields, mktime normalization, and strftime format specifiers as concrete interfaces.
What you can do after
Apply it via C time.h interfaces to represent, compare, normalize, convert, and format calendar times.
How it connects
Connects calendar time with physical time, time zones, daylight-saving time, locales, and ISO 8601 formatting.

Also explained in

Compare how 3 books explain system time →

Related concepts

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