![]() |
VOOZH | about |
The time.h header file contains definitions of functions to get and manipulate date and time information. It also includes functions, types and macros, which are used by programs to measure time, manipulate dates and format time information. It describes three time-related data types.
The time.h header file contains time related operations like getting the current time, converting between different time formats. It also contains CLOCKS_PER_SEC macro which holds the number of times does the system clock ticks per second.
| Function Name | Explanation |
|---|---|
| asctime() | This function returns the date and time in the format day month date hours:minutes:seconds year. Eg: Sat Jul 27 11:26:03 2019. asctime() function returns a string by taking struct tm variable as a parameter. |
| clock() | This function returns the processor time consumed by a program |
| ctime() | This function returns the date and time in the format day month date hours:minutes:seconds year Eg: Sat Jul 27 11:26:03 2019 time is printed based on the pointer returned by Calendar Time |
| difftime() | This function returns the difference between the times provided. |
| gmtime() | This function prints the UTC (Coordinated Universal Time) Time and date. Format for both gmtime() and asctime() is same |
| mktime() | This function returns the calendar-time equivalent using struct tm. |
| time() | This function returns the calendar-time equivalent using data-type time_t. |
| strftime() | This function helps to format the string returned by other time functions using different format specifiers |
To get the current date and time we use the combination of time() , localtime(), and asctime() functions as shown:
Example: C program to get current local time
Tue Apr 15 07:22:42 2025
To print the UTC time, we use the gmtime() function that converts the time obtained by the time() function into a Coordinated Universal Time (also known as Greenwich Mean Time).
Tue Apr 15 12:44:17 2025
The time difference between the time_t variables recorded between some time intervals is used to check the execution time of the part of the code.
Example: The program uses the difftime() functions defined in the time .h header file. This function take two time value as the starting and ending time and return the difference between them.
Sum of 0 and 0 is 0 Time taken to print sum is 0.00 seconds
Note: If user gives input slowly that time also add up for total execution time.
The program uses clock() function defined in time.h which is used to find the number of clock ticks during the executions of a code. Which allows us to measure the time taken by CPU to execute the code.
The number of primes lower than 10, 000 is: 1229 No. of clicks 2837 clicks (0.002837 seconds).
The program uses strftime() function which formats the date and time as string according to the specified format by the user. To define the format for the time the string uses format specifiers such as %l for hour in 12 hour format, %M for minutes and %p to for AM or PM.
Time is 09:00AM.