pjlib系列之时间time

Linux下的几种时间

1、精确到秒级

time_t本质上是一个long型

time_t time(time_t *t);

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

struct tm {
   int tm_sec;         /* seconds */
   int tm_min;         /* minutes */
   int tm_hour;        /* hours */
   int tm_mday;        /* day of the month */
   int tm_mon;         /* month */
   int tm_year;        /* year */
   int tm_wday;        /* day of the week */
   int tm_yday;        /* day in the year */
   int tm_isdst;       /* daylight saving time */
};
           
//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 日光节约时间的旗标
    struct tm *localtime(const time_t *timep);
    struct tm *localtime_r(const time_t *timep, struct tm *result);

/*该函数将有time函数获取的值timep转换真实世界所使用的时间日期表示方法,然后将结果由结构tm返回*/
/**需要注意的是localtime函数可以将时间转换本地时间,但是localtime函数不是线程安全的。
多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的**/

time_t mktime(struct tm *tm);       
/**将时间结构体struct tm的值转化为经过的秒数**/

localtime将time_t转换成tm,mktime相反,把tm转换成time_t

2、精确到微妙级

int gettimeofday(struct timeval *tv, 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 */
};

gettimeofday函数获取当前时间(从1970-01-01 00:00:00到现在,精确到微妙)存于tv结构体中,相应的时区信息则存于tz结构体中,需要注意的是tz是依赖于系统,不同的系统可能存在获取不到的可能,因此通常设置为NULL

3、精确到纳秒级

int clock_gettime(clockid_t clk_id, struct timespect *tp);  

struct timespec  
{  
      time_t tv_sec; /* seconds */  
      long tv_nsec; /* nanoseconds */  
};  

    参数说明:  

    clockid_t clk_id 用于指定计时时钟的类型,有以下4种:  

        CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变  

        CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响  

        CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间  

        CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间  

    struct timespect *tp用来存储当前的时间  

pjlib的时间

/**
 * Representation of time value in this library.
 * This type can be used to represent either an interval or a specific time
 * or date. 
 */
typedef struct pj_time_val
{
    /** The seconds part of the time. */
    long    sec;

    /** The miliseconds fraction of the time. */
    long    msec;

} pj_time_val;

void 	pj_time_val_normalize (pj_time_val *t)
#define 	PJ_TIME_VAL_MSEC(t)
#define 	PJ_TIME_VAL_EQ(t1, t2)
#define 	PJ_TIME_VAL_GT(t1, t2)
#define 	PJ_TIME_VAL_GTE(t1, t2)
#define 	PJ_TIME_VAL_LT(t1, t2)
#define 	PJ_TIME_VAL_LTE(t1, t2)
#define 	PJ_TIME_VAL_ADD(t1, t2)
#define 	PJ_TIME_VAL_SUB(t1, t2)

pj_status_t 	pj_gettimeofday (pj_time_val *tv)

pj_time_val跟Linux的timeval基本一致,只不过前者是毫秒级,后者是微妙级,我们程序中一般到毫秒级就够了。PJ_TIME_XX几个宏用来判断两个pj_time_val的大小,加减等。pj_gettimeofday用来获取当前时间,其本质是调用系统的gettimeofday。

pj_time_val和几个宏定义在types.h,pj_gettimeofday定义在os_time_unix.c。

/**
 * This structure represent the parsed representation of time.
 * It is acquired by calling #pj_time_decode().
 */
typedef struct pj_parsed_time
{
    /** This represents day of week where value zero means Sunday */
    int wday;

    /* This represents day of the year, 0-365, where zero means
     *  1st of January.
     */
    /*int yday; */

    /** This represents day of month: 1-31 */
    int day;

    /** This represents month, with the value is 0 - 11 (zero is January) */
    int mon;

    /** This represent the actual year (unlike in ANSI libc where
     *  the value must be added by 1900).
     */
    int year;

    /** This represents the second part, with the value is 0-59 */
    int sec;

    /** This represents the minute part, with the value is: 0-59 */
    int min;

    /** This represents the hour part, with the value is 0-23 */
    int hour;

    /** This represents the milisecond part, with the value is 0-999 */
    int msec;

} pj_parsed_time;

pjlib提供的另一个结构体pj_parsed_time类似tm,并提供了一些转换函数。pj_parsed_time定义在types.h,几个函数定义在os_time_common.c

pj_status_t 	pj_time_decode (const pj_time_val *tv, pj_parsed_time *pt)
pj_status_t 	pj_time_encode (const pj_parsed_time *pt, pj_time_val *tv)
pj_status_t 	pj_time_local_to_gmt (pj_time_val *tv)
pj_status_t 	pj_time_gmt_to_local (pj_time_val *tv)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值