From 3fbd988470403abd8184839a38a19f5e55e05241 Mon Sep 17 00:00:00 2001 From: Boshi LIAN Date: Thu, 27 Jul 2023 04:26:39 -0700 Subject: [PATCH 1/5] init support for rfc3339 nano --- src/KubernetesClient.Models/KubernetesJson.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/KubernetesClient.Models/KubernetesJson.cs b/src/KubernetesClient.Models/KubernetesJson.cs index 70709c6b7..83ee13b5c 100644 --- a/src/KubernetesClient.Models/KubernetesJson.cs +++ b/src/KubernetesClient.Models/KubernetesJson.cs @@ -26,18 +26,19 @@ public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializer private sealed class KubernetesDateTimeOffsetConverter : JsonConverter { - 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 RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK"; + private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffK"; + private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK"; 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(str, new[] { RFC3339Format, RFC3339MicroFormat, RFC3339NanoFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None); } public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) { - writer.WriteStringValue(value.ToString(SerializeFormat)); + writer.WriteStringValue(value.ToString(RFC3339MicroFormat)); } } From 9ac10a1a00ca78368502dfd42eb14d4bf1dd742e Mon Sep 17 00:00:00 2001 From: Boshi LIAN Date: Thu, 27 Jul 2023 04:28:26 -0700 Subject: [PATCH 2/5] add test cases --- .../KubernetesJsonTests.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/KubernetesClient.Tests/KubernetesJsonTests.cs diff --git a/tests/KubernetesClient.Tests/KubernetesJsonTests.cs b/tests/KubernetesClient.Tests/KubernetesJsonTests.cs new file mode 100644 index 000000000..445cc77e1 --- /dev/null +++ b/tests/KubernetesClient.Tests/KubernetesJsonTests.cs @@ -0,0 +1,48 @@ +using System; +using Xunit; + +namespace k8s.Tests; + +public class KubernetesJsonTests +{ + private class RfcTime + { + public DateTime rfc3339 { get; set; } + public DateTime rfc3339micro { get; set; } + public DateTime Rfc3339nano { get; set; } + } + + + [Fact] + public void RFC3339() + { + /* go code to generate the json https://go.dev/play/p/Owyci2bAKfe + + const RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00" + const RFC3339Nano = "2006-01-02T15:04:05.000000000Z07:00" + + func main() { + t := time.Now() + type Time struct { + RFC3339 string `json:"rfc3339"` + RFC3339Micro string `json:"rfc3339micro"` + RFC3339Nano string `json:"rfc3339nano"` + } + t1 := Time{ + RFC3339: t.Format(time.RFC3339), + RFC3339Micro: t.Format(RFC3339Micro), + RFC3339Nano: t.Format(RFC3339Nano), + } + b, err := json.Marshal(t1) + if err != nil { + fmt.Println("error:", err) + } + fmt.Println(string(b)) + } + */ + + var json = "{\"rfc3339\":\"2009-11-10T23:00:00Z\",\"rfc3339micro\":\"2009-11-10T23:00:00.000000Z\",\"rfc3339nano\":\"2009-11-10T13:00:00.000000000Z\"}\r\n"; + + var t = KubernetesJson.Deserialize(json); + } +} From e1b4a2ed58e31d75be08c35648c18aa0a66604be Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Fri, 25 Aug 2023 11:56:28 -0700 Subject: [PATCH 3/5] impl trimming --- src/KubernetesClient.Models/KubernetesJson.cs | 17 ++++++++++- .../KubernetesJsonTests.cs | 28 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/KubernetesClient.Models/KubernetesJson.cs b/src/KubernetesClient.Models/KubernetesJson.cs index 83ee13b5c..9b57820dc 100644 --- a/src/KubernetesClient.Models/KubernetesJson.cs +++ b/src/KubernetesClient.Models/KubernetesJson.cs @@ -1,6 +1,7 @@ using k8s.Models; using System.Globalization; using System.IO; +using System.Text.RegularExpressions; using System.Xml; namespace k8s @@ -33,7 +34,21 @@ private sealed class KubernetesDateTimeOffsetConverter : JsonConverter(json); + + Assert.Equal(new DateTime(2009, 11, 10, 23, 45, 12, DateTimeKind.Utc), t.rfc3339); + + Assert.Equal(2009, t.rfc3339micro.Year); + Assert.Equal(11, t.rfc3339micro.Month); + Assert.Equal(10, t.rfc3339micro.Day); + Assert.Equal(23, t.rfc3339micro.Hour); + Assert.Equal(45, t.rfc3339micro.Minute); + Assert.Equal(12, t.rfc3339micro.Second); + Assert.Equal(123, t.rfc3339micro.Millisecond); + + Assert.Equal(2009, t.Rfc3339nano.Year); + Assert.Equal(11, t.Rfc3339nano.Month); + Assert.Equal(10, t.Rfc3339nano.Day); + Assert.Equal(13, t.Rfc3339nano.Hour); + Assert.Equal(24, t.Rfc3339nano.Minute); + Assert.Equal(56, t.Rfc3339nano.Second); + Assert.Equal(123, t.Rfc3339nano.Millisecond); + +#if NET7_0_OR_GREATER + Assert.Equal(456, t.rfc3339micro.Microsecond); + Assert.Equal(456, t.Rfc3339nano.Microsecond); + Assert.Equal(700, t.Rfc3339nano.Nanosecond); +#endif } } From 65b52ed490a5f8dbbbe40b7bbdeef5f5566e753c Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Tue, 29 Aug 2023 08:03:52 -0700 Subject: [PATCH 4/5] add 1-8 digits support --- src/KubernetesClient.Models/KubernetesJson.cs | 4 +- .../KubernetesJsonTests.cs | 82 +++++++++++++++---- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/KubernetesClient.Models/KubernetesJson.cs b/src/KubernetesClient.Models/KubernetesJson.cs index 9b57820dc..f978bba29 100644 --- a/src/KubernetesClient.Models/KubernetesJson.cs +++ b/src/KubernetesClient.Models/KubernetesJson.cs @@ -40,9 +40,9 @@ public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConver return result; } - // try RFC3339NanoFormat by trimming 9 digits to 7 digits + // try RFC3339NanoLenient by trimming 1-9 digits to 7 digits var originalstr = str; - str = Regex.Replace(str, @"(?<=\.\d{7})\d{2}", ""); + str = Regex.Replace(str, @"\.\d+", m => (m.Value + "000000000").Substring(0, 7 + 1)); // 7 digits + 1 for the dot if (DateTimeOffset.TryParseExact(str, new[] { RFC3339NanoFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None, out result)) { return result; diff --git a/tests/KubernetesClient.Tests/KubernetesJsonTests.cs b/tests/KubernetesClient.Tests/KubernetesJsonTests.cs index bfd544ae1..c585e0f4c 100644 --- a/tests/KubernetesClient.Tests/KubernetesJsonTests.cs +++ b/tests/KubernetesClient.Tests/KubernetesJsonTests.cs @@ -1,3 +1,4 @@ +using FluentAssertions.Extensions; using System; using Xunit; @@ -8,16 +9,25 @@ public class KubernetesJsonTests [System.Diagnostics.CodeAnalysis.SuppressMessage("Build", "CA1812:'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). (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1812)", Justification = "json type")] private class RfcTime { - public DateTime rfc3339 { get; set; } - public DateTime rfc3339micro { get; set; } + public DateTime Rfc3339 { get; set; } + public DateTime Rfc3339micro { get; set; } public DateTime Rfc3339nano { get; set; } + public DateTime Rfc3339nanolenient1 { get; set; } + public DateTime Rfc3339nanolenient2 { get; set; } + public DateTime Rfc3339nanolenient3 { get; set; } + public DateTime Rfc3339nanolenient4 { get; set; } + public DateTime Rfc3339nanolenient5 { get; set; } + public DateTime Rfc3339nanolenient6 { get; set; } + public DateTime Rfc3339nanolenient7 { get; set; } + public DateTime Rfc3339nanolenient8 { get; set; } + public DateTime Rfc3339nanolenient9 { get; set; } } [Fact] public void RFC3339() { - /* go code to generate the json https://go.dev/play/p/Owyci2bAKfe + /* go code to generate the json https://go.dev/play/p/VL95pugm6o8 const RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00" const RFC3339Nano = "2006-01-02T15:04:05.000000000Z07:00" @@ -28,11 +38,31 @@ type Time struct { RFC3339 string `json:"rfc3339"` RFC3339Micro string `json:"rfc3339micro"` RFC3339Nano string `json:"rfc3339nano"` + + RFC3339NanoLenient1 string `json:"rfc3339nanolenient1"` + RFC3339NanoLenient2 string `json:"rfc3339nanolenient2"` + RFC3339NanoLenient3 string `json:"rfc3339nanolenient3"` + RFC3339NanoLenient4 string `json:"rfc3339nanolenient4"` + RFC3339NanoLenient5 string `json:"rfc3339nanolenient5"` + RFC3339NanoLenient6 string `json:"rfc3339nanolenient6"` + RFC3339NanoLenient7 string `json:"rfc3339nanolenient7"` + RFC3339NanoLenient8 string `json:"rfc3339nanolenient8"` + RFC3339NanoLenient9 string `json:"rfc3339nanolenient9"` } t1 := Time{ - RFC3339: t.Format(time.RFC3339), - RFC3339Micro: t.Format(RFC3339Micro), - RFC3339Nano: t.Format(RFC3339Nano), + RFC3339: t.Add(45 * time.Minute).Add(12 * time.Second).Add(123456789 * time.Nanosecond).Format(time.RFC3339), + RFC3339Micro: t.Add(45 * time.Minute).Add(12 * time.Second).Add(123456789 * time.Nanosecond).Format(RFC3339Micro), + RFC3339Nano: t.Add(24 * time.Minute).Add(56 * time.Second).Add(123456789 * time.Nanosecond).Format(RFC3339Nano), + + RFC3339NanoLenient1: t.Add(100000000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient2: t.Add(120000000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient3: t.Add(123000000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient4: t.Add(123400000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient5: t.Add(123450000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient6: t.Add(123456000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient7: t.Add(123456700 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient8: t.Add(123456780 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient9: t.Add(123456789 * time.Nanosecond).Format(time.RFC3339Nano), } b, err := json.Marshal(t1) if err != nil { @@ -42,33 +72,49 @@ type Time struct { } */ - // second changed from go playground - var json = "{\"rfc3339\":\"2009-11-10T23:45:12Z\",\"rfc3339micro\":\"2009-11-10T23:45:12.123456Z\",\"rfc3339nano\":\"2009-11-10T13:24:56.123456789Z\"}\r\n"; + var json = "{\"rfc3339\":\"2009-11-10T23:45:12Z\",\"rfc3339micro\":\"2009-11-10T23:45:12.123456Z\",\"rfc3339nano\":\"2009-11-10T23:24:56.123456789Z\",\"rfc3339nanolenient1\":\"2009-11-10T23:00:00.1Z\",\"rfc3339nanolenient2\":\"2009-11-10T23:00:00.12Z\",\"rfc3339nanolenient3\":\"2009-11-10T23:00:00.123Z\",\"rfc3339nanolenient4\":\"2009-11-10T23:00:00.1234Z\",\"rfc3339nanolenient5\":\"2009-11-10T23:00:00.12345Z\",\"rfc3339nanolenient6\":\"2009-11-10T23:00:00.123456Z\",\"rfc3339nanolenient7\":\"2009-11-10T23:00:00.1234567Z\",\"rfc3339nanolenient8\":\"2009-11-10T23:00:00.12345678Z\",\"rfc3339nanolenient9\":\"2009-11-10T23:00:00.123456789Z\"}\r\n"; var t = KubernetesJson.Deserialize(json); - Assert.Equal(new DateTime(2009, 11, 10, 23, 45, 12, DateTimeKind.Utc), t.rfc3339); + Assert.Equal(new DateTime(2009, 11, 10, 23, 45, 12, DateTimeKind.Utc), t.Rfc3339); - Assert.Equal(2009, t.rfc3339micro.Year); - Assert.Equal(11, t.rfc3339micro.Month); - Assert.Equal(10, t.rfc3339micro.Day); - Assert.Equal(23, t.rfc3339micro.Hour); - Assert.Equal(45, t.rfc3339micro.Minute); - Assert.Equal(12, t.rfc3339micro.Second); - Assert.Equal(123, t.rfc3339micro.Millisecond); + Assert.Equal(2009, t.Rfc3339micro.Year); + Assert.Equal(11, t.Rfc3339micro.Month); + Assert.Equal(10, t.Rfc3339micro.Day); + Assert.Equal(23, t.Rfc3339micro.Hour); + Assert.Equal(45, t.Rfc3339micro.Minute); + Assert.Equal(12, t.Rfc3339micro.Second); + Assert.Equal(123, t.Rfc3339micro.Millisecond); Assert.Equal(2009, t.Rfc3339nano.Year); Assert.Equal(11, t.Rfc3339nano.Month); Assert.Equal(10, t.Rfc3339nano.Day); - Assert.Equal(13, t.Rfc3339nano.Hour); + Assert.Equal(23, t.Rfc3339nano.Hour); Assert.Equal(24, t.Rfc3339nano.Minute); Assert.Equal(56, t.Rfc3339nano.Second); Assert.Equal(123, t.Rfc3339nano.Millisecond); #if NET7_0_OR_GREATER - Assert.Equal(456, t.rfc3339micro.Microsecond); + Assert.Equal(456, t.Rfc3339micro.Microsecond); Assert.Equal(456, t.Rfc3339nano.Microsecond); Assert.Equal(700, t.Rfc3339nano.Nanosecond); + + Assert.Equal(100, t.Rfc3339nanolenient1.Millisecond); + Assert.Equal(120, t.Rfc3339nanolenient2.Millisecond); + Assert.Equal(123, t.Rfc3339nanolenient3.Millisecond); + + Assert.Equal(400, t.Rfc3339nanolenient4.Microsecond); + Assert.Equal(450, t.Rfc3339nanolenient5.Microsecond); + Assert.Equal(456, t.Rfc3339nanolenient6.Microsecond); + + Assert.Equal(456, t.Rfc3339nanolenient7.Microsecond); + Assert.Equal(456, t.Rfc3339nanolenient8.Microsecond); + Assert.Equal(456, t.Rfc3339nanolenient9.Microsecond); + + Assert.Equal(700, t.Rfc3339nanolenient7.Nanosecond); + Assert.Equal(700, t.Rfc3339nanolenient8.Nanosecond); + Assert.Equal(700, t.Rfc3339nanolenient9.Nanosecond); #endif + } } From 44645b98a3f9dbbcec85cdf03e32767eb5a78ce4 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Tue, 29 Aug 2023 08:07:13 -0700 Subject: [PATCH 5/5] happy build --- tests/KubernetesClient.Tests/KubernetesJsonTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/KubernetesClient.Tests/KubernetesJsonTests.cs b/tests/KubernetesClient.Tests/KubernetesJsonTests.cs index c585e0f4c..f923fd083 100644 --- a/tests/KubernetesClient.Tests/KubernetesJsonTests.cs +++ b/tests/KubernetesClient.Tests/KubernetesJsonTests.cs @@ -1,4 +1,3 @@ -using FluentAssertions.Extensions; using System; using Xunit;