37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace NX.ERP.Model
|
|
{
|
|
public class TimestampConverter : JsonConverter
|
|
{
|
|
public override bool CanConvert(Type objectType)
|
|
{
|
|
return objectType == typeof(DateTime);
|
|
}
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
{
|
|
return ConvertIntDateTime(int.Parse(reader.Value.ToString()));
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
{
|
|
writer.WriteValue(ConvertDateTimeInt((DateTime)value));
|
|
}
|
|
|
|
public static DateTime ConvertIntDateTime(int aSeconds)
|
|
{
|
|
return new DateTime(1970, 1, 1).AddSeconds(aSeconds);
|
|
}
|
|
|
|
public static int ConvertDateTimeInt(DateTime aDT)
|
|
{
|
|
return (int)(aDT - new DateTime(1970, 1, 1)).TotalSeconds;
|
|
}
|
|
}
|
|
}
|