10 examples of 'golang int to duration' in Go

Every line of 'golang int to duration' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
99func durationToTimeDuration(d *duration.Duration) time.Duration {
100 return time.Duration(d.Nanos) + time.Second*time.Duration(d.Seconds)
101}
807func ToDuration(t interface{}, args ...string) time.Duration {
808 td := time.Second
809 if len(args) > 0 {
810 switch args[0] {
811 case `ns`:
812 td = time.Nanosecond
813 case `us`:
814 td = time.Microsecond
815 case `s`:
816 td = time.Second
817 case `ms`:
818 td = time.Millisecond
819 case `h`:
820 td = time.Hour
821 case `m`:
822 td = time.Minute
823 }
824 }
825 switch v := t.(type) {
826 case time.Duration:
827 return v
828 case int64:
829 td = time.Duration(v) * td
830 case int:
831 td = time.Duration(v) * td
832 case uint:
833 td = time.Duration(v) * td
834 case int32:
835 td = time.Duration(v) * td
836 case uint32:
837 td = time.Duration(v) * td
838 case uint64:
839 td = time.Duration(v) * td
840 default:
841 td = time.Duration(com.Int64(t)) * td
842 }
843 return td
844}
163func SecondsToDuration(d float64) time.Duration {
164 return time.Duration(1000000000.0 * d)
165}
243func Duration(v time.Duration) predicate.FieldType {
244 vc := int64(v)
245 return predicate.FieldType(func(t *dsl.Traversal) {
246 t.Has(Label, FieldDuration, p.EQ(vc))
247 })
248}
30func DurationFromDateTime(t time.Time) ASEDuration {
31 y := int64(t.Year())
32 m := int64(t.Month())
33 d := int64(t.Day())
34 // Calculate JDN, formula from Calendars by Doggett
35 jd := (1461*(y+4800+(int64(m)-14)/12))/4 + (367*(m-2-12*((m-14)/12)))/12 - (3*((y+4900+(m-14)/12)/100))/4 + d - 32075
36
37 // Calculate Rata Die from JDN and convert to microseconds
38 rataDie := int64(jd-1721425) * (int64(time.Duration(24)*time.Hour) / 1000)
39 // While Sybase uses Rata Die it still seems to count year 0
40 rataDie += int64((time.Hour * 24) * 365 / 1000)
41
42 return ASEDuration(rataDie) + DurationFromTime(t)
43}
61func DurationFromProto(duration *google_protobuf.Duration) time.Duration {
62 if duration == nil {
63 return 0
64 }
65 return time.Duration((duration.Seconds * int64(time.Second)) + int64(duration.Nanos))
66}
416func getDuration(seconds int) time.Duration {
417 return time.Duration(seconds) * time.Second
418}
202func absDuration(d time.Duration) time.Duration {
203 if d < 0 {
204 return -1 * d
205 }
206
207 return d
208}
39func ToDurationE(i interface{}) (d time.Duration, err error) {
40 i = indirect(i)
41 jww.TRACE.Println("ToDurationE called on type:", reflect.TypeOf(i))
42
43 switch s := i.(type) {
44 case time.Duration:
45 return s, nil
46 case int64:
47 d = time.Duration(s)
48 return
49 case float64:
50 d = time.Duration(s)
51 return
52 case string:
53 d, err = time.ParseDuration(s)
54 return
55 default:
56 err = fmt.Errorf("Unable to Cast %#v to Duration\n", i)
57 return
58 }
59}
164func postgresToDuration(s string) (duration.Duration, error) {
165 var d duration.Duration
166 l := intervalLexer{str: s, offset: 0, err: nil}
167 for l.offset != len(l.str) {
168 v := l.consumeInt()
169 l.consumeSpaces()
170 u := l.consumeUnit(' ')
171 l.consumeSpaces()
172 if unit, ok := postgresUnitMap[u]; ok {
173 d = d.Add(unit.Mul(v))
174 } else {
175 return d, fmt.Errorf("interval: unknown unit %s in postgres duration %s", u, s)
176 }
177 }
178
179 return d, nil
180}

Related snippets