Pls can anybody explain me these two lines
// Current date/time based on current system
time_t now = time(0);
// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
Pls can anybody explain me these two lines
// Current date/time based on current system
time_t now = time(0);
// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
That is a very old C-styled way of determining local date & time based on time.h
. The documentation for that header file explains a lot, but to summarize, time()
returns a system-time in seconds from Epoch (00:00 on 1-Jan-1970) when passed a null pointer (hence the zero), or time from another set time_t
instance. The pointer here is essential because it drives different behaviors of the function based on the pointer value.
localtime()
takes system-time information and converts it to a local-time based upon current system settings. I've never been sure why this took a pointer as an argument, but I assume it's just to maintain consistency with the API.
A better, more C++ way of getting local-time is to use the std::chrono
library (http://www.cplusplus.com/reference/chrono/?kw=chrono). You could get higher resolution local-time (instead of just accurate to the nearest second), without pointer-jockeying. For example, please refer to: Outputting Date and Time in C++ using std::chrono