C++

C++ Time Function

As we all know, time is the most important thing in every aspect of our lives. It has taken a huge part in development and software as well. The C++ language has come up with the “ctime” library that has been filled with many time-related functions to handle time-related issues in programs. The simpler one is the time() function to get the current time of your system. Therefore, we will discuss this function of C++ in our code and other time functions. We have been utilizing the Ubuntu operating system here to complete this goal. Let’s get started with the quick opening of Ubuntu’s terminal to start the implementation, i.e. Ctrl+Alt+T. Before going further, we need to create or open a C++ file to make code in it via the terminal’s “touch” and “nano” editor commands, as already shown below.

Example 01:

We will start our article with a very simple example of using the time() function in C++. Any code is inexecutable without its header libraries. So, we have started the code within the editor with the C++ well-known and most used library “iostream” to utilize the input-output stream within it. As we have to use the “time” function, we need to include its header as well. Therefore, C++ “ctime” library has been added with “#include”. Here comes our main() function to start execution from. The main() method only includes a single line of code to cout the total seconds passed from 1 January 1970 till now on our Ubuntu terminal using the “time()” function of the “ctime” library. It takes “NULL” as its parameter to let us know about the total seconds passed. As we didn’t utilize the “using namespace std” line, therefore we have to use the “std” keyword within the cout statement. Now, the code has been ready to be executed. Let’s save it and close the file.

Let’s compile our first code with the g++ compiler of the Ubuntu 20.04 system. It returns no errors and makes it successful. While, after the execution of this new code via the “./a.out” command, we have got the time in seconds passed after 1 January 1970.

At the same time() function can be utilized with other arguments in the code. We will be looking at the argument “0” of the time() function. It will work the same as the “NULL” function. So, the overall code is the same as the above code, and we have replaced “NULL” with “0”. Save the code to run this newly updated code.

After the updated code compilation and execution, we have got the time in seconds from 1 January 1970 till date using the “0” parameter in time() function.

Example 02:

Let’s look at another simple example of using the time() function in C++ to get time in standard format, i.e. string format. For this, you need to convert the seconds in string date/time format using the ctime() function of the ctime library. So, we have started our code with the same iostream and ctime header libraries. The main() function got started with the time_t alias for representing the seconds into actual “time” format returned to the variable “t” via the time() function of C++. Now, the current time has been saved in the “t” variable. On the very next line, we have declared a character type pointer “st” to save the converted date/time into standard string format via the “ctime” function while utilizing the “t” variable. The next cout statement has been declared to display the string type standard local date/time on the terminal shell via the “st” pointer. The updated code must be saved before the compilation via “Ctrl+S”.

It’s time to make our code error-free after saving it using the g++ compiler on the terminal query area. The code is finally error-free, and we have exected it with the “./a.out” instruction. The output returns to display the Local date and time of our system in the standard format, i.e. day, month, date, time, and year.

Example 03:

Let’s begin with the new example to show time on our shell. This time, we will be adding a code to display the local date/time and the UTC converted time on the shell in a quite human-readable format. So, we have started this code with the header libraries and the main() function. The same format has been utilized to get seconds since 1 January 1970 and convert it to standard time format via the time_t alias “t”. The character type pointer “st” has been used to convert the time to string format of local time, and the cout statement is displayed.

Here comes the “tm” structure used to store the date and time in C structure format. The “ct” pointer has been used as a “tm” structure getting the converted time to calender time or UTC format for C++ using the function “gmtime()”. The “gmtime()” function utilizes the current date/time got from the variable “t” in its parameters. Now the converted time to calendar format or UTC has been saved to the “ct” pointer through reference. On the very next line, we have been utilizing the converted calendar time “ct” to character representation format, i.e. human readable-form using the “asctime” function. This UTC date and time will be saved to the variable pointer type variable “st” and displayed on the shell via the “cout” statement.

After the code compilation and execution, we have got the local time first and then the UTC format date/time.

Example 04:

Let’s take a glance at our last example of using the time() function. This time we will convert the local time into parts using the “tm” structure of the time header. So, the same header files with the “Std” namespace have been included. The main () function is started with the “time_t” alias to add the current date/time in exact time format within the “t” variable. The standard “cout” statement will display that current date/time. The local time has been fetched with the help of the “local time()” function referencing towards “t” alias and saving it within the “tm” pointer structure named “l”.

The “l” variable has been used in many ways to get the components of date/time. For example, tm-hour is for getting an exact hour, tm_min is to get minutes, tm_Sec is to get seconds, tm_mday is to get the exact current date of the month, tm_mon is to get the current month, and “tm-year” is to get the current year. All the components have been got from different formulas applied on the “tm” structure pointer “l” i.e. local time. The cout statements have been using these formulas to directly covert and display the results on the shell.

After the code has been compiled and executed, we got the seconds that are displayed on the shell and then the time/date separately as we needed and required through formulas.

Conclusion:

Finally, we have done all the necessary examples to show the working of the C++ time() function in the Ubuntu 20.04 system. We have solved all the time-related issues for our users to the current time in seconds, in the local timestamp, UTC format, and many more. We have tried a simple time() function and some supporting functions of the “ctime” library. Thus, this article has been implemented, keeping all the necessary things to make it the best.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.