linux时间子系统 - 时间概念

1. UTC与GMT


UTC,协调世界时(Coordinated Universal Time),又称世界统一时间,世界标准时间,国际协调时间。协调世界时是以原子时秒长为基础,在时刻上尽量接近于世界时的一种时间计量系统。这套时间系统被应用于许多互联网和万维网的标准中,例如,网络时间协议就是协调世界时在互联网中使用的一种方式。

GMT,格林尼治标准时间(Greenwich Mean Time,GMT)是指位于伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。 理论上来说,格林尼治标准时间的正午是指当太阳横穿格林尼治子午线时的时间。由于地球在它的椭圆轨道里的运动速度不均匀,这个时刻可能和实际的太阳时相差16分钟。 地球每天的自转是有些不规则的,而且正在缓慢减速。所以,格林尼治时间已经不再被作为标准时间使用。现在的标准时间——协调世界时(UTC)——由原子钟提供。 自1924年2月5日开始,格林尼治天文台每隔一小时会向全世界发放调时信息。而UTC是基于标准的GMT提供的准确时间。

简单来说,北京时间=UTC+8=GMT+8。

2. linux对于时间的不同表达


2.1 对于秒的表达

linux中的时间是基于1970年1月1日以来到现在的秒数。

linux中对于秒的表达有三种精度表达:

  • __kernel_time_t
typedef __kernel_long_t __kernel_time_t;
typedef long        __kernel_long_t;

__kernel_time_t的最小精度是“”级的

  • timeval

(include/uapi/linux/time.h)

struct timeval {
    __kernel_time_t     tv_sec;     /* seconds */
    __kernel_suseconds_t    tv_usec;    /* microseconds */
};

timeval的最小精度是“微秒“级的

  • timespec

(include/uapi/linux/time.h)

struct timespec {
    __kernel_time_t tv_sec;         /* seconds */
    long        tv_nsec;        /* nanoseconds */
};

timespec的最小精度是”纳秒“级的

(time64.h) - 64位的表达

struct timespec64 {
    time64_t    tv_sec;         /* seconds */
    long        tv_nsec;        /* nanoseconds */
};

2.2 人类时间的表达

我们一般比较熟悉的时间都是用“年月日时分秒”来表达的,所以在linux中有一个结构体来表示这样的信息,此结构体为struct tm:
(include/linux/time.h)

struct tm {
    /*
     * the number of seconds after the minute, normally in the range
     * 0 to 59, but can be up to 60 to allow for leap seconds
     */
    int tm_sec;
    /* the number of minutes after the hour, in the range 0 to 59*/
    int tm_min;
    /* the number of hours past midnight, in the range 0 to 23 */
    int tm_hour;
    /* the day of the month, in the range 1 to 31 */
    int tm_mday;
    /* the number of months since January, in the range 0 to 11 */
    int tm_mon;
    /* the number of years since 1900 */
    long tm_year;
    /* the number of days since Sunday, in the range 0 to 6 */
    int tm_wday;
    /* the number of days since January 1, in the range 0 to 365 */
    int tm_yday;
};

3. linux的时间记录


3.1 jiffies

jiffies变量是一个计数器,用来记录自系统启动以来产生的节拍总数,jiffies是一个32位的变量,因此每隔大约50天它的值会重置为0。另一个变量jiffies_64是一个64位的变量。

extern u64 __jiffy_data jiffies_64;
extern unsigned long volatile __jiffy_data jiffies;

当jiffies溢出时,time_after、time_before、time_after_eq、time_before_eq等一些宏会进行处理。

(jiffies.h)

#define time_after(a,b)     \
    (typecheck(unsigned long, a) && \
     typecheck(unsigned long, b) && \
     ((long)((b) - (a)) < 0))
#define time_before(a,b)    time_after(b,a)

#define time_after_eq(a,b)  \
    (typecheck(unsigned long, a) && \
     typecheck(unsigned long, b) && \
     ((long)((a) - (b)) >= 0))
#define time_before_eq(a,b) time_after_eq(b,a)

3.2 时间记录

monotonic:代表系统启动以来走过的秒数,系统启动刚开始值为0;
wall_to_monotonic:代表一个偏移;
xtime_sec:代表人们看到的实际时间,也就是系统时间。

这里写图片描述

Change Log


datecontentlinux
2016.12.11初稿kernel 4.6.3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值