15
// uses 0, 3, 6 or 9 fractional digits.
16
// Copied from https://github.com/protocolbuffers/protobuf-go/blob/0b2c87d84c27802dae7248480444e22421ba577d/encoding/protojson/well_known_types.go#L749C1-L826C2
18
>
x := t.UTC().Format("2006-01-02T15:04:05.000000000")
19
>
x = strings.TrimSuffix(x, "000")
20
>
x = strings.TrimSuffix(x, "000")
21
>
x = strings.TrimSuffix(x, ".000")
22
>
return x + "Z"
23
>
}
24
25
// unmarshalTimestamp unmarshals a string into a Time instance. Uses RFC 3339, with some extra validation to ensure that
26
// seconds and subseconds are with an expected range.
27
// Copied from https://github.com/protocolbuffers/protobuf-go/blob/0b2c87d84c27802dae7248480444e22421ba577d/encoding/protojson/well_known_types.go#L749C1-L826C2
29
>
t, err := time.Parse(time.RFC3339Nano, s)
30
>
if err != nil {
32
>
}
33
// Validate seconds.
35
>
if secs < minTimestampSeconds || secs > maxTimestampSeconds {
36
return t, fmt.Errorf("second value out of range: %v", secs)
37
}
38
// Validate subseconds.
40
>
j := strings.LastIndexAny(s, "Z-+") // start of timezone field
41
>
if i >= 0 && j >= i && j-i > len(".999999999") {
43
>
}
45
}