How to display the time in minutes in the command line after the program has started?
3 answers
If you want to display the time elapsed since the launch of the program and not be afraid of translating the system clock, you can use the following code (c ++ 11 and higher):
#include <chrono> #include <thread> #include <iostream> using namespace std; auto start = chrono::steady_clock::now(); // запоминаем время начала int main() { this_thread::sleep_for(std::chrono::seconds(2)); // какая-то полезная деятельность auto current = chrono::steady_clock::now(); // время в момент проверки cout << "Work time: " << chrono::duration_cast<chrono::seconds>(current - start).count() << " sec.\n"; } The example shows seconds so that you can see the result in the online compiler.
- It was always interesting who the author is to such an extent, um ... a perverted record of work with time in the standard? It just breaks out of the rest of the standard ... - Harry
- @Harry and what exactly confuses? - αλεχολυτ
- Verbosity and mediation ... - Harry
- @Harry, however, allows you to select any time intervals without introducing different functions for (seconds / milliseconds / nanoseconds ...), and not to use the
sleep(1000000000);recordssleep(1000000000);when you need to wait 1 second, and the parameter takes a value in nanoseconds. All this is based on the classstd::ratioin large. - αλεχολυτ
|
Perceiving your question as "time elapsed after the launch of the program", I would act something like this:
int main() { time_t start_time = time(0); ... cout << (time(0)-start_time)/60 << " минут"; If you mean something else, specify more precisely what you need.
A variable can be made global - then it is initialized even before main() :)
- Now the main thing is that no one should touch the system clock. - αλεχολυτ
- @alexolut Needless to say :) - Harry
|
The answer from the English-speaking forum link
#include <ctime> #include <iostream> using namespace std; int main() { time_t t = time(0); // get time now struct tm * now = localtime( & t ); cout << (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday << endl; } |