You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.0 KiB
C

1 year ago
#ifndef _NTP_TIME
#define _NTP_TIME
#if (defined(_WIN32) || defined(_MSC_VER))
#include <windows.h>
#endif
#include <time.h>
#include <assert.h>
#include "datatype.h"
#ifndef _WIN32
typedef struct _SYSTEMTIME {
unsigned short wYear;
unsigned short wMonth;
unsigned short wDayOfWeek;
unsigned short wDay;
unsigned short wHour;
unsigned short wMinute;
unsigned short wSecond;
unsigned short wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
#endif
static double NTP_FRACTIONAL_TO_MS = (((double)1000.0)/0xFFFFFFFF); //230ms->10λʮ<CEBB><CAAE><EFBFBD><EFBFBD>,NTP_TO_SECOND<4E><44>ʾʮ<CABE><CAAE><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBA3AC><EFBFBD>ǰ<EFBFBD>1<EFBFBD><31><EFBFBD>и<EFBFBD><D0B8><EFBFBD>32bit<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static double NTP_TO_SECOND = (((double)1.0)/0xFFFFFFFF);
static long JAN_1ST_1900 = 2415021;
//Representation of an NTP timestamp
typedef struct
{
ulong m_dwInteger;
ulong m_dwFractional; // 2 packets of 32bit<69><74>one is integer part,the other is frantional part
}NTPTIMEPACKET;
//General functions
SYSTEMTIME NtpTimePacket2SystemTime(NTPTIMEPACKET pNtpTime);
double NtpTimePacket2Double(NTPTIMEPACKET pNtpTime);
NTPTIMEPACKET Double2CNtpTimePacket(double f);
NTPTIMEPACKET Systemtime2CNtpTimePacket(SYSTEMTIME st);
NTPTIMEPACKET MyGetCurrentTime();
NTPTIMEPACKET NewTime(double nOffset);
ulong Ms2NtpFraction(u16_t wMilliSeconds);
u16_t NtpFraction2Ms(ulong dwFraction);
double NtpFraction2Second(ulong dwFraction);
long GetJulianDay(u16_t Year, u16_t Month, u16_t Day);
NTPTIMEPACKET host2network(NTPTIMEPACKET ntp);
NTPTIMEPACKET network2host(NTPTIMEPACKET packet);
//////////////////////////////////////////////////////////////////////////
int send_sntp_packet(char* pszBuf, int nBuf);
int is_readible(int *bReadible);
int rcv_sntp_packet(char* pszBuf, int nBuf);
void close_socket();
int set_client_time(NTPTIMEPACKET * NewTime);
//////////////////////////////////////////////////////////////////////////
void set_last_error(unsigned long dwerror);
long get_last_error();
#endif