NTP time Conversion

Source: Internet
Author: User
Tags unpack


NTP time conversion, Baidu has long been no results. I have tried it for myself.

The time the packet is sent on the device is NTP format, and the raw data in the Wireshark is:

DA E5 D0 E5


1, through the struct.unpack, into the integral type:

S4=struct.unpack (' 4 B ', buf[0][102:106])

The results are as follows: 218 229 208 229


2. Each integer is converted to a 16 string, and the result is as follows:

T1=str (Hex (s4[0))

temp= ' 0xda0xe50xd00xe5 '

by Temp=temp.replace (' 0x ', '), delete ' 0x ', the result is as follows:

' Dae5d0e5 '


3, conversion to 10 data,

Timestamp = Int (temp,16)

Results: 3672494309


4, through the function of datetime, converted to datetime

Datearray = Datetime.datetime.utcfromtimestamp (timestamp)


5, the above time, the year also need to subtract (NTP time from 1900/1/1 00:00:00 start counting, and UTC time starting from 1970)

Con_time =datetime.datetime (Datearray-70,datearray.month,datearray.day,

Datearray.hour,datearray.minute,datearray.second)

Last available equipment time: 2016.5.17 17:18:29


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.