LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 647|回复: 3

时间函数示例程序

[复制链接]
发表于 2004-8-25 14:34:06 | 显示全部楼层 |阅读模式
下面的示例程序中包含了大部分C中所用的常用时间函数,这是我在学C时记的笔记,不知对朋友们是否有用.


  1. #include <time.h>
  2. #include <stdio.h>
  3. #define TIME_SIZE        80        // hope this is big enough

  4. // time(), ctime(), asctime(), localtime(), time_t
  5. timefunc()
  6. {
  7.         time_t        now;
  8.        
  9. //        now = time(NULL);
  10.         time(&now);
  11.         printf("The current dat and time is(ctime): %s", ctime(&now));
  12.         printf("The current dat and time is(asctime): %s", asctime(localtime(&now)));
  13. }

  14. // clock()
  15. clockfunc()
  16. {
  17.         clock_t        start, stop;
  18.         unsigned long        i;

  19.         start=clock();
  20.         for(i=0;i<10000000;i++);
  21.         timefunc();
  22.         stop=clock();
  23.         printf("Star:%f.\nStop:%f.\n", (double)start/CLOCKS_PER_SEC, (double)stop/CLOCKS_PER_SEC);
  24. }

  25. // localtime(), time_t, struct tm
  26. void get_date(int *year, int *month, int *day, char *cweek)
  27. {
  28.         int week;
  29.         char *cweeks="日一二三四五六";
  30.         time_t t;
  31.         struct tm *local_time;

  32.         t = time(0);
  33.         local_time = localtime(&t);

  34.         *year  = 1900 + local_time->tm_year;
  35.         *month = local_time->tm_mon+1;
  36.         *day   = local_time->tm_mday;
  37.         week   = local_time->tm_wday;

  38.         strncpy(cweek,cweeks+week*2,2);
  39.         cweek[2]='\0';

  40.         return ;
  41. }

  42.        
  43. // localtime(), time_t
  44. void get_time(int *hour, int *minute, int *second)
  45. {
  46.         time_t t;
  47.         struct tm *local_time;

  48.         t = time(NULL);
  49.         local_time = localtime(&t);

  50.         *hour   = local_time->tm_hour;
  51.         *minute = local_time->tm_min;
  52.         *second = local_time->tm_sec;

  53.         return ;
  54. }

  55. // 函数返回顾1990年4月15日午夜与当前日期和时间之间相差的秒数
  56. // difftime(), mktime(), time(), struct tm, time_t
  57. double difftimefunc()
  58. {
  59.         struct tm        Apr_15_struct = {0};        //Set all fields to 0
  60.         time_t                Apr_15_t;

  61.         Apr_15_struct.tm_year = 90;  // 1900 + 90 = 1990
  62.         Apr_15_struct.tm_mon = 3;        // 3 + 1 = 4
  63.         Apr_15_struct.tm_mday = 15;

  64.         Apr_15_t = mktime( &Apr_15_struct );

  65.         if (Apr_15_t == (time_t) -1 )
  66.                 return 0.0;        // error
  67.         else
  68.                 return difftime( time(NULL), Apr_15_t);
  69. }

  70. // strftime()
  71. char *asctime2( const struct tm *tm )
  72. {
  73.         static char time_buffer[TIME_SIZE];
  74.         size_t        len;

  75.         len = strftime( time_buffer, TIME_SIZE,
  76.                                 "%a %b %d %H:%M:%S %Y\n", tm );
  77.         if ( len == 0 )
  78.                 return NULL;        // time_buffer is too short
  79.         else
  80.                 return time_buffer;
  81. }

  82. main()
  83. {
  84.         double        seconds;
  85.         int        year, month, day, hour,minute, second;
  86.         char        cweek[3];
  87.         time_t        now;

  88.         printf("clock():\n");
  89.         printf("--------------------------------------------------------\n");
  90.         clockfunc();
  91.         printf("Done.\n");

  92.         printf("\nlocaltime():\n");
  93.         printf("--------------------------------------------------------\n");
  94.         get_date(&year, &month, &day, cweek);
  95.         get_time(&hour, &minute, &second);
  96.         printf("今天:%04d年%02d月%02d日-星期%s.\n",year,month,day,cweek);
  97.         printf("当前时间:%02d时%02d分%02d秒.\n",hour,minute,second);
  98.         printf("Done.\n");

  99.         printf("\ndifftime():\n");
  100.         printf("--------------------------------------------------------\n");
  101.         seconds=difftimefunc();
  102.         printf("The seconds from 1990/04/15: %f.\n",seconds);
  103.         printf("Done.\n");

  104.         printf("\nstrftime():\n");
  105.         printf("--------------------------------------------------------\n");
  106.         time(&now);
  107.         printf("NOW: %s\n", asctime2(localtime(&now)));
  108.         printf("All done.\n");
  109. }

复制代码
 楼主| 发表于 2004-8-25 14:39:07 | 显示全部楼层

怎样才能保持我的源码不变?

看上面,用[code]变成了:%, 用[php]又乱码
发表于 2004-8-25 14:59:08 | 显示全部楼层
发贴时把表情符号的那个钩打掉
 楼主| 发表于 2004-8-25 15:02:24 | 显示全部楼层

OK

THX.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表