Time Structure in C

"time.h" Header File in C

In this page, we have explained all about time structure in C and “time.h” Header File in C. Go through the content of this page, to know more about Time Structure in C. 

"time.h" Header File in C with examples

In the C programming language, the time.h header file defines a number of functions and types for working with time and date values.

There are three types of time related data-types, under this:-

Here is an example of how the struct tm type can be used in a C program:

Run

#include <time.h>
#include <stdio.h>
int main() {
time_t current_time;
struct tm *time_info;
char time_string[100];

time(&current_time);
time_info = localtime(&current_time);

strftime(time_string, 100, "%Y-%m-%d %H:%M:%S", time_info);
printf("The current date and time is: %s\n", time_string);

return 0;
}

Output

The current date and time is: 2022-12-24 07:19:41

Important Functions in Time Structure in C

‘time function’

 time(&current_time);
 time_info = localtime(&current_time);
  • The time function is called with the address of the current_time variable as its argument.
  • This function fills the current_time variable with the current time, measured as the number of seconds elapsed since the epoch (midnight on January 1, 1970).
  • The localtime function is then called with the address of the current_time variable as its argument.
  • This function converts the time value stored in current_time to a struct tm value that represents the same point in time, but in the local time zone

‘strftime function’

 strftime(time_string, 100, "%Y-%m-%d %H:%M:%S", time_info);
  • The strftime function is called with following arguments:
    • the time_string array, the size of the array (100 characters in this case),
    • a format string that specifies how the struct tm value should be formatted as a string,
    • and the time_info variable that contains the struct tm value.
    • The format string in this case specifies that the string should include the year (%Y), month (%m), day (%d), hour (%H), minute (%M), and second (%S) of the struct tm value, separated by hyphens and spaces.
    • The strftime function formats the struct tm value according to the specified format and stores the resulting string in the time_string array.

‘printf function’

 printf("The current date and time is: %s\n", time_string);
  • The printf function is called to print the string stored in the time_string array to the console.
  • The %s format specifier in the format string tells printf to treat the argument as a string and print it to the console.
  • The newline character (\n) at the end of the format string causes the output to be printed on a new line.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription