-
Notifications
You must be signed in to change notification settings - Fork 32
Open
Description
The properties StartedOn
and UpdatedOn
are currently converted into the wrong format. From CouchDB docs:
started_on (number) – Task start time as unix timestamp
updated_on (number) – Unix timestamp of last operation update
Current

Fixed

The fix is simple; I added a new converter:
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace CouchDB.Driver.Helpers;
public class UnixTimestampSecondsConverter : DateTimeConverterBase
{
private static readonly DateTime Epoch =
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is DateTime dt)
{
var seconds = (long)(dt.ToUniversalTime() - Epoch).TotalSeconds;
writer.WriteValue(seconds);
}
else
{
throw new JsonSerializationException("Expected DateTime object.");
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
switch (reader.Value)
{
case null:
return null!;
case long seconds:
case string str when long.TryParse(str, out seconds):
return Epoch.AddSeconds(seconds);
default:
throw new JsonSerializationException("Expected integer Unix timestamp in seconds.");
}
}
}
Currently testing it. Can PR soon too ofc. 😉
Metadata
Metadata
Assignees
Labels
No labels