Skip to content

Commit 4a9ef92

Browse files
committed
Implemented two edge cases to support of up to nine digit nanoseconds format. Added unit tests.
1 parent cc4315e commit 4a9ef92

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/KubernetesClient.Models/KubernetesJson.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,21 @@ private static string FormatDateTimeOffsetToSevenDigitsNanoseconds(string dateTi
4040
if (nanoSecondsDelimiterIndex > -1)
4141
{
4242
var nanoSecondsAsString = dateTimeWithoutZ.Substring(nanoSecondsDelimiterIndex + 1);
43+
44+
if (nanoSecondsAsString.Length > 9)
45+
{
46+
throw new ArgumentException("Invalid format for nanoseconds, too many digits.");
47+
}
48+
4349
var leadingZeroes = nanoSecondsAsString.TakeWhile(c => c == '0').Count();
4450
var nanoSecondsWithoutLeadingZeroesAsString = nanoSecondsAsString.Substring(leadingZeroes);
4551
sevenDigitNanoseconds = nanoSecondsAsString.Length > 7
4652
? nanoSecondsAsString.Substring(0, 7)
4753
: new string('0', leadingZeroes)
48-
+ (int.Parse(nanoSecondsWithoutLeadingZeroesAsString)
49-
* (int)Math.Pow(10, 7 - leadingZeroes - nanoSecondsWithoutLeadingZeroesAsString.Length));
54+
+ (string.IsNullOrEmpty(nanoSecondsWithoutLeadingZeroesAsString)
55+
? new string('0', 7 - leadingZeroes)
56+
: int.Parse(nanoSecondsWithoutLeadingZeroesAsString)
57+
* (int)Math.Pow(10, 7 - leadingZeroes - nanoSecondsWithoutLeadingZeroesAsString.Length));
5058
}
5159

5260
return withoutNanoseconds + "." + sevenDigitNanoseconds + (isUTC ? "Z" : "");
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace k8s.Tests;
5+
6+
public class KubernetesJsonTests
7+
{
8+
private class RfcTime
9+
{
10+
public DateTime rfc3339 { get; set; }
11+
}
12+
13+
[Fact]
14+
public void RFC3339()
15+
{
16+
var json = "{\"rfc3339\":\"2009-11-10T23:00:00Z\","
17+
+ "\"rfc3339\":\"2009-11-10T23:00:00.000000Z\","
18+
+ "\"rfc3339\":\"2009-11-10T13:00:00.000000000Z\","
19+
+ "\"rfc3339\":\"2009-11-10T13:00:00.123456789Z\","
20+
+ "\"rfc3339\":\"2009-11-10T13:00:00.12345678Z\","
21+
+ "\"rfc3339\":\"2009-11-10T13:00:00.1234567Z\","
22+
+ "\"rfc3339\":\"2009-11-10T13:00:00.123456Z\","
23+
+ "\"rfc3339\":\"2009-11-10T13:00:00.12345Z\","
24+
+ "\"rfc3339\":\"2009-11-10T13:00:00.1234Z\","
25+
+ "\"rfc3339\":\"2009-11-10T13:00:00.123Z\","
26+
+ "\"rfc3339\":\"2009-11-10T13:00:00.12Z\","
27+
+ "\"rfc3339\":\"2009-11-10T13:00:00.1Z\","
28+
+ "\"rfc3339\":\"2009-11-10T13:00:00.01Z\","
29+
+ "\"rfc3339\":\"2009-11-10T13:00:00.001Z\","
30+
+ "\"rfc3339\":\"2009-11-10T13:00:00.0001Z\","
31+
+ "\"rfc3339\":\"2009-11-10T13:00:00.00001Z\","
32+
+ "\"rfc3339\":\"2009-11-10T13:00:00.000001Z\","
33+
+ "\"rfc3339\":\"2009-11-10T13:00:00.0000001Z\","
34+
+ "\"rfc3339\":\"2009-11-10T13:00:00.0000001\"}\r\n";
35+
36+
KubernetesJson.Deserialize<RfcTime>(json);
37+
}
38+
}

0 commit comments

Comments
 (0)