duration.go 3.8 KB

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