How to use Duration method of proto Package

Best Rod code snippet using proto.Duration

duration.pb.go

Source:duration.pb.go Github

copy

Full Screen

...35 protoimpl "google.golang.org/protobuf/runtime/protoimpl"36 reflect "reflect"37 sync "sync"38)39// A Duration represents a signed, fixed-length span of time represented40// as a count of seconds and fractions of seconds at nanosecond41// resolution. It is independent of any calendar and concepts like "day"42// or "month". It is related to Timestamp in that the difference between43// two Timestamp values is a Duration and it can be added or subtracted44// from a Timestamp. Range is approximately +-10,000 years.45//46// # Examples47//48// Example 1: Compute Duration from two Timestamps in pseudo code.49//50// Timestamp start = ...;51// Timestamp end = ...;52// Duration duration = ...;53//54// duration.seconds = end.seconds - start.seconds;55// duration.nanos = end.nanos - start.nanos;56//57// if (duration.seconds < 0 && duration.nanos > 0) {58// duration.seconds += 1;59// duration.nanos -= 1000000000;60// } else if (duration.seconds > 0 && duration.nanos < 0) {61// duration.seconds -= 1;62// duration.nanos += 1000000000;63// }64//65// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.66//67// Timestamp start = ...;68// Duration duration = ...;69// Timestamp end = ...;70//71// end.seconds = start.seconds + duration.seconds;72// end.nanos = start.nanos + duration.nanos;73//74// if (end.nanos < 0) {75// end.seconds -= 1;76// end.nanos += 1000000000;77// } else if (end.nanos >= 1000000000) {78// end.seconds += 1;79// end.nanos -= 1000000000;80// }81//82// Example 3: Compute Duration from datetime.timedelta in Python.83//84// td = datetime.timedelta(days=3, minutes=10)85// duration = Duration()86// duration.FromTimedelta(td)87//88// # JSON Mapping89//90// In JSON format, the Duration type is encoded as a string rather than an91// object, where the string ends in the suffix "s" (indicating seconds) and92// is preceded by the number of seconds, with nanoseconds expressed as93// fractional seconds. For example, 3 seconds with 0 nanoseconds should be94// encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should95// be expressed in JSON format as "3.000000001s", and 3 seconds and 196// microsecond should be expressed in JSON format as "3.000001s".97//98//99type Duration struct {100 state protoimpl.MessageState101 sizeCache protoimpl.SizeCache102 unknownFields protoimpl.UnknownFields103 // Signed seconds of the span of time. Must be from -315,576,000,000104 // to +315,576,000,000 inclusive. Note: these bounds are computed from:105 // 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years106 Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"`107 // Signed fractions of a second at nanosecond resolution of the span108 // of time. Durations less than one second are represented with a 0109 // `seconds` field and a positive or negative `nanos` field. For durations110 // of one second or more, a non-zero value for the `nanos` field must be111 // of the same sign as the `seconds` field. Must be from -999,999,999112 // to +999,999,999 inclusive.113 Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`114}115func (x *Duration) Reset() {116 *x = Duration{}117 if protoimpl.UnsafeEnabled {118 mi := &file_google_protobuf_duration_proto_msgTypes[0]119 ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))120 ms.StoreMessageInfo(mi)121 }122}123func (x *Duration) String() string {124 return protoimpl.X.MessageStringOf(x)125}126func (*Duration) ProtoMessage() {}127func (x *Duration) ProtoReflect() protoreflect.Message {128 mi := &file_google_protobuf_duration_proto_msgTypes[0]129 if protoimpl.UnsafeEnabled && x != nil {130 ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))131 if ms.LoadMessageInfo() == nil {132 ms.StoreMessageInfo(mi)133 }134 return ms135 }136 return mi.MessageOf(x)137}138// Deprecated: Use Duration.ProtoReflect.Descriptor instead.139func (*Duration) Descriptor() ([]byte, []int) {140 return file_google_protobuf_duration_proto_rawDescGZIP(), []int{0}141}142func (x *Duration) GetSeconds() int64 {143 if x != nil {144 return x.Seconds145 }146 return 0147}148func (x *Duration) GetNanos() int32 {149 if x != nil {150 return x.Nanos151 }152 return 0153}154var File_google_protobuf_duration_proto protoreflect.FileDescriptor155var file_google_protobuf_duration_proto_rawDesc = []byte{156 0x0a, 0x1e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75,157 0x66, 0x2f, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,158 0x12, 0x0f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75,159 0x66, 0x22, 0x3a, 0x0a, 0x08, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x18, 0x0a,160 0x07, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07,161 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6e, 0x61, 0x6e, 0x6f, 0x73,162 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6e, 0x61, 0x6e, 0x6f, 0x73, 0x42, 0x7c, 0x0a,163 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74,164 0x6f, 0x62, 0x75, 0x66, 0x42, 0x0d, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x50, 0x72,165 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f,166 0x6d, 0x2f, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75,167 0x66, 0x2f, 0x70, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f,168 0x6e, 0xf8, 0x01, 0x01, 0xa2, 0x02, 0x03, 0x47, 0x50, 0x42, 0xaa, 0x02, 0x1e, 0x47, 0x6f, 0x6f,169 0x67, 0x6c, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x57, 0x65, 0x6c,170 0x6c, 0x4b, 0x6e, 0x6f, 0x77, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f,171 0x74, 0x6f, 0x33,172}173var (174 file_google_protobuf_duration_proto_rawDescOnce sync.Once175 file_google_protobuf_duration_proto_rawDescData = file_google_protobuf_duration_proto_rawDesc176)177func file_google_protobuf_duration_proto_rawDescGZIP() []byte {178 file_google_protobuf_duration_proto_rawDescOnce.Do(func() {179 file_google_protobuf_duration_proto_rawDescData = protoimpl.X.CompressGZIP(file_google_protobuf_duration_proto_rawDescData)180 })181 return file_google_protobuf_duration_proto_rawDescData182}183var file_google_protobuf_duration_proto_msgTypes = make([]protoimpl.MessageInfo, 1)184var file_google_protobuf_duration_proto_goTypes = []interface{}{185 (*Duration)(nil), // 0: google.protobuf.Duration186}187var file_google_protobuf_duration_proto_depIdxs = []int32{188 0, // [0:0] is the sub-list for method output_type189 0, // [0:0] is the sub-list for method input_type190 0, // [0:0] is the sub-list for extension type_name191 0, // [0:0] is the sub-list for extension extendee192 0, // [0:0] is the sub-list for field type_name193}194func init() { file_google_protobuf_duration_proto_init() }195func file_google_protobuf_duration_proto_init() {196 if File_google_protobuf_duration_proto != nil {197 return198 }199 if !protoimpl.UnsafeEnabled {200 file_google_protobuf_duration_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {201 switch v := v.(*Duration); i {202 case 0:203 return &v.state204 case 1:205 return &v.sizeCache206 case 2:207 return &v.unknownFields208 default:209 return nil210 }211 }212 }213 type x struct{}214 out := protoimpl.TypeBuilder{215 File: protoimpl.DescBuilder{...

Full Screen

Full Screen

duration_test.go

Source:duration_test.go Github

copy

Full Screen

...13 minGoSeconds = math.MinInt64 / int64(1e9)14 maxGoSeconds = math.MaxInt64 / int64(1e9)15)16var durationTests = []struct {17 proto *durpb.Duration18 isValid bool19 inRange bool20 dur time.Duration21}{22 // The zero duration.23 {&durpb.Duration{Seconds: 0, Nanos: 0}, true, true, 0},24 // Some ordinary non-zero durations.25 {&durpb.Duration{Seconds: 100, Nanos: 0}, true, true, 100 * time.Second},26 {&durpb.Duration{Seconds: -100, Nanos: 0}, true, true, -100 * time.Second},27 {&durpb.Duration{Seconds: 100, Nanos: 987}, true, true, 100*time.Second + 987},28 {&durpb.Duration{Seconds: -100, Nanos: -987}, true, true, -(100*time.Second + 987)},29 // The largest duration representable in Go.30 {&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, true, math.MaxInt64},31 // The smallest duration representable in Go.32 {&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, true, math.MinInt64},33 {nil, false, false, 0},34 {&durpb.Duration{Seconds: -100, Nanos: 987}, false, false, 0},35 {&durpb.Duration{Seconds: 100, Nanos: -987}, false, false, 0},36 {&durpb.Duration{Seconds: math.MinInt64, Nanos: 0}, false, false, 0},37 {&durpb.Duration{Seconds: math.MaxInt64, Nanos: 0}, false, false, 0},38 // The largest valid duration.39 {&durpb.Duration{Seconds: maxSeconds, Nanos: 1e9 - 1}, true, false, 0},40 // The smallest valid duration.41 {&durpb.Duration{Seconds: minSeconds, Nanos: -(1e9 - 1)}, true, false, 0},42 // The smallest invalid duration above the valid range.43 {&durpb.Duration{Seconds: maxSeconds + 1, Nanos: 0}, false, false, 0},44 // The largest invalid duration below the valid range.45 {&durpb.Duration{Seconds: minSeconds - 1, Nanos: -(1e9 - 1)}, false, false, 0},46 // One nanosecond past the largest duration representable in Go.47 {&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64-1e9*maxGoSeconds) + 1}, true, false, 0},48 // One nanosecond past the smallest duration representable in Go.49 {&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64-1e9*minGoSeconds) - 1}, true, false, 0},50 // One second past the largest duration representable in Go.51 {&durpb.Duration{Seconds: maxGoSeconds + 1, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, false, 0},52 // One second past the smallest duration representable in Go.53 {&durpb.Duration{Seconds: minGoSeconds - 1, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, false, 0},54}55func TestValidateDuration(t *testing.T) {56 for _, test := range durationTests {57 err := validateDuration(test.proto)58 gotValid := (err == nil)59 if gotValid != test.isValid {60 t.Errorf("validateDuration(%v) = %t, want %t", test.proto, gotValid, test.isValid)61 }62 }63}64func TestDuration(t *testing.T) {65 for _, test := range durationTests {66 got, err := Duration(test.proto)67 gotOK := (err == nil)68 wantOK := test.isValid && test.inRange69 if gotOK != wantOK {70 t.Errorf("Duration(%v) ok = %t, want %t", test.proto, gotOK, wantOK)71 }72 if err == nil && got != test.dur {73 t.Errorf("Duration(%v) = %v, want %v", test.proto, got, test.dur)74 }75 }76}77func TestDurationProto(t *testing.T) {78 for _, test := range durationTests {79 if test.isValid && test.inRange {80 got := DurationProto(test.dur)81 if !proto.Equal(got, test.proto) {82 t.Errorf("DurationProto(%v) = %v, want %v", test.dur, got, test.proto)83 }84 }85 }86}

Full Screen

Full Screen

Duration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t := time.Now()4 d, err := ptypes.DurationProto(t.Sub(time.Unix(0, 0)))5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(d)9}

Full Screen

Full Screen

Duration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 duration, err := ptypes.DurationProto(time.Second * 10)4 if err != nil {5 fmt.Println("Error:", err)6 }7 fmt.Println("Duration in proto format:", duration)8 fmt.Println("Duration in seconds:", duration.Seconds)9 fmt.Println("Duration in nanoseconds:", duration.Nanos)10}

Full Screen

Full Screen

Duration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 d, _ := ptypes.DurationProto(24 * time.Hour)4 fmt.Println(d)5 days, _ := ptypes.Duration(d)6 fmt.Println(days)7}

Full Screen

Full Screen

Duration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 duration := &duration.Duration{4 }5 d, err := ptypes.Duration(duration)6 if err != nil {7 fmt.Println(err)8 }9 fmt.Println(d)10 d2, err := ptypes.DurationProto(time.Second * 10)11 if err != nil {12 fmt.Println(err)13 }14 fmt.Println(d2)15}

Full Screen

Full Screen

Duration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 now := ptypes.TimestampNow()4 t, err := ptypes.Timestamp(now)5 if err != nil {6 panic(err)7 }8 fmt.Println(t)9 fmt.Println(ptypes.DurationProto(time.Hour))10}

Full Screen

Full Screen

Duration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t := time.Now()4 pbts, err := ptypes.TimestampProto(t)5 if err != nil {6 fmt.Println("Error:", err)7 }8 t2, err := ptypes.Timestamp(pbts)9 if err != nil {10 fmt.Println("Error:", err)11 }12 fmt.Println(t2)13}

Full Screen

Full Screen

Duration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t := time.Now()4 t1 := time.Date(2019, 12, 31, 0, 0, 0, 0, time.UTC)5 d := t.Sub(t1)6 pd, err := ptypes.DurationProto(d)7 if err != nil {8 fmt.Println("Error in creating Duration protobuf message")9 }10 fmt.Println("Duration protobuf message:", pd)11}

Full Screen

Full Screen

Duration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 start := time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC)4 end := time.Date(2017, 1, 1, 1, 0, 0, 0, time.UTC)5 duration, err := ptypes.Duration(start, end)6 if err != nil {7 fmt.Println(err)8 }9 fmt.Println("Duration in hours:", duration.Hours())10 fmt.Println("Duration in minutes:", duration.Minutes())11 fmt.Println("Duration in seconds:", duration.Seconds())12 fmt.Println("Duration in nanoseconds:", duration.Nanoseconds())

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Rod automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful