41. エポック時間で解決
41
public static int ToEpoch(this DateTime date)
{
if (date == null) return int.MinValue;
DateTime epoch = new DateTime(1970, 1, 1);
TimeSpan epochTimeSpan = date - epoch;
return (int)epochTimeSpan.TotalSeconds;
}
DateTimeからエポック時間を得られる拡張メソッドを用意
int epoch = DateTime.Now.ToEpoch();
42. エポック時間で解決
42
public class DatetimeEpoch
{
public DateTime Date { get; set; }
public int Epoch
{
get
{
return (this.Date.Equals(null) ||
this.Date.Equals(DateTime.MinValue)) ? int.MinValue :
this.Date.ToEpoch();
}
}
}
DateTimeとEpochを保持するクラスを用意
43. エポック時間で解決
43
public class Item
{
[JsonProperty(PropertyName="id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "description")]
public string Description { get; set; }
[JsonProperty(PropertyName = "isComplete")]
public bool Completed { get; set; }
[JsonProperty(PropertyName = "createdDatetimeEpoch")]
public DatetimeEpoch CreatedDatetimeEpoch { get; set; }
}
JSONのモデルクラスにDatetimeEpochクラスのプロパティを指定