C库中与时间相关的函数

C语言中的时间相关的函数
<time.h> 是C标准函数库中获取时间与日期、对时间与日期数据操作及格式化的头文件。
宏:
    NULL null是一个null指针常量的值
    CLOCKS_PER_SEC 每秒的时钟数

变量:
    typedef size_t 类型定义
    typedef clock_t类型定义
     struct tm 结构体
     struct tm {
     int tm_sec;  /*  秒 – 取值区间为[0,59]  */
     int tm_min;  /*  分 - 取值区间为[0,59]  */
     int tm_hour;  /*  时 - 取值区间为[0,23]  */
     int tm_mday;  /*  一个月中的日期 - 取值区间为[1,31]  */
     int tm_mon;  /*  月份(从一月开始,0代表一月) - 取值区间为[0,11]  */
     int tm_year;  /*  年份,其值等于实际年份减去1900  */
     int tm_wday;  /*  星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推  */
     int tm_yday;  /*  从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推  */
     int tm_isdst;  /*  夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。 */
    };
函数:
clock_t  clock( void);; 返回从程序运行开始所经过的CPU时钟数

示例程序:
/*  clock example: frequency of primes  */
#include <stdio.h>
#include <time.h>
#include <math.h>

int frequency_of_primes ( int n) {
   int i,j;
   int freq=n- 1;
   for (i= 2; i<=n; ++i)  for (j=sqrt(i);j> 1;--j)  if (i%j== 0) {--freq;  break;}
   return freq;
}

int main ()
{
   int f;
   int t;
  printf ( " Calculating...\n ");
  f = frequency_of_primes ( 99999);
  t = clock();
  printf ( " The number of primes lower than 100,000 is: %d\n ",f);
  printf ( " It took me %d clicks (%f seconds).\n ",t,(( float)t)/CLOCKS_PER_SEC);
   return  0;
}

相关函数如下:
time_t   time(time_t *timer); 取得目前的时间
struct tm *localtime( const time_t *timer);把time_t转换为当地时间和日期
double   difftime(time_t time1, time_t time2); 计算两个时刻之间的时间差
time_t  mktime( struct tm *timeptr);将时间结构数据转换成经过的秒数
char *asctime( const  struct tm *timeptr); 将时间和日期以字符串格式表示
char *ctime( const time_t *timer); 把日期和时间转换为字符串
struct tm *gmtime( const time_t *timer); 把日期和时间time_t转换为(GMT)时间
size_t   strftime( char *ptr, size_t maxsize,  const  char *format,  const  struct tm *timeptr); 将时间格式化为字符串

示例程序如下:
#include <stdio.h>
#include <time.h>
#define MST (-7)
#define UTC (0)
#define CCT (+8)
int main(){
    time_t rawtime;
     struct tm *timeinfo,*ptm;
    time_t start,end;
     char szInput [ 256];
     char buffer[ 80];
     double dif;
     char *weekday[]={ " Sunday ", " Monday ", " Tuesday ", " Wednesday ", " Thursday ",
         " Friday ", " Saturday "};
    time(&rawtime);
    printf( " The current local time is:<ctime>%s ",ctime(&rawtime));

    timeinfo=localtime(&rawtime);
    printf( " The current local time is:<asctime>%s ",asctime(timeinfo));
    
    strftime (buffer, 80, " Now it's %I:%M%p. ",timeinfo);
    puts (buffer);

    mktime(timeinfo);
    printf( " Today is a %s.\n ",weekday[timeinfo->tm_wday]);    

    ptm=gmtime(&rawtime);
    puts ( " Current time around the World: ");
    printf ( " Phoenix, AZ (U.S.) :  %2d:%02d\n ", (ptm->tm_hour+MST)% 24, ptm->tm_min);
    printf ( " Reykjavik (Iceland) : %2d:%02d\n ", (ptm->tm_hour+UTC)% 24, ptm->tm_min);
    printf ( " Beijing (China) :     %2d:%02d\n ", (ptm->tm_hour+CCT)% 24, ptm->tm_min);

    time (&start);
    printf ( " Please, enter your name:  ");
    fgets (szInput, 256,stdin);
    time (&end);
    dif = difftime (end,start);
    printf ( " Hi %s.\n ", szInput);
    printf ( " It took you %.2lf seconds to type your name.\n ", dif );             
}
          




转载于:https://www.cnblogs.com/xkfz007/archive/2012/10/11/2720628.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值