RTKlib中关于结构体gtime_t
的一些记录:
该结构体的定义:
typedef struct { /* time struct */
time_t time; /* time (s) expressed by standard time_t */
double sec; /* fraction of second under 1 s */
} gtime_t;
其实该结构体储存的是自1970年1月1日0时0分0秒至目标时间的秒数time
及不足整秒sec
,结构体time_t
为一个64位的整型。下面给出一个例子:
int main()
{
double ep_[6];
ep_[0] = 2019; ep_[1] = 2; ep_[2] = 27;
ep_[3] = 11; ep_[4] = 7; ep_[5] = 15.23;
double *ep;
ep = &ep_[0];
gtime_t t;
t = epoch2time(ep);
printf("%d \t", t.time);
printf("%f \n", t.sec);
system("pause");
return 0;
}
输出结果:

另外,还需记录一下epoch2time
函数,定义如下:
extern gtime_t epoch2time(const double *ep)
{
/* 平年每月的第一天的年积日*/
const int doy[]={1,32,60,91,121,152,182,213,244,274,305,335};
gtime_t time={0};
int days,sec,year=(int)ep[0],mon=(int)ep[1],day=(int)ep[2];
if (year<1970||2099<year||mon<1||12<mon) return time;
/* leap year if year%4==0 in 1901-2099 */
days=(year-1970)*365+(year-1969)/4+doy[mon-1]+day-2+(year%4==0&&mon>=3?1:0);
sec=(int)floor(ep[5]);
time.time=(time_t)days*86400+(int)ep[3]*3600+(int)ep[4]*60+sec;
time.sec=ep[5]-sec;
return time;
}
着重说明一下天数的求解,
days=(year-1970)*365+(year-1969)/4+doy[mon-1]+day-2+(year%4==0&&mon>=3?1:0);
先举一个例子,例如我们要求2019年1月2日0时0分10秒到2019年1月1日0时0分0秒的秒数,1月2日的年积日为2,但是我们求解时肯定是1月2日之前那一天的年积日换算成秒数,然后加上1月2日经过的秒数。
那么我们求解某一时刻A至起点所经过的秒数,所要求解的年积日就是A前一天的年积日。所以对于doy[mon-1]+day-2
就比较容易理解。例如我们要知道2.11至起点的秒数,那我们要求的年积日是2.10的,doy(2-1)+11-2=41。
对于(year%4==0&&mon>=3?1:0)
,当这一年为闰年且月份大于3时,得出的结果等于1,即闰年2月之后的某一天要加一天。
对于(year-1970)*365+(year-1969)/4
,当求解1972年(闰年)的时间时,(year%4==0&&mon>=3?1:0)
起到加一天的作用,当求解1973年的时间时,(year-1970)*365
将1972年当成365天计算了,(year-1969)/4
补充了这一天。对于其他年份也是如此。值得注意的是,time.time=(time_t)days*86400+(int)ep[3]*3600+(int)ep[4]*60+sec;
将days
进行了取整。