C MoreLesson 49Free preview
C Date
This lesson is part of C · All levels
Summary
The time.h header provides a time_t type to store time in seconds since the January 1, 1970 UTC epoch and struct tm for components such as years, months, and hours. The functions time(), localtime(), gmtime(), and strftime() facilitate the formation of time into text as needed. Important note: months in the struct are calculated from 0 and years are calculated from 1900.
Key points
- Time(NULL) returns the present time as time_t in units of seconds.
- Localtime converts time_t to local time struct tm, whereas gmtime generates UTC time.
- Strftime(buf, n, "%Y-%m-%d", tm) format time into strings according to a given pattern.
- Clock() and CLOCKS_PER_SEC are used to measure the length of execution of a program segment.
- Tm_mon starts from 0 for January and Tm_year is the year minus 1900.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.