| 1 |
package chart |
| 2 |
|
| 3 |
import "time" |
| 4 |
|
| 5 |
|
| 6 |
const ( |
| 7 |
SecondsPerHour = 60 * 60 |
| 8 |
SecondsPerDay = 60 * 60 * 24 |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
func TimeMillis(d time.Duration) float64 { |
| 13 |
return float64(d) / float64(time.Millisecond) |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
func DiffHours(t1, t2 time.Time) (hours int) { |
| 18 |
t1n := t1.Unix() |
| 19 |
t2n := t2.Unix() |
| 20 |
var diff int64 |
| 21 |
if t1n > t2n { |
| 22 |
diff = t1n - t2n |
| 23 |
} else { |
| 24 |
diff = t2n - t1n |
| 25 |
} |
| 26 |
return int(diff / (SecondsPerHour)) |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func TimeMin(times ...time.Time) (min time.Time) { |
| 31 |
if len(times) == 0 { |
| 32 |
return |
| 33 |
} |
| 34 |
min = times[0] |
| 35 |
for index := 1; index < len(times); index++ { |
| 36 |
if times[index].Before(min) { |
| 37 |
min = times[index] |
| 38 |
} |
| 39 |
|
| 40 |
} |
| 41 |
return |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func TimeMax(times ...time.Time) (max time.Time) { |
| 46 |
if len(times) == 0 { |
| 47 |
return |
| 48 |
} |
| 49 |
max = times[0] |
| 50 |
|
| 51 |
for index := 1; index < len(times); index++ { |
| 52 |
if times[index].After(max) { |
| 53 |
max = times[index] |
| 54 |
} |
| 55 |
} |
| 56 |
return |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func TimeMinMax(times ...time.Time) (min, max time.Time) { |
| 61 |
if len(times) == 0 { |
| 62 |
return |
| 63 |
} |
| 64 |
min = times[0] |
| 65 |
max = times[0] |
| 66 |
|
| 67 |
for index := 1; index < len(times); index++ { |
| 68 |
if times[index].Before(min) { |
| 69 |
min = times[index] |
| 70 |
} |
| 71 |
if times[index].After(max) { |
| 72 |
max = times[index] |
| 73 |
} |
| 74 |
} |
| 75 |
return |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
func TimeToFloat64(t time.Time) float64 { |
| 80 |
return float64(t.UnixNano()) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func TimeFromFloat64(tf float64) time.Time { |
| 85 |
return time.Unix(0, int64(tf)) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
type TimeDescending []time.Time |
| 90 |
|
| 91 |
|
| 92 |
func (d TimeDescending) Len() int { return len(d) } |
| 93 |
|
| 94 |
|
| 95 |
func (d TimeDescending) Swap(i, j int) { d[i], d[j] = d[j], d[i] } |
| 96 |
|
| 97 |
|
| 98 |
func (d TimeDescending) Less(i, j int) bool { return d[i].After(d[j]) } |
| 99 |
|
| 100 |
|
| 101 |
type TimeAscending []time.Time |
| 102 |
|
| 103 |
|
| 104 |
func (a TimeAscending) Len() int { return len(a) } |
| 105 |
|
| 106 |
|
| 107 |
func (a TimeAscending) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 108 |
|
| 109 |
|
| 110 |
func (a TimeAscending) Less(i, j int) bool { return a[i].Before(a[j]) } |
| 111 |
|
| 112 |
|
| 113 |
func Days(days int) []time.Time { |
| 114 |
var values []time.Time |
| 115 |
for day := days; day >= 0; day-- { |
| 116 |
values = append(values, time.Now().AddDate(0, 0, -day)) |
| 117 |
} |
| 118 |
return values |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
func Hours(start time.Time, totalHours int) []time.Time { |
| 124 |
times := make([]time.Time, totalHours) |
| 125 |
|
| 126 |
last := start |
| 127 |
for i := 0; i < totalHours; i++ { |
| 128 |
times[i] = last |
| 129 |
last = last.Add(time.Hour) |
| 130 |
} |
| 131 |
|
| 132 |
return times |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func HoursFilled(xdata []time.Time, ydata []float64) ([]time.Time, []float64) { |
| 137 |
start, end := TimeMinMax(xdata...) |
| 138 |
totalHours := DiffHours(start, end) |
| 139 |
|
| 140 |
finalTimes := Hours(start, totalHours+1) |
| 141 |
finalValues := make([]float64, totalHours+1) |
| 142 |
|
| 143 |
var hoursFromStart int |
| 144 |
for i, xd := range xdata { |
| 145 |
hoursFromStart = DiffHours(start, xd) |
| 146 |
finalValues[hoursFromStart] = ydata[i] |
| 147 |
} |
| 148 |
|
| 149 |
return finalTimes, finalValues |
| 150 |
} |