1 struct tm { 2 int tm_sec; 3 int tm_min; 4 int tm_hour; 5 int tm_mday; 6 int tm_mon; 7 int tm_year; 8 int tm_wday; 9 int tm_yday; 10 int tm_isdst; 11 };
参数说明: timer-使用time()函数获得的机器时间
1 #include <time.h> 2 #include <stdio.h> 3 #include <dos.h> 4 int main() { 5 time_t timer; 6 struct tm *tblock; 7 timer=time(NULL); 8 tblock=localtime(&timer); 9 printf("Local time is: %s",asctime(tblock)); 10 return 0; 11 }
1 #include <stdio.h> 2 #include <string.h> 3 #include <time.h> 4 int main() { 5 struct tm t; 6 char str[80]; 7 t.tm_sec=1; 8 t.tm_min=3; 9 t.tm_hour=7; 10 t.tm_mday=22; 11 t.tm_mon=11; 12 t.tm_year=56; 13 t.tm_wday=4; 14 t.tm_yday=0; 15 t.tm_isdst=0; 16 strcpy(str,asctime(&t)); 17 printf("%s",str); 18 return 0; 19 }
1 #include <stdio.h> 2 #include <time.h> 3 int main() { 4 time_t t; 5 time(&t); 6 printf("Today‘s date and time: %s",ctime(&t)); 7 return 0; 8 }
@函数名称: difftime
1 #include <time.h> 2 #include <stdio.h> 3 #include <dos.h> 4 #include <conio.h> 5 int main() { 6 time_t first, second; 7 clrscr(); 8 first=time(NULL); 9 delay(2000); 10 second=time(NULL); 11 printf("The difference is: %f seconds",difftime(second,first)); 12 getch(); 13 return 0; 14 }
@函数名称: gmtime
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <dos.h> 5 char *tzstr="TZ=PST8PDT"; 6 int main() { 7 time_t t; 8 struct tm *gmt, *area; 9 putenv(tzstr); 10 tzset(); 11 t=time(NULL); 12 area=localtime(&t); 13 printf("Local time is:%s", asctime(area)); 14 gmt=gmtime(&t); 15 printf("GMT is:%s", asctime(gmt)); 16 return 0; 17 }
@函数名称: time
1 #include <time.h> 2 #include <stdio.h> 3 #include <dos.h> 4 int main() { 5 time_t t; 6 t=time(); 7 printf("The number of seconds since January 1,1970 is %ld",t); 8 return 0; 9 }
@函数名称: tzset
1 #include <time.h> 2 #include <stdlib.h> 3 #include <stdio.h> 4 int main() { 5 time_t td; 6 putenv("TZ=PST8PDT"); 7 tzset(); 8 time(&td); 9 printf("Current time=%s",asctime(localtime(&td))); 10 return 0; 11 }
转自:http://blog.csdn.net/wangluojisuan/article/details/7045592
原文:http://www.cnblogs.com/skactor/p/4950657.html