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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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位十进制,NTP_TO_SECOND表示十进制中的1代表多少秒就是把1秒切割成32bit的数。
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 32bitone 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