liveBook · Manning

Calendar

As explained in Modern C

Calendar time is a representation of a date and time in units such as years, months, days, minutes, and seconds.

Calendar time represents dates and times in human-oriented units, from years and months down to seconds. In C, time.h provides interfaces for obtaining, comparing, converting, and formatting these values. section 8.5

Comparing two moments

Call time(0) at two different moments to obtain time_t timestamps, then pass them to difftime to express the elapsed difference. section 8.5

Represent calendar values in the form that matches the task:

  • Use struct tm for hierarchical human-readable fields such as tm_year, tm_mon, tm_mday, and tm_sec. section 8.5
  • Remember the irregular field ranges: tm_mon and tm_wday start at zero, tm_mday starts at one, and tm_year requires adding 1900. section 8.5
  • Use mktime to normalize date members, calculate tm_wday and tm_yday, update daylight-saving information, and obtain a time_t value. section 8.5
  • Use strftime to format a struct tm with format specifiers for fields such as seconds, minutes, months, weekdays, and years. section 8.5
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);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":11},{\"line\":1,\"ch\":15}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":11},{\"line\":3,\"ch\":15}],[{\"line\":6,\"ch\":4},{\"line\":6,\"ch\":8}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":6}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":15}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":13}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":20}],[{\"line\":5,\"ch\":16},{\"line\":5,\"ch\":22}],[{\"line\":6,\"ch\":17},{\"line\":6,\"ch\":23}],[{\"line\":2,\"ch\":21},{\"line\":2,\"ch\":26}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":6}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":15}],[{\"line\":3,\"ch\":16},{\"line\":3,\"ch\":20}],[{\"line\":4,\"ch\":16},{\"line\":4,\"ch\":20}],[{\"line\":3,\"ch\":23},{\"line\":3,\"ch\":29}],[{\"line\":4,\"ch\":34},{\"line\":4,\"ch\":40}],[{\"line\":5,\"ch\":35},{\"line\":5,\"ch\":41}],[{\"line\":6,\"ch\":36},{\"line\":6,\"ch\":42}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":6}],[{\"line\":4,\"ch\":16},{\"line\":4,\"ch\":26}],[{\"line\":3,\"ch\":23},{\"line\":3,\"ch\":29}],[{\"line\":4,\"ch\":34},{\"line\":4,\"ch\":40}],[{\"line\":5,\"ch\":35},{\"line\":5,\"ch\":41}],[{\"line\":6,\"ch\":36},{\"line\":6,\"ch\":42}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":20}],[{\"line\":5,\"ch\":16},{\"line\":5,\"ch\":22}],[{\"line\":6,\"ch\":17},{\"line\":6,\"ch\":23}],[{\"line\":2,\"ch\":21},{\"line\":2,\"ch\":23}],[{\"line\":5,\"ch\":23},{\"line\":5,\"ch\":25}],[{\"line\":4,\"ch\":21},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":26},{\"line\":5,\"ch\":31}],[{\"line\":2,\"ch\":21},{\"line\":2,\"ch\":23}],[{\"line\":5,\"ch\":23},{\"line\":5,\"ch\":25}],[{\"line\":3,\"ch\":23},{\"line\":3,\"ch\":29}],[{\"line\":4,\"ch\":34},{\"line\":4,\"ch\":40}],[{\"line\":5,\"ch\":35},{\"line\":5,\"ch\":41}],[{\"line\":6,\"ch\":36},{\"line\":6,\"ch\":42}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":3}],[{\"line\":6,\"ch\":4},{\"line\":6,\"ch\":16}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":20}],[{\"line\":5,\"ch\":16},{\"line\":5,\"ch\":22}],[{\"line\":6,\"ch\":17},{\"line\":6,\"ch\":23}],[{\"line\":6,\"ch\":4},{\"line\":6,\"ch\":12}],[{\"line\":3,\"ch\":23},{\"line\":3,\"ch\":29}],[{\"line\":4,\"ch\":34},{\"line\":4,\"ch\":40}],[{\"line\":5,\"ch\":35},{\"line\":5,\"ch\":41}],[{\"line\":6,\"ch\":36},{\"line\":6,\"ch\":42}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":3}]]"}
!@%STYLE%@!

Browse the principal time.h declarations for obtaining, comparing, converting, formatting, and querying calendar times. section 8.5

How Modern C covers it

How deep it goes
Explains struct tm fields, exceptional ranges, mktime normalization, and formatting, while distinguishing calendar from physical time.
Examples
Uses time(0) timestamps, difftime comparisons, struct tm date members, mktime normalization, and strftime format specifiers.
What you can do after
Apply calendar time via time.h by obtaining timestamps, comparing moments, normalizing struct tm values, and formatting dates.
How it connects
Connects calendar time with time_t, struct tm, difftime, mktime, strftime, daylight saving time, and physical time.

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.