天天看点

linux 下设置时间函数 1 settimeofday2stime

 1 settimeofday

int set_time(struct tm * p_tm)

{

    struct timeval tv;

    struct timezone tz;

    gettimeofday(&tv, &tz);

    tv.tv_sec = mktime(p_tm);

    settimeofday(&tv, &tz);

    return 0;

}

int set_time_t(time_t timestamp)

{

    struct timeval tv;

    struct timezone tz;

    gettimeofday(&tv, &tz);

    tv.tv_sec = timestamp;

    settimeofday(&tv, &tz);

    return 0;

}

2stime

stime(time_t*)

time(NULL)

gmtime()     格林威治(GMT)标准时间

localtime()    本地时间

gmtime() localtime()  这两个函数都是把time_t* 转化为struct tm * 类型,但是一个转换为格林威治(GMT)标准时间,一个转换为本地时间。

mktime()刚好与上面2个相反,是把struct tm*转换为 time_t*类型,

我经常使用上面函数进行时区转换。

struct tm* utc2local(struct tm* p_src, uint8 time_zone,  char a)

{   

    time_t  time32; 

    time32=mktime(p_src);

    if(('E'==a)||('e'==a))

        time32+=time_zone*60*60;    //东8区

    if(('W'==a)||('w'==a))

        time32-=time_zone*60*60;    //东8区

    return localtime(&time32);

}

继续阅读