A calendar date in C
For 3 April 2019 at 10:00:47, initialize `struct tm today = { .tm_year = 2019-1900, .tm_mon = 4-1, .tm_mday = 3, .tm_hour = 10, .tm_min = 0, .tm_sec = 47 };`. The subtractions matter: `tm_year` counts years since 1900, and `tm_mon` counts months since January, starting at 0. section 6.3 section 8.5
Defined in `<time.h>`, `struct tm` groups named calendar-time components into one C type. Member names avoid the positional ambiguity of an array: `today.tm_year` identifies the year component and can be used in an expression, such as `today.tm_year + 1900`. section 6.3
29 struct tm today = {
30 .tm_year = 2019-1900,
31 .tm_mon = 4-1,
32 .tm_mday = 3,
33 .tm_hour = 10,
34 .tm_min = 0,
35 .tm_sec = 47,
36 };
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":8},{\"line\":0,\"ch\":14}],[{\"line\":0,\"ch\":15},{\"line\":0,\"ch\":17}],[{\"line\":1,\"ch\":11},{\"line\":1,\"ch\":13}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":13}],[{\"line\":3,\"ch\":11},{\"line\":3,\"ch\":13}],[{\"line\":4,\"ch\":11},{\"line\":4,\"ch\":13}],[{\"line\":5,\"ch\":11},{\"line\":5,\"ch\":13}],[{\"line\":6,\"ch\":11},{\"line\":6,\"ch\":13}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":11},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":17}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":3,\"ch\":11},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":4,\"ch\":11},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":5,\"ch\":11},{\"line\":5,\"ch\":17}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":2}],[{\"line\":6,\"ch\":11},{\"line\":6,\"ch\":17}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":2}]]"}
!@%STYLE%@!
The initializer names each component, so its member order need not match the structure declaration. section 6.3
TAKEAWAY — Check each member’s counting rule before using it:
- `tm_mday` starts at 1; `tm_mon` starts at 0 for January, and `tm_wday` starts at 0 for Sunday. section 8.5
- `tm_sec` can range from 0 through 60; the upper value accommodates leap seconds. section 8.5
- `tm_wday` gives the weekday, `tm_yday` the day in the year, and `tm_isdst` daylight-saving-time information for the local time zone. section 8.5
TAKEAWAY — Omitted members in a struct initializer become 0. Thus the date above leaves `tm_wday`, `tm_yday`, and `tm_isdst` at 0; the first two do not necessarily agree with the initialized date. `mktime` normalizes the date members, sets the corresponding weekday and day of year, and returns a `time_t` value. A negative `tm_isdst` lets it determine the daylight-saving-time flag for the local platform. section 6.3 section 8.5
`struct tm` is the component-based calendar representation; `time_t` is an arithmetic type better suited to computing with times. Other `<time.h>` interfaces include `time` for a current timestamp, `difftime` for a difference between timestamps, and `strftime` for formatted calendar output. section 8.5

