ntp

package module
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

NTP协议实现 example下的main是ntp客户端,支持window、linux下的自动同步时钟

Documentation

Index

Constants

View Source
const (
	UNIX_STA_TIMESTAMP = 2208988800
)

Variables

This section is empty.

Functions

func Calc

func Calc(res []byte, reqTs, resTs int64) (d int64, t int64)

根据NTP响应及收发时戳计算

func GetT2T3Unix

func GetT2T3Unix(ntpRes []byte) (int64, int64)

从ntp响应报文中提取服务器回复时戳TransmitTimestamp

func NewNtpV3Req

func NewNtpV3Req() []byte

创建一个ver3的ntp请求报文

func NtpTimestamp2Unix

func NtpTimestamp2Unix(ts uint64) int64

NTP时戳转为UNIX时戳

func ParseTransmitTimestamp

func ParseTransmitTimestamp(ntpRes []byte) time.Time

从ntp响应报文中提取服务器回复时戳TransmitTimestamp

func SimpleNtpQuery

func SimpleNtpQuery(ntp_server_addr string) (now time.Time, cast time.Duration, err error)

ntp快速查询

func TimeOfNtpServer

func TimeOfNtpServer(ntp_server_addr string) (now time.Time, cast time.Duration, err error)

获取NTP服务器时戳及往返耗时

func UnixTimestamp2Ntp

func UnixTimestamp2Ntp(uts int64) uint64

UNIX时戳转为NTP时戳

Types

type Ntp

type Ntp struct {
	Li                  uint8  //2 bits 闰秒标志
	Vn                  uint8  //3 bits NTP版本号,3或4
	Mode                uint8  //3 bits 3-客户端请求,4-服务器响应
	Stratum             uint8  //服务器时钟的层级,1最高
	Poll                uint8  //
	Precision           uint8  //服务器的时钟精度
	RootDelay           int32  //服务器与主时钟源的最大往返通讯时延/秒
	RootDispersion      int32  //服务器相对主时钟源的最大误差/秒
	ReferenceIdentifier int32  //主时钟源标识
	ReferenceTimestamp  uint64 //服务器时钟最后一次校准的时间
	OriginateTimestamp  uint64 //客户向服务器发起请求的时间
	ReceiveTimestamp    uint64 //服务器收到客户请求的时间
	TransmitTimestamp   uint64 //服务器向客户发时间戳的时间
}

* NTP协议 http://www.ntp.org/documentation.html NTP报文-报文头总长度48字节

func NewNtp

func NewNtp() (p *Ntp)

func (*Ntp) Decode

func (this *Ntp) Decode(buf []byte, useUnixSec bool)

解析服务器回复的ntp报文bf useUnixSec指示是否将服务器回复的ntp时戳转换为unix时戳

func (*Ntp) Encode

func (this *Ntp) Encode() []byte

* 转换为NTP协议报文

func (*Ntp) ToString

func (this *Ntp) ToString() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL