C time (time.h) Library
C time Functions
The <time.h> header provides functions for working with dates, times, and measuring durations in C.
A list of common time.h functions can be found in the table below:
| Function | Description |
|---|---|
| asctime() | Converts a struct tm to a string in a standard date/time format |
| clock() | Returns the number of processor clock ticks used by the program (can be used to measure execution time) |
| ctime() | Converts a time_t value into a readable string (e.g. Thu Jun 26 10:30:00 2025) |
| difftime() | Calculates the difference (in seconds) between two time_t values |
| gmtime() | Converts a time_t value to UTC time (also as a struct tm) |
| localtime() | Converts a time_t value to local time and returns a pointer to a struct tm |
| mktime() | Converts a filled struct tm into a time_t value |
| strftime() | Formats a struct tm into a custom date and time string |
| time() | Returns the current calendar time as a time_t value (seconds since Jan 1, 1970) |
The struct tm Structure
Several functions return or use a struct tm. It holds the parts of a date and time in separate int members:
| Member | Description |
|---|---|
| tm_sec | Seconds after the minute (0-60, 60 is used for a leap second) |
| tm_min | Minutes after the hour (0-59) |
| tm_hour | Hours since midnight (0-23) |
| tm_mday | Day of the month (1-31) |
| tm_mon | Months since January (0-11) |
| tm_year | Years since 1900 |
| tm_wday | Days since Sunday (0-6) |
| tm_yday | Days since January 1 (0-365) |
| tm_isdst | Daylight saving time: positive if it is in effect, 0 if not, and negative if it is not known |
Note: tm_year counts the years since 1900, and tm_mon starts at 0 for January. So for June 26, 2025, tm_year is 125 and tm_mon is 5.