* update github.com/blevesearch/bleve v2.0.2 -> v2.0.3 * github.com/denisenkom/go-mssqldb v0.9.0 -> v0.10.0 * github.com/editorconfig/editorconfig-core-go v2.4.1 -> v2.4.2 * github.com/go-chi/cors v1.1.1 -> v1.2.0 * github.com/go-git/go-billy v5.0.0 -> v5.1.0 * github.com/go-git/go-git v5.2.0 -> v5.3.0 * github.com/go-ldap/ldap v3.2.4 -> v3.3.0 * github.com/go-redis/redis v8.6.0 -> v8.8.2 * github.com/go-sql-driver/mysql v1.5.0 -> v1.6.0 * github.com/go-swagger/go-swagger v0.26.1 -> v0.27.0 * github.com/lib/pq v1.9.0 -> v1.10.1 * github.com/mattn/go-sqlite3 v1.14.6 -> v1.14.7 * github.com/go-testfixtures/testfixtures v3.5.0 -> v3.6.0 * github.com/issue9/identicon v1.0.1 -> v1.2.0 * github.com/klauspost/compress v1.11.8 -> v1.12.1 * github.com/mgechev/revive v1.0.3 -> v1.0.6 * github.com/microcosm-cc/bluemonday v1.0.7 -> v1.0.8 * github.com/niklasfasching/go-org v1.4.0 -> v1.5.0 * github.com/olivere/elastic v7.0.22 -> v7.0.24 * github.com/pelletier/go-toml v1.8.1 -> v1.9.0 * github.com/prometheus/client_golang v1.9.0 -> v1.10.0 * github.com/xanzy/go-gitlab v0.44.0 -> v0.48.0 * github.com/yuin/goldmark v1.3.3 -> v1.3.5 * github.com/6543/go-version v1.2.4 -> v1.3.1 * do github.com/lib/pq v1.10.0 -> v1.10.1 again ...
112 lines
3.6 KiB
Go
Vendored
112 lines
3.6 KiB
Go
Vendored
// Copyright 2016 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package ptypes
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
timestamppb "github.com/golang/protobuf/ptypes/timestamp"
|
|
)
|
|
|
|
// Range of google.protobuf.Duration as specified in timestamp.proto.
|
|
const (
|
|
// Seconds field of the earliest valid Timestamp.
|
|
// This is time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
|
|
minValidSeconds = -62135596800
|
|
// Seconds field just after the latest valid Timestamp.
|
|
// This is time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
|
|
maxValidSeconds = 253402300800
|
|
)
|
|
|
|
// Timestamp converts a timestamppb.Timestamp to a time.Time.
|
|
// It returns an error if the argument is invalid.
|
|
//
|
|
// Unlike most Go functions, if Timestamp returns an error, the first return
|
|
// value is not the zero time.Time. Instead, it is the value obtained from the
|
|
// time.Unix function when passed the contents of the Timestamp, in the UTC
|
|
// locale. This may or may not be a meaningful time; many invalid Timestamps
|
|
// do map to valid time.Times.
|
|
//
|
|
// A nil Timestamp returns an error. The first return value in that case is
|
|
// undefined.
|
|
//
|
|
// Deprecated: Call the ts.AsTime and ts.CheckValid methods instead.
|
|
func Timestamp(ts *timestamppb.Timestamp) (time.Time, error) {
|
|
// Don't return the zero value on error, because corresponds to a valid
|
|
// timestamp. Instead return whatever time.Unix gives us.
|
|
var t time.Time
|
|
if ts == nil {
|
|
t = time.Unix(0, 0).UTC() // treat nil like the empty Timestamp
|
|
} else {
|
|
t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
|
|
}
|
|
return t, validateTimestamp(ts)
|
|
}
|
|
|
|
// TimestampNow returns a google.protobuf.Timestamp for the current time.
|
|
//
|
|
// Deprecated: Call the timestamppb.Now function instead.
|
|
func TimestampNow() *timestamppb.Timestamp {
|
|
ts, err := TimestampProto(time.Now())
|
|
if err != nil {
|
|
panic("ptypes: time.Now() out of Timestamp range")
|
|
}
|
|
return ts
|
|
}
|
|
|
|
// TimestampProto converts the time.Time to a google.protobuf.Timestamp proto.
|
|
// It returns an error if the resulting Timestamp is invalid.
|
|
//
|
|
// Deprecated: Call the timestamppb.New function instead.
|
|
func TimestampProto(t time.Time) (*timestamppb.Timestamp, error) {
|
|
ts := ×tamppb.Timestamp{
|
|
Seconds: t.Unix(),
|
|
Nanos: int32(t.Nanosecond()),
|
|
}
|
|
if err := validateTimestamp(ts); err != nil {
|
|
return nil, err
|
|
}
|
|
return ts, nil
|
|
}
|
|
|
|
// TimestampString returns the RFC 3339 string for valid Timestamps.
|
|
// For invalid Timestamps, it returns an error message in parentheses.
|
|
//
|
|
// Deprecated: Call the ts.AsTime method instead,
|
|
// followed by a call to the Format method on the time.Time value.
|
|
func TimestampString(ts *timestamppb.Timestamp) string {
|
|
t, err := Timestamp(ts)
|
|
if err != nil {
|
|
return fmt.Sprintf("(%v)", err)
|
|
}
|
|
return t.Format(time.RFC3339Nano)
|
|
}
|
|
|
|
// validateTimestamp determines whether a Timestamp is valid.
|
|
// A valid timestamp represents a time in the range [0001-01-01, 10000-01-01)
|
|
// and has a Nanos field in the range [0, 1e9).
|
|
//
|
|
// If the Timestamp is valid, validateTimestamp returns nil.
|
|
// Otherwise, it returns an error that describes the problem.
|
|
//
|
|
// Every valid Timestamp can be represented by a time.Time,
|
|
// but the converse is not true.
|
|
func validateTimestamp(ts *timestamppb.Timestamp) error {
|
|
if ts == nil {
|
|
return errors.New("timestamp: nil Timestamp")
|
|
}
|
|
if ts.Seconds < minValidSeconds {
|
|
return fmt.Errorf("timestamp: %v before 0001-01-01", ts)
|
|
}
|
|
if ts.Seconds >= maxValidSeconds {
|
|
return fmt.Errorf("timestamp: %v after 10000-01-01", ts)
|
|
}
|
|
if ts.Nanos < 0 || ts.Nanos >= 1e9 {
|
|
return fmt.Errorf("timestamp: %v: nanos not in range [0, 1e9)", ts)
|
|
}
|
|
return nil
|
|
}
|