Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ print current time

// Current date/time based on current system
time_t now = time(0);

// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
cout << "The local date and time is: " << asctime(localtm) << endl;

// Convert now to tm struct for UTC
tm* gmtm = gmtime(&now);
if (gmtm != NULL) {
cout << "The UTC date and time is: " << asctime(gmtm) << endl;
}
else {
cerr << "Failed to get the UTC date and time" << endl;
return EXIT_FAILURE;
}
Comment

c++ show current time

#include <iostream>
#include <chrono>
#include <ctime>    

int main()
{
    auto start = std::chrono::system_clock::now();
    // Some computation here
    auto end = std::chrono::system_clock::now();
 
    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);
 
    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "s"
              << std::endl;
}
Comment

c++ get time

#include <chrono>

// Get the time since epoch (realtime clock), in milliseconds
int64_t getCurrentMSinceEpoch()
{
	return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

// Get the time since started, in milliseconds
// Better for time difference because it's faster than system_clock
int64_t getTimeMS()
{
	return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
Comment

c++ show current time

#include <iostream>
#include <chrono>
#include <ctime>    

int main()
{
    auto start = std::chrono::system_clock::now();
    // Some computation here
    auto end = std::chrono::system_clock::now();
 
    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);
 
    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "s"
              << std::endl;
}

finished computation at Mon Oct  2 00:59:08 2017
elapsed time: 1.88232s
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ main function 
Cpp :: c++ program to calculate discount 
Cpp :: c++ read integers from file 
Cpp :: arduino notone 
Cpp :: cmath sqrt 
Cpp :: heap buffer overflow c++ 
Cpp :: mpi_bcast 
Cpp :: getch c++ library 
Cpp :: how to run a c++ program in the background 
Cpp :: create random vectors c++ 
Cpp :: min vector c++ 
Cpp :: cpp bubble sort 
Cpp :: convert int to enum c++ 
Cpp :: c++ iterate over vector 
Cpp :: fiunction in c++ 
Cpp :: c++ sleep 
Cpp :: change to lowercase character c++ 
Cpp :: loop through a vector in c++ 
Cpp :: int_max cpp 
Cpp :: how to clear vector c++ 
Cpp :: convert refference to pointer c++ 
Cpp :: C++ std::string find and replace 
Cpp :: c++ get ascii value of char 
Cpp :: vector size for loop 
Cpp :: input in c++ 
Cpp :: how to split a string in c++ 
Cpp :: how to return char* from function in c++ 
Cpp :: initialize vector of vector c++ 
Cpp :: deep copy c++ 
Cpp :: inline function in c++ 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =