zoukankan      html  css  js  c++  java
  • Linux时间操作(time、gettimeofday)

    一、time函数   

    1. #include <time.h>
    2. time_t time(time_t *calptr);

    返回距计算机元年的秒数
    一旦取得这种以秒计的很大的时间值后,通常要调用另一个时间函数将其变换为人们可读的时间和日期

    #include <time.h>    
    //calendar time into a broken-down time expressed as UTC
    struct tm *gmtime(const time_t *calptr);

    //converts the calendar time to the local time, taking into account the local time zone and
    //daylight saving time flag
    struct tm *localtime(const time_t *calptr);

    //converts it into a time_t value
    time_t mktime(struct tm *tmptr);

      struct tm {        /* a broken-down time */
         int  tm_sec;     /* seconds after the minute: [0 - 60] */
         int  tm_min;     /* minutes after the hour: [0 - 59] */
         int  tm_hour;    /* hours after midnight: [0 - 23] */
         int  tm_mday;    /* day of the month: [1 - 31] */
         int  tm_mon;     /* months since January: [0 - 11] */
         int  tm_year;    /* years since 1900 */
         int  tm_wday;    /* days since Sunday: [0 - 6] */
         int  tm_yday;    /* days since January 1: [0 - 365] */
         int  tm_isdst;   /* daylight saving time flag: <0, 0, >0 */

      // 以下两个字段在有些版本中是存在的,使用时需要查看对应的头文件确认

      long int tm_gmtoff; /* Seconds east of UTC. */
      const char *tm_zone; /* Timezone abbreviation. */

       };  

    1. char *asctime(const struct tm *tmptr);
    2. char *ctime(const time_t *calptr);
    3. asctime()和ctime()函数产生形式的26字节字符串,这与date命令的系统默认输出形式类似:
          Tue Feb 10 18:27:38 2004/n/0


    二、gettimeofday函数得到更精确的时间

    1. #include <sys/time.h>
    2. int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
    3. 第二个形参是基于平台实现的,使用的时候最好用NULL

    struct timeval{
        time_t tv_sec;        /*** second ***/
        susecond_t tv_usec;    /*** microsecond 微妙***/
    }

    1秒=1000毫秒,
    1毫秒=1000微秒,
    1微妙=1000纳秒,
    1纳秒=1000皮秒。
    秒用s表现,毫秒用ms,微秒用μs表示,纳秒用ns表示,皮秒用ps表示。

    三、内核时间 

    内核有两个重要的全局变量:
    unsigned long jiffies;
    timeval xtime ;


    jiffies 是记录着从电脑开机到现在总共的"时钟中断"的次数。
    文件linux-2.6.24/kernel/timer.c  
    void do_timer(unsigned long ticks)
    {
        jiffies_64 += ticks;
        update_times(ticks);
    }


    xtime 是从cmos电路或rtc芯片中取得的时间,一般是从某一历史时刻开始到现在的时间。
    这个就是所谓的"墙上时钟walltimer",通过它可计算得出操作系统需要的日期时间,它的精确度是微秒。

    xtime第一次赋值是在系统启动时调用timekeeping_init或time_init进行的
    再调用read_persistent_clock进一步调用get_rtc_time得到的

    PS:在/proc/uptime里面的两个数字分别表示:   
    the uptime of the system(seconds),
    and the amount of time spent in idle process(seconds). 

     注:

      今天写一个时间管理模块,updateTime(time)  time为year month day hour minute second minlliseconds任意一个,包括正数 负数

        其中一个约定为  1970年之后的时间管理。 主要通过闰年来管理日期。对某个量进行设置后,对应调整日期,比较麻烦。

         如果利用 系统的  mktime 和 localtime/gmtime  在 tm size_t 之间进行转换,及其方便!  

    参考:

    http://blog.csdn.net/scottgly/article/details/6568513

  • 相关阅读:
    CF1539 VP 记录
    CF1529 VP 记录
    CF875C National Property 题解
    CF1545 比赛记录
    CF 1550 比赛记录
    CF1539E Game with Cards 题解
    CF1202F You Are Given Some Letters... 题解
    vmware Linux虚拟机挂载共享文件夹
    利用SOLR搭建企业搜索平台 之九(solr的查询语法)
    利用SOLR搭建企业搜索平台 之四(MultiCore)
  • 原文地址:https://www.cnblogs.com/yaozhongxiao/p/3020353.html
Copyright © 2011-2022 走看看