C++ ClassesLesson 42Free preview
C++ Date
This lesson is part of C++ · All levels
Summary
C++ works with time through the time_t type of the <ctime> header, which stores the number of seconds since epoch (January 1, 1970). The time(nullptr) function takes the current time, then localtime() or gmtime() transforms it into a structure containing years, months, days, hours, and so on. To show it neatly, use strftime that formats time_t into a string according to the pattern we define.
Key points
- Returns the present time in the form of time_t.
- Localtime() changes time_t to struct tm according to the local time zone.
- In structural time, the year is calculated from 1970 (plus 1900) and the month is 0-11 (plus 1).
- Strftime(buffer, ukuran, format, waktu) format time to string, for example %d-%m-%Y.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.