mostly working
This commit is contained in:
parent
26eaa1d898
commit
5f42a580a9
47 changed files with 914 additions and 637 deletions
45
timeutil.go
45
timeutil.go
|
|
@ -80,6 +80,11 @@ func TimeToFloat64(t time.Time) float64 {
|
|||
return float64(t.UnixNano())
|
||||
}
|
||||
|
||||
// TimeFromFloat64 returns a time from a float64.
|
||||
func TimeFromFloat64(tf float64) time.Time {
|
||||
return time.Unix(0, int64(tf))
|
||||
}
|
||||
|
||||
// TimeDescending sorts a given list of times ascending, or min to max.
|
||||
type TimeDescending []time.Time
|
||||
|
||||
|
|
@ -103,3 +108,43 @@ func (a TimeAscending) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|||
|
||||
// Less implements sort.Sorter
|
||||
func (a TimeAscending) Less(i, j int) bool { return a[i].Before(a[j]) }
|
||||
|
||||
// Days generates a seq of timestamps by day, from -days to today.
|
||||
func Days(days int) []time.Time {
|
||||
var values []time.Time
|
||||
for day := days; day >= 0; day-- {
|
||||
values = append(values, time.Now().AddDate(0, 0, -day))
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
// Hours returns a sequence of times by the hour for a given number of hours
|
||||
// after a given start.
|
||||
func Hours(start time.Time, totalHours int) []time.Time {
|
||||
times := make([]time.Time, totalHours)
|
||||
|
||||
last := start
|
||||
for i := 0; i < totalHours; i++ {
|
||||
times[i] = last
|
||||
last = last.Add(time.Hour)
|
||||
}
|
||||
|
||||
return times
|
||||
}
|
||||
|
||||
// HoursFilled adds zero values for the data bounded by the start and end of the xdata array.
|
||||
func HoursFilled(xdata []time.Time, ydata []float64) ([]time.Time, []float64) {
|
||||
start, end := TimeMinMax(xdata...)
|
||||
totalHours := DiffHours(start, end)
|
||||
|
||||
finalTimes := Hours(start, totalHours+1)
|
||||
finalValues := make([]float64, totalHours+1)
|
||||
|
||||
var hoursFromStart int
|
||||
for i, xd := range xdata {
|
||||
hoursFromStart = DiffHours(start, xd)
|
||||
finalValues[hoursFromStart] = ydata[i]
|
||||
}
|
||||
|
||||
return finalTimes, finalValues
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue