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 conversions between google.protobuf.Duration
35// and time.Duration.
36
37import (38"errors"39"fmt"40"time"41)
42
43const (44// Range of a Duration in seconds, as specified in45// google/protobuf/duration.proto. This is about 10,000 years in seconds.46maxSeconds = int64(10000 * 365.25 * 24 * 60 * 60)47minSeconds = -maxSeconds48)
49
50// validateDuration determines whether the Duration is valid according to the
51// definition in google/protobuf/duration.proto. A valid Duration
52// may still be too large to fit into a time.Duration (the range of Duration
53// is about 10,000 years, and the range of time.Duration is about 290).
54func validateDuration(d *duration) error {55if d == nil {56return errors.New("duration: nil Duration")57}58if d.Seconds < minSeconds || d.Seconds > maxSeconds {59return fmt.Errorf("duration: %#v: seconds out of range", d)60}61if d.Nanos <= -1e9 || d.Nanos >= 1e9 {62return fmt.Errorf("duration: %#v: nanos out of range", d)63}64// Seconds and Nanos must have the same sign, unless d.Nanos is zero.65if (d.Seconds < 0 && d.Nanos > 0) || (d.Seconds > 0 && d.Nanos < 0) {66return fmt.Errorf("duration: %#v: seconds and nanos have different signs", d)67}68return nil69}
70
71// DurationFromProto converts a Duration to a time.Duration. DurationFromProto
72// returns an error if the Duration is invalid or is too large to be
73// represented in a time.Duration.
74func durationFromProto(p *duration) (time.Duration, error) {75if err := validateDuration(p); err != nil {76return 0, err77}78d := time.Duration(p.Seconds) * time.Second79if int64(d/time.Second) != p.Seconds {80return 0, fmt.Errorf("duration: %#v is out of range for time.Duration", p)81}82if p.Nanos != 0 {83d += time.Duration(p.Nanos)84if (d < 0) != (p.Nanos < 0) {85return 0, fmt.Errorf("duration: %#v is out of range for time.Duration", p)86}87}88return d, nil89}
90
91// DurationProto converts a time.Duration to a Duration.
92func durationProto(d time.Duration) *duration {93nanos := d.Nanoseconds()94secs := nanos / 1e995nanos -= secs * 1e996return &duration{97Seconds: secs,98Nanos: int32(nanos),99}100}
101