1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-29 03:15:19 +00:00
Radarr/NzbDrone.Core/Helpers/Converters/EpochDateTimeConverter.cs
2012-12-21 00:35:20 -08:00

50 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;
}
}
}