C Date and Time

The C Time Model
C separates an instant, calendar fields, processor usage, and display text. The <time.h> header supplies types and functions that move between these forms instead of forcing one date representation on every system.
A common workflow is: call time() to get the current instant, call localtime() or gmtime() to obtain named fields, then call strftime() to create readable text. Every stage has its own return check.
A time_t is suitable for passing an instant through the C library. Do not assume its epoch, signedness, width, or exact unit when portable code can use library functions.
Core Types in time.h
| Type | Purpose | First API to learn |
|---|---|---|
time_t | Calendar-time value representing an instant. | time() |
struct tm | Broken-down year, month, day, and clock fields. | localtime() |
clock_t | Implementation-defined processor-time count. | clock() |
struct timespec | Whole seconds and a nanosecond fraction. | timespec_get() |
Keep values in their declared types. Convert to a display type only at the final print boundary when necessary, and never use an integer cast as a replacement for the library's conversion rules.
Calendar Time vs Processor Time
| Question | Calendar time | Processor time |
|---|---|---|
| What does it describe? | An instant or elapsed real-world interval. | CPU time consumed by the program. |
| Main type | time_t or struct timespec | clock_t |
| Main function | time() or timespec_get() | clock() |
| Conversion | difftime(end, start) | (end-start) / CLOCKS_PER_SEC |
| Does sleeping count? | Normally yes for elapsed calendar time. | Often little or not at all. |
Choose based on the question. Benchmark CPU work with clock(); timestamp an event or compare real instants with calendar-time functions.
Understanding struct tm
struct tm gives names to calendar components, but several fields use library-friendly ranges rather than everyday notation.
| Field | Meaning | Convention |
|---|---|---|
tm_year | Year | Years since 1900 |
tm_mon | Month | 0 through 11 |
tm_mday | Day of month | 1 through 31 |
tm_hour | Hour | 0 through 23 |
tm_min, tm_sec | Minute and second | Normally start at 0 |
tm_wday | Weekday | Sunday is 0 |
tm_yday | Day of year | January 1 is 0 |
tm_isdst | Daylight-saving status | Use -1 to ask mktime() to determine it |
Starter: Print the Current Local Time
This complete program follows the standard three-stage workflow and checks every operation.
#include <stdio.h>
#include <time.h>
int main(void) {
time_t now = time(NULL);
if (now == (time_t)-1) {
printf("Calendar time is unavailable.\n");
return 1;
}
struct tm *local = localtime(&now);
if (local == NULL) {
printf("Local conversion failed.\n");
return 1;
}
char text[64];
if (strftime(text, sizeof text, "%Y-%m-%d %H:%M:%S", local) == 0) {
printf("Formatting failed.\n");
return 1;
}
printf("Local time: %s\n", text);
return 0;
}How It Works
- Read an instant.
time(NULL)returns current calendar time, and the cast-1check catches failure. - Apply local rules.
localtime()converts that instant into local year, month, day, and clock fields. - Choose the text layout.
strftime()expands numeric directives into a caller-owned buffer and reports whether they fit. - Print validated text. The final
printf()runs only after acquisition, conversion, and formatting all succeed.
Three Functions to Learn First
Frequently Asked Questions
Browse time.h functions
Choose the right API for measurement, conversion, or formatting.
10 people found this page helpful
