mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-14 10:47:08 -07:00
Backend work for nzbx.co
This commit is contained in:
parent
8581896f58
commit
117edd4286
14 changed files with 488 additions and 2 deletions
50
NzbDrone.Core/Helpers/Converters/EpochDateTimeConverter.cs
Normal file
50
NzbDrone.Core/Helpers/Converters/EpochDateTimeConverter.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue