C++C++17Introduction to C++Learn C++

Learn To Use Time And Its Properties In Your C++ App

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.

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,

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,

How to display the local time in C++

We can obtain local time from the system clock as below,

How to work with UTC Time in a C++ app

We can obtain UTC time from the system clock as below,

How to time an event’s duration in C++

We can also get current counter of the system clock as below,

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,

Is there a full example of working with Time in a C++ app?

Her we sum all above in a fıll example,

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?