x

C – Time related functions

Prev     Next

Time functions in C are used to interact with system time routine and formatted time outputs are displayed. Example programs for the time functions are given below.


Function
Description
setdate() This function used to modify the system date
getdate() This function is used to get the CPU time
clock() This function is used to get current system time
time() This function is used to get current system time as structure
difftime() This function is used to get the difference between two given times
strftime() This function is used to modify the actual time format
mktime() This function interprets tm structure as calendar time
localtime() This function shares the tm structure that contains date and time informations
gmtime() This function shares the tm structure that contains date and time informations
ctime() This function is used to return string that contains date and time informations
asctime() Tm structure contents are interpreted by this function as calendar time. This time is converted into string.

Example program for setdate() function in C:

This function is used to modify the system date. Please note that other C compilers may not support this setdate() function except turbo C.


Output:

Enter new date in the format (day month year)
01 12 2012
Now, current system date is 01-12-2012

Example program for getdate() function in C:

This function is used to get the CPU time. Please note that other C compilers may not support this getdate() function except turbo C.

Output:

 Operating system’s current date is 12-01-2012

Example program for clock() function in C:

This function is used to get current system time.

 Output:

CPU start time is : 0

CPU end time is  : 380000

Example program for time() function in C:

This function is used to get current system time as structure.

Output:

Number of hours since 1970 Jan 1st is 374528

Example program for difftime() function in C:

This function is used to get the difference between two given times.

Output:

for loop used 15.000000 seconds to complete the execution

Example program for strftime(), asctime() and localtime() in C:

 

  • strftime()    – This function is used to modify the actual time format.
  • asctime()   – tm structure contents are interpreted by asctime() function as calendar time. This time is converted into string.
  • localtime() – This function shares the tm structure that contains date and time informations.

Output:

Sat Sep 22 01:15:03 2012

Today is Saturday, Sep 22.

Time is 01:15 AM.

Example program for mktime() and ctime() functions in C:

  • mktime() function interprets tm structure as calendar time.
  • ctime() function is used to return string that contains date and time informations.

Output:

Mon Feb 4 02:30:38 2008

Example program for gmtime() function in C:

This function shares the tm structure that contains date and time informations.

Output:

Universal Time is Sat Sep 22 08:11:40 2012

Prev     Next



Like it? Please Spread the word!