#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 32bit，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 
