您的位置:首页 > 运维架构 > Linux

《linux 下的时间获取函数》

2014-08-03 17:37 429 查看
一. linux时间漫谈
linux内部对时间是用从Epoch时间开始计时的秒数来表示的。Epoch也就是通用协调时间(UTC,也就是格林尼治平均时间或格林威治标准时间,CMT)。linux将这个数据存储在time_t结构体变量里,目前的32为系统最多支撑到2038年,64位系统就还早着呢。

谈到时间,不得不讲一下时区的问题。以格林尼治所在地区为标准,向东向西各划分出12个时区,每个时区之间经度相差15度,时间相差1小时。例如,北京在东八区,以东经120度(15*8)为中轴(范围在112.5~127.5度),东八区的时间比格林尼治地区时间早8小时。也就是说,如果linux以格林尼治地区1970年01月01日0点开始计时,处在东八区的北京则实际上是以1970年01月01日早上8点开始计时。

不信你试试下面两个命令,看看输出什么:

[root@Brandy ~]# date --date="19700101 00:00:00" +%s
-28800
[root@Brandy ~]# date --date="19700101 08:00:00" +%s
0


二. linux时间的获取和转换。
-----------------------------------------------------------------------------------------------------------------------------------

| 系统调用 | 库函数

-----------------------------------------------------------------------------------------------------------------------------------

读取时间 | gettimeofday,time | ftime,

-----------------------------------------------------------------------------------------------------------------------------------

格式转换 | | ctime,localtime,asctime,strftime,gmtime;mktime,

-----------------------------------------------------------------------------------------------------------------------------------

设置时间 | settimeofday,stime |

-----------------------------------------------------------------------------------------------------------------------------------

1. 获取基本时间信息

time函数返回从Epoch(1970年1月1日00:00:00 UTC)开始所经过的秒数。
localtime /gmtime函数分别获取年月日时分秒的数值(localtime是转换成本地时间,gmtime是转换成格林尼治时间)。
asctime/ctime函数把时间转换成字符串(ctime函数直接将time函数返回的time_t类型时间转换成字符串)。

#include <time.h>

time_t time(time_t *t);

struct tm *localtime(const time_t *timep);

struct tm *gmtime(const time_t *timep);

char *asctime(const struct tm *tm);

例如:

[root@HPC-NQS multi_job_submit]# cat get_cur_time.c

#include <stdio.h>
#include <time.h>

int main()
{
time_t cur_t;
struct tm* cur_tm;

time(&cur_t);
cur_tm=localtime(&cur_t);
printf("Current time is: %s",asctime(cur_tm));

cur_tm=gmtime(&cur_t);
printf("Current time is: %s",asctime(cur_tm));

printf("Current time is: %s",ctime(&cur_t));

return 0;
}

执行结果:

[root@HPC-NQS multi_job_submit]# ./get_cur_time

Current time is: Sun Aug 3 17:32:28 2014

Current time is: Sun Aug 3 09:32:28 2014

Current time is: Sun Aug 3 17:32:28 2014

2. 获取精确时间

gettimeofday获取精确到微秒的系统时间,结构体类型为struct timeval。
struct timeval类型中的元素tv_sec等价于time函数获取的系统时间,可进一步利用localtime/gmtime函数和asctime函数转换格式。

#include <sys/time.h>

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

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

struct timeval {

time_t tv_sec; /* seconds */

suseconds_t tv_usec; /* microseconds */

};

struct timezone {

int tz_minuteswest; /* minutes west of Greenwich */

int tz_dsttime; /* type of DST correction */

};

PS:使用函数gettimeofday/settimeofday的时候通常第二个参数设置为NULL。

例如:

[root@HPC-NQS multi_job_submit]# cat get_cur_utime.c

#include <time.h>
#include <sys/time.h>

int main()
{
struct timeval cur_t;
struct tm* cur_tm;

gettimeofday(&cur_t,NULL);
cur_tm=localtime(&cur_t.tv_sec);
printf("Current time is: %sMicro seconds is: %d.\n",asctime(cur_tm),cur_t.tv_usec);

return 0;
}

输出结果:

[root@HPC-NQS multi_job_submit]# ./get_cur_utime

Current time is: Sun Aug 3 17:35:09 2014

Micro seconds is: 93337.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: