C Programming

Ctime() Function in C Language

As we explained in several articles about the functions defined in “time.h”, time data is calculated from the seconds elapsed since 00:00:00 UTC on January 1, 1970 or UNIX time. To use this data in a user-readable format, you must convert this time to a string in datetime format.

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

char * str ctime ( const time_t *t_ptr );

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:

"Www Mmm dd hh:mm:ss yyyy \n\0"

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:

#include <time.h>

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:

#include <stdio.h>

#include <time.h>

void main()

  {

time_t Unix_t;

U_time = time(NULL);

printf ("UNIX Time: %ld\n", U_time);

  }

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:

const char* str_ptr;

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:

#include <stdio.h>

#include <time.h>

void main()
   {
    time_tU_time;       //Define U_time as output of time()
    const char* str_ptr;    //Define the string pointer
    U_time = time ( NULL ); //We get the UTC time in U_time

    str_ptr =ctime (&U_time );
    printf ( "Local time is: %s\n", str_ptr );
   }

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:

~$ gcc Documents/main.c -o ctime

To execute, we then type “/” followed by the name of the application. In this case, it’s ctime:

~$ ./ctime

The following image shows the string which is returned by the ctime() function:

Conclusion

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.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).