ptp与ntp时间戳专题讲解
来源:互联网 发布:java poi读取excel文件 编辑:程序博客网 时间:2024/06/05 15:22
在二次开发ntp和ptp模块的时候,会有将系统时间转换为ptp时间或者ntp时间,一般初学者会比较迷糊,现将其总结如下
1 Linux时间结构
获取linux系统时间一般有两个函数,获取us级的时间函数和获取ns级的时间函数。一般linux系统时间整数秒从1900年0时0分0秒开始计时,在ptp中启示也是这个值,但是在ntp整数秒计数起时是1970年0时0分0秒,所以在将系统时间转换为ntp时间整数秒的时候,会补偿1970到1900时间差0x83aa7e80,细节见下ntp讲解。
函数gettimeofday(struct timeval *tv, struct timezone *tz)
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
函数int clock_gettime(clockid_t clk_id, struct timespec *tp);
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
2 PTP时间结构
ptp时间分为整数秒和小数秒,整数秒为32位数据,小数秒为32位数据,但在PTP时间填充syn,delay等数据包的时候,是Timestamp数据结构,整数秒的最高位多出16为,一般填写0,
typedef struct {
uint16_t msb;
uint32_t lsb;
} UInteger48;
typedef struct {
Integer32 seconds;
Integer32 nanoseconds;
}TimeInternal;
typedef struct {
UInteger48 secondsField;
UInteger32 nanosecondsField;
}Timestamp;
从linux获取时间,可以直接将整数秒和小数秒赋值给ptp时间结构变量,不需要任何转换
2.1 时延偏差计算
2.1.1 E2E 模式下时延和偏差计算
Ø MeanDelay ( (t4-t3)+ (t2-t1) - syncorrection -delayrespcorrection) /2
Ø TimeOffset T2-T1 - delaymeanPath - synCorrection
2.1.2 P2P模式下时延和偏差计算
Ø MeanDelay ( t6-t3 - pdelayrespCorrection) /2
Ø TimeOffset T2-T1 - delaymeanPath – synCorrection
3 NTP时间结构
typedef struct {
union {
u_int32 Xl_ui;
int32 Xl_i;
} Ul_i;
union {
u_int32 Xl_uf;
int32 Xl_f;
} Ul_f;
} l_fp;
#define l_ui Ul_i.Xl_ui /* unsigned integral part */
#define l_i Ul_i.Xl_i /* signed integral part */
#define l_uf Ul_f.Xl_uf /* unsigned fractional part */
#define l_f Ul_f.Xl_f /* signed fractional part */
Ntp时间结构体的定义比较特殊,官方定义采用两个联合体定义方式,但其实原理是一样的,整数秒为32位数,小数秒也是32位数,只是该结构体定义方式多了有符号和无符号的方式。
Linux系统时间转换为ntp时间的时候,整数秒需要补偿1900-1970年的整数秒,小数秒需要一定的转换。转换方式见下:小数秒需要先乘以1e-9,赋值给double数据,然后乘以“FRAC”,得到小数秒;
#define JAN_1970 0x83aa7e80 /* 2208988800 1970 - 1900 in seconds */
#define FRAC 4294967296. /* 2^32 as a double */
3.1 时延偏差计算
MeanDelay di = ( (t2 - t1)-(t3 - t4) )
Timeoffset ci= ((t2 - t1) + (t3 - t4))/2
Over!!
Thanks!
Author: Felix
- ptp与ntp时间戳专题讲解
- NTP与PTP
- 与服务器同步时间NTP
- NTP和RTP时间戳
- Linux的时间设置与同步 (NTP)
- Linux时间设置与同步(NTP)
- Linux的时间设置与同步(NTP)
- Linux时间设置与NTP服务配置
- Linux的时间设置与同步(NTP)
- Linux的时间设置与同步(NTP)
- Linux的时间设置与同步(NTP)
- Linux的时间设置与同步(NTP)
- Linux的时间设置与同步(NTP)
- Linux的时间设置与同步(NTP)
- 主机与ntp时间同步脚本
- Linux的时间设置与同步(NTP)
- 精确时间协议 (Precision Time Protocol, PTP)
- linux配置ntp讲解
- c语言 timer
- JavaScript 对象
- iOS滑动tableView来改变导航栏的颜色
- SIGPIPE信号--解决Linux socket中接收端退出导致发送端send退出问题
- OCP-1Z0-051-2015-13题
- ptp与ntp时间戳专题讲解
- HTML初步
- linux环境变量设置方法总结(PATH/LD_LIBRARY_PATH)
- express 与 http 的区别
- QQ协议分析(一)
- RMAN主要命令 show,list,crosscheck,delete详解
- U盘做为系统盘安装系统,出现start booting from usb device和boot failed解决方案
- Android小技巧(3)
- Android开发之属于你的短信验证码(二)