The general format of the time stamp 10 that is divided into two (sec) 13 timestamp (in milliseconds) time stamp
Timestamp is also divided into two types, ie the local time stamp and world unity (UTC) timestamps
Ado, directly on the code:
First, the time stamp obtaining method
/// <Summary> /// Get timestamp /// </ Summary> /// <param name = "timeKind"> Time Type (only to the Local, Utc) </ param> /// <param name = "format"> stamp format (as only 10,13) </ param> /// <Returns> </ Returns> Private Double getTimestamp ( int the format, the DateTimeKind timeKind) { the timeSpan timeSpan = new new the timeSpan (); Switch (timeKind) { Case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new new the DateTime ( 1970 , . 1 ,1, 0, 0, 0, timeKind); break; case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break; default: throw new Exception("时间类型 只能为 Local、Utc"); } switch (format) { case 10: return timeSpan.TotalSeconds; Case 13 is : return timeSpan.TotalMilliseconds; default : the throw new new Exception ( " timestamp format is only 10,13 " ); } } /// <Summary> /// Get timestamp 10 /// </ Summary> /// <param name = "timeKind"> time type (only for the Local, Utc, default the Local) </ param> /// <Returns> </ Returns> public int Get10Timestamp (the DateTimeKind timeKind = DateTimeKind.Local) { return Convert.ToInt32 (getTimestamp ( 10 , timeKind)); } /// <Summary> /// Get timestamp 13 /// </ Summary> /// <param name = "timeKind"> Time Type (only for the Local, Utc, default the Local) </ param> / // <Returns> </ Returns> public Long Get13Timestamp (the DateTimeKind timeKind = DateTimeKind.Local) { return Convert.ToInt64 (getTimestamp ( 13 is , timeKind)); }
Second, the time stamp verification method
/// <Summary> /// validation stamp (10, 13 Jieke) /// </ Summary> /// <param name = "timestamp"> timestamp </ param> /// <param name = "timeDiff"> allow time difference (time 10 seconds, when 13 milliseconds) </ param> /// <param name = "timeKind"> time type (only for the Local, Utc, default Local ) </ param> /// <Returns> </ Returns> public BOOL ValidateTimestamp ( Double timestamp, int the timeDiff, the DateTimeKind timeKind = DateTimeKind.Local) { the timeSpan timeSpan = new new the timeSpan (); switch (timeKind) { case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break; case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break; default: throw new Exception("时间类型 只能为 Local、Utc"); } Double nowTimestamp = 0 ; // current timestamp int the format timestamp.ToString = ( " F0 " ) .Length; Switch (the format) { Case 10 : nowTimestamp = timeSpan.TotalSeconds; BREAK ; Case 13 is : nowTimestamp = timeSpan.TotalMilliseconds ; BREAK ; default : the throw new new Exception ( " timestamp format error " ); } Double nowTimeDiff = nowTimestamp - timestamp; // current difference if (-timeDiff <= nowTimeDiff && nowTimeDiff <= timeDiff) return true; else return false; }
Third, the method of converting the timestamp DateTime
/// <Summary> /// timestamp installed for the DateTime (10 bits Jieke 13) /// </ Summary> /// <param name = "timestamp"> timestamp </ param> / // <param name = "timeKind"> time type (only for the Local, Utc, default the Local) </ param> /// <param name = "toTimeKind"> time return type (only for the Local, Utc, consistent with the default timeKind) </ param> /// <Returns> </ Returns> public the DateTime TimestampToDateTime ( Double timestamp, the DateTimeKind timeKind = DateTimeKind.Local, the DateTimeKind toTimeKind = DateTimeKind.Unspecified) { the DateTime the startTime; toTimeKind = timeKind; switch (timeKind) { case DateTimeKind.Utc: startTime = new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break; case DateTimeKind.Local: startTime = new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break; default: throw new Exception("时间类型 只能为 Local、Utc"); } DateTime newTime; int format = timestamp.ToString("f0").Length; switch (format) { case 10: newTime = startTime.AddSeconds(timestamp); break; case 13: newTime = startTime.AddMilliseconds(timestamp); break; default: throw new Exception("时间戳格式 错误"); } if (newTime.Kind != toTimeKind) newTime = toTimeKind == DateTimeKind.Local ? newTime.ToLocalTime() : newTime.ToUniversalTime(); return newTime; }