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.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<stdio.h> #include<dos.h> #include<conio.h> int main() {    struct date dt;    printf("Enter new date in the format(day month year)");    scanf("%d%d%d",&dt.da_day,&dt.da_mon,&dt.da_year);    setdate(&dt);    printf("Now, current system date is %d-%d-%d\n"    ,dt.da_day,dt.da_mon,dt.da_year);    return 0; } | 
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.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<stdio.h> #include<dos.h> int main() {    struct date dt;    getdate(&dt);    printf("Operating system's current date is %d-%d-%d\n"    ,dt.da_day,dt.da_mon,dt.da_year);    return 0; } | 
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.
| 1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #include <time.h> #include <math.h> int main() {     int i;     clock_t CPU_time_1 = clock();     printf("CPU start time is : %d \n", CPU_time_1);     for(i = 0; i < 150000000; i++);     clock_t CPU_time_2 = clock();     printf("CPU end time is : %d", CPU_time_2); } | 
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.
| 1 2 3 4 5 6 7 8 9 10 11 | #include <stdio.h> #include <time.h> int main () {     time_t seconds;     seconds = time (NULL);     printf ("Number of hours since 1970 Jan 1st " \             "is %ld \n", seconds/3600);     return 0; } | 
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.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h> #include <time.h>  int main()  {     time_t begin,end;     long i;     begin= time(NULL);     for(i = 0; i < 150000000; i++);     end = time(NULL);     printf("for loop used %f seconds to complete the " \            "execution\n", difftime(end, begin));     return 0; } | 
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.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> #include <time.h> #define LEN 150 int main () {    char buf[LEN];    time_t curtime;    struct tm *loc_time;    //Getting current time of system    curtime = time (NULL);    // Converting current time to local time    loc_time = localtime (&curtime);    // Displaying date and time in standard format    printf("%s", asctime (loc_time));    strftime (buf, LEN, "Today is %A, %b %d.\n", loc_time);    fputs (buf, stdout);    strftime (buf, LEN, "Time is %I:%M %p.\n", loc_time);    fputs (buf, stdout);    return 0; } | 
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.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h>       #include <time.h>  int main() {     struct tm strtime;     time_t timeoftheday;     strtime.tm_year = 2008-1900;     strtime.tm_mon = 1;     strtime.tm_mday = 4;     strtime.tm_hour = 02;     strtime.tm_min = 30;     strtime.tm_sec = 38;     strtime.tm_isdst = 0;     timeoftheday = mktime(&strtime);     printf(ctime(&timeoftheday));     return 0;         } | 
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.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h> #include <time.h> int main() {     time_t orig_format;     time(&orig_format);     printf ("Universal Time is %s",     asctime(gmtime(&orig_format)));     return 0; } | 
Output:
| Universal Time is Sat Sep 22 08:11:40 2012 | 
