How can we use Time in our C++ App? How can we obtain local time or UTC time? How can we get each individual property of the clock (hours, mins, seconds)?
When we write programs, it’s very common to come across two important parameters, which are Date and Time properties. The Date and Time property is obtained from a timer or time module which is a physical device on the hardware you are using. For example, a real-time clock (RTC) DS3231 module is used to count and ‘remember’ the correct time on IoTs. Other devices such as PC use a similar chip. All are backed by a battery, generally a flat ‘watch’ +3V CR200 battery. Normally, in most devices, you must set the correct time manually at the beginning after which the battery-backed timer chip takes over. With the advent of the Internet, we now have internet web sites to check real time based on your location via GPS tools. For example, Windows frequently checks keeps the time accurate using one or more of these ‘time servers’ also sometimes called network time protocol (NTP) servers.
We need Date and Time when checking or displaying the current time in our applications. We need to note this in the configurations of our apps, or to set the time of files produced, game saves, tables, local databases, and online databases require these. Generally, we use Date and Time together. And it is good to know when that data line is required, so we can query our data by minutes, hours, days, or months or yearly analyses that can be applied to our data.
Table of Contents
How to use Time and its properties in your C++ App
In a standard C++ there is a chrono library that allows you to perform all sorts of date, time and timer operations. We can use it to display date and time using modern techniques.
How to display the system time in C++
In C++, we can simply get time from the system clock and display it as below,
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <chrono> int main() { auto now = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() ); std::cout << std::ctime(&now) << '\n'; getchar(); } |
In C and C++, we can use time.h library, we can get all date, time and timer values. In this library we use time_t datatype to define time(). Here is a Time example below,
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> #include <stdint.h> #include <time.h> int main(void) { time_t timenow = time(NULL); if(timenow != (time_t)(-1)) printf("Time is %s\n", asctime(gmtime(&result)) ); } |
How to display the local time in C++
We can obtain local time from the system clock as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <chrono> #include <ctime> int main() { //---- GET TIME NOW auto now = std::chrono::system_clock::now(); // auto is std::chrono::system_clock::time_point auto tt = std::chrono::system_clock::to_time_t(now); // auto is time_t //---- LOCAL TIME NOW tm loc = *localtime(&tt); std::cout << "LOCAL TIME: "; std::cout << loc.tm_year + 1900 << '-'; std::cout << loc.tm_mon + 1 << '-'; std::cout << loc.tm_mday << ' '; std::cout << loc.tm_hour << ':'; std::cout << loc.tm_min << ':'; std::cout << loc.tm_sec << '\n'; } |
How to work with UTC Time in a C++ app
We can obtain UTC time from the system clock as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <chrono> #include <chrono> #include <ctime> int main() { //---- GET TIME NOW auto now = std::chrono::system_clock::now(); // auto is std::chrono::system_clock::time_point auto tt = std::chrono::system_clock::to_time_t(now); // auto is time_t //---- UTC TIME NOW tm utc = *gmtime(&tt); std::cout << "UTC TIME : "; std::cout << utc.tm_year + 1900 << '-'; std::cout << utc.tm_mon + 1 << '-'; std::cout << utc.tm_mday << ' '; std::cout << utc.tm_hour << ':'; std::cout << utc.tm_min << ':'; std::cout << utc.tm_sec << '\n'; } |
How to time an event’s duration in C++
We can also get current counter of the system clock as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <chrono> #include <ctime> int main() { //---- GET TIME NOW auto now = std::chrono::system_clock::now(); // auto is std::chrono::system_clock::time_point auto tt = std::chrono::system_clock::to_time_t(now); // auto is time_t //---- TIME DURATION COUNT NOW typedef std::chrono::duration<int, std::ratio_multiply<std::chrono::hours::period, std::ratio<24> >::type> days; std::chrono::system_clock::duration dur = now.time_since_epoch(); std::cout << "DURATION COUNT: " << dur.count() << " (" << std::chrono::system_clock::duration::period::num << '/' << std::chrono::system_clock::duration::period::den << ")\n"; } |
How to get the individual time properties such as days, hours, mins, and seconds in C++
We can obtain each part of timer of clock as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <chrono> #include <ctime> int main() { //---- GET TIME NOW auto now = std::chrono::system_clock::now(); // auto is std::chrono::system_clock::time_point auto tt = std::chrono::system_clock::to_time_t(now); // auto is time_t //---- TIME DURATION NOW days d = std::chrono::duration_cast<days>(dur); dur -= d; std::chrono::hours h = std::chrono::duration_cast<std::chrono::hours>(dur); dur -= h; std::chrono::minutes m = std::chrono::duration_cast<std::chrono::minutes>(dur); dur -= m; std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(dur); dur -= s; std::cout << "DURATION NOW :" ; std::cout << d.count() << " days " << h.count() << " hours " << m.count() << " mins " << s.count() << " secs\n"; std::cout << "DURATION COUNT: " << dur.count() << " (" << std::chrono::system_clock::duration::period::num << '/' << std::chrono::system_clock::duration::period::den << ")\n"; getchar(); } |
Is there a full example of working with Time in a C++ app?
Her we sum all above in a fıll example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <iostream> #include <chrono> #include <ctime> int main() { //---- GET TIME NOW auto now = std::chrono::system_clock::now(); // auto is std::chrono::system_clock::time_point auto tt = std::chrono::system_clock::to_time_t(now); // auto is time_t //---- LOCAL TIME NOW tm loc = *localtime(&tt); std::cout << "LOCAL TIME: "; std::cout << loc.tm_year + 1900 << '-'; std::cout << loc.tm_mon + 1 << '-'; std::cout << loc.tm_mday << ' '; std::cout << loc.tm_hour << ':'; std::cout << loc.tm_min << ':'; std::cout << loc.tm_sec << '\n'; //---- UTC TIME NOW tm utc = *gmtime(&tt); std::cout << "UTC TIME : "; std::cout << utc.tm_year + 1900 << '-'; std::cout << utc.tm_mon + 1 << '-'; std::cout << utc.tm_mday << ' '; std::cout << utc.tm_hour << ':'; std::cout << utc.tm_min << ':'; std::cout << utc.tm_sec << '\n'; //---- TIME DURATION COUNT NOW typedef std::chrono::duration<int, std::ratio_multiply<std::chrono::hours::period, std::ratio<24> >::type> days; std::chrono::system_clock::duration dur = now.time_since_epoch(); std::cout << "DURATION COUNT: " << dur.count() << " (" << std::chrono::system_clock::duration::period::num << '/' << std::chrono::system_clock::duration::period::den << ")\n"; //---- TIME DURATION NOW days d = std::chrono::duration_cast<days>(dur); dur -= d; std::chrono::hours h = std::chrono::duration_cast<std::chrono::hours>(dur); dur -= h; std::chrono::minutes m = std::chrono::duration_cast<std::chrono::minutes>(dur); dur -= m; std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(dur); dur -= s; std::cout << "DURATION NOW :" ; std::cout << d.count() << " days " << h.count() << " hours " << m.count() << " mins " << s.count() << " secs\n"; std::cout << "DURATION COUNT: " << dur.count() << " (" << std::chrono::system_clock::duration::period::num << '/' << std::chrono::system_clock::duration::period::den << ")\n"; getchar(); } |