📄 ntptime.cpp
字号:
Seconds = (Seconds * 60) + st.wMinute;
Seconds = (Seconds * 60) + st.wSecond;
// ASSERT(Seconds <= 0xFFFFFFFF); //NTP Only supports up to 2036
m_Time = (Seconds << 32) + MsToNtpFraction(st.wMilliseconds);
}
//#endif
long CNtpTime::GetJulianDay(unsigned short Year, unsigned short Month, unsigned short Day)
{
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;
}
long c = y / 100;
long ya = y - 100 * c;
long j = (146097L * c) / 4 + (1461L * ya) / 4 + (153L * m + 2) / 5 + d + 1721119L;
return j;
}
void CNtpTime::GetGregorianDate(long JD, unsigned short& Year, unsigned short& Month, unsigned short& Day)
{
long j = JD - 1721119;
long y = (4 * j - 1) / 146097;
j = 4 * j - 1 - 146097 * y;
long d = j / 4;
j = (4 * d + 3) / 1461;
d = 4 * d + 3 - 1461 * j;
d = (d + 4) / 4;
long 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;
}
Year = (unsigned short) y;
Month = (unsigned short) m;
Day = (unsigned short) d;
}
CNtpTime& CNtpTime::operator=(const CNtpTime& time)
{
m_Time = time.m_Time;
return *this;
}
double CNtpTime::operator-(const CNtpTime& time) const
{
if (m_Time >= time.m_Time)
{
CNtpTime diff;
diff.m_Time = m_Time - time.m_Time;
return diff.Seconds() + NtpFractionToSecond(diff.Fraction());
}
else
{
CNtpTime diff;
diff.m_Time = time.m_Time - m_Time;
return -(diff.Seconds() + NtpFractionToSecond(diff.Fraction()));
}
}
CNtpTime CNtpTime::operator+(const double& timespan) const
{
CNtpTime rVal;
rVal.m_Time = m_Time;
if (timespan >= 0)
{
// unsigned __int64 diff = ((unsigned __int64) timespan) << 32;
//unsigned long long diff = ((unsigned __int64) timespan) << 32;
unsigned long long diff = ((unsigned long long) timespan) << 32;
double intpart;
double frac = modf(timespan, &intpart);
// diff += (unsigned __int64) (frac * 0xFFFFFFFF);
diff += (unsigned long long) (frac * 0xFFFFFFFF);
rVal.m_Time += diff;
}
else
{
double d = -timespan;
//unsigned __int64 diff = ((unsigned __int64) d) << 32;
unsigned long long diff = ((unsigned long long) d) << 32;
double intpart;
double frac = modf(d, &intpart);
// diff += (unsigned __int64) (frac * 0xFFFFFFFF);
diff += (unsigned long long) (frac * 0xFFFFFFFF);
rVal.m_Time -= diff;
}
return rVal;
}
//#ifdef __Win32__
CNtpTime::operator SYSTEMTIME() const
{
//Currently this function only operates correctly in
//the 1900 - 2036 primary epoch defined by NTP
SYSTEMTIME st;
unsigned long s = Seconds();
st.wSecond = (unsigned short)(s % 60);
s /= 60;
st.wMinute = (unsigned short)(s % 60);
s /= 60;
st.wHour = (unsigned short)(s % 24);
s /= 24;
long JD = s + JAN_1ST_1900;
st.wDayOfWeek = (unsigned short)((JD + 1) % 7);
GetGregorianDate(JD, st.wYear, st.wMonth, st.wDay);
st.wMilliseconds = NtpFractionToMs(Fraction());
return st;
}
//#endif
unsigned long CNtpTime::Seconds() const
{
UInt64 t1 = 0xFFFFFFFF;
t1 = t1 << 32;
return (unsigned long) ((m_Time & t1) >> 32);
}
unsigned long CNtpTime::Fraction() const
{
return (unsigned long) (m_Time & 0xFFFFFFFF);
}
CNtpTime::operator CNtpTimePacket() const
{
CNtpTimePacket ntp;
ntp.m_dwInteger = htonl(Seconds());
ntp.m_dwFractional = htonl(Fraction());
return ntp;
}
CNtpTime CNtpTime::GetCurrentTime()
{
SYSTEMTIME st;
GetSystemTime(&st);
CNtpTime t(st);
// CNtpTime t;
return t;
}
unsigned long CNtpTime::MsToNtpFraction(unsigned short wMilliSeconds)
{
// ASSERT(wMilliSeconds < 1000);
return m_MsToNTP[wMilliSeconds];
}
unsigned short CNtpTime::NtpFractionToMs(unsigned long dwFraction)
{
return (unsigned short)((((double)dwFraction) * NTP_FRACTIONAL_TO_MS) + 0.5);
}
double CNtpTime::NtpFractionToSecond(unsigned long dwFraction)
{
double d = (double)dwFraction;
d *= NTP_TO_SECOND;
return ((double)dwFraction) * NTP_TO_SECOND;
}
#ifdef __REDHATLINUX__
/// 提供GMT时间
Bool8 GetSystemTime(SYSTEMTIME *st)
{
struct timeval tp;
struct timezone tzp;
int mIsError = gettimeofday(&tp,&tzp);
struct tm mLocalTime = *gmtime(&tp.tv_sec);
st->wYear = mLocalTime.tm_year + 1900;
st->wMonth = mLocalTime.tm_mon + 1;
st->wDayOfWeek = mLocalTime.tm_wday;
st->wDay = mLocalTime.tm_mday;
st->wHour = mLocalTime.tm_hour;
st->wMinute = mLocalTime.tm_min;
st->wSecond = mLocalTime.tm_sec;
st->wMilliseconds = tp.tv_usec/1000;
return true;
}
Bool8 SetSystemTime(SYSTEMTIME st)
{
struct timeval tp;
struct timezone tzp;
int mIsError = gettimeofday(&tp,&tzp);
struct tm utcTm;
struct tm tTm1;
tTm1 = *localtime(&tp.tv_sec);
utcTm.tm_year = st.wYear - 1900;
utcTm.tm_mon = st.wMonth - 1;
utcTm.tm_wday = st.wDayOfWeek ;
utcTm.tm_mday = st.wDay ;
utcTm.tm_hour = st.wHour;
utcTm.tm_min = st.wMinute;
utcTm.tm_sec = st.wSecond;
utcTm.tm_isdst = 0;
tp.tv_usec = st.wMilliseconds * 1000;
tp.tv_sec = mktime(&utcTm);
//struct tm test1 = *localtime(&tp.tv_sec);
//printf("修改系统时间为,当前UTC时间是%04d-%02d-%02d %02d:%02d:%02d:%03d \n",
// test1.tm_year, test1.tm_mon, test1.tm_mday, test1.tm_hour, test1.tm_min, test1.tm_sec,tp.tv_usec);
tp.tv_sec = tp.tv_sec - tzp.tz_minuteswest * 60;
//struct tm test2 = * localtime(&tp.tv_sec);
//printf("修改系统时间为,本地时间是%04d-%02d-%02d %02d:%02d:%02d:%03d \n",
// test2.tm_year, test2.tm_mon, test2.tm_mday, test2.tm_hour, test2.tm_min, test2.tm_sec,tp.tv_usec);
mIsError = settimeofday(&tp,&tzp);
return true;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -