#include <time.h>
time_t time(time_t *tloc);
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main()
{
int i;
time_t the_time;
for(i = 1; i <= 10; i++) {
the_time = time((time_t *)0);
printf("The time is %ld\n", the_time);
sleep(2);
}
exit(0);
}
[root@localhost C]# ./envtime.exe
The time is 1235037957
The time is 1235037959
The time is 1235037961
The time is 1235037963
The time is 1235037965
The time is 1235037967
The time is 1235037969
The time is 1235037971
The time is 1235037973
The time is 1235037975
#include <time.h>
struct tm *localtime(const time_t *timeval);
#include <time.h>
time_t mktime(struct tm *timeptr);
#include <time.h>
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timeval);
#include <stdio.h>
#include <time.h>
int main()
{
struct tm *timeptr;
time_t the_time;
time(&the_time);
timeptr = localtime(&the_time);
printf("%s", asctime(timeptr) ); //由于asctime()函数输出字符串时会自动换行这里就没必要再用\n
return 0;
}
[root@localhost C]# ./asctime.exe
Fri Feb 20 12:12:37 2009
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t timeval;
(void)time(&timeval);
printf("The date is: %s",ctime(&timeval) );
exit(0);
}
[root@localhost C]# ./ctime.exe
The date is: Fri Feb 20 12:15:45 2009
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |