linux时间知多少

一、几个概念

1.硬件时间:指主机板上BIOS中的时间,这个时间由主板上电池供电维持,如果主板电池电量耗尽,并断电的话,恢复到出厂设置。
2.系统时间:就是Linux系统中显示的本地时间,该时间取决于系统开机后读取的硬件时间以及当前的时区设置。时区配置文件为 /etc/localtime。/usr/share/zoneinfo下保存了所有时区的配置文件,找个合适的,复制到 /etc/localtime 就可以了。

$ date 
2018311日 星期日 230110CST

3.GMT(Greenwich Mean Time,格林威治标准时间):基本可以理解为一个标准时间,时区的原点,按照经线将地球分为24个时区。
4.UTC(Coordinated Universal Time,世界协调时间):在一般精度要求下,UTC与GMT是一样的,但GMT是根据地球的自转和公转来计算时间, UTC 是以原子钟校准的,更精确。在UTC中,每一年或两年会有一个“闰秒”。

$ date -u
2018311日 星期日 150116秒 UTC

5.时区:全球被划分为24个时区,0时区为基准,每个时区相隔1小时,往东则加时间,往西则减时间。中国位于东8区,又称为CST(China Standard Time) CST时间 = UTC时间 + 8小时。
这里写图片描述

$ date -R
Sun, 11 Mar 2018 23:01:19 +0800

6.夏令时:又称日光节约时间(DST,Daylight saving time),是一种为节约能源而人为规定地方时间的制度,在这一制度实行期间所采用的统一时间称为“夏令时间”。各个采纳夏时制的国家具体规定不同,以美国为例,从每年3月第2个星期天凌晨开始(将时钟拨快一小时),到11月第1个星期天凌晨结束(拨慢一小时)。在夏令时的实施期间,标准时间的上午10点就成了夏令时的上午11点。

二、linux时间操作

1.time

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

此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。
一旦取得这种以秒计的很大的时间值后,通常要调用另一个时间函数将其变换为人们可读的时间和日期

2.gtime

#include <time.h>    
//calendar time into a broken-down time expressed as UTC
//gmtime转出来的是0时区的标准时间
struct tm *gmtime(const time_t *calptr);

结构tm代表目前UTC时间

  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 */
   }; 

3.localtime

//converts the calendar time to the local time, taking into account the local time zone and
//daylight saving time flag
//localtime是考虑时区和夏令时的本地时间
struct tm *localtime(const time_t *calptr);

4.ctime

#include<time.h> 
char *ctime(const time_t *timep); 

ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。若再调用相关的时间日期函数,此字符串可能会被破坏。

5.mktime

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

6.gettimeofday

#include <sys/time.h>
int gettimeofday(struct timeval *restrict tp, void *restrict tzp);

第二个形参是基于平台实现的,使用的时候最好用NULL

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

得到精确到us的时间。

三、对比

测试UTC时间为: 2012-09-15 13:30:15

Date {Sat Sep 15 2012 21:30:15 GMT+0800 (China Standard Time)} CST: 2012-09-15 21:30:15

Date {Sat Sep 15 2012 05:30:15 GMT-0800 (Pacific Standard Time)} PST: 2012-09-15 05:30:15

Date {Sat Sep 15 2012 06:30:15 GMT-0700 (Pacific Daylight Time)} PDT: 2012-09-15 06:30:15

Date {Sat Sep 15 2012 06:30:15 GMT-0700 (Mountain Standard Time)} MST: 2012-09-15 06:30:15

Date {Sat Sep 15 2012 07:30:15 GMT-0600 (Mountain Daylight Time)} MDT: 2012-09-15 07:30:15

参考

时间标准总结 IAT、UT、UTC、GMT、夏令时
Linux时间时区详解与常用时间函数
UTC时区与夏令时转换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值