📄 ntptime.c
字号:
#include "NtpTime.h"
#include "datatype.h"
#include <stdio.h>
#include <windows.h>
#define FALSE 0
SYSTEMTIME st;
CNtpTimePacket host2network(CNtpTimePacket ntp) //transform the host byte order to network byte order,before filling the packet
{
ntp.m_dwInteger = htonl(ntp.m_dwInteger);
ntp.m_dwFractional = htonl(ntp.m_dwFractional);
return ntp;
}
CNtpTimePacket network2host(CNtpTimePacket packet) //transform the network byte order to host byte order,after receiving the packet
{
CNtpTimePacket hosttime;
hosttime.m_dwFractional = ntohl(packet.m_dwFractional);
hosttime.m_dwInteger = ntohl(packet.m_dwInteger);
return hosttime;
}
////////////////////////////////////////////////////////////////////////////
CNtpTimePacket Systemtime2CNtpTimePacket(SYSTEMTIME st) //transform year,month,day,h,m,s to 2 packets of 32 bit
{
CNtpTimePacket t;
//Currently this function only operates correctly in
//the 1900 - 2036 primary epoch defined by NTP
long Seconds;
long JD = GetJulianDay(st.wYear, st.wMonth, st.wDay);
JD -= JAN_1ST_1900; //year,month,day form to 32bit data
assert(JD >= 0); //NTP only supports dates greater than 1900
Seconds = JD;
Seconds = (Seconds * 24) + st.wHour;
Seconds = (Seconds * 60) + st.wMinute;
Seconds = (Seconds * 60) + st.wSecond;
assert(Seconds <= 0xFFFFFFFF); //NTP Only supports up to 2036
t.m_dwInteger=Seconds;
t.m_dwFractional=Ms2NtpFraction(st.wMilliseconds);
return t;
}
SYSTEMTIME CNtpTimePacket2Systemtime(CNtpTimePacket pNtpTime)//transform 2 packets of 32 bit to year,month,day
{
//Currently this function only operates correctly in
//the 1900 - 2036 primary epoch defined by NTP
SYSTEMTIME st;
ulong s = pNtpTime.m_dwInteger; //integer part of pNtpTime to s
long JD;
long d;
long m;
long j;
long y;
st.wSecond = (u16_t)(s % 60);
s /= 60;
st.wMinute = (u16_t)(s % 60);
s /= 60;
st.wHour = (u16_t)(s % 24);
s /= 24;
JD = s + JAN_1ST_1900;
st.wDayOfWeek = (u16_t)((JD + 1) % 7);
j = JD - 1721119;
y = (4 * j - 1) / 146097;
j = 4 * j - 1 - 146097 * y;
d = j / 4;
j = (4 * d + 3) / 1461;
d = 4 * d + 3 - 1461 * j;
d = (d + 4) / 4;
m = (5 * d - 3) / 153;
d = 5 * d - 3 - 153 * m;
d = (d + 5) / 5;
y = 100 * y + j;
if (m < 10)
m = m + 3;
else
{
m = m - 9;
y = y + 1;
}
st.wYear = (u16_t) y;
st.wMonth= (u16_t) m;
st.wDay = (u16_t) d;
st.wMilliseconds = NtpFraction2Ms(pNtpTime.m_dwFractional);
return st;
}
long GetJulianDay(u16_t Year, u16_t Month, u16_t Day) //transform year,month,day to 1 integer of 32 bit
{
long c;
long ya;
long j;
long y = (long) Year;
long m = (long) Month;
long d = (long) Day;
if (m > 2)
m = m - 3;
else
{
m = m + 9;
y = y - 1;
}
c = y / 100;
ya = y - 100 * c;
j = (146097L * c) / 4 + (1461L * ya) / 4 + (153L * m + 2) / 5 + d + 1721119L;
return j;
}
double CNtpTimePacket2Double(CNtpTimePacket pNtpTime) //transform 2 packets of 32 bit to double packet Round trip delay and c-s clock offset
{
double t;
t = (pNtpTime.m_dwInteger << 32) +NtpFraction2Second (pNtpTime.m_dwFractional);
return t;
}
CNtpTimePacket Double2CNtpTimePacket(double dt) //transform double packet Round trip delay and c-s clock offset to 2 packets of 32 bit
{
CNtpTimePacket t;
t.m_dwInteger= (ulong) (dt); //force dt to 1 integer of 32 bit,drop the fractional part
NTP_TO_SECOND=(((double)1.0)/0xFFFFFFFF);
t.m_dwFractional=(ulong) (dt /NTP_TO_SECOND);
return t;
}
ulong Ms2NtpFraction(u16_t wMilliSeconds)
{
assert(wMilliSeconds < 1000);
return m_Ms2NTP[wMilliSeconds]; //fractional part is the 32bit data in look up table
}
u16_t NtpFraction2Ms(ulong dwFraction)
{
NTP_FRACTIONAL_TO_MS = (((double)1000.0)/0xFFFFFFFF);
return (u16_t) ((((double) dwFraction) * NTP_FRACTIONAL_TO_MS) + 0.5);
}
double NtpFraction2Second(ulong dwFraction) //fractional part to seconds,transform 32 bit integer to 0.xxxxxx s
{
double d = (double) dwFraction;
NTP_TO_SECOND=(((double)1.0)/0xFFFFFFFF);
d *= NTP_TO_SECOND;
return ((double) dwFraction) * NTP_TO_SECOND;
}
CNtpTimePacket myGetCurrentTime()
{
SYSTEMTIME st;
GetSystemTime(&st);
return Systemtime2CNtpTimePacket(st);
}
int SetClientTime(CNtpTimePacket *NewTime) //system get the new time
{
int bSuccess = FALSE;
SYSTEMTIME st =CNtpTimePacket2Systemtime(*NewTime);
bSuccess = SetSystemTime(&st);
if (!bSuccess)
printf("Failed in call to set the system time\n");
return bSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -