Skip to content
FollowtheDamnCode

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

FieldMeaningRange
tm_yearyears since 19002026 is 126
tm_monmonth, starting at 00 (Jan) – 11 (Dec)
tm_mdayday of the month1 – 31
tm_hour, tm_min, tm_sectime of day0–23, 0–59, 0–60
tm_wdayday of the week0 (Sunday) – 6
tm_ydayday of the year0 – 365
tm_isdstdaylight 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).

CodeGivesExample
%Y4-digit year2026
%mmonth 01–1210
%dday 01–3103
%H %M %Shour, minute, second14 05 09
%A / %aweekday name / shortSaturday / Sat
%B / %bmonth name / shortOctober / Oct
%jday of the year276
%%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 double before dividing.
  • Example: double secs = (double) (clock() - start) / CLOCKS_PER_SEC;
Building a date by hand
#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 035

Run 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?