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++ swapping two numbers 
Cpp :: convert long int to binary string c++ 
Cpp :: c++ print vector without loop 
Cpp :: arduino buildin let 
Cpp :: google pdf iframe viwer 
Cpp :: how to find length of character array in c++ 
Cpp :: gfgdf 
Cpp :: change to lowercase in notepad++ 
Cpp :: c++ for in 
Cpp :: how to do nCr in c++ 
Cpp :: cpp unions 
Cpp :: check if file is empty c++ 
Cpp :: c++ function as param 
Cpp :: how to make a loop in c++ 
Cpp :: SetUnhandledExceptionFilter 
Cpp :: Resize method in c++ for arrays 
Cpp :: memset in c++ 
Cpp :: vector search by element 
Cpp :: how to convert string into lowercase in cpp 
Cpp :: set was not declared in this scope 
Cpp :: sort a 2d vector c++ stl 
Cpp :: declaring 2d dynamic array c++ 
Cpp :: debugging c/c++ with visual studio code 
Cpp :: check if set contains element c++ 
Cpp :: string to upper c++ 
Cpp :: team fortress 
Cpp :: how to search in array c++ 
Cpp :: unreal engine c++ 
Cpp :: sleep in c++ 
Cpp :: c preprocessor operations 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =