Reference: time.h
The clock, the calendar, strftime, and timing code.
4 minute read
Big idea
<time.h> reads the clock and the calendar: the current time, breaking it into year/month/day, formatting it as text, and measuring how long code takes.
The types
time_t— a point in time, usually seconds since 00:00 UTC on 1 January 1970.struct tm— a time broken into parts (see the table below).clock_t— processor time used by your program, in "ticks".
struct tm fields
| Field | Meaning | Range |
|---|---|---|
tm_year | years since 1900 | 2026 is 126 |
tm_mon | month, starting at 0 | 0 (Jan) – 11 (Dec) |
tm_mday | day of the month | 1 – 31 |
tm_hour, tm_min, tm_sec | time of day | 0–23, 0–59, 0–60 |
tm_wday | day of the week | 0 (Sunday) – 6 |
tm_yday | day of the year | 0 – 365 |
tm_isdst | daylight saving: >0 yes, 0 no, <0 unknown |
Getting the time
time
time_t time(time_t *out);
The current calendar time. Pass NULL (or a pointer to also store it there).
- Example:
time_t now = time(NULL);
localtime / gmtime
struct tm *localtime(const time_t *t);
struct tm *gmtime(const time_t *t);
Break a time_t into a struct tm, in local time or in UTC.
- Gotcha: they return a pointer to one shared static struct — the next call overwrites it. Copy it (
struct tm parts = *localtime(&now);) if you need two at once.
mktime
time_t mktime(struct tm *parts);
The reverse: turns a (local) struct tm into a time_t. It also normalises out-of-range fields — set tm_mday += 40 and it rolls over into the right month.
- Example: adding days to a date safely.
Formatting
strftime
size_t strftime(char *buf, size_t size, const char *format, const struct tm *parts);
Formats a time into buf, like printf for dates. Returns the number of characters written (0 if it didn't fit).
| Code | Gives | Example |
|---|---|---|
%Y | 4-digit year | 2026 |
%m | month 01–12 | 10 |
%d | day 01–31 | 03 |
%H %M %S | hour, minute, second | 14 05 09 |
%A / %a | weekday name / short | Saturday / Sat |
%B / %b | month name / short | October / Oct |
%j | day of the year | 276 |
%% | a literal % | % |
- Example:
strftime(buf, sizeof buf, "%Y-%m-%d", &parts);
difftime
double difftime(time_t end, time_t start);
Seconds between two time_ts, as a double.
ctime / asctime
char *ctime(const time_t *t);
A quick fixed-format string like "Sat Oct 3 14:05:09 2026\n" — note the newline at the end. strftime gives you control.
Measuring how long code takes
clock
clock_t clock(void);
Processor time used so far. Divide a difference by CLOCKS_PER_SEC for seconds.
- Gotcha: it measures CPU time, not wall-clock time — time spent waiting for input doesn't count. Cast to
doublebefore dividing. - Example:
double secs = (double) (clock() - start) / CLOCKS_PER_SEC;
#include <stdio.h>
#include <time.h>
int main(void) {
struct tm d = {0};
d.tm_year = 2026 - 1900; /* years since 1900 */
d.tm_mon = 0; /* January is 0 */
d.tm_mday = 25;
d.tm_hour = 12; /* midday avoids daylight-saving edges */
d.tm_isdst = -1; /* let the library work it out */
d.tm_mday += 10; /* 10 days later: rolls into February */
mktime(&d); /* normalises the fields */
char text[64];
strftime(text, sizeof text, "%Y-%m-%d (%B), day %j", &d);
printf("%s\n", text);
return 0;
}output
2026-02-04 (February), day 035Run and edit it in the app.
Run Math, Random Numbers, and Time's examples to see time(NULL) and clock() with live values.
localtime() fills t->tm_mon with 9. Which month is it?
Check yourself
What year does tm_year = 126 stand for?
How do you turn a clock() difference into seconds?