C time.h Functions

Beginner
10 min read
Updated: Nov 2026
14 tutorials
<time.h>

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

RepresentationMeaningTypical functions
time_tCalendar time representing an instant.time(), difftime()
struct tmNamed calendar fields such as year, month, and hour.localtime(), gmtime(), mktime()
clock_tProcessor-time count for program CPU work.clock()
struct timespecWhole seconds plus a nanosecond fraction.timespec_get(), timespec_getres()

Choose a Function by Job

I want to...Start withRemember
Get the current calendar timetime()Check (time_t)-1.
Measure CPU useclock()Divide by CLOCKS_PER_SEC.
Compare two instantsdifftime()The order is time2 minus time1.
Get local fieldslocaltime()The basic form uses static storage.
Get UTC fieldsgmtime()UTC does not apply the local-zone offset.
Create custom textstrftime()A zero result usually means the buffer was too small.
Get fractional calendar timetimespec_get()Use TIME_UTC.

Jump to a category

How to Browse This Index

  1. Start from the job. Calendar time, processor time, conversion, and formatting are different concerns.
  2. Pick the representation. Decide whether you currently have a time_t, struct tm, or text buffer.
  3. Prefer caller-owned output. Reentrant POSIX conversions and strftime() avoid shared text or structure buffers.
  4. Check the sentinel. Different functions use NULL, zero, or a cast -1 to report failure.
  5. Read portability notes. The _r functions, 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.

FunctionBest forTutorial
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.

FunctionBest forTutorial
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.

FunctionBest forTutorial
asctime()turns a broken-down date and time into a fixed English text line.Open
ctime()formats a calendar-time value as a fixed English text line.Open
strftime()formats broken-down date and time into a caller-provided text buffer.Open

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(), and localtime() when they must survive another call.
  • Formatting is separate from conversion. Convert to struct tm, then use strftime() for controlled text.

Frequently Asked Questions

It declares types and functions for calendar time, processor time, broken-down dates, UTC and local conversions, and text formatting.
time_t is the implementation-defined type used to represent calendar time.
struct tm stores broken-down fields such as year, month, day, hour, minute, and second.
No. clock() measures processor time used by the program. Use calendar-time APIs for elapsed wall time.
Start with time(), localtime(), and strftime(), then learn difftime() and UTC conversion.

Start with asctime()

Learn the first function in the complete time.h sequence.

asctime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful