Skip to content

Commit

Permalink
Implemented two edge cases to support of up to nine digit nanoseconds…
Browse files Browse the repository at this point in the history
… format. Added unit tests.
  • Loading branch information
ndallmeyer committed Aug 15, 2023
1 parent cc4315e commit 4a9ef92
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/KubernetesClient.Models/KubernetesJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ private static string FormatDateTimeOffsetToSevenDigitsNanoseconds(string dateTi
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)
+ (int.Parse(nanoSecondsWithoutLeadingZeroesAsString)
* (int)Math.Pow(10, 7 - leadingZeroes - nanoSecondsWithoutLeadingZeroesAsString.Length));
+ (string.IsNullOrEmpty(nanoSecondsWithoutLeadingZeroesAsString)
? new string('0', 7 - leadingZeroes)
: int.Parse(nanoSecondsWithoutLeadingZeroesAsString)
* (int)Math.Pow(10, 7 - leadingZeroes - nanoSecondsWithoutLeadingZeroesAsString.Length));
}

return withoutNanoseconds + "." + sevenDigitNanoseconds + (isUTC ? "Z" : "");
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

Check failure on line 8 in tests/KubernetesClient.Tests/KubernetesJsonTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'KubernetesJsonTests.RfcTime' is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it 'static' (Module in Visual Basic).

Check failure on line 8 in tests/KubernetesClient.Tests/KubernetesJsonTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'KubernetesJsonTests.RfcTime' is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it 'static' (Module in Visual Basic).

Check failure on line 8 in tests/KubernetesClient.Tests/KubernetesJsonTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'KubernetesJsonTests.RfcTime' is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it 'static' (Module in Visual Basic).

Check failure on line 8 in tests/KubernetesClient.Tests/KubernetesJsonTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'KubernetesJsonTests.RfcTime' is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it 'static' (Module in Visual Basic).
{
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);
}
}

0 comments on commit 4a9ef92

Please sign in to comment.