Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
39 changes: 35 additions & 4 deletions src/KubernetesClient.Models/KubernetesJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,49 @@ public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializer

private sealed class KubernetesDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
{
private const string SerializeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK";
private const string Iso8601Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK";
private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffK";

private static string FormatDateTimeOffsetToSevenDigitsNanoseconds(string dateTime)
{
var isUTC = dateTime.EndsWith("Z");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too complex
why not just trim extra input?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As already pointed out by @anekdoti in #1350 the lastTransitionTimestamp should be deserializable as a Golang Time struct that follows RFC3339 where an arbitrary number of nansecond digits from 0 to 9 is valid. Therefore trimming alone would not help here.
This method allows an arbitrary number of nanosecond digits from 0 to 9.

var dateTimeWithoutZ = isUTC ? dateTime.Substring(0, dateTime.Length - 1) : dateTime;

var nanoSecondsDelimiterIndex = dateTimeWithoutZ.LastIndexOf(".", StringComparison.Ordinal);
var withoutNanoseconds = nanoSecondsDelimiterIndex > -1 ? dateTimeWithoutZ.Substring(0, nanoSecondsDelimiterIndex) : dateTimeWithoutZ;

var sevenDigitNanoseconds = "0000000";
if (nanoSecondsDelimiterIndex > -1)
{
var nanoSecondsAsString = dateTimeWithoutZ.Substring(nanoSecondsDelimiterIndex + 1);

if (nanoSecondsAsString.Length > 9)
{
throw new ArgumentException("Invalid format for nanoseconds, too many digits.");
}

var leadingZeroes = nanoSecondsAsString.TakeWhile(c => c == '0').Count();
var nanoSecondsWithoutLeadingZeroesAsString = nanoSecondsAsString.Substring(leadingZeroes);
sevenDigitNanoseconds = nanoSecondsAsString.Length > 7
? nanoSecondsAsString.Substring(0, 7)
: new string('0', leadingZeroes)
+ (string.IsNullOrEmpty(nanoSecondsWithoutLeadingZeroesAsString)
? new string('0', 7 - leadingZeroes)
: int.Parse(nanoSecondsWithoutLeadingZeroesAsString)
* (int)Math.Pow(10, 7 - leadingZeroes - nanoSecondsWithoutLeadingZeroesAsString.Length));
}

return withoutNanoseconds + "." + sevenDigitNanoseconds + (isUTC ? "Z" : "");
}

public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var str = reader.GetString();
return DateTimeOffset.ParseExact(str, new[] { Iso8601Format, SerializeFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None);
return DateTimeOffset.ParseExact(FormatDateTimeOffsetToSevenDigitsNanoseconds(str), new[] { RFC3339NanoFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None);
}

public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(SerializeFormat));
writer.WriteStringValue(value.ToString(RFC3339NanoFormat));
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/KubernetesClient.Tests/KubernetesJsonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Xunit;

namespace k8s.Tests;

public class KubernetesJsonTests
{
private class RfcTime
{
public DateTime rfc3339 { get; set; }
}

[Fact]
public void RFC3339()
{
var json = "{\"rfc3339\":\"2009-11-10T23:00:00Z\","
+ "\"rfc3339\":\"2009-11-10T23:00:00.000000Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.000000000Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.123456789Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.12345678Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.1234567Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.123456Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.12345Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.1234Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.123Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.12Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.1Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.01Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.001Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.0001Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.00001Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.000001Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.0000001Z\","
+ "\"rfc3339\":\"2009-11-10T13:00:00.0000001\"}\r\n";

KubernetesJson.Deserialize<RfcTime>(json);
}
}