zoukankan      html  css  js  c++  java
  • 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 ",pNow);

    例子:

    void testTime()

    {

          time_t sec;

          struct tm *pNow;

          char szTime[128];

         

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

          sec=time(NULL);

    //   time(&sec);

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

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

          pNow=localtime(&sec);

         

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

          printf("DATE:%04d-%02d-%02d TIME:%02d:%02d:%02d WEEK:%d "

          ,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 ",secc);

         

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

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

          printf("%s ",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 TIME:%02d:%02d:%02d WEEK:%d "

          ,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 ",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 ",sec,usec);    

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

    }

  • 相关阅读:
    Nacos启动异常:failed to req API:/api//nacos/v1/ns/instance after all servers([127.0.0.1:8848])
    多节点集群思路
    内网dns配置
    MySQL集群配置思路
    pycharm常用快捷键
    2020年11月新版CKA考试心得
    JavaScript的Map、Set、WeakMap和WeakSet
    AJAX传输二进制数据
    linux性能监测与优化的指令
    八千字硬核长文梳理Linux内核概念及学习路线
  • 原文地址:https://www.cnblogs.com/gd-luojialin/p/9215995.html
Copyright © 2011-2022 走看看