Skip to content

Commit

Permalink
fix: 修正 offset 的計算公式
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew0928 committed Jan 5, 2017
1 parent 1fb1656 commit 0ce6e92
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions WebServerClock/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ private void button1_Click(object sender, EventArgs e)
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(this.textWebSiteURL.Text);

HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Head, "/");
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Head, "/");

DateTime start = DateTime.Now;
HttpResponseMessage rsp = client.SendAsync(hrm, HttpCompletionOption.ResponseHeadersRead).Result;
TimeSpan deviation = DateTime.Now - start;
DateTime t0 = DateTime.Now;
HttpResponseMessage rsp = client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead).Result;
DateTime t3 = DateTime.Now;
TimeSpan duration = t3 - t0;

DateTime t1p = DateTime.Parse(rsp.Headers.GetValues("Date").First());
this.Offset = t1p - t0.AddMilliseconds(duration.TotalMilliseconds / 2);

this.Offset = DateTime.Parse(rsp.Headers.GetValues("Date").First()) - DateTime.Now;
this.labelOffset.Text = string.Format(
@"時間差: {0} msec, 最大誤差值: {1} msec",
this.Offset.TotalMilliseconds,
deviation.TotalMilliseconds);

duration.TotalMilliseconds / 2);
}
}
}

0 comments on commit 0ce6e92

Please sign in to comment.