mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-03-25 16:43:37 +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
|
||||
|
||||
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()
|
||||
nanos := m.Get(fds.ByName("nanos")).Int()
|
||||
switch {
|
||||
case secs < minTimestamp || secs > maxTimestamp:
|
||||
return nil
|
||||
case nanos < 0 || nanos >= 1e9:
|
||||
if nanos < 0 || nanos >= 1e9 {
|
||||
return nil
|
||||
}
|
||||
|
||||
t := time.Unix(secs, nanos).UTC()
|
||||
x := t.Format("2006-01-02T15:04:05.000000000") // RFC 3339
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
@ -160,18 +154,11 @@ func appendKnownMessage(b []byte, m protoreflect.Message) []byte {
|
||||
return append(b, x+"Z"...)
|
||||
|
||||
case "Duration":
|
||||
const absDuration = 315576000000 // 10000yr * 365.25day/yr * 24hr/day * 60min/hr * 60sec/min
|
||||
secs := m.Get(fds.ByName("seconds")).Int()
|
||||
nanos := m.Get(fds.ByName("nanos")).Int()
|
||||
switch {
|
||||
case secs < -absDuration || secs > +absDuration:
|
||||
return nil
|
||||
case nanos <= -1e9 || nanos >= 1e9:
|
||||
return nil
|
||||
case (secs > 0 && nanos < 0) || (secs < 0 && nanos > 0):
|
||||
if nanos <= -1e9 || nanos >= 1e9 || (secs > 0 && nanos < 0) || (secs < 0 && nanos > 0) {
|
||||
return nil
|
||||
}
|
||||
|
||||
x := fmt.Sprintf("%d.%09d", secs, int64(math.Abs(float64(nanos))))
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
|
Loading…
x
Reference in New Issue
Block a user