爲了便於傳輸,通訊過程當中傳輸的都是:當前時間跟標準時間相隔的秒數,而且是以16進制字節的形式傳輸的。spa
1 public double ConvertDateTimeInt(System.DateTime time)//將時間格式的數據類型轉換成浮點數類型 2 { 3 double intResult = 0; 4 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); 5 intResult =(time - startTime).TotalSeconds; 6 return intResult; 7 } 8 9 public DateTime ConvertIntDatetime(double utc) 10 { 11 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); 12 startTime = startTime.AddSeconds(utc); 13 startTime = startTime.AddHours(8);//轉化爲北京時間(北京時間=UTC時間+8小時 ) 14 return startTime; 15 }
1 public byte[] ConvertDoubleByte(double d) 2 { 3 byte[] result = new byte[4]; 4 float f=(float)d; 5 result = BitConverter.GetBytes(f); 6 return result; 7 } 8 9 public double ConvertByteDouble(byte [] d) 10 { 11 double result=0; 12 float q = BitConverter.ToSingle(d, 0); 13 result = (double)q; 14 return result; 15 }
1 private void button1_Click(object sender, EventArgs e)//這種只是顯示的形式不一樣,未達到目的 2 { 3 DateTime utc_tem = DateTime.UtcNow; 4 5 double utc = ConvertDateTimeInt(utc_tem);//相對應的秒數! 6 byte[] s = new byte[4]; 7 s = ConvertDoubleByte(utc); 8 9 10 DateTime mytime = ConvertIntDatetime(1429647491.3936753); 11 byte[] b = new byte[4] { 0x80,0x6d,0xaa,0x4e}; 12 mytime = ConvertIntDatetime(ConvertByteDouble(b)); 13 }