In this Linux Hint article, we will explain how to get the time and use the ctime() function to convert the Unix times to strings. We give a theoretical description of the syntax of the function and its input and output arguments, as well as the data types that each of them supports. We will then implement the use of this feature with practical examples using code snippets and images.
Ctime() Function Syntax in C Language
Ctime() Function Description in the C Language
The ctime() function returns the pointer to a string in *str which contains the Unix time sent in time_t and converted to strings in datetime format.
While the asctime() function converts the local or GMT time stored in the tm structure into a string, ctime() directly computes the Unix time and returns it as a string in datetime format. Therefore, in some cases, this function is a simpler and more convenient method than asctime() to get the time string.
The format that this function returns in the output string is as follows:
Both days and months are represented by the first three letters of their names. The dates are represented by two digits for the days and months and four digits for the year. The str string is terminated with a newline and a null character.
The ctime() function and its variables are defined in the “time.h” header file. Before you can use them, you must add them to your “.c” or “.h” header file as follows:
Once we include the “time.h” header file, we can use the ctime(), asctime() and all other functions that are defined in this header.
How to Convert the Unix Time into Strings with a Date Format Using the Ctime() Function in C
In this example, we explain the step-by-step process on how to get the Unix time and then use the ctime() function to convert the number of seconds into a string which is formatted as the date and time.
In the following snippet, time() gets the Unix_t time and prints it on the command line:
In the C language, time_t is a variable defined in the “time.h” library header where some of the time() functions return their result.
The following image shows the Unix time which is returned by time() in U_time:
Once we obtain the Unix time in U_time, we need to convert it to the date and time format of strings using the ctime() function. To use this function, we must first define a pointer of type const char* for the string in which to store the results. In this example, this is str_ptr and is defined as follows:
To convert the Unix time which is stored in U_time to a string in datetime format, we must call ctime(), passing the pointer which is just defined as the output argument and the address (&) of U_time as the input argument.
Next, we see the complete code of all the steps that we learned before and the correct way to call the ctime(). Using the printf() function, we display the obtained string in the command console:
To compile the code, we type the gcc followed by the path of the “.c” file and -o followed by the name of the output at the command line:
To execute, we then type “/” followed by the name of the application. In this case, it’s ctime:
The following image shows the string which is returned by the ctime() function:
In this Linux Hint article, we explained the step-by-step process on how to use ctime() to retrieve the Unix time from the system and convert it to a string in date and time format.
In the example that we explored, we explained in detail on how to create the variables and pointers that you need to use in this function. We also briefly discussed the additional functions needed in the “time” library to process and retrieve the time data, and apply them in the example. We hope that you found this article useful. For more Linux tips on the C language, use the search engine on our website.