github.com/blend/go-sdk@v1.20220411.3/protoutil/duration.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package protoutil 9 10 import ( 11 "time" 12 13 "github.com/golang/protobuf/ptypes/duration" 14 ) 15 16 // Duration returns a proto duration. 17 func Duration(d time.Duration) *duration.Duration { 18 if d == 0 { 19 return nil 20 } 21 nanos := d.Nanoseconds() 22 secs := nanos / 1e9 23 nanos -= secs * 1e9 24 25 return &duration.Duration{Seconds: int64(secs), Nanos: int32(nanos)} 26 } 27 28 // FromDuration returns a time.Duration. 29 func FromDuration(dur *duration.Duration) time.Duration { 30 if dur == nil { 31 return 0 32 } 33 d := time.Duration(dur.Seconds) * time.Second 34 if dur.Nanos > 0 { 35 d += time.Duration(dur.Nanos) * time.Nanosecond 36 } 37 return d 38 }