Skip to content

Instantly share code, notes, and snippets.

@yuka1984
Created March 4, 2016 11:46
Show Gist options
  • Select an option

  • Save yuka1984/b99b776c28feaf779f69 to your computer and use it in GitHub Desktop.

Select an option

Save yuka1984/b99b776c28feaf779f69 to your computer and use it in GitHub Desktop.
日本標準時刻を取得する ref: http://qiita.com/yu_ka1984/items/42bd268a6adbe74e5459
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