12时钟

系统时间

#include<time.h>

time_t time(time_t &tSec)

获取当前时间,1900年1月1日0时到现在的秒钟数

 

double difftime(time_t  timeEnd, time_t timeStart)

时间差

 

 

系统时间

两个互逆的函数

struct tm*  localtime(const time_t *pTSec)

秒钟数转为系统时间

 

time_t  mktime(struct tm *pstTime)

系统时间转为秒钟数

 

结构体原型:

struct tm {

       int tm_sec;         /* seconds */

       int tm_min;         /* minutes */

       int tm_hour;        /* hours */

       int tm_mday;        /* day of the month */

       int tm_mon;         /* month */

       int tm_year;        /* year */

       int tm_wday;        /* day of the week */

       int tm_yday;        /* day in the year */

       int tm_isdst;       /* daylight saving time */

       };

 

 

系统时间

size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);

如:

strftime(szTime,sizeof(szTime), "%Y-%m-%d %X: ", pTime);

strftime(szTime,sizeof(szTime),"DateTime:%x %X\n",pNow);

 

 

 

例子:

void testTime()

{

      time_t sec;

      struct tm *pNow;

      char szTime[128];

     

      //获取从1900年到现在的秒数

      sec=time(NULL);

//   time(&sec);

 

      printf("time:%d\n",(int)time);

      //把秒数转换成本地时间格式 如:2018-2-22 11:11:11

      pNow=localtime(&sec);

     

      //秒数从1900年开始,如今年份要加1900,月份默认从0月开始

      printf("DATE:%04d-%02d-%02d\nTIME:%02d:%02d:%02d\nWEEK:%d\n"

      ,pNow->tm_year+1900,pNow->tm_mon+1,pNow->tm_mday,pNow->tm_hour,

      pNow->tm_min,pNow->tm_sec,pNow->tm_wday);

     

      time_t secc=mktime(pNow);

      printf("secc=%d\n",secc);

     

      //字符串格式输出时间 %x--日期  %X--时间

      strftime(szTime,sizeof(szTime),"DateTime:%x %X\n",pNow);

      printf("%s\n",szTime);

}

 

 

系统时间-微秒级

#include<sys/time.h>

#include<unistd.h>

 

gettimeofday(struct timeval *tv, struct timezone *tz);

获取系统时间和时区

settimeofday(struct timeval *tv, struct timezone *tz);

修改系统时间和时区; 需要root权限

 

结构体原型:

系统时间-微秒级

struct  timeval

{

      long tv_sec                  /*秒钟*/   

      long tv_usec                /*微秒:  10^(-6) 秒*/

}

struct  timezone

{

      int tz_minuteswest         /*和格林威治时间相差分钟数*/          

      int tz_dsttime             /*修正时间*/

}

东 8 区 ==》minuteswest = -60 * 8

西 N 区 ==》minuteswest = 60 * N

 

 

例子:

void testMicroTime()

{

      struct timeval tv;

      struct timezone tz;

 

      //获取系统时间和时区

      gettimeofday(&tv,&tz);

      struct tm *pNow=localtime(&(tv.tv_sec));

     

      printf("DATE:%04d-%02d-%02d\nTIME:%02d:%02d:%02d\nWEEK:%d\n"

      ,pNow->tm_year+1900,pNow->tm_mon+1,pNow->tm_mday,pNow->tm_hour,

      pNow->tm_min,pNow->tm_sec,tv.tv_usec);

     

      printf("Zone:offset minute:%d,dst=%d\n",tz.tz_minuteswest,tz.tz_dsttime);

     

      //测试

      time_t t1,t2;

      struct timeval tv1,tv2;

     

      time(&t1);

      gettimeofday(&tv1,NULL);

      int i=0,sum=0;

      while(i++<0xfffffff)

      {

           sum+=i;

      }   

      time(&t2);

      gettimeofday(&tv2,NULL);

     

      int usec=tv2.tv_usec-tv1.tv_usec;

      int sec=tv2.tv_sec-tv2.tv_sec;

     

      if(usec<0)

      {

           usec+=1000*1000;

           sec--;

      }

      printf("Interval time:%d.%d\n",sec,usec);    

      printf("Interval second:%d\n",(int)(t2-t1));

}

 

转载于:https://www.cnblogs.com/gd-luojialin/p/9215995.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值