podman
1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2016 The Go Authors. All rights reserved.
4// https://github.com/golang/protobuf
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32package proto33
34// This file implements operations on google.protobuf.Timestamp.
35
36import (37"errors"38"fmt"39"time"40)
41
42const (43// Seconds field of the earliest valid Timestamp.44// This is time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix().45minValidSeconds = -6213559680046// Seconds field just after the latest valid Timestamp.47// This is time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC).Unix().48maxValidSeconds = 25340230080049)
50
51// validateTimestamp determines whether a Timestamp is valid.
52// A valid timestamp represents a time in the range
53// [0001-01-01, 10000-01-01) and has a Nanos field
54// in the range [0, 1e9).
55//
56// If the Timestamp is valid, validateTimestamp returns nil.
57// Otherwise, it returns an error that describes
58// the problem.
59//
60// Every valid Timestamp can be represented by a time.Time, but the converse is not true.
61func validateTimestamp(ts *timestamp) error {62if ts == nil {63return errors.New("timestamp: nil Timestamp")64}65if ts.Seconds < minValidSeconds {66return fmt.Errorf("timestamp: %#v before 0001-01-01", ts)67}68if ts.Seconds >= maxValidSeconds {69return fmt.Errorf("timestamp: %#v after 10000-01-01", ts)70}71if ts.Nanos < 0 || ts.Nanos >= 1e9 {72return fmt.Errorf("timestamp: %#v: nanos not in range [0, 1e9)", ts)73}74return nil75}
76
77// TimestampFromProto converts a google.protobuf.Timestamp proto to a time.Time.
78// It returns an error if the argument is invalid.
79//
80// Unlike most Go functions, if Timestamp returns an error, the first return value
81// is not the zero time.Time. Instead, it is the value obtained from the
82// time.Unix function when passed the contents of the Timestamp, in the UTC
83// locale. This may or may not be a meaningful time; many invalid Timestamps
84// do map to valid time.Times.
85//
86// A nil Timestamp returns an error. The first return value in that case is
87// undefined.
88func timestampFromProto(ts *timestamp) (time.Time, error) {89// Don't return the zero value on error, because corresponds to a valid90// timestamp. Instead return whatever time.Unix gives us.91var t time.Time92if ts == nil {93t = time.Unix(0, 0).UTC() // treat nil like the empty Timestamp94} else {95t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()96}97return t, validateTimestamp(ts)98}
99
100// TimestampProto converts the time.Time to a google.protobuf.Timestamp proto.
101// It returns an error if the resulting Timestamp is invalid.
102func timestampProto(t time.Time) (*timestamp, error) {103seconds := t.Unix()104nanos := int32(t.Sub(time.Unix(seconds, 0)))105ts := ×tamp{106Seconds: seconds,107Nanos: nanos,108}109if err := validateTimestamp(ts); err != nil {110return nil, err111}112return ts, nil113}
114