Created
March 4, 2016 11:46
-
-
Save yuka1984/b99b776c28feaf779f69 to your computer and use it in GitHub Desktop.
日本標準時刻を取得する ref: http://qiita.com/yu_ka1984/items/42bd268a6adbe74e5459
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TimeService | |
| { | |
| /// <summary> | |
| // 現在時刻を取得します。 | |
| /// </summary> | |
| /// <returns></returns> | |
| public async Task<DateTime> GetNow() | |
| { | |
| try | |
| { | |
| HttpClient client = new HttpClient(); | |
| client.Timeout = TimeSpan.FromSeconds(3); | |
| var result = await client.GetAsync("https://ntp-a1.nict.go.jp/cgi-bin/json"); | |
| if (result.IsSuccessStatusCode) | |
| { | |
| var time = JsonConvert.DeserializeObject<NitcTime>(await result.Content.ReadAsStringAsync()); | |
| var now = | |
| new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Convert.ToDouble(time.st)) | |
| .ToLocalTime(); | |
| return now; | |
| } | |
| } | |
| catch | |
| { | |
| } | |
| return DateTime.Now; | |
| } | |
| public class NitcTime | |
| { | |
| public string id { get; set; } | |
| public decimal it { get; set; } | |
| public decimal st { get; set; } | |
| public int leap { get; set; } | |
| public decimal next { get; set; } | |
| public int step { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment