Linux中表示“时间”的结构体和相关函数相关

转自:http://blog.chinaunix.net/uid-25909722-id-2827364.html

Linux中表示“时间”的结构体和相关函数

在Linux系统中,表示“时间”概念的结构体有多个,相关的时间处理函数也有很多,给人以很混乱的感觉。导致了当我们真正要使用这些结构体和函数的时候,却不知道到底该用哪个结构体和哪些函数。有必要加以归纳总结一下。通过查看头文件/usr/include/time.h 和 /usr/include/bits/time.h

  • (1)我们可以找到下列四种表示“时间”的结构体:
/* Returned by `time'. */
typedef __time_t time_t;

/* A time value that is accurate to the nearest
   microsecond but also has a range of years. */
struct timeval
{
    __time_t tv_sec;       /* Seconds. */
    __suseconds_t tv_usec; /* Microseconds. */
};

struct timespec
{
    __time_t tv_sec;        /* Seconds. */
    long int tv_nsec;       /* Nanoseconds. */
};
struct tm
{
  int tm_sec;            /* Seconds.    [0-60] (1 leap second) */
  int tm_min;            /* Minutes.    [0-59] */
  int tm_hour;           /* Hours.    [0-23] */
  int tm_mday;           /* Day.        [1-31] */
  int tm_mon;            /* Month.    [0-11] */
  int tm_year;           /* Year    - 1900. */
  int tm_wday;           /* Day of week.    [0-6] */
  int tm_yday;           /* Days in year.[0-365]    */
  int tm_isdst;          /* DST.        [-1/0/1]*/

#ifdef    __USE_BSD
  long int tm_gmtoff;        /* Seconds east of UTC. */
  __const char *tm_zone;     /* Timezone abbreviation. */
#else
  long int __tm_gmtoff;       /* Seconds east of UTC. */
  __const char *__tm_zone;    /* Timezone abbreviation. */
#endif
};

其中:
time_t 是一个长整型,用来表示秒数。
struct timeval 结构体是用秒和微妙来表示时间。
struct timespec 结构体是用秒和纳秒来表示时间。
struct tm 直接用秒、分、小时、天、月、年等来表示时间。
很显然它们的精度是各不相同的。位各种不同的需要提供各种不同的选择。

  • (2)在Linux系统中我们如何获取当前的时间?
#include
time_t time(time_t *t);

可以获取精确到秒的当前距离1970-01-01 00:00:00 +0000 (UTC)的秒数。

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

可以获取精确到微秒当前距离1970-01-01 00:00:00 +0000 (UTC)的微秒数。

#include
int clock_gettime(clockid_t clk_id, struct timespec *tp);

可以获取精确到纳秒当前距离1970-01-01 00:00:00 +0000 (UTC)的纳秒数。
例子:

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

int main()
{
        struct timespec ts;

        clock_gettime(CLOCK_REALTIME, &ts);
        printf("%.24s %ld Nanoseconds\n", ctime(&ts.tv_sec), ts.tv_nsec);

        return 0;
}

编译:gcc -Wall -lrt -o mytime time.c (注意要加入链接选项 -lrt )
运行:./mytime
结果:Tue Sep 13 18:04:44 2011 740953975 Nanoseconds
总结:我们可以通过上面三个函数获得三种不同精度的当前时间。
注:POSIX.1-2008 marks gettimeofday() as obsolete, recommending the use of clock_gettime(2) instead.
并且,有人曾经做过测试,连续两次使用gettimeofday时,会以一种小概率出现”时光倒流”的现象,第二次函数调用得到的时间要小于或说早于第一次调用得到的时间。gettimeofday函数并不是那么稳定,没有times或clock计时准确,但它们用法相似。clock有计时限制,据说是 596.5+小时,一般情况足以应付。”(摘录自网上)
“ntpd之类的进程可能会修改系统时间,导致计时出现误差。据网上的讨论来看,TSC和HPET中断之类的东西,可能导致系统的wall time回退。这个应该和具体的系统实现有关了,总之gettimeofday函数并没有保证提供怎样的精度,也不保证得到系统的准确时间,它返回的结果是”the system’s best guess at wall time”(摘录自网上)
有可能的话,尽量使用clock_gettime(CLOCK_MONOTONIC),不过不是所有系统都实现了posix realtime,例如macosx”(摘录自网上)所以现在应该用:int clock_gettime(CLOCK_MONOTONIC, struct timespec *tp);
CLOCK_MONOTONIC:Clock that cannot be set and represents monotonic time since some unspecified starting point.

  • (3) 秒、毫秒、微秒、纳秒之间的转换
    1秒 == 1000毫秒
    1毫秒 == 1000微秒
    1微秒 == 1000纳秒
    so:
    1秒 == 1000,000微秒(一百万微秒)
    1秒 == 1000,000,000纳秒(十亿纳秒)
    从秒到毫秒,毫秒到微秒,微秒到纳秒都是1000的倍关系,也就是多3个0的关系。
    另:个人电脑的微处理器执行一道指令(如将两数相加)约需2至4纳秒。所以程序只要精确到纳秒就够了。
  • (4)我们如何对时间进行格式化和输出呢?
#include <time.h>

char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

sizt_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

很显然,要严格控制时间的输出格式,只能用strftime函数,但是该函数只能对struct tm表示的时间进行输出,所以这里要涉及到struct timeval, struct timespec, time_t等表示的时间如何转换成struct tm的形式。另外因为strutc tm只能精确到秒,所i毫秒、微秒、纳秒只能另外进行输出了。
所以,我们采取的方式是:
首先将struct timeval, struct timespec等转换成time_t表示的秒数

struct timeval tv;
struct timespec ts;
time_t sec_tv = tv.tv_sec;
time_t sec_ts = ts.ts_sec;

然后利用下列函数将time_t转换成struct tm,

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
或者:
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);

最后利用strftime函数进行格式化,得到最后的时间字符串。
至于毫秒、微秒、纳秒另外用进行输出。
例子

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

int main()
{
        struct timeval tv;
        char strTime[32];

        gettimeofday(&tv, NULL);
        struct tm *ptm = gmtime(&tv.tv_sec);  //将秒转换成struct tm的形式
        strftime(strTime, 32, "%F %T", ptm);
        printf("%s ", strTime); //输出精确到秒
        printf("%ld Micorseconds\n", (long)tv.tv_usec); //输出微秒

        return 0;
}

输出结果:2011-09-14 03:22:42 427880 Micorseconds
另:
形如gmtime和形如gmtime_t函数的区别是,gmtime获得的返回的结果存在于一个static的struct tm型的变量中,可能被后面的gmtime调用覆盖掉,如果要防止覆盖,我们可以自己提供一个struct tm 型的变量,利用gmtime_r函数,将我们自己定义的变量的地址传进去,将结果保存在其中。这样就可以避免覆盖。

关于ctime和asctime等函数得到的时间字符串,它具有指定的形如(”Wed Jun 30 21:49:08 1993\n”)的格式,所以不利与我们不能进行格式化。注意该格式的最后具有换行符:’\n’.

linux c获得时间和设置时间

转自重点内容http://blog.csdn.net/heanyu/article/details/6552578

#include<time.h> //C语言的头文件   
#include<stdio.h> //C语言的I/O    

void main()   
{   
time_t now; //实例化time_t结构    
struct tm *timenow; //实例化tm结构指针    
time(&now);   
//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now    

timenow = localtime(&now);   
//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)    

printf("Local time is %s/n",asctime(timenow));   
//上句中asctime函数把时间转换成字符,通过printf()函数输出    
}   

注释:time_t是一个在time.h中定义好的结构体。而tm结构体的原形如下:

struct tm   
{   
int tm_sec;//seconds 0-61    
int tm_min;//minutes 1-59    
int tm_hour;//hours 0-23    
int tm_mday;//day of the month 1-31    
int tm_mon;//months since jan 0-11    
int tm_year;//years from 1900    
int tm_wday;//days since Sunday, 0-6    
int tm_yday;//days since Jan 1, 0-365    
int tm_isdst;//Daylight Saving time indicator    
};  

include “time.h”

time_t time(time_t *timer);
调用后将当前系统时间与1900年1月1日相差的秒数存入到timer中,timer可看成是一个长整型数
struct tm* localtime(const time_t *timer)
将time()函数调用的结果做为参数传入到localtime()函数中就能得到当前时间和日期,注意得到的年是和1970的差值,月份是和1月的差值

struct tm是一个结构体,定义如下

struct tm   
{   
int tm_sec; //当前秒    
int tm_min; //当前分钟    
int tm_hour; //当前小时    
int tm_mday; //当前在本月中的天,如11月1日,则为1    
int tm_mon; //当前月,范围是0~11    
int tm_year; //当前年和1900的差值,如2006年则为36    
int tm_wday; //当前在本星期中的天,范围0~6    
int tm_yday; //当前在本年中的天,范围0~365    
int tm_isdst; //这个我也不清楚    
}   

求当前时间的示例

int getSystemTime()   
{   
time_t timer;   
struct tm* t_tm;   
time(&timer);   
t_tm = localtime(&timer);   
printf("today is %4d%02d%02d%02d%02d%02d/n", t_tm.tm_year+1900,   
t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec);   
return 0;   
}   

/************************************************ 
设置操作系统时间 
参数:*dt数据格式为"2006-4-20 20:30:30" 
调用方法: 
    char *pt="2006-4-20 20:30:30"; 
    SetSystemTime(pt); 
**************************************************/  
int SetSystemTime(char *dt)  
{  
    struct rtc_time tm;  
    struct tm _tm;  
    struct timeval tv;  
    time_t timep;  
    sscanf(dt, "%d-%d-%d %d:%d:%d", &tm.tm_year,  
        &tm.tm_mon, &tm.tm_mday,&tm.tm_hour,  
        &tm.tm_min, &tm.tm_sec);  
    _tm.tm_sec = tm.tm_sec;  
    _tm.tm_min = tm.tm_min;  
    _tm.tm_hour = tm.tm_hour;  
    _tm.tm_mday = tm.tm_mday;  
    _tm.tm_mon = tm.tm_mon - 1;  
    _tm.tm_year = tm.tm_year - 1900;  

    timep = mktime(&_tm);  
    tv.tv_sec = timep;  
    tv.tv_usec = 0;  
    if(settimeofday (&tv, (struct timezone *) 0) < 0)  
    {  
    printf("Set system datatime error!/n");  
    return -1;  
    }  
    return 0;  
}  

其他时间的函数和结构还有:

timeval结构

#include <include/linux/time.h>

struct timeval
{
time_t tv_sec;
susecond_t tv_usec; //当前妙内的微妙数
};

tms结构
保存着一个进程及其子进程使用的cpu时间

struct tms
{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}

timer_struct结构

#include <include/linux/timer.h>

struct timer_struct
{
unsigned long expires; //定时器被激活的时刻
void (*fn)(void); //定时器激活后的处理函数
}

utime函数
更改文件的存取和修改时间

int utime(const char pathname, const struct utimbuf *times) // return value 0 or -1

times 为空指针,存取和修改时间设置为当前时间

struct utimbuf
{
time_t actime;
time_t modtime;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值