mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-04-01 13:20:07 +00:00
internal/msgfmt: simply time formatting logic
The msgfmt formatter is intended to only for debugging purposes. Remove unnecesary logic to detect out-of-range timestamps and durations. Change-Id: I060149ed71aa892bbe4fdb2508b1d0b9df4b5f37 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/225258 Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
28cb1e4f28
commit
f8d77f810a
@ -141,17 +141,11 @@ func appendKnownMessage(b []byte, m protoreflect.Message) []byte {
|
|||||||
return b
|
return b
|
||||||
|
|
||||||
case "Timestamp":
|
case "Timestamp":
|
||||||
const minTimestamp = -62135596800 // Seconds between 1970-01-01T00:00:00Z and 0001-01-01T00:00:00Z, inclusive
|
|
||||||
const maxTimestamp = +253402300799 // Seconds between 1970-01-01T00:00:00Z and 9999-12-31T23:59:59Z, inclusive
|
|
||||||
secs := m.Get(fds.ByName("seconds")).Int()
|
secs := m.Get(fds.ByName("seconds")).Int()
|
||||||
nanos := m.Get(fds.ByName("nanos")).Int()
|
nanos := m.Get(fds.ByName("nanos")).Int()
|
||||||
switch {
|
if nanos < 0 || nanos >= 1e9 {
|
||||||
case secs < minTimestamp || secs > maxTimestamp:
|
|
||||||
return nil
|
|
||||||
case nanos < 0 || nanos >= 1e9:
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
t := time.Unix(secs, nanos).UTC()
|
t := time.Unix(secs, nanos).UTC()
|
||||||
x := t.Format("2006-01-02T15:04:05.000000000") // RFC 3339
|
x := t.Format("2006-01-02T15:04:05.000000000") // RFC 3339
|
||||||
x = strings.TrimSuffix(x, "000")
|
x = strings.TrimSuffix(x, "000")
|
||||||
@ -160,18 +154,11 @@ func appendKnownMessage(b []byte, m protoreflect.Message) []byte {
|
|||||||
return append(b, x+"Z"...)
|
return append(b, x+"Z"...)
|
||||||
|
|
||||||
case "Duration":
|
case "Duration":
|
||||||
const absDuration = 315576000000 // 10000yr * 365.25day/yr * 24hr/day * 60min/hr * 60sec/min
|
|
||||||
secs := m.Get(fds.ByName("seconds")).Int()
|
secs := m.Get(fds.ByName("seconds")).Int()
|
||||||
nanos := m.Get(fds.ByName("nanos")).Int()
|
nanos := m.Get(fds.ByName("nanos")).Int()
|
||||||
switch {
|
if nanos <= -1e9 || nanos >= 1e9 || (secs > 0 && nanos < 0) || (secs < 0 && nanos > 0) {
|
||||||
case secs < -absDuration || secs > +absDuration:
|
|
||||||
return nil
|
|
||||||
case nanos <= -1e9 || nanos >= 1e9:
|
|
||||||
return nil
|
|
||||||
case (secs > 0 && nanos < 0) || (secs < 0 && nanos > 0):
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
x := fmt.Sprintf("%d.%09d", secs, int64(math.Abs(float64(nanos))))
|
x := fmt.Sprintf("%d.%09d", secs, int64(math.Abs(float64(nanos))))
|
||||||
x = strings.TrimSuffix(x, "000")
|
x = strings.TrimSuffix(x, "000")
|
||||||
x = strings.TrimSuffix(x, "000")
|
x = strings.TrimSuffix(x, "000")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user