Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
throw new JsonSerializationException(string.Format("Unexpected token parsing date. Expected Integer or String, got {0}.", reader.TokenType));
}

if (seconds >= 0)
// The maximum allowed value of seconds allowed for DateTime.AddSeconds(double d). If this value is converted (seconds since Unix Epoch Time), it is equivalent to December 31, 9999 23:59:59 UTC.
const long maxSecondsForDateTime = 253402300799;

// Determine if this value is milliseconds after Unix Epoch
if (seconds > maxSecondsForDateTime)
{
// Modify this value to convert it from milliseconds (since the Unix Epoch) to be seconds, by truncating the milliseconds.
seconds /= 1000;
return DateTimeUtils.UnixEpoch.AddSeconds(seconds);
}
else if (seconds >= 0)
{
return DateTimeUtils.UnixEpoch.AddSeconds(seconds);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace StripeTests.Infrastructure.JsonConverters
{
using System;
using Newtonsoft.Json;
using Stripe;
using Stripe.Infrastructure;
using Xunit;

public class UnixDateTimeConverterTest
{
[Fact]
public void Deserialize_UnixEpochSeconds()
{
var json = @"{ ""created-date"": 1625097600 }"; // Represents July 1, 2021
var obj = JsonConvert.DeserializeObject<TestObject>(json);
Assert.Equal(new DateTime(2021, 7, 1, 0, 0, 0, DateTimeKind.Utc), obj.CreatedDate);
}

[Fact]
public void Deserialize_UnixEpochMilliSeconds()
{
var json = @"{ ""created-date"": 1625097600000 }"; // Represents July 1, 2021
var obj = JsonConvert.DeserializeObject<TestObject>(json);
Assert.Equal(new DateTime(2021, 7, 1, 0, 0, 0, DateTimeKind.Utc), obj.CreatedDate);
}

private class TestObject : StripeEntity<TestObject>
{
[JsonProperty("created-date")]
[JsonConverter(typeof(UnixDateTimeConverter))]
internal DateTime CreatedDate { get; set; }
}
}
}
Loading