C++

C++ timer example

The timer() function in C++ returns the updated time as an object of the “time_t” type. The header file where this timer() function is defined is “ctime”. Here we will explain which type of functionalities a timer() can perform. The timer is used as a frequent module, and are multiple methods to use the timer() function to achieve the high performance of our application.

In most of the programs generally, the timer is sufficient to help to tackle a solo thread. The main advantage of the time_t() function is at the time of implementation of the code. Our system occupancy is increased by the percentage of ten points, and later on, it will become in its normal state. Because the algorithm, if the timer() function is very strong. You can set a timer() function or kill the function of your own choice. Each timer() has a unique id when we set it also required a call back function. We can also utilize the library.

Delay in the output:

Here we made a timer that delayed our output by entering the seconds we wanted to delay. The “clock_t” is a type that is the built-in type function in our time header library. This is the way because our clock() object return may be an unsigned variable having a long data type on the machine. A system time returns from the clock() function that we can imagine in a millisecond. In the header file of the ctime, there is a macro that is pre-define that is “clock_per_second”. From this, we will implement a code that is a loop with the help of a while loop to let the code delay for some seconds that the user entered.

#include<cstdio>
#include<iostream>
#include<ctime>

using namespace std;

int main() {

        int delay;
        cout<<"Enter the time in seconds to be delayed:"<<flush;

        cin>>delay;
        delay *= CLOCKS_PER_SEC;

        clock_t now = clock();

        while(clock() - now <delay);

        cout<<"Message Show after delay that you entered"<<endl;

        return 0;
}

Here we use our header files, and these header files, as explained above, accept iostream that has the definition of the input-output stream of the code. After the namespace standard, we start our main body, where we declare a variable and initialize it with the integer data type. Then we ask the user to enter seconds that they want to delay the result. After getting the value, we stored it into our variable. Then we make our variable a pointer and assign a macro “CLOCK_PRT_SEC” that we discussed above. By using “clock_t” which is also described before, we call our clock time function and start the loop. In this while loop, we check our state before the cycle starts until the given time is complete. After ending the loop by false the condition, we show our message and end the code.

Timer Using System call:

System () function is used when we have to execute a system command with the help of passing the command as an argument to the function. The sleep () function is used to make our program into sleep mode for a specific number of seconds that we provide as an argument. The <iomainip>library helps us to manipulate the result in the C++ program.

#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>

using namespace std;

int hours = 0;
int minutes = 0;
int seconds = 0;

void displayClock() {
    cout <<setfill(' ') <<setw(55) <<"         TIMER         \n";
    cout <<setfill(' ') <<setw(55) <<" --------------------------\n";
    cout <<setfill(' ') <<setw(29);

    cout <<"| " <<setfill('0') <<setw(2) <<hours <<" hrs | ";
    cout <<setfill('0') <<setw(2) <<minutes <<" min | ";
    cout <<setfill('0') <<setw(2) <<seconds <<" sec |" <<endl;
    cout <<setfill(' ') <<setw(55) <<" --------------------------\n";
}

void timer() {

    while (true) {

        displayClock();
        sleep(1);
        seconds++;

        if (seconds == 60) {

            minutes++;

            if (minutes == 60) {
                hours++;
                minutes = 0;
            }

            seconds = 0;
        }
    }
}

int main() {
    timer();
    return 0;
}

At the start of the code, we define header files of the standard general-purpose library. Also, input-output stream library files and other header files are discussed as above. After namespace standards, we initialized second, minutes, and hours with integer data type and assigned zero to all the values. Here we create a function for displaying the clock where we made a structure of the class. Then we write a function of timer() where we build the logic of our date and time in a “while” loop. When the loop will truly display function call after every second because we enter one in the sleep() function parameters. In the “if” statement, there will be the increment after every minute and then an hour again the second will be assigned by zero.

Get Time and date:

If we want the function or structures that are related to assessing the date and the time, we must need the ctime header file or library in our C++ code. The three time-related types are “clock_t”, “time_t”, and “tm”. These can display the date and the time of the system.

#include <iostream>
#include <ctime>
using namespace std;

int main() {

   time_t a = time(0);

   char* b = ctime(&a);

   cout <<"The local date and time is: " <<b <<endl;

   tm *gmtm = gmtime(&a);

   b = asctime(gmtm);

   cout <<"The UTC date and time is:"<<b <<endl;
}

In this code, we integrate our necessary libraries, then namespace standard, and call the main body of the code. Here we get our time as an object, pass it to the ctime function and assign this function to a character pointer. Then we display the local date and time that we get from our system. On the other side, we get our UTC date and time with the function “gmtime” and pass it to our variable to display the UTC and date. UTC date and time is defined as the time that is universal coordinated time while the GMT stands for Greenwich Mean Time.

Conclusion:

In this article, we have explained the timer() function and its structure and the functionality of the timer() function. Also, we discuss the header file that is used for the timer() function and all the supportive functions and objects that help to complete the operations with the help of the timer() function. Then we further explain our function with the help of various examples which use timer() function differently.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.