mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-11-01 00:12:30 +01:00
51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Converters;
|
|
|
|
namespace NzbDrone.Core.Helpers.Converters
|
|
{
|
|
public class EpochDateTimeConverter : DateTimeConverterBase
|
|
{
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
{
|
|
long ticks;
|
|
if (value is DateTime)
|
|
{
|
|
var epoch = new DateTime(1970, 1, 1);
|
|
var delta = ((DateTime)value) - epoch;
|
|
if (delta.TotalSeconds < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("value",
|
|
"Unix epoc starts January 1st, 1970");
|
|
}
|
|
ticks = (long)delta.TotalSeconds;
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Expected date object value.");
|
|
}
|
|
writer.WriteValue(ticks);
|
|
}
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
{
|
|
if (reader.TokenType != JsonToken.Integer)
|
|
{
|
|
throw new Exception(
|
|
String.Format("Unexpected token parsing date. Expected Integer, got {0}.",
|
|
reader.TokenType));
|
|
}
|
|
|
|
var ticks = (long)reader.Value;
|
|
|
|
var date = new DateTime(1970, 1, 1);
|
|
date = date.AddSeconds(ticks);
|
|
|
|
return date;
|
|
}
|
|
}
|
|
}
|