Sei sulla pagina 1di 8

C Time related functions

Prev
Next
C time functions are used to interact with system time routine and formatted time outputs
are displayed. Example programs for the time functions are given below.
S.no Functions Description
1 setdate() This function used to modify the system date
2 getdate() This function is used to get the CPU time
3 clock() This function is used to get current system time
4 time() This function is used to get current system time as structure
5 difftime()
This function is used to get the difference between two
given times
6 strftime() This function is used to modify the actual time format
7 mktime() This function interprets tm structure as calendar time
8 localtime()
This function shares the tm structure that contains date and
time informations
9 gmtime()
This function shares the tm structure that contains date and
time informations
10 ctime()
This function is used to return string that contains date and
time informations
11 asctime()
Tm structure contents are interpreted by this function as
calendar time. This time is converted into string.
Example program for setdate():
This function is used to modify the system date.
C
#include<stdio.h>
#include<dos.h>
#include<conio.h>
int main()

1
2
3
4
5
6
7
8
9
#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);
10
11
12
13
14
15
16
17

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():
This function is used to get the CPU time
C
#include<stdio.h>
#include<dos.h>
int main()
{

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 systems current date is 12-01-
2012
Example program for clock():
This function is used to get current system time
C
#include <stdio.h>
#include <time.h>
#include <math.h>
int main()

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():
This function is used to get current system time as structure
C
#include <stdio.h>
#include <time.h>
int main ()
{

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():
This function is used to get the difference between two given times
C
#include <stdio.h>
#include <time.h>
int main()
{

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():
strftime() - This function is used to modify the actual time format. asctime() tm structure
contents are interpreted by this function as calendar time. This time is converted into string.
localtime() This function shares the tm structure that contains date and time informations
C
#include <stdio.h>
#include <time.h>
#define LEN 150

1
2
#include <stdio.h>
#include <time.h>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#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():
mktime() This function interprets tm structure as calendar time ctime() - This function is
used to return string that contains date and time informations
C
#include <stdio.h>
#include <time.h>
int main()

1
2
3
4
5
6
7
#include <stdio.h>
#include <time.h>

int main()
{
struct tm strtime;
time_t timeoftheday;
8
9
10
11
12
13
14
15
16
17
18
19
20
21

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():
This function shares the tm structure that contains date and time informations.
C
#include <stdio.h>
#include <time.h>
int main()

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



header
<ctime> (time.h)
C Time Library
This header fle contains defnitons of functons to get and manipulate date and tme informaton.


Functions
Time manipulation
clock
Clock program (functon )
difime
Return diference between two tmes (functon )
mktme
Convert tm structure to tme_t (functon )
tme
Get current tme (functon )

Conversion
asctme
Convert tm structure to string (functon )
ctme
Convert tme_t value to string (functon )
gmtme
Convert tme_t to tm as UTC tme (functon )
localtme
Convert tme_t to tm as local tme (functon )
strfime
Format tme as string (functon )


Macro constants
CLOCKS_PER_SEC
Clock tcks per second (macro )
NULL
Null pointer (macro )


types
clock_t
Clock type (type )
size_t
Unsigned integral type (type )
tme_t
Time type (type )
struct tm
Time structure (type )

Potrebbero piacerti anche