|
下面的示例程序中包含了大部分C中所用的常用时间函数,这是我在学C时记的笔记,不知对朋友们是否有用.
- #include <time.h>
- #include <stdio.h>
- #define TIME_SIZE 80 // hope this is big enough
- // time(), ctime(), asctime(), localtime(), time_t
- timefunc()
- {
- time_t now;
-
- // now = time(NULL);
- time(&now);
- printf("The current dat and time is(ctime): %s", ctime(&now));
- printf("The current dat and time is(asctime): %s", asctime(localtime(&now)));
- }
- // clock()
- clockfunc()
- {
- clock_t start, stop;
- unsigned long i;
- start=clock();
- for(i=0;i<10000000;i++);
- timefunc();
- stop=clock();
- printf("Star:%f.\nStop:%f.\n", (double)start/CLOCKS_PER_SEC, (double)stop/CLOCKS_PER_SEC);
- }
- // localtime(), time_t, struct tm
- void get_date(int *year, int *month, int *day, char *cweek)
- {
- int week;
- char *cweeks="日一二三四五六";
- time_t t;
- struct tm *local_time;
- t = time(0);
- local_time = localtime(&t);
- *year = 1900 + local_time->tm_year;
- *month = local_time->tm_mon+1;
- *day = local_time->tm_mday;
- week = local_time->tm_wday;
- strncpy(cweek,cweeks+week*2,2);
- cweek[2]='\0';
- return ;
- }
-
- // localtime(), time_t
- void get_time(int *hour, int *minute, int *second)
- {
- time_t t;
- struct tm *local_time;
- t = time(NULL);
- local_time = localtime(&t);
- *hour = local_time->tm_hour;
- *minute = local_time->tm_min;
- *second = local_time->tm_sec;
- return ;
- }
- // 函数返回顾1990年4月15日午夜与当前日期和时间之间相差的秒数
- // difftime(), mktime(), time(), struct tm, time_t
- double difftimefunc()
- {
- struct tm Apr_15_struct = {0}; //Set all fields to 0
- time_t Apr_15_t;
- Apr_15_struct.tm_year = 90; // 1900 + 90 = 1990
- Apr_15_struct.tm_mon = 3; // 3 + 1 = 4
- Apr_15_struct.tm_mday = 15;
- Apr_15_t = mktime( &Apr_15_struct );
- if (Apr_15_t == (time_t) -1 )
- return 0.0; // error
- else
- return difftime( time(NULL), Apr_15_t);
- }
- // strftime()
- char *asctime2( const struct tm *tm )
- {
- static char time_buffer[TIME_SIZE];
- size_t len;
- len = strftime( time_buffer, TIME_SIZE,
- "%a %b %d %H:%M:%S %Y\n", tm );
- if ( len == 0 )
- return NULL; // time_buffer is too short
- else
- return time_buffer;
- }
- main()
- {
- double seconds;
- int year, month, day, hour,minute, second;
- char cweek[3];
- time_t now;
- printf("clock():\n");
- printf("--------------------------------------------------------\n");
- clockfunc();
- printf("Done.\n");
- printf("\nlocaltime():\n");
- printf("--------------------------------------------------------\n");
- get_date(&year, &month, &day, cweek);
- get_time(&hour, &minute, &second);
- printf("今天:%04d年%02d月%02d日-星期%s.\n",year,month,day,cweek);
- printf("当前时间:%02d时%02d分%02d秒.\n",hour,minute,second);
- printf("Done.\n");
- printf("\ndifftime():\n");
- printf("--------------------------------------------------------\n");
- seconds=difftimefunc();
- printf("The seconds from 1990/04/15: %f.\n",seconds);
- printf("Done.\n");
- printf("\nstrftime():\n");
- printf("--------------------------------------------------------\n");
- time(&now);
- printf("NOW: %s\n", asctime2(localtime(&now)));
- printf("All done.\n");
- }
复制代码 |
|