My operating system, Windows, keeps track of the time and date, it's at the bottom right corner of the desktop. If I want to get the time and date, I'd expect a call (maybe a system call) that retrieves those values written at the bottom right of my screen. Instead the way people tell you to get the system time is by using functions and types such at std::chrono::clock::now
and the C type time_t
which are just a single (or maybe more) integer. Then there's another step. Usually this is demonstrated with logging that type, for example:
auto start = std::chrono::system_clock::now();
auto legacyStart = std::chrono::system_clock::to_time_t(start);
std::cout << std::ctime(&legacyStart) << '\n';
The conversion I'm guessing involves something like Zeller's Algorithm to calculate the day of the week from a certain time point.
My question is, is there a more efficient way to just read the system time from your operating system?
Also, on a related note, is this how the operating system does it? Keeps time by adding whatever time has passed to an integer (possibly the number of seconds since POSIX time), and then does a conversion every time to display that date and time?
From what I now it is the most memory and time efficient way to retrieve a date. Your computer uses the same trick. You can read more about it here, but in general, there is a clock that measure time since start of its life, regardless if its turned on or off (That's why you have current time even when you are offline). So OS just take this number and shows you in nice format, taking care about your timezone etc.