C time.h Functions

C Date and Time Functions
<time.h> is C's standard date-and-time header. It defines types such as time_t, clock_t, struct tm, and struct timespec, then provides functions for obtaining, measuring, converting, comparing, and formatting time.
This catalog groups all 14 tutorials by the job you need to do. Each guide explains the exact declaration, parameter contract, return sentinel, portability notes, and three complete beginner examples.
Three representations to recognize
| Representation | Meaning | Typical functions |
|---|---|---|
time_t | Calendar time representing an instant. | time(), difftime() |
struct tm | Named calendar fields such as year, month, and hour. | localtime(), gmtime(), mktime() |
clock_t | Processor-time count for program CPU work. | clock() |
struct timespec | Whole seconds plus a nanosecond fraction. | timespec_get(), timespec_getres() |
Choose a Function by Job
| I want to... | Start with | Remember |
|---|---|---|
| Get the current calendar time | time() | Check (time_t)-1. |
| Measure CPU use | clock() | Divide by CLOCKS_PER_SEC. |
| Compare two instants | difftime() | The order is time2 minus time1. |
| Get local fields | localtime() | The basic form uses static storage. |
| Get UTC fields | gmtime() | UTC does not apply the local-zone offset. |
| Create custom text | strftime() | A zero result usually means the buffer was too small. |
| Get fractional calendar time | timespec_get() | Use TIME_UTC. |
Jump to a category
How to Browse This Index
- Start from the job. Calendar time, processor time, conversion, and formatting are different concerns.
- Pick the representation. Decide whether you currently have a
time_t,struct tm, or text buffer. - Prefer caller-owned output. Reentrant POSIX conversions and
strftime()avoid shared text or structure buffers. - Check the sentinel. Different functions use
NULL, zero, or a cast-1to report failure. - Read portability notes. The
_rfunctions,timegm(), and C23 resolution query have version or platform requirements.
Search All 14 Functions
Filter by a function name or a task such as UTC, format, processor, local, or resolution.
Get or measure time
Read calendar time, measure processor use, compare instants, or work with fractional time and resolution.
| Function | Best for | Tutorial |
|---|---|---|
time() | gets the current calendar time as a time_t value. | Open |
clock() | measures processor time used by the program. | Open |
difftime() | computes the difference between two calendar times in seconds. | Open |
timespec_get() | gets C11 calendar time with seconds and nanoseconds. | Open |
timespec_getres() | queries the resolution of a C23 time base. | Open |
Convert broken-down time
Move between time_t values and struct tm fields in local time or UTC.
| Function | Best for | Tutorial |
|---|---|---|
localtime() | breaks calendar time into local date and clock fields. | Open |
localtime_r() | converts calendar time to local fields in caller-owned storage. | Open |
gmtime() | breaks a calendar time into UTC date and clock fields. | Open |
gmtime_r() | converts calendar time to UTC in a caller-provided structure. | Open |
mktime() | converts local broken-down time to calendar time and normalizes fields. | Open |
timegm() | converts UTC broken-down time to calendar time. | Open |
Format time as text
Create readable text, from fixed legacy layouts to custom formats.
Key Takeaways
- Calendar and CPU time differ.
time()describes an instant;clock()measures processor use. - struct tm has special field conventions. Years start at 1900, months at 0, and day-of-year at 0.
- Local time depends on rules outside the value. Time zones and daylight-saving databases affect conversions.
- Static buffers are fragile. Copy results from
asctime(),ctime(),gmtime(), andlocaltime()when they must survive another call. - Formatting is separate from conversion. Convert to
struct tm, then usestrftime()for controlled text.
Frequently Asked Questions
Start with asctime()
Learn the first function in the complete time.h sequence.
14 people found this page helpful
