File: duration.go

package info (click to toggle)
golang-github-scaleway-scaleway-sdk-go 1.0.0~beta32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,444 kB
  • sloc: sh: 70; makefile: 3
file content (160 lines) | stat: -rw-r--r-- 4,162 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package marshaler

import (
	"encoding/json"
	"time"
)

// Duration implements a JSON Marshaler to encode a time.Duration in milliseconds.
type Duration int64

const milliSec = Duration(time.Millisecond)

// NewDuration converts a *time.Duration to a *Duration type.
func NewDuration(t *time.Duration) *Duration {
	if t == nil {
		return nil
	}
	d := Duration(t.Nanoseconds())
	return &d
}

// Standard converts a *Duration to a *time.Duration type.
func (d *Duration) Standard() *time.Duration {
	return (*time.Duration)(d)
}

// MarshalJSON encodes the Duration in milliseconds.
func (d Duration) MarshalJSON() ([]byte, error) {
	return json.Marshal(int64(d / milliSec))
}

// UnmarshalJSON decodes milliseconds to Duration.
func (d *Duration) UnmarshalJSON(b []byte) error {
	var tmp int64
	err := json.Unmarshal(b, &tmp)
	if err != nil {
		return err
	}
	*d = Duration(tmp) * milliSec
	return nil
}

// DurationSlice is a slice of *Duration
type DurationSlice []*Duration

// NewDurationSlice converts a []*time.Duration to a DurationSlice type.
func NewDurationSlice(t []*time.Duration) DurationSlice {
	ds := make([]*Duration, len(t))
	for i := range ds {
		ds[i] = NewDuration(t[i])
	}
	return ds
}

// Standard converts a DurationSlice to a []*time.Duration type.
func (ds *DurationSlice) Standard() []*time.Duration {
	t := make([]*time.Duration, len(*ds))
	for i := range t {
		t[i] = (*ds)[i].Standard()
	}
	return t
}

// Durationint32Map is a int32 map of *Duration
type Durationint32Map map[int32]*Duration

// NewDurationint32Map converts a map[int32]*time.Duration to a Durationint32Map type.
func NewDurationint32Map(t map[int32]*time.Duration) Durationint32Map {
	dm := make(Durationint32Map, len(t))
	for i := range t {
		dm[i] = NewDuration(t[i])
	}
	return dm
}

// Standard converts a Durationint32Map to a map[int32]*time.Duration type.
func (dm *Durationint32Map) Standard() map[int32]*time.Duration {
	t := make(map[int32]*time.Duration, len(*dm))
	for key, value := range *dm {
		t[key] = value.Standard()
	}
	return t
}

// LongDuration implements a JSON Marshaler to encode a time.Duration in days.
type LongDuration int64

const day = LongDuration(time.Hour) * 24

// NewLongDuration converts a *time.Duration to a *LongDuration type.
func NewLongDuration(t *time.Duration) *LongDuration {
	if t == nil {
		return nil
	}
	d := LongDuration(t.Nanoseconds())
	return &d
}

// Standard converts a *LongDuration to a *time.Duration type.
func (d *LongDuration) Standard() *time.Duration {
	return (*time.Duration)(d)
}

// MarshalJSON encodes the LongDuration in days.
func (d LongDuration) MarshalJSON() ([]byte, error) {
	return json.Marshal(int64(d / day))
}

// UnmarshalJSON decodes days to LongDuration.
func (d *LongDuration) UnmarshalJSON(b []byte) error {
	var tmp int64
	err := json.Unmarshal(b, &tmp)
	if err != nil {
		return err
	}
	*d = LongDuration(tmp) * day
	return nil
}

// LongDurationSlice is a slice of *LongDuration
type LongDurationSlice []*LongDuration

// NewLongDurationSlice converts a []*time.Duration to a LongDurationSlice type.
func NewLongDurationSlice(t []*time.Duration) LongDurationSlice {
	ds := make([]*LongDuration, len(t))
	for i := range ds {
		ds[i] = NewLongDuration(t[i])
	}
	return ds
}

// Standard converts a LongDurationSlice to a []*time.Duration type.
func (ds *LongDurationSlice) Standard() []*time.Duration {
	t := make([]*time.Duration, len(*ds))
	for i := range t {
		t[i] = (*ds)[i].Standard()
	}
	return t
}

// LongDurationint32Map is a int32 map of *LongDuration
type LongDurationint32Map map[int32]*LongDuration

// NewLongDurationint32Map converts a map[int32]*time.LongDuration to a LongDurationint32Map type.
func NewLongDurationint32Map(t map[int32]*time.Duration) LongDurationint32Map {
	dm := make(LongDurationint32Map, len(t))
	for i := range t {
		dm[i] = NewLongDuration(t[i])
	}
	return dm
}

// Standard converts a LongDurationint32Map to a map[int32]*time.LongDuration type.
func (dm *LongDurationint32Map) Standard() map[int32]*time.Duration {
	t := make(map[int32]*time.Duration, len(*dm))
	for key, value := range *dm {
		t[key] = value.Standard()
	}
	return t
}