|
| 1 | +using System; |
| 2 | +using Xunit; |
| 3 | + |
| 4 | +namespace k8s.Tests; |
| 5 | + |
| 6 | +public class KubernetesJsonTests |
| 7 | +{ |
| 8 | + [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")] |
| 9 | + private class RfcTime |
| 10 | + { |
| 11 | + public DateTime Rfc3339 { get; set; } |
| 12 | + public DateTime Rfc3339micro { get; set; } |
| 13 | + public DateTime Rfc3339nano { get; set; } |
| 14 | + public DateTime Rfc3339nanolenient1 { get; set; } |
| 15 | + public DateTime Rfc3339nanolenient2 { get; set; } |
| 16 | + public DateTime Rfc3339nanolenient3 { get; set; } |
| 17 | + public DateTime Rfc3339nanolenient4 { get; set; } |
| 18 | + public DateTime Rfc3339nanolenient5 { get; set; } |
| 19 | + public DateTime Rfc3339nanolenient6 { get; set; } |
| 20 | + public DateTime Rfc3339nanolenient7 { get; set; } |
| 21 | + public DateTime Rfc3339nanolenient8 { get; set; } |
| 22 | + public DateTime Rfc3339nanolenient9 { get; set; } |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void RFC3339() |
| 28 | + { |
| 29 | + /* go code to generate the json https://go.dev/play/p/VL95pugm6o8 |
| 30 | +
|
| 31 | + const RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00" |
| 32 | + const RFC3339Nano = "2006-01-02T15:04:05.000000000Z07:00" |
| 33 | +
|
| 34 | + func main() { |
| 35 | + t := time.Now() |
| 36 | + type Time struct { |
| 37 | + RFC3339 string `json:"rfc3339"` |
| 38 | + RFC3339Micro string `json:"rfc3339micro"` |
| 39 | + RFC3339Nano string `json:"rfc3339nano"` |
| 40 | +
|
| 41 | + RFC3339NanoLenient1 string `json:"rfc3339nanolenient1"` |
| 42 | + RFC3339NanoLenient2 string `json:"rfc3339nanolenient2"` |
| 43 | + RFC3339NanoLenient3 string `json:"rfc3339nanolenient3"` |
| 44 | + RFC3339NanoLenient4 string `json:"rfc3339nanolenient4"` |
| 45 | + RFC3339NanoLenient5 string `json:"rfc3339nanolenient5"` |
| 46 | + RFC3339NanoLenient6 string `json:"rfc3339nanolenient6"` |
| 47 | + RFC3339NanoLenient7 string `json:"rfc3339nanolenient7"` |
| 48 | + RFC3339NanoLenient8 string `json:"rfc3339nanolenient8"` |
| 49 | + RFC3339NanoLenient9 string `json:"rfc3339nanolenient9"` |
| 50 | + } |
| 51 | + t1 := Time{ |
| 52 | + RFC3339: t.Add(45 * time.Minute).Add(12 * time.Second).Add(123456789 * time.Nanosecond).Format(time.RFC3339), |
| 53 | + RFC3339Micro: t.Add(45 * time.Minute).Add(12 * time.Second).Add(123456789 * time.Nanosecond).Format(RFC3339Micro), |
| 54 | + RFC3339Nano: t.Add(24 * time.Minute).Add(56 * time.Second).Add(123456789 * time.Nanosecond).Format(RFC3339Nano), |
| 55 | +
|
| 56 | + RFC3339NanoLenient1: t.Add(100000000 * time.Nanosecond).Format(time.RFC3339Nano), |
| 57 | + RFC3339NanoLenient2: t.Add(120000000 * time.Nanosecond).Format(time.RFC3339Nano), |
| 58 | + RFC3339NanoLenient3: t.Add(123000000 * time.Nanosecond).Format(time.RFC3339Nano), |
| 59 | + RFC3339NanoLenient4: t.Add(123400000 * time.Nanosecond).Format(time.RFC3339Nano), |
| 60 | + RFC3339NanoLenient5: t.Add(123450000 * time.Nanosecond).Format(time.RFC3339Nano), |
| 61 | + RFC3339NanoLenient6: t.Add(123456000 * time.Nanosecond).Format(time.RFC3339Nano), |
| 62 | + RFC3339NanoLenient7: t.Add(123456700 * time.Nanosecond).Format(time.RFC3339Nano), |
| 63 | + RFC3339NanoLenient8: t.Add(123456780 * time.Nanosecond).Format(time.RFC3339Nano), |
| 64 | + RFC3339NanoLenient9: t.Add(123456789 * time.Nanosecond).Format(time.RFC3339Nano), |
| 65 | + } |
| 66 | + b, err := json.Marshal(t1) |
| 67 | + if err != nil { |
| 68 | + fmt.Println("error:", err) |
| 69 | + } |
| 70 | + fmt.Println(string(b)) |
| 71 | + } |
| 72 | + */ |
| 73 | + |
| 74 | + 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"; |
| 75 | + |
| 76 | + var t = KubernetesJson.Deserialize<RfcTime>(json); |
| 77 | + |
| 78 | + Assert.Equal(new DateTime(2009, 11, 10, 23, 45, 12, DateTimeKind.Utc), t.Rfc3339); |
| 79 | + |
| 80 | + Assert.Equal(2009, t.Rfc3339micro.Year); |
| 81 | + Assert.Equal(11, t.Rfc3339micro.Month); |
| 82 | + Assert.Equal(10, t.Rfc3339micro.Day); |
| 83 | + Assert.Equal(23, t.Rfc3339micro.Hour); |
| 84 | + Assert.Equal(45, t.Rfc3339micro.Minute); |
| 85 | + Assert.Equal(12, t.Rfc3339micro.Second); |
| 86 | + Assert.Equal(123, t.Rfc3339micro.Millisecond); |
| 87 | + |
| 88 | + Assert.Equal(2009, t.Rfc3339nano.Year); |
| 89 | + Assert.Equal(11, t.Rfc3339nano.Month); |
| 90 | + Assert.Equal(10, t.Rfc3339nano.Day); |
| 91 | + Assert.Equal(23, t.Rfc3339nano.Hour); |
| 92 | + Assert.Equal(24, t.Rfc3339nano.Minute); |
| 93 | + Assert.Equal(56, t.Rfc3339nano.Second); |
| 94 | + Assert.Equal(123, t.Rfc3339nano.Millisecond); |
| 95 | + |
| 96 | +#if NET7_0_OR_GREATER |
| 97 | + Assert.Equal(456, t.Rfc3339micro.Microsecond); |
| 98 | + Assert.Equal(456, t.Rfc3339nano.Microsecond); |
| 99 | + Assert.Equal(700, t.Rfc3339nano.Nanosecond); |
| 100 | + |
| 101 | + Assert.Equal(100, t.Rfc3339nanolenient1.Millisecond); |
| 102 | + Assert.Equal(120, t.Rfc3339nanolenient2.Millisecond); |
| 103 | + Assert.Equal(123, t.Rfc3339nanolenient3.Millisecond); |
| 104 | + |
| 105 | + Assert.Equal(400, t.Rfc3339nanolenient4.Microsecond); |
| 106 | + Assert.Equal(450, t.Rfc3339nanolenient5.Microsecond); |
| 107 | + Assert.Equal(456, t.Rfc3339nanolenient6.Microsecond); |
| 108 | + |
| 109 | + Assert.Equal(456, t.Rfc3339nanolenient7.Microsecond); |
| 110 | + Assert.Equal(456, t.Rfc3339nanolenient8.Microsecond); |
| 111 | + Assert.Equal(456, t.Rfc3339nanolenient9.Microsecond); |
| 112 | + |
| 113 | + Assert.Equal(700, t.Rfc3339nanolenient7.Nanosecond); |
| 114 | + Assert.Equal(700, t.Rfc3339nanolenient8.Nanosecond); |
| 115 | + Assert.Equal(700, t.Rfc3339nanolenient9.Nanosecond); |
| 116 | +#endif |
| 117 | + |
| 118 | + } |
| 119 | +} |
0 commit comments