Atlas › Test

TestUnmarshalTimestampRoundtripAndErrors

Exact test identity: go.temporal.io/server/common/nexus/nexusrpc/TestUnmarshalTimestampRoundtripAndErrors

Package
go.temporal.io/server/common/nexus/nexusrpc
Suite / test hierarchy
TestUnmarshalTimestampRoundtripAndErrors
Test
TestUnmarshalTimestampRoundtripAndErrors
Introduced at
timestamp.go ×2 Frontier kind: Joint frontier
Covered ranges
7
Covered lines
20
Covered files
1

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/nexus/nexusrpc/timestamp.go 20 covered LOC · 7 ranges

Open complete file

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
17 > func marshalTimestamp(t time.Time) string { timestamp.go
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
28 > func unmarshalTimestamp(s string) (time.Time, error) { timestamp.go
29 > t, err := time.Parse(time.RFC3339Nano, s)
30 > if err != nil {
31 > return t, err timestamp.go
32 > }
33 // Validate seconds.
34 > secs := t.Unix() timestamp.go
35 > if secs < minTimestampSeconds || secs > maxTimestampSeconds {
36 return t, fmt.Errorf("second value out of range: %v", secs)
37 }
38 // Validate subseconds.
39 > i := strings.LastIndexByte(s, '.') // start of subsecond field timestamp.go
40 > j := strings.LastIndexAny(s, "Z-+") // start of timezone field
41 > if i >= 0 && j >= i && j-i > len(".999999999") {
42 > return t, fmt.Errorf("invalid subsecond value %v", s) timestamp.go
43 > }
44 > return t, nil timestamp.go
45 }