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 :: find character in string c++ 
Cpp :: is C++ useful in 2021 
Cpp :: quadratic problem solution c++ 
Cpp :: how to clear screen in C++ console 
Cpp :: cout was not declared in this scope 
Cpp :: comment in c++ 
Cpp :: string to int arduino 
Cpp :: priority queue c++ type of pairs 
Cpp :: clang cpp compile command 
Cpp :: findung the mode in c++ 
Cpp :: 2d vector c++ declaration 
Cpp :: c++ initialize array with all zeros 
Cpp :: c++ mst kruskal 
Cpp :: apply pca to dataframe 
Cpp :: malloc in c++ 
Cpp :: factorial using recursion cpp 
Cpp :: parallelize for loop c++ 
Cpp :: c++ open all files in directory 
Cpp :: how to write something in power of a number in c++ 
Cpp :: c++ vector average 
Cpp :: slice std::array cpp 
Cpp :: resize 2d vector c++ 
Cpp :: find in string c++ 
Cpp :: less than operator overloading in c++ 
Cpp :: how to convert string into lowercase in cpp 
Cpp :: cpp cin 
Cpp :: read and write file in c++ 
Cpp :: c++ int 
Cpp :: 2d array c++ 
Cpp :: char size length c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =