time.h: Calendar and Processor Time
This chapter covers <time.h>: calendar time, processor time, and the functions and types for measuring and formatting time.
Why This Matters
Time is fundamental to logging, benchmarking, scheduling, and protocol engineering. <time.h> provides the standard, portable interface, though its resolution and epoch details are implementation-defined.
Prerequisites
c.stdlib.1— library overview.
Core Concept
Types and functions
typedef ... time_t; /* arithmetic type for calendar time */
typedef ... clock_t; /* arithmetic type for processor time */
struct tm { ... }; /* broken-down time */
time_t time(time_t *t);
clock_t clock(void);
double difftime(time_t a, time_t b);
struct tm *gmtime(const time_t *t);
struct tm *localtime(const time_t *t);
time_t mktime(struct tm *tm);
size_t strftime(char *s, size_t n, const char *fmt, const struct tm *tm);
time() returns the current calendar time (typically seconds since the Unix epoch, but the epoch is implementation-defined). clock() returns processor time used, for measuring CPU time (not wall time).
Syntax
#include <time.h>
time_t now = time(NULL);
struct tm *utc = gmtime(&now);
char buf[64];
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S", utc);
Examples
Current time
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now = time(NULL);
struct tm *local = localtime(&now);
char buf[64];
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S", local);
printf("%s\n", buf);
return 0;
}
Expected output is the current date/time.
Measuring elapsed wall time
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t start = time(NULL);
/* ... work ... */
time_t end = time(NULL);
printf("elapsed: %.0f seconds\n", difftime(end, start));
return 0;
}
Measuring processor time
#include <stdio.h>
#include <time.h>
int main(void)
{
clock_t start = clock();
/* ... CPU-bound work ... */
clock_t end = clock();
printf("CPU seconds: %f\n", (double)(end - start) / CLOCKS_PER_SEC);
return 0;
}
How It Works
time_t stores calendar time; clock_t stores processor time in implementation-defined units (convert with CLOCKS_PER_SEC). gmtime/ localtime convert a time_t into broken-down fields, and strftime formats them.
Variations
time_t epoch
The epoch is implementation-defined, but on POSIX and most systems it is 00:00:00 UTC, 1 Jan 1970. Do not assume this in strictly portable code.
Reentrancy
gmtime/localtime may return a pointer to static storage and are not thread-safe; POSIX provides gmtime_r/localtime_r.
Common Mistakes
- Assuming the epoch is the Unix epoch in portable code.
- Assuming
clock()measures wall time (it measures CPU time). - Assuming
time_tis a specific integer width. - Using
gmtime/localtimein threaded code without the_rvariants.
Undefined Behavior
- Passing
NULLtogmtime/localtime(implementation-defined or UB). - Using
strftimewith an invalid format or insufficient buffer is not UB, but
the result is truncated/unspecified.
Portability
time_t/clock_twidths and epochs are implementation-defined.CLOCKS_PER_SECis standard.gmtime_r/localtime_rare POSIX, not ISO C.
Under the Hood
time() and clock() are typically thin wrappers over OS/kernel time services. localtime applies the process's timezone and DST rules. strftime is a formatting loop.
Practical Usage
- Use
time(NULL)for wall-clock timestamps. - Use
clock()for CPU-time profiling (better: use high-resolution OS timers
like clock_gettime on POSIX, c.perf.1).
- Format times with
strftimefor human-readable output.
Exercises
1. Print the current UTC and local time using gmtime/localtime and strftime. 2. Measure elapsed wall time and CPU time for a loop. 3. Write a function that formats a time_t as an ISO 8601 string. 4. Explain the difference between time() and clock().
Deep Challenge
Implement a small ISO 8601 timestamp formatter that is independent of localtime's static-storage limitation (thread-safe), using gmtime carefully or mktime for validation. Discuss the portability of the epoch assumption.
Related Concepts
c.perf.1— benchmarking with high-resolution timers.c.conc.1— thread-safety.
References
- ISO/IEC 9899:2018 §7.27 (time.h).
Verification
time()returns calendar time;clock()returns processor time.VERIFIED- Epoch and type widths are implementation-defined.
VERIFIED - No example was executed during generation unless noted. `Execution not
verified.`
Progress
- [ ] Read
- [ ] Understand
- [ ] Complete examples
- [ ] Complete exercises
- [ ] Complete deep challenge
Concept checkboxes
- [ ] time_t and clock_t
- [ ] time/clock/difftime
- [ ] gmtime/localtime/mktime
- [ ] strftime
- [ ] Reentrancy concerns
Mastery levels
| Concept | Current level (0–8) | Target level |
|---|---|---|
| c.lib.time | 0 | 5 |