linux时间时区常用时间格式|Linux时间时区、常用时间函数、整形时间计算思路详解

更新时间:2020-03-12    来源:php函数    手机版     字体:

【www.bbyears.com--php函数】

Linux时间时区详解与常用时间函数

时间与时区

整个地球分为二十四时区,每个时区都有自己的本地时间。

Ø  UTC时间 与 GMT时间

我们可以认为格林威治时间就是时间协调时间(GMT = UTC),格林威治时间和UTC时间都用秒数来计算的。

Ø  UTC时间与本地时间

UTC + 时区差 = 本地时间

时区差东为正,西为负。在此,把东八区时区差记为 +0800

UTC + (+0800) = 本地(北京)时间

Ø  UTC与Unix时间戳

在计算机中看到的UTC时间都是从(1970年01月01日 0:00:00)开始计算秒数的。所看到的UTC时间那就是从1970年这个时间点起到具体时间共有多少秒。 这个秒数就是Unix时间戳。


time(取得目前的时间)

函数说明:

#include

time_t time(time_t *t);

此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。

返回:成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。

代码说明:


#include
#include
#include
#include

int main(int argc, char** argv)
{
    int seconds = time(NULL);
    printf("%d\n", seconds);
    return 0;
}



执行结果:

[root@VM_174_171_centos unixtime]# g++ -g -o unixtime_time unixtime_time.cpp

[root@VM_174_171_centos unixtime]# ./unixtime_time

1445008165


gmtime(取得目前时间和日期)

函数说明:

#include

struct tm*gmtime(const time_t*timep);

gmtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为:
struct tm
{
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。

返回:结构tm代表目前UTC 时间

 

代码说明:


#include
#include
#include
#include

int main(int argc, char** argv)
{
    const char* wday[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    time_t timep;
    struct tm* p;
    time(&timep);
    p = gmtime(&timep);
    printf("curday = %d-%d-%d\n", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
    printf("curweek = %s, curtime = %d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
    return 0;
}



结果说明:

[root@VM_174_171_centos unixtime]# g++ -g -o unixtime_gmtime unixtime_gmtime.cpp

[root@VM_174_171_centos unixtime]# ./unixtime_gmtime

curday = 2015-10-16

curweek = Fri, curtime = 15:12:12

[root@VM_174_171_centos unixtime]# date -u          

Fri Oct 16 15:12:13 UTC 2015

[root@VM_174_171_centos unixtime]# date

Fri Oct 16 23:12:16 CST 2015

[root@VM_174_171_centos unixtime]# date -R     #这里打印出时区信息,北京为东八区

Fri, 16 Oct 2015 23:12:18 +0800

可以看到gmtime返回的时间日期未经过时区转换,这里和date打印的刚好差8小时(中国时区)。


ctime(将时间和日期以字符串格式表示)

函数说明:

#include

char *ctime(const time_t *timep);

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

代码说明:


#include
#include
#include
#include

int main(int argc, char** argv)
{
    time_t timep;
    time(&timep);
    printf("%s",ctime(&timep));
    return 0;
}



结果说明:

[root@VM_174_171_centos unixtime]# g++ -g -o unixtime_ctime unixtime_ctime.cpp

[root@VM_174_171_centos unixtime]# ./unixtime_ctime

Fri Oct 16 23:14:33 2015

[root@VM_174_171_centos unixtime]# date

Fri Oct 16 23:14:34 CST 2015


asctime(将时间和日期以字符串格式表示)

函数说明:

#include

char * asctime(const struct tm * timeptr);

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

代码说明:


#include
#include
#include
#include

int main(int argc, char** argv)
{
    time_t timep;
    time(&timep);
    printf("%s", asctime(gmtime(&timep)));
}



结果说明:
[root@VM_174_171_centos unixtime]# g++ -g -o unixtime_asctime unixtime_asctime.cpp

[root@VM_174_171_centos unixtime]# ./unixtime_asctime

Fri Oct 16 15:15:54 2015

[root@VM_174_171_centos unixtime]# date

Fri Oct 16 23:15:55 CST 2015

[root@VM_174_171_centos unixtime]# date -u

Fri Oct 16 15:15:57 UTC 2015

[root@VM_174_171_centos unixtime]# date -R

Fri, 16 Oct 2015 23:16:01 +0800

注意这里struct tm结构的时间是通过gmtime返回的,因此也没有经过时区转换。


gettimeofday(取得目前的时间)

函数说明:

#include
#include

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

gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
struct timeval {
    long tv_sec; /*秒*/
    long tv_usec; /*微秒*/
};
timezone结构定义为:
struct timezone {
    int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
    int tz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h,tz_dsttime 所代表的状态如下
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/

返回:成功则返回0,失败返回-1,错误代码存于errno。

代码说明:


#include
#include
#include
#include

int main(int argc, char** argv)
{
    struct timeval tv;
    struct timezone tz;
    gettimeofday(&tv, &tz);
    printf("tv_sec = %d, tv_usec = %d, tz_minuteswest = %d, tz_dsttime = %d\n",
            tv.tv_sec, tv.tv_usec, tz.tz_minuteswest, tz.tz_dsttime) ;
    
    return 0;
}



结果说明:

[root@VM_174_centos unixtime]# g++ -g -o unixtime_gettimeofday unixtime_gettimeofday.cpp

[root@VM_174_centos unixtime]# ./unixtime_gettimeofday

tv_sec = 1445008619, tv_usec = 699804, tz_minuteswest = -480, tz_dsttime = 0

[root@VM_174_171_centos unixtime]# date

Fri Oct 16 23:17:00 CST 2015

[root@VM_174_171_centos unixtime]# date -u

Fri Oct 16 15:17:02 UTC 2015

这里时区差是-480,也就是说明GMT比我们(中国时区)晚8小时。


localtime(取得当地目前时间和日期)

函数说明:

#include

struct tm *localtime(const time_t * timep);

localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。

代码说明:


#include
#include
#include
#include

int main(int argc, char** argv)
{
    const char* wday[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    time_t timep;
    struct tm* p;
    time(&timep);
    p = localtime(&timep);
    printf("curday = %d-%d-%d\n", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
    printf("curweek = %s, curtime = %d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
    return 0;
}



结果说明:

[root@VM_174_171_centos unixtime]# g++ -g -o unixtime_localtime unixtime_localtime.cpp

[root@VM_174_171_centos unixtime]# ./unixtime_localtime

curday = 2015-10-16

curweek = Fri, curtime = 23:23:37

[root@VM_174_171_centos unixtime]# ./unixtime_gmtime   

curday = 2015-10-16

curweek = Fri, curtime = 15:23:37

这里的结果跟gmtime的结果进行比较,可以看出,gmtime给出的是GMT标准时间,localtime给出的是根据时区转换过的本地时间(这里是北京时间,东八区,+0800)。


mktime(将时间结构数据转换成经过的秒数)

函数说明:

time_t mktime(strcut tm * timeptr);

mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0秒算起至今的UTC时间所经过的秒数。

返回:返回经过的秒数。

代码说明:


/*
 * 用time()取得时间(秒数),利用localtime()
 * 转换成struct tm 再利用mktine()将struct tm转换成原来的秒数
 */
#include
#include
#include
#include

int main(int argc, char** argv)
{
    time_t timep;
    struct tm* p;
    time(&timep);
    printf("time() = %d\n", timep);
    p = localtime(&timep);
    timep = mktime(p);
    printf("time()->localtime()->mktime():%d\n", timep);
    return 0;
}



结果说明:

[root@VM_174_171_centos unixtime]# g++ -g -o unixtime_mktime unixtime_mktime.cpp

[root@VM_174_171_centos unixtime]# ./unixtime_mktime

time() = 1445010682

time()->localtime()->mktime():1445010682
settimeofday(设置目前时间)

函数说明:

#include
#include

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

settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。

返回:成功则返回0,失败返回-1,错误代码存于errno。

EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。



基于Linux整形时间的常用计算思路

本文基于Linux整形时间给出一些简化的的常用计算思路,试图从另外的角度去加强读者对时间处理的理解,希望对您有所帮助。


概述

在后台server 的开发中,经常需要基于日期、时间的比较、计算。类似的功能需求可能有:判断今天是星期几,判断两个时间是否在同一天,是否在同一周,判断当前时间是否在每日的特定时段内等等。虽然有系统函数localtime()可以很好的获取日期相关的详细信息,但由于其获取的信息足够详细,以至于在某些特定的简单功能上,使用localtime()实际上是有多余的开销。对于一些简单的判断,我们推荐采用更简单、更原始、更易于理解的方式来实现。


计算思路

在Unix/Linux下,系统时间以time_t类型表示,本质上是一个整形数值,数值含义为从历史上的一个基准点开始(格林威治时间1970年1月1日零点),至当前时刻持续的秒数。在Linux下,time_t被定义long类型,即有符号整型。

考虑到中国与格林威治的时区不同,对中国来说,时间的基准起始点是1970年1月1日早八点整。对于任意时区,time_t的表示规则可以由下图表示。
clip_image002

如上,T0 = 0,表示起始时间;T1为即T0以后,第一天的零点时间;T2则表示第二天的零点时间;可以看出,对于不同时区,表示规律上的区别只是T1取值不同。从T1时刻开始,T1,T2,T3...,Tn是一个等差序列,公差为一天的时间秒数,记为D = 86400(60*60*24)。

对于任意一个时间,可以表示成:

t = T1 + k × D + m                   …. 公式1

其中T1是一个时区相关的常量,m为本天之内的秒数,k可以理解为历史上的天数

经过变形可得出 k =(t - T1 - m) / D

由于m < D 可进一步简化:

k = (t - T1) / D                                       …. 公式2

k为t时刻所在当天,自T0开始的天数。

对于时刻t,其所在当天零点的时间:

tz  = T1 +(t - T1) / D  × D                     …. 公式3

tz为 t时刻所在当天零点时间。

 

基于公式2我们可以判断任意两个时刻t1,t2是否是同一天,基于公式3我们可以求出时刻t1在所在当天所处的时段。基于这两个公式我们还可以扩展更多的相关于天的日期计算,而很容易看出,公式所使用的计算仅仅为整数数值运算而已。


对于星期的计算,我们可以仿造上面的思路。所不同的只有T1的取值为第一个星期的起始时间,如周一的早上零点时刻;D的取值为一周的秒数604800(86400*7)。

通过任意时刻t,我们可以求出其所在当前的零点时间,可以求出所在星期的开始时间,再通过简单的比较,也很容易实现计算出当天星期几等一些相关的扩展,在此不再一一赘述。


常用函数实现

//获取tNow时间的当天零点时间值,零点作为一天的第一秒

time_t GetTodayZeroTime(time_t tNow)

{

    return ( ( (tNow - 57600)/86400 )*86400 + 57600 );

}

 

//判断两个时间是否在同一天, 一天的概念为00:00:00到23:59:59

bool IsInSameDay(time_t tTm1, time_t tTm2)

{

    return ( (tTm1 - 57600) / 86400 == (tTm2 - 57600) / 86400 );

}

 

//获取tNow时间所在这一周的开始时间,即这周周一的0点0分0秒

//计算思路,1980-01-07是周一,这一天0点的整形时间为316022400(按中国时区)

time_t GetWeekBeginTime(time_t tNow)

{

    return ( (tNow - 316022400) / 604800 * 604800 + 316022400 );

}

 

//获取tNow时间所在这一周的结束时间,即这周周日的23点59分59秒

time_t GetWeekEndTime(time_t tNow)

{

    return ( (tNow - 316022400) / 604800 * 604800 + 316627199 );    //316022400 + 604800 - 1 );

}

 

//判断两个时间是否在同一周, 一周的概念为周一的00:00:00到周日的23:59:59

bool IsInSameWeek(time_t tTm1, time_t tTm2)

{

    return ( (tTm1 - 316022400) / 604800 == (tTm2 - 316022400) / 604800 );

}

代码讲解


#include
#include
#include
#include

time_t GetTodayZeroTime(time_t tNow)
{
    return ( ( (tNow - 57600)/86400 )*86400 + 57600 );
}

bool IsInSameDay(time_t tTm1, time_t tTm2)
{
    return ( (tTm1 - 57600) / 86400 == (tTm2 - 57600) / 86400 );
}

bool IsInSameWeek(time_t tTm1, time_t tTm2)
{
    return ( (tTm1 - 316022400) / 604800 == (tTm2 - 316022400) / 604800 );
}

time_t GetWeekBeginTime(time_t tNow)
{
    return ( (tNow - 316022400) / 604800 * 604800 + 316022400 );
}

time_t GetWeekEndTime(time_t tNow)
{
    return ( (tNow - 316022400) / 604800 * 604800 + 316627199 );    //316022400 + 604800 - 1 );
}

int main(int argc, char** argv)
{
    time_t currtime, one_hour_after, one_day_after, one_week_after;
    time(&currtime);
    one_hour_after = currtime + 3600; // 1小时之后
    one_day_after = currtime + 86400; // 1天之后
    one_week_after = currtime + 604800; // 1周之后

    printf("Today zero time ==> %d\n", GetTodayZeroTime(currtime));
    printf("Week begin time ==> %d\n", GetWeekBeginTime(currtime));
    printf("Week end time ==> %d\n", GetWeekEndTime(currtime));
    printf("Is in same day ==> (currtime|one_hour_after = %d), (currtime|one_day_after = %d)\n",
            IsInSameDay(currtime, one_hour_after), IsInSameDay(currtime, one_day_after));
    printf("Is in same week ==> (currtime|one_week_after = %d), (one_day_after|one_week_after = %d)\n",
            IsInSameWeek(currtime, one_week_after), IsInSameWeek(one_day_after, one_week_after));        

    return 0;
}



结果说明

[root@VM_174_171_centos unixtime]# g++ -g -o unixtime_simplify unixtime_simplify.cpp

[root@VM_174_171_centos unixtime]# ./unixtime_simplify

Today zero time ==> 1445097600

Week begin time ==> 1444579200

Week end time ==> 1445183999

Is in same day ==> (currtime|one_hour_after = 1), (currtime|one_day_after = 0)

Is in same week ==> (currtime|one_week_after = 0), (one_day_after|one_week_after = 1)

[root@VM_174_171_centos unixtime]# date

Sun Oct 18 13:17:37 CST 2015

[root@VM_174_171_centos unixtime]# date -d @1445097600

Sun Oct 18 00:00:00 CST 2015

[root@VM_174_171_centos unixtime]# date -d @1444579200

Mon Oct 12 00:00:00 CST 2015

[root@VM_174_171_centos unixtime]# date -d @1445183999

Sun Oct 18 23:59:59 CST 2015

应用举例

在一些活动、任务逻辑中,常常会需要一个类似自然日内统计的数值,过了一天则数值清零。

对于这种需求,我们通常是以 [数值,更新时间] 来表示,在访问时刻进行时间比较,超过时效则清零。以按自然日清零规则来举例,即是在GetValue(), AddValue()时,判断数值的上次更新时间t_upd, 如果IsInSameDay(t_upd, t_now)则当前数值依然有效,否则清零数值后再进行相关操作。每次修改数值时都将t_upd更新成当前时刻。

国际化考虑

对于不同时区,公式的区别仅仅在于T1的取值,公式的形式和使用并不需要变化。

一种方式是将T1定义成宏,在国际化时对不同时区的版本,使用不同的T1数值。

另一种方式是将T1定义成全局变量,并在server启动时使用系统的localtime()函数,将T1按当地时区进行合适的初始化取值。


不适用于年、月的规则

由于每年的天数、每个月的天数不是固定不变的,所以本文的计算思路不适用于每月几号这样的时间点的判断,基于以往的经验,特定月份特定日期的功能需求并不是很普遍,对于这些功能还是使用localtime()函数来的方便一些。

本文来源:http://www.bbyears.com/jiaocheng/86525.html