Skip to content

Instantly share code, notes, and snippets.

@582033
Last active July 14, 2023 03:00
Show Gist options
  • Save 582033/75c37b3db78dad1291bcb14a700cf009 to your computer and use it in GitHub Desktop.
Save 582033/75c37b3db78dad1291bcb14a700cf009 to your computer and use it in GitHub Desktop.
计算
package main
import "fmt"
func main() {
time1 := 1626205761000 // 毫秒时间1
time2 := 1626205771000 // 毫秒时间2
interval := (time2 - time1 + 999) / 1000 // 计算时间间隔,不足秒的当做秒
fmt.Println(interval) // 输出时间间隔
}
type TemperatureData struct {
temperature int
timestamp int
}
func main() {
// 假设这是连续上报的温度数据
oldData := []TemperatureData{
{temperature: 20, timestamp: 100},
{temperature: 21, timestamp: 400},
{temperature: 22, timestamp: 700},
{temperature: 23, timestamp: 1100},
{temperature: 24, timestamp: 1300},
{temperature: 25, timestamp: 1600},
{temperature: 26, timestamp: 2000},
}
// 生成新的温度数据
newData := make([]TemperatureData, 0)
lastTemperature := 0
for i := 0; i < 10; i++ {
currentTimestamp := i * 1000
for j := 0; j < len(oldData); j++ {
if oldData[j].timestamp > currentTimestamp {
if j > 0 {
lastTemperature = oldData[j-1].temperature
}
break
} else if j == len(oldData)-1 {
lastTemperature = oldData[j].temperature
}
}
newData = append(newData, TemperatureData{temperature: lastTemperature, timestamp: currentTimestamp})
}
// 输出新的温度数据
for _, data := range newData {
fmt.Printf("Temperature: %d, Timestamp: %d\n", data.temperature, data.timestamp)
}
}
@582033
Copy link
Author

582033 commented Jul 14, 2023

有一组连续上报的温度数据,时间间隔几百毫秒至几千毫秒上报一次;
现在需要根据这个上报的温度数据组装一组新的温度数据,长度10,时间间隔是1秒;
根据就数据生成新数据的规则如下:
如果旧数据的时间刚好是1000毫秒,则用旧的温度数据; 如果旧数据的间隔小于1000毫秒,则覆盖上一秒的数据,如果大于1000毫秒则计入下一秒;如果1秒区间内没有数据则取相邻的两个温度的平均值
使用golang实现,温度类型int,时间类型int

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment